X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/e298a4a95de93951eee23d33ba7e8d1387a90a5b..2d049782d8d9179d28807567dfa0daeb39dbfb7e:/src/smartxml/smartxml.js diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index d168e6d..d085742 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -203,6 +203,10 @@ $.extend(ElementNode.prototype, { return toret; }, + containsNode: function(node) { + return node && (node.nativeNode === this.nativeNode || node._$.parents().index(this._$) !== -1); + }, + toXML: function() { var wrapper = $('
'); wrapper.append(this._getXMLDOMToDump()); @@ -354,7 +358,7 @@ $.extend(Document.prototype, Backbone.Events, { }, 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) { @@ -441,6 +445,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; @@ -451,17 +476,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 { @@ -540,6 +574,12 @@ $.extend(Document.prototype, Backbone.Events, { } }, + transaction: function(callback, context) { + this.startTransaction(); + callback.call(context); + this.endTransaction(); + }, + getNodeByPath: function(path) { var toret = this.root; path.forEach(function(idx) {