smartxml: testing three available levels of transformation undo-awareness
[fnpeditor.git] / src / smartxml / transformations.js
index 5f0756c..9aea150 100644 (file)
@@ -42,18 +42,40 @@ toret.createGenericTransformation = function(desc, name) {
         // });
 
         // potem spr na dotychczasowych undo/redo tests;
+        
+
         this.args.forEach(function(arg, idx, args) {
-            if(arg.nodeType) { // ~
-                var path = arg.getPath();
-                Object.defineProperty(args, idx, {
-                    get: function() {
-                        if(transformation.hasRun) {
-                            return transformation.document.getNodeByPath(path);
-                        } else {
-                            return arg;
+            var path;
+            if(arg) {
+                if(arg.nodeType) { // ~
+                    path = arg.getPath();
+                    Object.defineProperty(args, idx, {
+                        get: function() {
+                            if(transformation.hasRun && path) {
+                                return transformation.document.getNodeByPath(path);
+                            } else {
+                                return arg;
+                            }
                         }
-                    }
-                });
+                    });
+                } else if(_.isObject(arg)) {
+                    _.keys(arg).forEach(function(key) {
+                        var value = arg[key],
+                            path;
+                        if(value && value.nodeType) {
+                            path = value.getPath();
+                            Object.defineProperty(arg, key, {
+                                get: function() {
+                                    if(transformation.hasRun && path) {
+                                        return transformation.document.getNodeByPath(path);
+                                    } else {
+                                        return value;
+                                    }
+                                }
+                            });   
+                        }
+                    });
+                }
             }
         });
 
@@ -65,21 +87,22 @@ toret.createGenericTransformation = function(desc, name) {
     };
     _.extend(GenericTransformation.prototype, {
         name: name,
-        run: function() {
+        run: function(options) {
             var changeRoot;
-            if(!desc.undo) {
+            if(!desc.undo && options.beUndoable) {
                 changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root;
                 this.snapshot = changeRoot.clone();
                 this.changeRootPath = changeRoot.getPath();
             }
             //var toret = desc.impl.call(this.context, this.args); // a argumenty do metody?
-            var toret = desc.impl.apply(this.context, this.args);
+            var argsToPass = desc.undo ? [this].concat(this.args) : this.args;
+            var toret = desc.impl.apply(this.context, argsToPass);
             this.hasRun = true;
             return toret;
         },
         undo: function() {
             if(desc.undo) {
-                desc.undo.call(this.context);
+                desc.undo.call(this.context, this);
             } else {
                 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
             }