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');
43 this.document = document;
49 _.extend(GenericTransformation.prototype, {
54 changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root;
55 this.snapshot = changeRoot.clone();
56 this.changeRootPath = changeRoot.getPath();
58 var toret = desc.impl.call(this.context, this.args); // a argumenty do metody?
64 desc.undo.call(this.context);
66 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
71 return GenericTransformation;
73 // var T = createGenericTransformation({impl: function() {}});
74 // var t = T(doc, {a:1,b:2,c3:3});
77 toret.createContextTransformation = function(desc, name) {
78 // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation
79 var GenericTransformation = toret.createGenericTransformation(desc, name);
81 var ContextTransformation = function(document, object, args) {
82 GenericTransformation.call(this, document, args);
84 if(document === object) {
85 this.context = document;
87 var contextPath = object.getPath();
88 Object.defineProperty(this, 'context', {
90 // todo: to jakos inaczej, bo np. this.context w undo transformacji before to juz nie ten sam obiekt
91 // moze transformacja powinna zwracac zmodyfikowana sciezke do obiektu po dzialaniu run?
93 // tu tez trick z hasRun
94 return document.getNodeByPath(contextPath);
99 ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
100 return ContextTransformation;
102 // var T = createContextTransformation({impl: function() {}});
103 // var t = T(doc, node, {a:1,b:2,c3:3});
108 toret.TransformationStorage = function() {
109 this._transformations = {};
112 _.extend(toret.TransformationStorage.prototype, {
114 register: function(Transformation) {
115 var list = (this._transformations[Transformation.prototype.name] = this._transformations[Transformation.prototype.name] || []);
116 list.push(Transformation);
119 get: function(name) {
120 var transformations = this._transformations[name];
121 if(!transformations) {
122 throw new Error('Transformation "' + name + '" not found!');
124 // na razie zwraca pierwsza
125 return transformations[0];
131 // var registerTransformationFromMethod = (object, methodName, desc) {
132 // if(!object[methodName]) {
133 // throw new Exeption('Cannot register transformation from unknown method ' + methodName + ' on ' + object);
135 // desc.impl = object[name];
136 // Transformation = createContextTransformation(desc);
137 // object.prototype.registerContextTransformation(name, createContextTransformation(method));
141 // registerTransformationFromMethod(ElementNode, 'setAttr', {
142 // impl: function(args) {
143 // this.setAttr(args.name, args.value);
145 // getChangeRoot: function() {
146 // return this.context;