Skip to content

Commit 36be18c

Browse files
author
Friedrich W. H. Kossebau
committed
Add ops.OdtDocument.executeOperation() to concentrate/simpifly code
1 parent 14f1bb6 commit 36be18c

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

445445
/**
446446
* Called after an operation is executed, this
@@ -451,7 +451,7 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
451451
* @param {!ops.Operation} op
452452
* @return {undefined}
453453
*/
454-
this.finishOperationExecution = function (op) {
454+
function finishOperationExecution(op) {
455455
var opspec = op.spec(),
456456
memberId = opspec.memberid,
457457
date = new Date(opspec.timestamp).toISOString(),
@@ -500,6 +500,33 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
500500
if (op.isEdit) {
501501
eventNotifier.emit(ops.OdtDocument.signalMetadataUpdated, changedMetadata);
502502
}
503+
}
504+
505+
/**
506+
* @param {!Array.<!ops.Operation.Event>} events
507+
* @return {undefined}
508+
*/
509+
function emitEvents(events) {
510+
events.forEach(function(event) {
511+
eventNotifier.emit(event.eventid, event.args);
512+
});
513+
}
514+
515+
/**
516+
* @param {!ops.Operation} op
517+
* @return {!boolean}
518+
*/
519+
this.executeOperation = function(op) {
520+
var events;
521+
522+
prepareOperationExecution(op);
523+
events = op.execute(self);
524+
if (events !== null) {
525+
finishOperationExecution(op);
526+
emitEvents(events);
527+
return true;
528+
}
529+
return false;
503530
};
504531

505532
/**
@@ -911,16 +938,6 @@ ops.OdtDocument = function OdtDocument(odfCanvas) {
911938
return odfCanvas.getFormatting();
912939
};
913940

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