pre d: setattr, settext, split as ContextTransformations; detach in old format; pre...
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 27 Nov 2013 15:10:11 +0000 (16:10 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 2 Dec 2013 21:56:12 +0000 (22:56 +0100)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/keyboard.js
src/editor/modules/documentCanvas/canvas/wlxmlListener.js
src/editor/modules/documentToolbar/documentToolbar.js
src/editor/modules/nodePane/nodePane.js
src/smartxml/smartxml.js

index b6ddf71..e181195 100644 (file)
@@ -38,7 +38,8 @@ $.extend(TextHandler.prototype, {
         }
     },
     setText: function(text, node) {
         }
     },
     setText: function(text, node) {
-        this.canvas.wlxmlDocument.transform('setText', {node:node, text: text});
+        //this.canvas.wlxmlDocument.transform('setText', {node:node, text: text});
+        node.transform('setText', {text: text});
 
     }
 
 
     }
 
@@ -104,7 +105,7 @@ $.extend(Canvas.prototype, {
                         mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
                         canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
                     }
                         mutation.target.data = mutation.target.data.replace(utils.unicode.ZWS, '');
                         canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
                     }
-                    observer.observe(canvas.d[0], config);
+                    observer.observe(canvas.wrapper[0], config);
                     canvas.publisher('contentChanged');
 
                     var textElement = canvas.getDocumentElement(mutation.target),
                     canvas.publisher('contentChanged');
 
                     var textElement = canvas.getDocumentElement(mutation.target),
@@ -119,7 +120,7 @@ $.extend(Canvas.prototype, {
             });
         });
         var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
             });
         });
         var config = { attributes: false, childList: false, characterData: true, subtree: true, characterDataOldValue: true};
