Refactoring in WLXMLDocument.registerExtension
[fnpeditor.git] / src / smartxml / smartxml.js
index fc9715b..8110a97 100644 (file)
@@ -15,12 +15,13 @@ var TEXT_NODE = Node.TEXT_NODE;
 var INSERTION = function(implementation) {
     var toret = function(node) {
         var insertion = this.getNodeInsertion(node),
+            nodeWasContained = this.document.containsNode(insertion.ofNode),
             nodeParent;
         if(!(this.document.containsNode(this))) {
             nodeParent = insertion.ofNode.parent();
         }
         implementation.call(this, insertion.ofNode.nativeNode);
-        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent);
+        this.triggerChangeEvent(insertion.insertsNew ? 'nodeAdded' : 'nodeMoved', {node: insertion.ofNode}, nodeParent, nodeWasContained);
         return insertion.ofNode;
     };
     return toret;
@@ -180,13 +181,13 @@ $.extend(DocumentNode.prototype, {
         }
     },
 
-    triggerChangeEvent: function(type, metaData, origParent) {
+    triggerChangeEvent: function(type, metaData, origParent, nodeWasContained) {
         var node = (metaData && metaData.node) ? metaData.node : this,
             event = new events.ChangeEvent(type, $.extend({node: node}, metaData || {}));
         if(type === 'nodeDetached' || this.document.containsNode(event.meta.node)) {
             this.document.trigger('change', event);
         }
-        if((type === 'nodeAdded' || type === 'nodeMoved') && !(this.document.containsNode(this))) {
+        if((type === 'nodeAdded' || type === 'nodeMoved') && !this.document.containsNode(this) && nodeWasContained) {
              event = new events.ChangeEvent('nodeDetached', {node: node, parent: origParent});
              this.document.trigger('change', event);
         }
@@ -395,6 +396,17 @@ ElementNode.prototype.transformations.register(transformations.createContextTran
     }
 }));
 
+ElementNode.prototype.transformations.register(transformations.createContextTransformation({
+    name: 'smartxml.setAttr2',
+    impl: function(args) {
+        this.prevAttr = this.getAttr(args.name);
+        this.setAttr(args.name, args.value);
+    },
+    undo: function(args) {
+        this.setAttr(args.name, this.prevAttr);
+    }
+}));
+
 DocumentNode.prototype.transformations.register(transformations.createContextTransformation({
     name: 'smartxml.wrapWith',
     getChangeRoot: function() {
@@ -440,7 +452,7 @@ $.extend(TextNode.prototype, {
     },
 
     setText: function(text) {
-        console.log('smartxml: ' + text);
+        //console.log('smartxml: ' + text);
         this.nativeNode.data = text;
         this.triggerTextChangeEvent();
     },
@@ -542,6 +554,10 @@ TextNode.prototype.transformations.register(transformations.createContextTransfo
     },
     getChangeRoot: function() {
         return this.context.parent().parent();
+    },
+    isAllowed: function(args) {
+        var parent = this.parent();
+        return !!(parent && parent.parent());
     }
 }));
 
@@ -565,6 +581,9 @@ var Document = function(xml) {
     this.loadXML(xml);
     this.undoStack = [];
     this.redoStack = [];
+    this._transformationLevel = 0;
+    this.transformations = new transformations.TransformationStorage();
+
 };
 
 $.extend(Document.prototype, Backbone.Events, {
@@ -743,7 +762,7 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     transform: function(transformation, args) {
-        console.log('transform');
+        //console.log('transform');
         var Transformation, toret;
         if(typeof transformation === 'string') {
             Transformation = this.transformations.get(transformation);
@@ -752,9 +771,13 @@ $.extend(Document.prototype, Backbone.Events, {
             }
         } 
         if(transformation) {
+            this._transformationLevel++;
             toret = transformation.run();
-            this.undoStack.push(transformation);
-            console.log('clearing redo stack');
+            if(this._transformationLevel === 1) {
+                this.undoStack.push(transformation);
+            }
+            this._transformationLevel--;
+            //console.log('clearing redo stack');
             this.redoStack = [];
             return toret;
         } else {
@@ -820,7 +843,8 @@ return {
 
     Document: Document,
     DocumentNode: DocumentNode,
-    ElementNode: ElementNode
+    ElementNode: ElementNode,
+    TextNode: TextNode
 };
 
 });
\ No newline at end of file