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;
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, {
90 run: function(options) {
92 if(!desc.undo && options.beUndoable) {
93 if(desc.getChangeRoot) {
94 changeRoot = desc.getChangeRoot.call(this);
97 'Transformation {name} returned invalid change root value'
98 .replace('{name}', name)
102 changeRoot = this.document.root;
104 this.snapshot = changeRoot.clone();
105 this.changeRootPath = changeRoot.getPath();
107 var argsToPass = desc.undo ? [this].concat(this.args) : this.args;
108 var toret = desc.impl.apply(this.context, argsToPass);
114 desc.undo.call(this.context, this);
116 this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
121 return GenericTransformation;
124 toret.createContextTransformation = function(desc, name) {
125 var GenericTransformation = toret.createGenericTransformation(desc, name);
127 var ContextTransformation = function(document, object, args) {
128 GenericTransformation.call(this, document, args);
130 if(document === object) {
131 this.context = document;
133 var contextPath = object.getPath(),
134 transformation = this;
135 Object.defineProperty(this, 'context', {
137 if(transformation.hasRun) {
138 return transformation.document.getNodeByPath(contextPath);
146 ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
147 return ContextTransformation;