X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/bd0c9397ecd0cccdc7af0f0b0153adf5126e1538..d221e72024aa5d6f90e256728fa70f622d4e57d7:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index e525c14..8bf531b 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -33,14 +33,17 @@ $.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 clonedData = $(this).data(); + 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(); + clonedData[key] = value.clone(node.document.createDocumentNode(el)); } }); }); @@ -438,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; @@ -448,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 {