Skip to content

Commit 5da7c3c

Browse files
author
Friedrich W. H. Kossebau
committed
Add ops.OdtDocument.executeOperation() to concentrate/simpifly code
1 parent f200f41 commit 5da7c3c

4 files changed

Lines changed: 57 additions & 48 deletions

File tree

webodf/lib/ops/OdtDocument.js

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -441,9 +441,9 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
441441
* @param {!ops.Operation} op
442442
* @return {undefined}
443443
*/
444-
this.prepareOperationExecution = function(op) {
444+
function prepareOperationExecution(op) {
445445
eventNotifier.emit(ops.OdtDocument.signalOperationStart, op);
446-
};
446+
}
447447

448448
/**
449449
* Called after an operation is executed, this
@@ -454,7 +454,7 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
454454
* @param {!ops.Operation} op
455455
* @return {undefined}
456456
*/
457-
this.finishOperationExecution = function (op) {
457+
function finishOperationExecution(op) {
458458
var opspec = op.spec(),
459459
memberId = opspec.memberid,
460460
date = new Date(opspec.timestamp).toISOString(),
@@ -503,6 +503,33 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
503503
if (op.isEdit) {
504504
eventNotifier.emit(ops.OdtDocument.signalMetadataUpdated, changedMetadata);
505505
}
506+
}
507+
508+
/**
509+
* @param {!Array.<!ops.Operation.Event>} events
510+
* @return {undefined}
511+
*/
512+
function emitEvents(events) {
513+
events.forEach(function(event) {
514+
eventNotifier.emit(event.eventid, event.args);
515+
});
516+
}
517+
518+
/**
519+
* @param {!ops.Operation} op
520+
* @return {!boolean}
521+
*/
522+
this.executeOperation = function(op) {
523+
var events;
524+
525+
prepareOperationExecution(op);
526+
events = op.execute(self);
527+
if (events !== null) {
528+
finishOperationExecution(op);
529+
emitEvents(events);
530+
return true;
531+
}
532+
return false;
506533
};
507534

508535
/**
@@ -914,16 +941,6 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
914941
return odfCanvas.getFormatting();
915942
};
916943

917-
/**
918-
* @param {!Array.<!ops.Operation.Event>} events
919-
* @return {undefined}
920-
*/
921-
this.emitEvents = function (events) {
922-
events.forEach(function(event) {
923-
eventNotifier.emit(event.eventid, event.args);
924-
});
925-
};
926-
927944
/**
928945
* @param {!string} eventid
929946
* @param {!Function} cb

webodf/lib/ops/Session.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,7 @@ ops.Session = function Session(odfCanvas) {
8181
operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchStart, forwardBatchStart);
8282
operationRouter.subscribe(ops.OperationRouter.signalProcessingBatchEnd, forwardBatchEnd);
8383
opRouter.setPlaybackFunction(function (op) {
84-
var events;
85-
odtDocument.prepareOperationExecution(op);
86-
events = op.execute(odtDocument);
87-
if (events !== null) {
88-
odtDocument.finishOperationExecution(op);
89-
odtDocument.emitEvents(events);
90-
return true;
91-
}
92-
return false;
84+
return odtDocument.executeOperation(op);
9385
});
9486
opRouter.setOperationFactory(operationFactory);
9587
};

webodf/tests/gui/MetadataControllerTests.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,12 @@ gui.MetadataControllerTests = function MetadataControllerTests(runner) {
8080
operations.forEach(function(op) {
8181
var /**@type{?ops.Operation}*/
8282
timedOp,
83-
events,
8483
opspec = op.spec();
8584

8685
// need to set the timestamp, otherwise things fail in odtDocument
8786
opspec.timestamp = Date.now();
8887
timedOp = /**@type {!ops.Operation}*/(operationFactory.create(opspec));
89-
odtDocument.prepareOperationExecution(timedOp);
90-
events = timedOp.execute(odtDocument);
91-
if (events !== null) {
92-
odtDocument.finishOperationExecution(timedOp);
93-
odtDocument.emitEvents(events);
94-
}
88+
odtDocument.executeOperation(timedOp);
9589
});
9690
};
9791

