+ getNodeInsertion: function(node) {
+ var insertion = {};
+ if(node instanceof DocumentNode) {
+ insertion.ofNode = node;
+ insertion.insertsNew = !this.containsNode(node);
+ } else {
+ insertion.ofNode = this.createDocumentNode(node);
+ insertion.insertsNew = true;
+ }
+ return insertion;
+ },
+
+ registerMethod: function(methodName, method, dstName) {
+ var doc = this;
+ var destination = {
+ document: doc,
+ documentNode: doc._nodeMethods,
+ textNode: doc._textNodeMethods,
+ elementNode: doc._elementNodeMethods
+ }[dstName];
+ registerMethod(methodName, method, destination);
+ },
+
+ registerTransformation: function(desc, name, dstName) {
+ var doc = this;
+ var destination = {
+ document: doc,
+ documentNode: doc._nodeTransformations,
+ textNode: doc._textNodeTransformations,
+ elementNode: doc._elementNodeTransformations
+ }[dstName];
+ registerTransformation(desc, name, destination);
+ },
+
+ registerExtension: function(extension) {
+ var doc = this;
+
+ ['document', 'documentNode', 'elementNode', 'textNode'].forEach(function(dstName) {
+ var dstExtension = extension[dstName];
+ if(dstExtension) {
+ if(dstExtension.methods) {
+ _.pairs(dstExtension.methods).forEach(function(pair) {
+ var methodName = pair[0],
+ method = pair[1];
+
+ doc.registerMethod(methodName, method, dstName);
+
+ });
+ }
+
+ if(dstExtension.transformations) {
+ _.pairs(dstExtension.transformations).forEach(function(pair) {
+ var name = pair[0],
+ desc = pair[1];
+ doc.registerTransformation(desc, name, dstName);
+ });
+ }
+ }
+ });
+ },
+
+ ifChanged: function(context, action, documentChangedHandler, documentUnchangedHandler) {
+ var hasChanged = false,
+ changeMonitor = function() {
+ hasChanged = true;
+ };
+
+ this.on('change', changeMonitor);
+ action.call(context);
+ this.off('change', changeMonitor);
+
+ if(hasChanged) {
+ if(documentChangedHandler) {
+ documentChangedHandler.call(context);
+ }
+ } else {
+ if(documentUnchangedHandler) {
+ documentUnchangedHandler.call(context);
+ }
+ }
+ },
+
+ transform: function(Transformation, args) {
+ var toret, transformation;
+
+ if(!this._currentTransaction) {
+ return this.transaction(function() {
+ return this.transform(Transformation, args);
+ }, {context: this});
+ }
+
+ 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);
+ this.trigger('operationEnd');
+ }
+ },
+ 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);
+ this.trigger('operationEnd');
+
+ }
+ },
+
+ 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.trigger('operationEnd');
+ }
+ 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;
+ this._transformationLevel = 0;
+ },
+
+ 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();
+ if(params.success) {
+ params.success(toret);
+ }
+ 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});
+ },
+
+ createFragment: function(Type, params) {
+ if(!Type.prototype instanceof fragments.Fragment) {
+ throw new Error('Can\'t create a fragment: `Type` is not a valid Fragment');
+ }
+ return new Type(this, params);
+ }
+});
+
+var Transaction = function(transformations, metadata) {
+ this.transformations = transformations || [];
+ this.metadata = metadata;
+};
+$.extend(Transaction.prototype, {
+ pushTransformation: function(transformation) {
+ this.transformations.push(transformation);
+ },
+ hasTransformations: function() {
+ return this.transformations.length > 0;
+ }