editor: canvas cursor fix - handle node with Zero Width Space only correctly
[fnpeditor.git] / src / smartxml / smartxml.js
index 000f722..8bf531b 100644 (file)
@@ -33,14 +33,17 @@ $.extend(DocumentNode.prototype, {
     },
 
     clone: function() {
     },
 
     clone: function() {
-        var clone = this._$.clone(true, true);
+        var clone = this._$.clone(true, true),
+            node = this;
         clone.find('*').addBack().each(function() {
         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)) {
             _.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));
                 }
             });
         });
                 }
             });
         });
@@ -260,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 = [];
     this.undoStack = [];
     this.redoStack = [];
     this._transactionStack = [];
@@ -275,6 +277,11 @@ var Document = function(xml) {
     this._elementNodeTransformations = {};
     
     this.registerExtension(coreTransformations);
     this._elementNodeTransformations = {};
     
     this.registerExtension(coreTransformations);
+
+    (extensions || []).forEach(function(extension) {
+        this.registerExtension(extension);
+    }.bind(this));
+    this.loadXML(xml);
 };
 
 $.extend(Document.prototype, Backbone.Events, {
 };
 
 $.extend(Document.prototype, Backbone.Events, {
@@ -434,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;
 
     transform: function(Transformation, args) {
         var toret, transformation;
 
@@ -444,17 +472,26 @@ $.extend(Document.prototype, Backbone.Events, {
         }
         if(transformation) {
             this._transformationLevel++;
         }
         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 {
             this._transformationLevel--;
             return toret;
         } else {