webodf/tests/ops/OperationTests.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ ops.OperationTests = function OperationTests(runner) {
177177
opsElement = before.nextElementSibling,
178178
after = opsElement.nextElementSibling,
179179
ops = [],
180-
op,
180+
opElement, opspec,
181+
now = Date.now(),
181182
setup;
182183
runtime.assert(before.localName === "before", "Expected <before/> in " + name + ".");
183184
runtime.assert(checkWhitespace(before, "s", " "), "Unexpanded text:s element or text:c attribute found in " + name + ".");
@@ -187,11 +188,20 @@ ops.OperationTests = function OperationTests(runner) {
187188
runtime.assert(checkWhitespace(after, "s", " "), "Unexpanded text:s element or text:c attribute found in " + name + ".");
188189
runtime.assert(checkWhitespace(after, "tab", "\t"), "Unexpanded text:tab element found in " + name + ".");
189190
opsTestHelper.removeInsignificantTextNodes(node);
190-
op = opsElement.firstElementChild;
191-
while (op) {
192-
runtime.assert(op.localName === "op", "Expected <op/> in " + name + ".");
193-
ops.push(parseOperation(op));
194-
op = op.nextElementSibling;
191+
opElement = opsElement.firstElementChild;
192+
while (opElement) {
193+
runtime.assert(opElement.localName === "op", "Expected <op/> in " + name + ".");
194+
opspec = parseOperation(opElement);
195+
// default to Alice
196+
if (!opspec.memberid) {
197+
opspec.memberid = 'Alice';
198+
}
199+
// default to now
200+
if (!opspec.timestamp) {
201+
opspec.timestamp = now;
202+
}
203+
ops.push(opspec);
204+
opElement = opElement.nextElementSibling;
195205
}
196206
setup = self.setUps.hasOwnProperty(name) ? self.setUps[name]() : null;
197207
if (hasSetup) {
@@ -259,7 +269,6 @@ ops.OperationTests = function OperationTests(runner) {
259269
factory = new ops.OperationFactory(),
260270
i,
261271
op,
262-
events,
263272
textbefore = getOfficeTextElement(test.before),
264273
textafter = getOfficeTextElement(test.after),
265274
styles = t.odfContainer.rootElement.styles,
@@ -290,12 +299,7 @@ ops.OperationTests = function OperationTests(runner) {
290299
// execute test ops
291300
for (i = 0; i < test.ops.length; i += 1) {
292301
op = /**@type {!ops.Operation}*/(factory.create(test.ops[i]));
293-
t.odtDocument.prepareOperationExecution(op);
294-
events = op.execute(t.odtDocument);
295-
if (metabefore) {
296-
t.odtDocument.finishOperationExecution(op);
297-
}
298-
t.odtDocument.emitEvents(events);
302+
t.odtDocument.executeOperation(op);
299303
checkForEmptyTextNodes(t.odtDocument.getCanvas().getElement());
300304
}
301305

@@ -406,18 +410,20 @@ ops.OperationTests = function OperationTests(runner) {
406410
}
407411

408412
this.setUp = function () {
409-
var testarea, properties;
413+
var testarea;
410414
t = {};
411415
testarea = core.UnitTest.provideTestAreaDiv();
412416
t.odfcanvas = new odf.OdfCanvas(testarea);
413417
t.odfContainer = new odf.OdfContainer(odf.OdfContainer.DocumentType.TEXT, null);
414418
t.odfcanvas.setOdfContainer(t.odfContainer);
415419
t.odtDocument = new ops.OdtDocument(t.odfcanvas);
416-
properties = new ops.MemberProperties();
417-
properties.color = "black";
418-
properties.fullName = "Alice";
419-
properties.imageUrl = "";
420-
t.odtDocument.addMember(new ops.Member('Alice', properties));
420+
["Alice", "Bob", "Eve", "Joe"].forEach(function(name) {
421+
var properties = new ops.MemberProperties();
422+
properties.color = "black";
423+
properties.fullName = name;
424+
properties.imageUrl = "";
425+
t.odtDocument.addMember(new ops.Member(name, properties));
426+
});
421427
};
422428
this.tearDown = function () {
423429
t.odfcanvas.destroy(function () { return; });

0 commit comments

Comments
 (0)