X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/8464a511c74dff643095ce419659df60b0580b7a..4ebb1730539288b8198f6db38b0ac9d56b0e521d:/src/smartxml/transformations.js diff --git a/src/smartxml/transformations.js b/src/smartxml/transformations.js index 53dc384..5f0756c 100644 --- a/src/smartxml/transformations.js +++ b/src/smartxml/transformations.js @@ -5,31 +5,58 @@ define(function(require) { var _ = require('libs/underscore'), toret = {}; +var getTransDesc = function(desc, name) { + if(typeof desc === 'function') { + desc = {impl: desc}; + } + if(!desc.impl) { + throw new Error('Got transformation description without implementation.') + } + return desc; +}; -toret.createGenericTransformation = function(desc) { +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, { + // _.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) { + if(arg.nodeType) { // ~ + var path = arg.getPath(); + Object.defineProperty(args, idx, { get: function() { if(transformation.hasRun) { - console.log('returning via path'); return transformation.document.getNodeByPath(path); } else { - console.log('returning original arg'); - return value; - + return arg; } } }); } }); + this.document = document; this.hasRun = false; if(desc.init) { @@ -37,7 +64,7 @@ toret.createGenericTransformation = function(desc) { } }; _.extend(GenericTransformation.prototype, { - name: desc.name, + name: name, run: function() { var changeRoot; if(!desc.undo) { @@ -45,7 +72,8 @@ toret.createGenericTransformation = function(desc) { 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 toret = desc.impl.apply(this.context, this.args); this.hasRun = true; return toret; }, @@ -64,9 +92,9 @@ toret.createGenericTransformation = function(desc) { // var t = T(doc, {a:1,b:2,c3:3}); -toret.createContextTransformation = function(desc) { +toret.createContextTransformation = function(desc, name) { // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation - var GenericTransformation = toret.createGenericTransformation(desc); + var GenericTransformation = toret.createGenericTransformation(desc, name); var ContextTransformation = function(document, object, args) { GenericTransformation.call(this, document, args); @@ -74,14 +102,21 @@ toret.createContextTransformation = function(desc) { 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; + + } } }); } @@ -95,10 +130,11 @@ toret.createContextTransformation = function(desc) { -toret.TransformationStorage = function() {}; +toret.TransformationStorage = function() { + this._transformations = {}; +}; _.extend(toret.TransformationStorage.prototype, { - _transformations: {}, register: function(Transformation) { var list = (this._transformations[Transformation.prototype.name] = this._transformations[Transformation.prototype.name] || []);