linting, cleanup, removing unused code
[fnpeditor.git] / src / smartxml / smartxml.js
index 2ed9c47..9c233bd 100644 (file)
@@ -10,9 +10,6 @@ define([
 'use strict';
 /* globals Node */
 
-var TEXT_NODE = Node.TEXT_NODE;
-
-
 
 var DocumentNode = function(nativeNode, document) {
     if(!document) {
@@ -37,17 +34,14 @@ $.extend(DocumentNode.prototype, {
 
     clone: function() {
         var clone = this._$.clone(true, true);
-        // clone.find('*').addBack().each(function() {
-        //     var n = $(this);
-        //     if(n.data('canvasElement')) {
-        //         n.data('canvasElement', $.extend(true, {}, n.data('canvasElement')));
-        //         n.data('canvasElement').$element = n.data('canvasElement').$element.clone(true, true);
-        //     }
-        // });
         return this.document.createDocumentNode(clone[0]);
     },
 
     getPath: function(ancestor) {
+        if(!(this.document.containsNode(this))) {
+            return null;
+        }
+
         var nodePath = [this].concat(this.parents()),
             toret, idx;
         ancestor = ancestor || this.document.root;
@@ -233,12 +227,11 @@ var parseXML = function(xml) {
 
 var registerTransformation = function(desc, name, target) {
     var Transformation = transformations.createContextTransformation(desc, name);
-    //+ to sie powinna nazywac registerTransformationFromDesc or sth
-    //+ ew. spr czy nie override (tylko jesli powyzej sa prototypy to trudno do nich dojsc)
-    target[name] = function(args) {
-        var instance = this;
+    target[name] = function() {
+        var instance = this,
+            args = Array.prototype.slice.call(arguments, 0);
         return instance.transform(Transformation, args);
-    }
+    };
 };
 
 var registerMethod = function(methodName, method, target) {
@@ -264,6 +257,8 @@ var Document = function(xml) {
     this._nodeTransformations = {};
     this._textNodeTransformations = {};
     this._elementNodeTransformations = {};
+    
+    this.registerExtension(coreTransformations);
 };
 
 $.extend(Document.prototype, Backbone.Events, {
@@ -298,8 +293,15 @@ $.extend(Document.prototype, Backbone.Events, {
         var toret = new Factory(from, this);
         _.extend(toret, this._nodeMethods);
         _.extend(toret, typeMethods);
+        
         _.extend(toret, this._nodeTransformations);
         _.extend(toret, typeTransformations);
+        
+        toret.__super__ = _.extend({}, this._nodeMethods, this._nodeTransformations);
+        _.keys(toret.__super__).forEach(function(key) {
+            toret.__super__[key] = _.bind(toret.__super__[key], toret);
+        });
+
         return toret;
     },
 
@@ -339,7 +341,6 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     trigger: function() {
-        //console.log('trigger: ' + arguments[0] + (arguments[1] ? ', ' + arguments[1].type : ''));
         Backbone.Events.trigger.apply(this, arguments);
     },
 
@@ -378,9 +379,7 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     registerExtension: function(extension) {
-        //debugger;
-        var doc = this,
-            existingPropertyNames = _.values(this);
+        var doc = this;
 
         ['document', 'documentNode', 'elementNode', 'textNode'].forEach(function(dstName) {
             var dstExtension = extension[dstName];
@@ -407,11 +406,8 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     transform: function(Transformation, args) {
-        //console.log('transform');
         var toret, transformation;
 
-        // ref: odrebnie przygotowanie transformacji, odrebnie jej wykonanie (to pierwsze to analog transform z node)
-
         if(typeof Transformation === 'function') {
             transformation = new Transformation(this, this, args);
         } else {
@@ -419,13 +415,14 @@ $.extend(Document.prototype, Backbone.Events, {
         }
         if(transformation) {
             this._transformationLevel++;
-            toret = transformation.run();
-            if(this._transformationLevel === 1) {
+            toret = transformation.run({beUndoable:this._transformationLevel === 1});
+            if(this._transformationLevel === 1 && !this._undoInProgress) {
                 this.undoStack.push(transformation);
             }
             this._transformationLevel--;
-            //console.log('clearing redo stack');
-            this.redoStack = [];
+            if(!this._undoInProgress) {
+                this.redoStack = [];
+            }
             return toret;
         } else {
             throw new Error('Transformation ' + transformation + ' doesn\'t exist!');
@@ -434,15 +431,20 @@ $.extend(Document.prototype, Backbone.Events, {
     undo: function() {
         var transformation = this.undoStack.pop();
         if(transformation) {
+            this._undoInProgress = true;
             transformation.undo();
+            this._undoInProgress = false;
             this.redoStack.push(transformation);
         }
     },
     redo: function() {
         var transformation = this.redoStack.pop();
         if(transformation) {
-            transformation.run();
+            this._transformationLevel++;
+            transformation.run({beUndoable: true});
+            this._transformationLevel--;
             this.undoStack.push(transformation);
+
         }
     },
 
@@ -469,7 +471,6 @@ $.extend(Document.prototype, Backbone.Events, {
 return {
     documentFromXML: function(xml) {
         var doc = new Document(xml);
-        doc.registerExtension(coreTransformations);
         return doc;
     },