1 define(function(require) {
5 var _ = require('libs/underscore'),
8 var getTransDesc = function(desc) {
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;
46 var patchObject = function(obj, depth) {
47 depth = _.isNumber(depth) ? depth : 1;
51 _.keys(obj).forEach(function(key) {
56 path = value.getPath();
57 Object.defineProperty(obj, key, {
59 if(transformation.hasRun && path) {
60 return transformation.document.getNodeByPath(path);
66 } else if(_.isObject(value)) {
67 patchObject(value, depth+1);
73 this.args.forEach(function(arg, idx, args) {
76 if(arg.nodeType) { // ~
78 Object.defineProperty(args, idx, {
80 if(transformation.hasRun && path) {
81 return transformation.document.getNodeByPath(path);
87 } else if(_.isObject(arg)) {
93 this.document = document;
99 _.extend(GenericTransformation.prototype, {
101 run: function(options) {
103 if(!desc.undo && options.beUndoable) {
104 changeRoot = this.getChangeRoot();
107 'Transformation {name} returned invalid change root value'
108 .replace('{name}', name)
111 this.changeRootPath = changeRoot.getPath();
112 this.snapshot = changeRoot.clone();
114 var argsToPass = desc.undo ? [this].concat(this.args) : this.args;
115 var toret = desc.impl.apply(this.context, argsToPass);
121 desc.undo.call(this.context, this);
123 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
126 getChangeRoot: desc.getChangeRoot || function() {
127 return this.document.root;
131 return GenericTransformation;
134 toret.createContextTransformation = function(desc, name) {
135 var GenericTransformation = toret.createGenericTransformation(desc, name);
137 var ContextTransformation = function(document, object, args) {
138 GenericTransformation.call(this, document, args);
140 if(document === object) {
141 this.context = document;
143 var contextPath = object.getPath(),
144 transformation = this;
145 Object.defineProperty(this, 'context', {
147 if(transformation.hasRun) {
148 return transformation.document.getNodeByPath(contextPath);
156 ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
157 return ContextTransformation;