Skip to content

Commit 7c461a9

Browse files
author
Friedrich W. H. Kossebau
committed
Add ops.OdtDocument.executeOperation() to concentrate/simpifly code
1 parent ec54089 commit 7c461a9

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
@@ -436,9 +436,9 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
436436
* @param {!ops.Operation} op
437437
* @return {undefined}
438438
*/
439-
this.prepareOperationExecution = function(op) {
439+
function prepareOperationExecution(op) {
440440
eventNotifier.emit(ops.OdtDocument.signalOperationStart, op);
441-
};
441+
}
442442

443443
/**
444444
* Called after an operation is executed, this
@@ -449,7 +449,7 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
449449
* @param {!ops.Operation} op
450450
* @return {undefined}
451451
*/
452-
this.finishOperationExecution = function (op) {
452+
function finishOperationExecution(op) {
453453
var opspec = op.spec(),
454454
memberId = opspec.memberid,
455455
date = new Date(opspec.timestamp).toISOString(),
@@ -498,6 +498,33 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
498498
if (op.isEdit) {
499499
eventNotifier.emit(ops.OdtDocument.signalMetadataUpdated, changedMetadata);
500500
}
501+
}
502+
503+
/**
504+
* @param {!Array.<!ops.Operation.Event>} events
505+
* @return {undefined}
506+
*/
507+
function emitEvents(events) {
508+
events.forEach(function(event) {
509+
eventNotifier.emit(event.eventid, event.args);
510+
});
511+
}
512+
513+
/**
514+
* @param {!ops.Operation} op
515+
* @return {!boolean}
516+
*/
517+
this.executeOperation = function(op) {
518+
var events;
519+
520+
prepareOperationExecution(op);
521+
events = op.execute(self);
522+
if (events !== null) {
523+
finishOperationExecution(op);
524+
emitEvents(events);
525+
return true;
526+
}
527+
return false;
501528
};
502529

503530
/**
@@ -909,16 +936,6 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
909936
return odfCanvas.getFormatting();
910937
};
911938

912-
/**
913-
* @param {!Array.<!ops.Operation.Event>} events
914-
* @return {undefined}
915-
*/
916-
this.emitEvents = function (events) {
917-
events.forEach(function(event) {
918-
eventNotifier.emit(event.eventid, event.args);
919-
});
920-
};
921-
922939
/**
923940
* @param {!string} eventid
924941
* @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)