1 define(function(require) {
5 var _ = require('libs/underscore'),
9 toret.createGenericTransformation = function(desc) {
11 var GenericTransformation = function(document, args) {
12 this.args = args || {};
14 var transformation = this;
15 _.keys(this.args).forEach(function(key) {
16 if(transformation.args[key].nodeType) { //@@ change to instanceof check, fix circular dependency
17 var value = transformation.args[key],
18 path = value.getPath();
19 Object.defineProperty(transformation.args, key, {
21 if(transformation.hasRun) {
22 console.log('returning via path');
23 return transformation.document.getNodeByPath(path);
25 console.log('returning original arg');
33 this.document = document;
39 _.extend(GenericTransformation.prototype, {
44 changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root;
45 this.snapshot = changeRoot.clone();
46 this.changeRootPath = changeRoot.getPath();
48 var toret = desc.impl.call(this.context, this.args); // a argumenty do metody?
54 desc.undo.call(this.context);
56 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
61 return GenericTransformation;
63 // var T = createGenericTransformation({impl: function() {}});
64 // var t = T(doc, {a:1,b:2,c3:3});
67 toret.createContextTransformation = function(desc) {
68 // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation
69 var GenericTransformation = toret.createGenericTransformation(desc);
71 var ContextTransformation = function(document, object, args) {
72 GenericTransformation.call(this, document, args);
74 if(document === object) {
75 this.context = document;
77 var contextPath = object.getPath();
78 Object.defineProperty(this, 'context', {
80 // todo: to jakos inaczej, bo np. this.context w undo transformacji before to juz nie ten sam obiekt
81 // moze transformacja powinna zwracac zmodyfikowana sciezke do obiektu po dzialaniu run?
83 // tu tez trick z hasRun
84 return document.getNodeByPath(contextPath);
89 ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
90 return ContextTransformation;
92 // var T = createContextTransformation({impl: function() {}});
93 // var t = T(doc, node, {a:1,b:2,c3:3});
98 toret.TransformationStorage = function() {};
100 _.extend(toret.TransformationStorage.prototype, {
101 _transformations: {},
103 register: function(Transformation) {
104 var list = (this._transformations[Transformation.prototype.name] = this._transformations[Transformation.prototype.name] || []);
105 list.push(Transformation);
108 get: function(name) {
109 var transformations = this._transformations[name];
110 if(!transformations) {
111 throw new Error('Transformation "' + name + '" not found!');
113 // na razie zwraca pierwsza
114 return transformations[0];
120 // var registerTransformationFromMethod = (object, methodName, desc) {
121 // if(!object[methodName]) {
122 // throw new Exeption('Cannot register transformation from unknown method ' + methodName + ' on ' + object);
124 // desc.impl = object[name];
125 // Transformation = createContextTransformation(desc);
126 // object.prototype.registerContextTransformation(name, createContextTransformation(method));
130 // registerTransformationFromMethod(ElementNode, 'setAttr', {
131 // impl: function(args) {
132 // this.setAttr(args.name, args.value);
134 // getChangeRoot: function() {
135 // return this.context;