editor: fix - perform inserting empty footnote operation within one transaction
[fnpeditor.git] / src / smartxml / transformations.js
1 define(function(require) {
2     
3 'use strict';
4
5 var _ = require('libs/underscore'),
6     toret = {};
7
8
9 var getTransDesc = function(desc) {
10     if(typeof desc === 'function') {
11         desc = {impl: desc};
12     }
13     if(!desc.impl) {
14         throw new Error('Got transformation description without implementation.');
15     }
16     return desc;
17 };
18
19 toret.createGenericTransformation = function(desc, name) {
20     desc = getTransDesc(desc);
21     
22     var GenericTransformation = function(document, args) {
23         this.args = args || [];
24         var transformation = this;
25         
26         var patchObject = function(obj, depth) {
27             depth = _.isNumber(depth) ? depth : 1;
28             if(depth > 3) {
29                 return;
30             }
31             _.keys(obj).forEach(function(key) {
32                 var value = obj[key];
33                 if(value) {
34                     if(value.nodeType) {
35                         transformation.wrapNodeProperty(obj, key);
36                     } else if(_.isObject(value)) {
37                         patchObject(value, depth+1);
38                     }
39                 }
40             });
41         };
42
43         this.args.forEach(function(arg, idx, args) {
44             if(arg) {
45                 if(arg.nodeType) { // ~
46                     transformation.wrapNodeProperty(args, idx);
47                 } else if(_.isObject(arg)) {
48                     patchObject(arg);
49                 }
50             }
51         });
52
53         this.document = document;
54         this.runCount = 0;
55         if(desc.init) {
56             desc.init.call(this);
57         }
58     };
59     _.extend(GenericTransformation.prototype, {
60         name: name,
61         run: function(options) {
62             var changeRoot;
63             if(!desc.undo && options.beUndoable) {
64                 changeRoot = this.getChangeRoot();
65                 if(!changeRoot) {
66                      throw new Error(
67                          'Transformation {name} returned invalid change root value'
68                          .replace('{name}', name)
69                      );
70                 }
71                 this.changeRootPath = changeRoot.getPath();
72                 this.snapshot = changeRoot.clone();
73             }
74             var argsToPass = desc.undo ? [this].concat(this.args) : this.args;
75             var toret = desc.impl.apply(this.context, argsToPass);
76             this.runCount++;
77             return toret;
78         },
79         undo: function() {
80             if(desc.undo) {
81                 desc.undo.call(this.context, this);
82             } else {
83                 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
84             }
85         },
86         getChangeRoot: desc.getChangeRoot || function() {
87             return this.document.root;
88         },
89         wrapNodeProperty: function(object, propName, value) {
90             var transformation = this,
91                 lastRunNumber = 0,
92                 path;
93             
94             value = value || object[propName];
95             if(value && value.nodeType) {
96                 path = value.getPath();
97                 Object.defineProperty(object, propName, {
98                     get: function() {
99                         if((lastRunNumber !== transformation.runCount) && path) {
100                             value = transformation.document.getNodeByPath(path);
101                             lastRunNumber = transformation.runCount;
102                         }
103                         return value;
104                     }
105                 });
106             }
107         }
108     });
109
110     return GenericTransformation;
111 };
112
113 toret.createContextTransformation = function(desc, name) {
114     var GenericTransformation = toret.createGenericTransformation(desc, name);
115
116     var ContextTransformation = function(document, object, args) {
117         GenericTransformation.call(this, document, args);
118
119         if(document === object) {
120             this.context = document;
121         } else {
122             this.wrapNodeProperty(this, 'context', object);
123         }
124     };
125     ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
126     return ContextTransformation;
127 };
128
129 return toret;
130
131 });