X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/fb23a597811c4c60dd1b9bddeab4335a33bee8c1..a8dddd8aadd5c5c56c7a91d833bc770b42d0ccdc:/src/smartxml/smartxml.js?ds=inline diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index f92bf01..8bf531b 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -33,7 +33,20 @@ $.extend(DocumentNode.prototype, { }, 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]); }, @@ -222,7 +235,12 @@ $.extend(TextNode.prototype, { 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) { @@ -245,8 +263,7 @@ var registerMethod = function(methodName, method, target) { }; -var Document = function(xml) { - this.loadXML(xml); +var Document = function(xml, extensions) { this.undoStack = []; this.redoStack = []; this._transactionStack = []; @@ -260,6 +277,11 @@ var Document = function(xml) { this._elementNodeTransformations = {}; this.registerExtension(coreTransformations); + + (extensions || []).forEach(function(extension) { + this.registerExtension(extension); + }.bind(this)); + this.loadXML(xml); }; $.extend(Document.prototype, Backbone.Events, { @@ -276,6 +298,9 @@ $.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) { @@ -416,6 +441,27 @@ $.extend(Document.prototype, Backbone.Events, { }); }, + 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; @@ -426,17 +472,26 @@ $.extend(Document.prototype, Backbone.Events, { } 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 {