if(this._transactionInProgress) {
this._transactionStack.push(transformation);
} else {
- this.undoStack.push(transformation);
+ this.undoStack.push(new Transaction([transformation]));
}
}
if(!this._undoInProgress && this._transformationLevel === 1) {
}
},
undo: function() {
- var transformationObject = this.undoStack.pop(),
+ var transaction = this.undoStack.pop(),
doc = this,
transformations, stopAt;
- if(transformationObject) {
+ if(transaction) {
this._undoInProgress = true;
- if(_.isArray(transformationObject)) {
- // We will modify this array in a minute so make sure we work on a copy.
- transformations = transformationObject.slice(0);
- } else {
- // Lets normalize single transformation to a transaction containing one transformation.
- transformations = [transformationObject];
- }
+ // 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.
});
this._undoInProgress = false;
- this.redoStack.push(transformationObject);
+ this.redoStack.push(transaction);
}
},
redo: function() {
- var transformationObject = this.redoStack.pop(),
- transformations;
- if(transformationObject) {
+ var transaction = this.redoStack.pop();
+ if(transaction) {
this._transformationLevel++;
- transformations = _.isArray(transformationObject) ? transformationObject : [transformationObject];
- transformations.forEach(function(t) {
+ transaction.transformations.forEach(function(t) {
t.run({beUndoable: true});
});
this._transformationLevel--;
- this.undoStack.push(transformationObject);
+ this.undoStack.push(transaction);
}
},
}
this._transactionInProgress = false;
if(this._transactionStack.length) {
- this.undoStack.push(this._transactionStack);
+ this.undoStack.push(new Transaction(this._transactionStack));
this._transactionStack = [];
}
},
transaction: function(callback, context) {
+ var toret;
this.startTransaction();
- callback.call(context);
+ toret = callback.call(context);
this.endTransaction();
+ return toret;
},
getNodeByPath: function(path) {
_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});
}
});
+var Transaction = function(transformations) {
+ this.transformations = transformations || [];
+};
+
return {
documentFromXML: function(xml) {