+
+ if(typeof Transformation === 'function') {
+ transformation = new Transformation(this, this, args);
+ } else {
+ transformation = Transformation;
+ }
+ if(transformation) {
+ this._transformationLevel++;
+
+ this.ifChanged(
+ this,
+ function() {
+ toret = transformation.run({beUndoable:this._transformationLevel === 1});
+ },
+ function() {
+ if(this._transformationLevel === 1 && !this._undoInProgress) {
+ this._currentTransaction.pushTransformation(transformation);
+ this.redoStack = [];
+ }
+ }
+ );
+
+ this._transformationLevel--;
+ return toret;
+ } else {
+ throw new Error('Transformation ' + transformation + ' doesn\'t exist!');
+ }
+ },
+ undo: function() {
+ var transaction = this.undoStack.pop(),
+ doc = this,
+ transformations, stopAt;
+
+ if(transaction) {
+ this._undoInProgress = true;
+
+ // We will modify this array in a minute so make sure we work on a copy.
+ transformations = transaction.transformations.slice(0);
+
+ if(transformations.length > 1) {
+ // In case of real transactions we don't want to run undo on all of transformations if we don't have to.
+ stopAt = undefined;
+ transformations.some(function(t, idx) {
+ if(!t.undo && t.getChangeRoot().sameNode(doc.root)) {
+ stopAt = idx;
+ return true; //break
+ }
+ });
+ if(stopAt !== undefined) {
+ // We will get away with undoing only this transformations as the one at stopAt reverses the whole document.
+ transformations = transformations.slice(0, stopAt+1);
+ }
+ }
+
+ transformations.reverse();
+ transformations.forEach(function(t) {
+ t.undo();
+ });
+
+ this._undoInProgress = false;
+ this.redoStack.push(transaction);
+ }
+ },
+ redo: function() {
+ var transaction = this.redoStack.pop();
+ if(transaction) {
+ this._transformationLevel++;
+ transaction.transformations.forEach(function(t) {
+ t.run({beUndoable: true});
+ });
+ this._transformationLevel--;
+ this.undoStack.push(transaction);
+ }
+ },
+
+ startTransaction: function(metadata) {
+ if(this._currentTransaction) {
+ throw new Error('Nested transactions not supported!');
+ }
+ this._rollbackBackup = this.root.clone();
+ this._currentTransaction = new Transaction([], metadata);
+ },
+
+ endTransaction: function() {
+ if(!this._currentTransaction) {
+ throw new Error('End of transaction requested, but there is no transaction in progress!');
+ }
+ if(this._currentTransaction.hasTransformations()) {
+ this.undoStack.push(this._currentTransaction);
+ }
+ this._currentTransaction = null;
+ },
+
+ rollbackTransaction: function() {
+ if(!this._currentTransaction) {
+ throw new Error('Transaction rollback requested, but there is no transaction in progress!');
+ }
+ this.replaceRoot(this._rollbackBackup);
+ this._rollbackBackup = null;
+ this._currentTransaction = null;
+ },
+
+ transaction: function(callback, params) {
+ var toret;
+ params = params || {};
+ this.startTransaction(params.metadata);
+ try {
+ toret = callback.call(params.context || this);
+ } catch(e) {
+ if(params.error) {
+ params.error(e);
+ }
+ this.rollbackTransaction();
+ return;
+ }
+ this.endTransaction();
+ return toret;
+ },
+
+ getNodeByPath: function(path) {
+ var toret = this.root;
+ path.forEach(function(idx) {
+ toret = toret.contents()[idx];
+ });
+ return toret;
+ },
+
+ _defineDocumentProperties: function($document) {
+ var doc = this;
+ Object.defineProperty(doc, 'root', {get: function() {
+ if(!$document) {
+ return null;
+ }
+ return doc.createDocumentNode($document[0]);
+ }, configurable: true});
+ Object.defineProperty(doc, 'dom', {get: function() {
+ if(!$document) {
+ return null;
+ }
+ return $document[0];
+ }, configurable: true});