refactoring: move getNearest/Next/PreviousTextElement out of DocumentElment
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 3 Apr 2014 08:59:31 +0000 (10:59 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Tue, 6 May 2014 09:37:47 +0000 (11:37 +0200)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/documentElement.js
src/editor/modules/documentCanvas/canvas/keyboard.js

index 0ec22a1..a43799c 100644 (file)
@@ -233,7 +233,7 @@ $.extend(Canvas.prototype, Backbone.Events, {
 
         this.eventBus.on('elementToggled', function(toggle, element) {
             if(!toggle) {
-                canvas.setCurrentElement(element.getPreviousTextElement());
+                canvas.setCurrentElement(canvas.getPreviousTextElement(element));
             }
         });
     },
@@ -270,6 +270,20 @@ $.extend(Canvas.prototype, Backbone.Events, {
         }
     },
 
+    getPreviousTextElement: function(relativeToElement, includeInvisible) {
+        return this.getNearestTextElement('above', relativeToElement, includeInvisible);
+    },
+
+    getNextTextElement: function(relativeToElement, includeInvisible) {
+        return this.getNearestTextElement('below', relativeToElement, includeInvisible);
+    },
+
+    getNearestTextElement: function(direction, relativeToElement, includeInvisible) {
+        includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
+        var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
+        return this.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, relativeToElement.dom()[0]));
+    },
+
     contains: function(element) {
         return element.dom().parents().index(this.wrapper) !== -1;
     },
index 1da7b3e..7b7f7f9 100644 (file)
@@ -51,20 +51,6 @@ $.extend(DocumentElement.prototype, {
         return other && (typeof other === typeof this) && other.dom()[0] === this.dom()[0];
     },
 
-    getPreviousTextElement: function(includeInvisible) {
-        return this.getNearestTextElement('above', includeInvisible);
-    },
-
-    getNextTextElement: function(includeInvisible) {
-        return this.getNearestTextElement('below', includeInvisible);
-    },
-
-    getNearestTextElement: function(direction, includeInvisible) {
-        includeInvisible = includeInvisible !== undefined ? includeInvisible : false;
-        var selector = '[document-text-element]' + (includeInvisible ? '' : ':visible');
-        return this.canvas.getDocumentElement(utils.nearestInDocumentOrder(selector, direction, this.dom()[0]));
-    },
-
     trigger: function() {
         //this.canvas.bus.trigger()
     }
index 4cf3d77..f0439d6 100644 (file)
@@ -226,7 +226,7 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
 
             if(direction === 'above') {
                 if(start.offsetAtBeginning) {
-                    goto = start.element.getNearestTextElement('above');
+                    goto = canvas.getNearestTextElement('above', start.element);
                     caretTo = 'end';
                 } else {
                     goto = start.element;
@@ -234,7 +234,7 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
                 }
             } else {
                 if(end.offsetAtEnd) {
-                    goto = start.element.getNearestTextElement('below');
+                    goto = canvas.getNearestTextElement('below', start.element);
                     caretTo = 'start';
                 } else {
                     goto = end.element;
@@ -283,18 +283,18 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
                     if(grandParent && grandParent.children().length === 1) {
                         goto = grandParent.wlxmlNode.append({text: ''});
                     } else {
-                        goto = element.getNearestTextElement(direction);
+                        goto = canvas.getNearestTextElement(direction, element);
                     }
                     parent.wlxmlNode.detach();
                 } else {
-                    goto = element.getNearestTextElement(direction);
+                    goto = canvas.getNearestTextElement(direction, element);
                     element.wlxmlNode.detach();
                 }
                 canvas.setCurrentElement(goto, {caretTo: caretTo});
             }
             else if(cursorAtOperationEdge) {
                 if(direction === 'below') {
-                    element = element.getNearestTextElement(direction);
+                    element = canvas.getNearestTextElement(direction, element);
                 }
                 if(element) {
                     goto = element.wlxmlNode.mergeContentUp();