X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/cee08f0b14be951a31544ae708b2a92f6255e61a..7509ad5d22654faa0abd75465de9f3fa632b1f3a:/src/smartxml/transformations.js diff --git a/src/smartxml/transformations.js b/src/smartxml/transformations.js index eaa292c..9aea150 100644 --- a/src/smartxml/transformations.js +++ b/src/smartxml/transformations.js @@ -19,27 +19,66 @@ toret.createGenericTransformation = function(desc, name) { desc = getTransDesc(desc); var GenericTransformation = function(document, args) { - this.args = args || {}; + this.args = args || []; var transformation = this; - _.keys(this.args).forEach(function(key) { - if(transformation.args[key].nodeType) { //@@ change to instanceof check, fix circular dependency - var value = transformation.args[key], - path = value.getPath(); - Object.defineProperty(transformation.args, key, { - get: function() { - if(transformation.hasRun) { - //console.log('returning via path'); - return transformation.document.getNodeByPath(path); - } else { - //console.log('returning original arg'); - return value; - + // _.keys(this.args).forEach(function(key) { + // if(transformation.args[key].nodeType) { //@@ change to instanceof check, fix circular dependency + // var value = transformation.args[key], + // path = value.getPath(); + // Object.defineProperty(transformation.args, key, { + // get: function() { + // if(transformation.hasRun) { + // //console.log('returning via path'); + // return transformation.document.getNodeByPath(path); + // } else { + // //console.log('returning original arg'); + // return value; + + // } + // } + // }); + // } + // }); + + // potem spr na dotychczasowych undo/redo tests; + + + this.args.forEach(function(arg, idx, args) { + 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; + } + } + }); + } + }); + } } }); + this.document = document; this.hasRun = false; if(desc.init) { @@ -48,20 +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.call(this.context, this.args); // a argumenty do metody? + 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); } @@ -84,14 +125,21 @@ toret.createContextTransformation = function(desc, name) { if(document === object) { this.context = document; } else { - var contextPath = object.getPath(); + var contextPath = object.getPath(), + transformation = this; Object.defineProperty(this, 'context', { get: function() { // todo: to jakos inaczej, bo np. this.context w undo transformacji before to juz nie ten sam obiekt // moze transformacja powinna zwracac zmodyfikowana sciezke do obiektu po dzialaniu run? - // tu tez trick z hasRun - return document.getNodeByPath(contextPath); + if(transformation.hasRun) { + //console.log('returning via path'); + return transformation.document.getNodeByPath(contextPath); + } else { + //console.log('returning original arg'); + return object; + + } } }); }