},
clone: function() {
- var clone = this._$.clone(true, true);
+ var clone = this._$.clone(true, true),
+ node = this;
+ clone.find('*').addBack().each(function() {
+ var el = this,
+ clonedData = $(this).data();
+
+ _.pairs(clonedData).forEach(function(pair) {
+ var key = pair[0],
+ value = pair[1];
+ if(_.isFunction(value.clone)) {
+ clonedData[key] = value.clone(node.document.createDocumentNode(el));
+ }
+ });
+ });
return this.document.createDocumentNode(clone[0]);
},
}
});
- if(idx !== 'undefined') {
+ if(idx !== undefined) {
nodePath = nodePath.slice(0, idx);
}
toret = nodePath.map(function(node) {return node.getIndex(); });
var node = (metaData && metaData.node) ? metaData.node : this,
event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
+ if(type === 'nodeMoved') {
+ event.meta.parent = origParent;
+ }
this.document.trigger('change', event);
}
if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) {
return toret;
},
+ containsNode: function(node) {
+ return node && (node.nativeNode === this.nativeNode || node._$.parents().index(this._$) !== -1);
+ },
+
toXML: function() {
var wrapper = $('<div>');
wrapper.append(this._getXMLDOMToDump());
var parseXML = function(xml) {
- return $($.trim(xml))[0];
+ var toret = $($.trim(xml));
+ if(!toret.length) {
+ throw new Error('Unable to parse XML: ' + xml);
+ }
+ return toret[0];
+
};
var registerTransformation = function(desc, name, target) {
};
-var Document = function(xml) {
- this.loadXML(xml);
+var Document = function(xml, extensions) {
this.undoStack = [];
this.redoStack = [];
this._transactionStack = [];
this._elementNodeTransformations = {};
this.registerExtension(coreTransformations);
+
+ (extensions || []).forEach(function(extension) {
+ this.registerExtension(extension);
+ }.bind(this));
+ this.loadXML(xml);
};
$.extend(Document.prototype, Backbone.Events, {
/* globals document */
from = document.createTextNode(from.text);
} else {
+ if(!from.tagName) {
+ throw new Error('tagName missing');
+ }
var node = $('<' + from.tagName + '>');
_.keys(from.attrs || {}).forEach(function(key) {
},
containsNode: function(node) {
- return this.root && (node.nativeNode === this.root.nativeNode || node._$.parents().index(this.root._$) !== -1);
+ return this.root && this.root.containsNode(node);
},
getSiblingParents: function(params) {
});
},
+ 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(transformation) {
this._transformationLevel++;
- toret = transformation.run({beUndoable:this._transformationLevel === 1});
- if(this._transformationLevel === 1 && !this._undoInProgress) {
- if(this._transactionInProgress) {
- this._transactionStack.push(transformation);
- } else {
- this.undoStack.push(transformation);
+
+ this.ifChanged(
+ this,
+ function() {
+ toret = transformation.run({beUndoable:this._transformationLevel === 1});
+ },
+ function() {
+ if(this._transformationLevel === 1 && !this._undoInProgress) {
+ if(this._transactionInProgress) {
+ this._transactionStack.push(transformation);
+ } else {
+ this.undoStack.push(transformation);
+ }
+ }
+ if(!this._undoInProgress && this._transformationLevel === 1) {
+ this.redoStack = [];
+ }
}
- }
- if(!this._undoInProgress && this._transformationLevel === 1) {
- this.redoStack = [];
- }
+ );
+
this._transformationLevel--;
return toret;
} else {
throw new Error('End of transaction requested, but there is no transaction in progress!');
}
this._transactionInProgress = false;
- this.undoStack.push(this._transactionStack);
- this._transactionStack = [];
+ if(this._transactionStack.length) {
+ this.undoStack.push(this._transactionStack);
+ this._transactionStack = [];
+ }
+ },
+
+ transaction: function(callback, context) {
+ this.startTransaction();
+ callback.call(context);
+ this.endTransaction();
},
getNodeByPath: function(path) {