053ed10e103f04d9e6d7a630ad5e0ef1ed67394b
[fnpeditor.git] / src / smartxml / transformations.js
1 define(function(require) {
2     
3 'use strict';
4
5 var _ = require('libs/underscore'),
6     toret = {};
7
8 var getTransDesc = function(desc) {
9     if(typeof desc === 'function') {
10         desc = {impl: desc};
11     }
12     if(!desc.impl) {
13         throw new Error('Got transformation description without implementation.');
14     }
15     return desc;
16 };
17
18 toret.createGenericTransformation = function(desc, name) {
19     desc = getTransDesc(desc);
20     
21     var GenericTransformation = function(document, args) {
22         this.args = args || [];
23
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, {
30         //             get: function() {
31         //                 if(transformation.hasRun) {
32         //                     //console.log('returning via path');
33         //                     return transformation.document.getNodeByPath(path);
34         //                 } else {
35         //                     //console.log('returning original arg');
36         //                     return value;
37
38         //                 }
39         //             }
40         //         });
41         //     }
42         // });
43
44         // potem spr na dotychczasowych undo/redo tests;
45         
46
47         this.args.forEach(function(arg, idx, args) {
48             var path;
49             if(arg) {
50                 if(arg.nodeType) { // ~
51                     path = arg.getPath();
52                     Object.defineProperty(args, idx, {
53                         get: function() {
54                             if(transformation.hasRun && path) {
55                                 return transformation.document.getNodeByPath(path);
56                             } else {
57                                 return arg;
58                             }
59                         }
60                     });
61                 } else if(_.isObject(arg)) {
62                     _.keys(arg).forEach(function(key) {
63                         var value = arg[key],
64                             path;
65                         if(value && value.nodeType) {
66                             path = value.getPath();
67                             Object.defineProperty(arg, key, {
68                                 get: function() {
69                                     if(transformation.hasRun && path) {
70                                         return transformation.document.getNodeByPath(path);
71                                     } else {
72                                         return value;
73                                     }
74                                 }
75                             });
76                         }
77                     });
78                 }
79             }
80         });
81
82         this.document = document;
83         this.hasRun = false;
84         if(desc.init) {
85             desc.init.call(this);
86         }
87     };
88     _.extend(GenericTransformation.prototype, {
89         name: name,
90         run: function(options) {
91             var changeRoot;
92             if(!desc.undo && options.beUndoable) {
93                 changeRoot = this.getChangeRoot();
94                 if(!changeRoot) {
95                      throw new Error(
96                          'Transformation {name} returned invalid change root value'
97                          .replace('{name}', name)
98                      );
99                 }
100                 this.changeRootPath = changeRoot.getPath();
101                 this.snapshot = changeRoot.clone();
102             }
103             var argsToPass = desc.undo ? [this].concat(this.args) : this.args;
104             var toret = desc.impl.apply(this.context, argsToPass);
105             this.hasRun = true;
106             return toret;
107         },
108         undo: function() {
109             if(desc.undo) {
110                 desc.undo.call(this.context, this);
111             } else {
112                 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
113             }
114         },
115         getChangeRoot: desc.getChangeRoot || function() {
116             return this.document.root;
117         }
118     });
119
120     return GenericTransformation;
121 };
122
123 toret.createContextTransformation = function(desc, name) {
124     var GenericTransformation = toret.createGenericTransformation(desc, name);
125
126     var ContextTransformation = function(document, object, args) {
127         GenericTransformation.call(this, document, args);
128
129         if(document === object) {
130             this.context = document;
131         } else {
132             var contextPath = object.getPath(),
133                 transformation = this;
134             Object.defineProperty(this, 'context', {
135                 get: function() {
136                     if(transformation.hasRun) {
137                         return transformation.document.getNodeByPath(contextPath);
138                     } else {
139                         return object;
140                     }
141                 }
142             });
143         }
144     };
145     ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
146     return ContextTransformation;
147 };
148
149 return toret;
150
151 });