},
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]);
},
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) {
});
},
+ 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 = [];
+ }
},
getNodeByPath: function(path) {