smartxml: transformations fix - wrapping nodes arguments with path accessor one level...
[fnpeditor.git] / src / smartxml / transformations.js
index 25c0ec1..0d2012e 100644 (file)
@@ -43,6 +43,32 @@ toret.createGenericTransformation = function(desc, name) {
 
         // potem spr na dotychczasowych undo/redo tests;
         
+        var patchObject = function(obj, depth) {
+            depth = _.isNumber(depth) ? depth : 1;
+            if(depth > 3) {
+                return;
+            }
+            _.keys(obj).forEach(function(key) {
+                var value = obj[key],
+                    path;
+                if(value) {
+                    if(value.nodeType) {
+                        path = value.getPath();
+                        Object.defineProperty(obj, key, {
+                            get: function() {
+                                if(transformation.hasRun && path) {
+                                    return transformation.document.getNodeByPath(path);
+                                } else {
+                                    return value;
+                                }
+                            }
+                        });
+                    } else if(_.isObject(value)) {
+                        patchObject(value, depth+1);
+                    }
+                }
+            });
+        };
 
         this.args.forEach(function(arg, idx, args) {
             var path;
@@ -59,22 +85,7 @@ toret.createGenericTransformation = function(desc, name) {
                         }
                     });
                 } 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;
-                                    }
-                                }
-                            });
-                        }
-                    });
+                    patchObject(arg);
                 }
             }
         });
@@ -90,9 +101,15 @@ toret.createGenericTransformation = function(desc, name) {
         run: function(options) {
             var changeRoot;
             if(!desc.undo && options.beUndoable) {
-                changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root;
-                this.snapshot = changeRoot.clone();
+                changeRoot = this.getChangeRoot();
+                if(!changeRoot) {
+                     throw new Error(
+                         'Transformation {name} returned invalid change root value'
+                         .replace('{name}', name)
+                     );
+                }
                 this.changeRootPath = changeRoot.getPath();
+                this.snapshot = changeRoot.clone();
             }
             var argsToPass = desc.undo ? [this].concat(this.args) : this.args;
             var toret = desc.impl.apply(this.context, argsToPass);
@@ -106,6 +123,9 @@ toret.createGenericTransformation = function(desc, name) {
                 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
             }
         },
+        getChangeRoot: desc.getChangeRoot || function() {
+            return this.document.root;
+        }
     });
 
     return GenericTransformation;