1 define(function(require) {
5 var _ = require('libs/underscore'),
8 var getTransDesc = function(desc, name) {
9 if(typeof desc === 'function') {
13 throw new Error('Got transformation description without implementation.')
18 toret.createGenericTransformation = function(desc, name) {
19 desc = getTransDesc(desc);
21 var GenericTransformation = function(document, args) {
22 this.args = args || [];
24 var transformation = this;
25 // _.keys(this.args).forEach(function(key) {
26 // if(transformation.args[key].nodeType) { //@@ change to instanceof check, fix circular dependency
27 // var value = transformation.args[key],
28 // path = value.getPath();
29 // Object.defineProperty(transformation.args, key, {
31 // if(transformation.hasRun) {
32 // //console.log('returning via path');
33 // return transformation.document.getNodeByPath(path);
35 // //console.log('returning original arg');
44 // potem spr na dotychczasowych undo/redo tests;
47 this.args.forEach(function(arg, idx, args) {
50 if(arg.nodeType) { // ~
52 Object.defineProperty(args, idx, {
54 if(transformation.hasRun && path) {
55 return transformation.document.getNodeByPath(path);
61 } else if(_.isObject(arg)) {
62 _.keys(arg).forEach(function(key) {
65 if(value && value.nodeType) {
66 path = value.getPath();
67 Object.defineProperty(arg, key, {
69 if(transformation.hasRun && path) {
70 return transformation.document.getNodeByPath(path);
82 this.document = document;
88 _.extend(GenericTransformation.prototype, {
93 changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root;
94 this.snapshot = changeRoot.clone();
95 this.changeRootPath = changeRoot.getPath();
97 //var toret = desc.impl.call(this.context, this.args); // a argumenty do metody?
98 var toret = desc.impl.apply(this.context, this.args);
104 desc.undo.call(this.context);
106 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
111 return GenericTransformation;
113 // var T = createGenericTransformation({impl: function() {}});
114 // var t = T(doc, {a:1,b:2,c3:3});
117 toret.createContextTransformation = function(desc, name) {
118 // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation
119 var GenericTransformation = toret.createGenericTransformation(desc, name);
121 var ContextTransformation = function(document, object, args) {
122 GenericTransformation.call(this, document, args);
124 if(document === object) {
125 this.context = document;
127 var contextPath = object.getPath(),
128 transformation = this;
129 Object.defineProperty(this, 'context', {
131 // todo: to jakos inaczej, bo np. this.context w undo transformacji before to juz nie ten sam obiekt
132 // moze transformacja powinna zwracac zmodyfikowana sciezke do obiektu po dzialaniu run?
134 if(transformation.hasRun) {
135 //console.log('returning via path');
136 return transformation.document.getNodeByPath(contextPath);
138 //console.log('returning original arg');
146 ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
147 return ContextTransformation;
149 // var T = createContextTransformation({impl: function() {}});
150 // var t = T(doc, node, {a:1,b:2,c3:3});
155 toret.TransformationStorage = function() {
156 this._transformations = {};
159 _.extend(toret.TransformationStorage.prototype, {
161 register: function(Transformation) {
162 var list = (this._transformations[Transformation.prototype.name] = this._transformations[Transformation.prototype.name] || []);
163 list.push(Transformation);
166 get: function(name) {
167 var transformations = this._transformations[name];
168 if(!transformations) {
169 throw new Error('Transformation "' + name + '" not found!');
171 // na razie zwraca pierwsza
172 return transformations[0];
178 // var registerTransformationFromMethod = (object, methodName, desc) {
179 // if(!object[methodName]) {
180 // throw new Exeption('Cannot register transformation from unknown method ' + methodName + ' on ' + object);
182 // desc.impl = object[name];
183 // Transformation = createContextTransformation(desc);
184 // object.prototype.registerContextTransformation(name, createContextTransformation(method));
188 // registerTransformationFromMethod(ElementNode, 'setAttr', {
189 // impl: function(args) {
190 // this.setAttr(args.name, args.value);
192 // getChangeRoot: function() {
193 // return this.context;