-        observer.observe(this.d[0], config);
+        observer.observe(this.wrapper[0], config);
 
 
         this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
 
 
         this.wrapper.on('mouseover', '[document-node-element], [document-text-element]', function(e) {
index 16f2f68..dfe3f66 100644 (file)
@@ -74,7 +74,8 @@ handlers.push({key: KEYS.ENTER,
                     return false; // top level element is unsplittable
                 }
 
                     return false; // top level element is unsplittable
                 }
 
-                var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
+                //var nodes = position.element.data('wlxmlNode').split({offset: position.offset}),
+                var nodes = position.element.data('wlxmlNode').transform('split', {offset: position.offset}),
                     newEmpty,
                     goto,
                     gotoOptions;
                     newEmpty,
                     goto,
                     gotoOptions;
@@ -85,7 +86,7 @@ handlers.push({key: KEYS.ENTER,
                     newEmpty = nodes.second;
                 
                 if(newEmpty) {
                     newEmpty = nodes.second;
                 
                 if(newEmpty) {
-                    goto = newEmpty.append({text: ''});
+                    //goto = newEmpty.append({text: ''});
                     gotoOptions = {};
                 } else {
                     goto = nodes.second;
                     gotoOptions = {};
                 } else {
                     goto = nodes.second;
index 80da027..ffabfec 100644 (file)
@@ -68,6 +68,7 @@ var handlers = {
         canvasNode.detach();
     },
     nodeTextChange: function(event) {
         canvasNode.detach();
     },
     nodeTextChange: function(event) {
+        console.log('wlxmlListener: ' + event.meta.node.getText());
         var canvasElement = utils.findCanvasElement(event.meta.node),
             toSet = event.meta.node.getText();
         if(toSet === '') {
         var canvasElement = utils.findCanvasElement(event.meta.node),
             toSet = event.meta.node.getText();
         if(toSet === '') {
index 7fbf6c1..75fcc72 100644 (file)
@@ -36,6 +36,12 @@ return function(sandbox) {
                     params.meta = meta;
                 }
 
                     params.meta = meta;
                 }
 
+                if(command === 'undo' || command === 'redo') {
+                    params.callback = function(disable) {
+                        btn.attr('disabled', !disable);
+                    }
+                }
+
                 sandbox.publish('command', command, params);
             });
         },
                 sandbox.publish('command', command, params);
             });
         },
index 7473c2e..dbdd951 100644 (file)
@@ -18,7 +18,11 @@ return function(sandbox) {
         var attr = target.attr('class').split('-')[3] === 'tagSelect' ? 'Tag' : 'Class',
             value = target.val().replace(/-/g, '.');
                 
         var attr = target.attr('class').split('-')[3] === 'tagSelect' ? 'Tag' : 'Class',
             value = target.val().replace(/-/g, '.');
                 
-        currentNode['set' + attr](value);
+        if(attr === 'Class') {
+            //currentNode.document.transform('setClass', {node: currentNode, klass: value});
+            currentNode.transform('setAttr', {name: 'class', value: value});
+        }
+        //currentNode['set' + attr](value);
     });
     
     return {
     });
     
     return {
index 7b59b18..1379153 100644 (file)
@@ -43,6 +43,16 @@ var DocumentNode = function(nativeNode, document) {
 };
 
 $.extend(DocumentNode.prototype, {
 };
 
 $.extend(DocumentNode.prototype, {
+
+    transform: function(name, args) {
+        var Transformation = contextTransformations[name],
+            transformation;
+        if(Transformation) {
+            transformation = new Transformation(this.document, this, args);
+        }
+        return this.document.transform(transformation);
+    },
+
     _setNativeNode: function(nativeNode) {
         this.nativeNode = nativeNode;
         this._$ = $(nativeNode);
     _setNativeNode: function(nativeNode) {
         this.nativeNode = nativeNode;
         this._$ = $(nativeNode);
@@ -643,14 +653,21 @@ $.extend(Document.prototype, Backbone.Events, {
 
     transform: function(transformationName, args) {
         console.log('transform');
 
     transform: function(transformationName, args) {
         console.log('transform');
-        var Transformation = transformations[transformationName],
-            transformation;
-        if(Transformation) {
-            transformation = new Transformation(args);
-            transformation.run();
+        var Transformation, transformation, toret;
+        if(typeof transformationName === 'string') {
+            Transformation = transformations[transformationName];
+            if(Transformation) {
+                transformation = new Transformation(args);
+            }
+        } else {
+            transformation = transformationName;
+        }
+        if(transformation) {
+            toret = transformation.run();
             this.undoStack.push(transformation);
             console.log('clearing redo stack');
             this.redoStack = [];
             this.undoStack.push(transformation);
             console.log('clearing redo stack');
             this.redoStack = [];
+            return toret;
         } else {
             throw new Error('Transformation ' + transformationName + ' doesn\'t exist!');
         }
         } else {
             throw new Error('Transformation ' + transformationName + ' doesn\'t exist!');
         }
@@ -806,6 +823,111 @@ var createTransformation = function(desc) {
     return NodeTransformation;
 }
 
     return NodeTransformation;
 }
 
+///
+var Transformation = function(args) {
+    this.args = args;
+};
+$.extend(Transformation.prototype, {
+    run: function() {
+        throw new Error('not implemented');
+    },
+    undo: function() {
+        throw new Error('not implemented');
+    },
+});
+
+var createGenericTransformation = function(desc) {
+    var GenericTransformation = function(document, args) {
+        //document.getNodeByPath(contextPath).call(this, args);
+        this.args = args;
+        this.document = document;
+    };
+    $.extend(GenericTransformation.prototype, {
+        run: function() {
+            var changeRoot = desc.getChangeRoot ? desc.getChangeRoot.call(this) : this.document.root;
+            this.snapshot = changeRoot.clone();
+            this.changeRootPath = changeRoot.getPath();
+            return desc.impl.call(this.context, this.args); // a argumenty do metody?
+        },
+        undo: function() {
+            this.document.getNodeByPath(this.changeRootPath).replaceWith(this.snapshot);
+        },
+    });
+
+    return GenericTransformation;
+};
+
+// var T = createGenericTransformation({impl: function() {}});
+// var t = T(doc, {a:1,b:2,c3:3});
+
+
+var createContextTransformation = function(desc) {
+    // mozna sie pozbyc przez przeniesienie object/context na koniec argumentow konstruktora generic transformation
+    var GenericTransformation = createGenericTransformation(desc);
+
+    var ContextTransformation = function(document, object, args) {
+        var contextPath = object.getPath();
+
+        GenericTransformation.call(this, document, args);
+        
+        Object.defineProperty(this, 'context', {
+            get: function() {
+                return document.getNodeByPath(contextPath);
+            }
+        });
+    }
+    ContextTransformation.prototype = Object.create(GenericTransformation.prototype);
+    return ContextTransformation;
+}
+// var T = createContextTransformation({impl: function() {}});
+// var t = T(doc, node, {a:1,b:2,c3:3});
+///
+
+var contextTransformations = {};
+contextTransformations['setText'] = createContextTransformation({
+    impl: function(args) {
+        this.setText(args.text);
+    },
+    getChangeRoot: function() {
+        return this.context;
+    }
+});
+
+contextTransformations['setAttr'] = createContextTransformation({
+    impl: function(args) {
+        this.setAttr(args.name, args.value);
+    },
+    getChangeRoot: function() {
+        return this.context;
+    }
+});
+
+contextTransformations['split'] = createContextTransformation({
+    impl: function(args) {
+        return this.split({offset: args.offset});
+    }//,
+    // getChangeRoot: function() {
+    //     return this.context.parent().parent();
+    // }
+});
+
+// var TRANSFORMATION2 = function(f, getChangeRoot, undo) {
+//     var context = this,
+
+
+//     var transformation = createContextTransformation({
+//         impl: f,
+//         getChangeRoot: getChangeRoot,
+
+//     });
+
+//     var toret = function() {
+//         var 
+//         f.apply(context, createArgs ? createArgs(arguments) : arguments)
+//     };
+//     return toret;
+// }
+
 transformations['detach2'] = createTransformation({
     // impl: function() {
     //     //this.setAttr('class', 'cite'); //  
 transformations['detach2'] = createTransformation({
     // impl: function() {
     //     //this.setAttr('class', 'cite'); //  
@@ -817,7 +939,7 @@ transformations['detach2'] = createTransformation({
 
 });
 
 
 });
 
-transformations['setText'] = createTransformation({
+transformations['setText-old'] = createTransformation({
     impl: function(args) {
         this.setText(args.text)
     },
     impl: function(args) {
         this.setText(args.text)
     },
@@ -827,6 +949,15 @@ transformations['setText'] = createTransformation({
 
 });
 
 
 });
 
+transformations['setClass-old'] = createTransformation({
+    impl: function(args) {
+        this.setClass(args.klass);
+    },
+    getRoot: function(node) {
+        return node;
+    }
+})
+
 //3. detach z pełnym własnym redo
 
 var Detach3NodeTransformation = function(args) {
 //3. detach z pełnym własnym redo
 
 var Detach3NodeTransformation = function(args) {
@@ -867,6 +998,22 @@ $.extend(Detach3NodeTransformation.prototype, {
 });
 transformations['detach3'] = Detach3NodeTransformation;
 
 });
 transformations['detach3'] = Detach3NodeTransformation;
 
+
+var registerTransformationsFromObject = function(object) {
+    _.pairs(object).filter(function(pair) {
+        var property = pair[1];
+        return typeof property === 'function' && property._isTransformation;
+    })
+    .forEach(function(pair) {
+        var name = pair[0],
+            method = pair[1];
+        object.registerTransformation(name, createContextTransformation(method));
+    });
+};
+registerTransformationsFromObject(ElementNode.prototype);
+registerTransformationsFromObject(TextNode.prototype);
+registerTransformationsFromObject(Document.prototype);
+
 return {
     documentFromXML: function(xml) {
         return new Document(xml);
 return {
     documentFromXML: function(xml) {
         return new Document(xml);