]> git.za3k.com Git - flowy.git/commitdiff
Add move up and down
authorZachary Vance <vanceza@gmail.com>
Thu, 28 May 2015 02:09:19 +0000 (19:09 -0700)
committerZachary Vance <vanceza@gmail.com>
Thu, 28 May 2015 02:09:19 +0000 (19:09 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
src/models/todo.js
src/views/todo.js

index 547ad42d99ac8ced88ffa730522e904cda00f78c..5594612e3666f7edeed48614941376279241cfbc 100644 (file)
@@ -419,8 +419,8 @@ var TodoView = Backbone.View.extend({
     'Shortcut("expand", "Not Done - Expand / collapse", "ctrl+space") > .text': 'expand',
     'Shortcut("indent", "Indent", "tab,alt+shift+right") > .text': 'indent',
     'Shortcut("outdent", "Outdent", "shift+tab,alt+shift+left") > .text': 'outdent',
-    'Shortcut("moveDown", "Not Done - Move", "alt+shift+down") > .text': 'moveDown',
-    'Shortcut("moveUp", "Not Done - Move", "alt+shift+up") > .text': 'moveUp',
+    'Shortcut("moveDown", "Move", "alt+shift+down") > .text': 'moveDown',
+    'Shortcut("moveUp", "Move", "alt+shift+up") > .text': 'moveUp',
     'Shortcut("toggleComplete", "Complete", "ctrl+enter") > .text': "toggleComplete",
     'Shortcut("delete", "Delete", "ctrl+shift+backspace") > .text': 'delete',
   },
@@ -735,6 +735,9 @@ var TodoModel = Backbone.Model.extend({
     getChild: function(index) {
         return this.collection.get(this.get("bullets")[index]);
     },
+    lastChild: function() {
+        return this.getChild(this.getChildrenCount()-1);
+    },
     getParent: function(options) {
         options = _.defaults({}, options, { rootAllowed: true });
         var parent = this.collection.get(this.get("parent"));
@@ -785,7 +788,7 @@ var TodoModel = Backbone.Model.extend({
             return previous;
         }
     },
-    getView: function() { //TODO: remove
+    getView: function() {
         return this.collection.app.getView(this);
     },
     isTopLevel: function() {
@@ -840,10 +843,59 @@ var TodoModel = Backbone.Model.extend({
         return this;
     },
     moveUp: function() {
-        // Before previous sibling, then as last child of previous node of parent, then before parent, then nothing
+        // Before previous sibling, then as last descendent of previous node of parent, then before parent, then nothing
+        var previousSibling = this.getPreviousSibling();
+        if (previousSibling) {
+            return this.moveTo({
+                parent: this.getParent(),
+                index: this.getParent().findChild(previousSibling.id),
+            });
+        }
+        var parent = this.getParent();
+        if (parent.isRoot()) return undefined; // Top-level node with no previous sibling
+        var previousSiblingOfParent = parent.getPreviousSibling();
+        if (previousSiblingOfParent) {
+            // Insert as final child
+            return this.moveTo({
+                parent: previousSiblingOfParent,
+                index: previousSiblingOfParent.getChildrenCount(),
+            });
+        } else {
+            // This is a first child of a first child of ... the first top-level node.
+            // Place before the parent.
+            var grandparent = parent.getParent();
+            var parentIndex = grandparent.findChild(parent.id);
+            return this.moveTo({
+                parent: grandparent,
+                index: parentIndex,
+            });
+        }
     },
     moveDown: function() {
-        // After next sibling, then as first child of next node after parent, then up one level, then nothing
+        // After next sibling, then as first child of next node after parent, then up one level (after parent), then nothing
+        var nextSibling = this.getNextSibling();
+        var parent = this.getParent();
+        if (nextSibling) {
+            return this.moveTo({
+                parent: parent,
+                index: parent.findChild(nextSibling.id),
+            });
+        }
+        var parentNextSibling = parent.getNextSibling();
+        if (parentNextSibling) {
+            return this.moveTo({
+                parent: parentNextSibling,
+                index: 0,
+            });
+        } else if (!this.isTopLevel()) { // Last child of last child of ... of last top-level node
+            var grandparent = parent.getParent();
+            return this.moveTo({
+                parent: grandparent,
+                index: grandparent.findChild(parent.id) + 1,
+            });
+        } else {
+            return undefined; // Last top-level node
+        }
     },
     indent: function() {
         // Last child of previous sibling, then nothing
index c168b12adcd058f7cb9d11fcdc0175b77dd6a4d4..ae7f1810833a4d20dec5c6d1557ce453bc64f3d6 100644 (file)
@@ -418,8 +418,8 @@ var TodoView = Backbone.View.extend({
     'Shortcut("expand", "Not Done - Expand / collapse", "ctrl+space") > .text': 'expand',
     'Shortcut("indent", "Indent", "tab,alt+shift+right") > .text': 'indent',
     'Shortcut("outdent", "Outdent", "shift+tab,alt+shift+left") > .text': 'outdent',
-    'Shortcut("moveDown", "Not Done - Move", "alt+shift+down") > .text': 'moveDown',
-    'Shortcut("moveUp", "Not Done - Move", "alt+shift+up") > .text': 'moveUp',
+    'Shortcut("moveDown", "Move", "alt+shift+down") > .text': 'moveDown',
+    'Shortcut("moveUp", "Move", "alt+shift+up") > .text': 'moveUp',
     'Shortcut("toggleComplete", "Complete", "ctrl+enter") > .text': "toggleComplete",
     'Shortcut("delete", "Delete", "ctrl+shift+backspace") > .text': 'delete',
   },
@@ -734,6 +734,9 @@ var TodoModel = Backbone.Model.extend({
     getChild: function(index) {
         return this.collection.get(this.get("bullets")[index]);
     },
+    lastChild: function() {
+        return this.getChild(this.getChildrenCount()-1);
+    },
     getParent: function(options) {
         options = _.defaults({}, options, { rootAllowed: true });
         var parent = this.collection.get(this.get("parent"));
@@ -784,7 +787,7 @@ var TodoModel = Backbone.Model.extend({
             return previous;
         }
     },
-    getView: function() { //TODO: remove
+    getView: function() {
         return this.collection.app.getView(this);
     },
     isTopLevel: function() {
@@ -839,10 +842,59 @@ var TodoModel = Backbone.Model.extend({
         return this;
     },
     moveUp: function() {
-        // Before previous sibling, then as last child of previous node of parent, then before parent, then nothing
+        // Before previous sibling, then as last descendent of previous node of parent, then before parent, then nothing
+        var previousSibling = this.getPreviousSibling();
+        if (previousSibling) {
+            return this.moveTo({
+                parent: this.getParent(),
+                index: this.getParent().findChild(previousSibling.id),
+            });
+        }
+        var parent = this.getParent();
+        if (parent.isRoot()) return undefined; // Top-level node with no previous sibling
+        var previousSiblingOfParent = parent.getPreviousSibling();
+        if (previousSiblingOfParent) {
+            // Insert as final child
+            return this.moveTo({
+                parent: previousSiblingOfParent,
+                index: previousSiblingOfParent.getChildrenCount(),
+            });
+        } else {
+            // This is a first child of a first child of ... the first top-level node.
+            // Place before the parent.
+            var grandparent = parent.getParent();
+            var parentIndex = grandparent.findChild(parent.id);
+            return this.moveTo({
+                parent: grandparent,
+                index: parentIndex,
+            });
+        }
     },
     moveDown: function() {
-        // After next sibling, then as first child of next node after parent, then up one level, then nothing
+        // After next sibling, then as first child of next node after parent, then up one level (after parent), then nothing
+        var nextSibling = this.getNextSibling();
+        var parent = this.getParent();
+        if (nextSibling) {
+            return this.moveTo({
+                parent: parent,
+                index: parent.findChild(nextSibling.id),
+            });
+        }
+        var parentNextSibling = parent.getNextSibling();
+        if (parentNextSibling) {
+            return this.moveTo({
+                parent: parentNextSibling,
+                index: 0,
+            });
+        } else if (!this.isTopLevel()) { // Last child of last child of ... of last top-level node
+            var grandparent = parent.getParent();
+            return this.moveTo({
+                parent: grandparent,
+                index: grandparent.findChild(parent.id) + 1,
+            });
+        } else {
+            return undefined; // Last top-level node
+        }
     },
     indent: function() {
         // Last child of previous sibling, then nothing
index b58241ce4bdb1a66d4e95607c47025c81f1b1aca..ab6a810bc574326029b3253060adedb1fb8b05cf 100644 (file)
@@ -31,6 +31,9 @@ var TodoModel = Backbone.Model.extend({
     getChild: function(index) {
         return this.collection.get(this.get("bullets")[index]);
     },
+    lastChild: function() {
+        return this.getChild(this.getChildrenCount()-1);
+    },
     getParent: function(options) {
         options = _.defaults({}, options, { rootAllowed: true });
         var parent = this.collection.get(this.get("parent"));
@@ -81,7 +84,7 @@ var TodoModel = Backbone.Model.extend({
             return previous;
         }
     },
-    getView: function() { //TODO: remove
+    getView: function() {
         return this.collection.app.getView(this);
     },
     isTopLevel: function() {
@@ -136,10 +139,59 @@ var TodoModel = Backbone.Model.extend({
         return this;
     },
     moveUp: function() {
-        // Before previous sibling, then as last child of previous node of parent, then before parent, then nothing
+        // Before previous sibling, then as last descendent of previous node of parent, then before parent, then nothing
+        var previousSibling = this.getPreviousSibling();
+        if (previousSibling) {
+            return this.moveTo({
+                parent: this.getParent(),
+                index: this.getParent().findChild(previousSibling.id),
+            });
+        }
+        var parent = this.getParent();
+        if (parent.isRoot()) return undefined; // Top-level node with no previous sibling
+        var previousSiblingOfParent = parent.getPreviousSibling();
+        if (previousSiblingOfParent) {
+            // Insert as final child
+            return this.moveTo({
+                parent: previousSiblingOfParent,
+                index: previousSiblingOfParent.getChildrenCount(),
+            });
+        } else {
+            // This is a first child of a first child of ... the first top-level node.
+            // Place before the parent.
+            var grandparent = parent.getParent();
+            var parentIndex = grandparent.findChild(parent.id);
+            return this.moveTo({
+                parent: grandparent,
+                index: parentIndex,
+            });
+        }
     },
     moveDown: function() {
-        // After next sibling, then as first child of next node after parent, then up one level, then nothing
+        // After next sibling, then as first child of next node after parent, then up one level (after parent), then nothing
+        var nextSibling = this.getNextSibling();
+        var parent = this.getParent();
+        if (nextSibling) {
+            return this.moveTo({
+                parent: parent,
+                index: parent.findChild(nextSibling.id),
+            });
+        }
+        var parentNextSibling = parent.getNextSibling();
+        if (parentNextSibling) {
+            return this.moveTo({
+                parent: parentNextSibling,
+                index: 0,
+            });
+        } else if (!this.isTopLevel()) { // Last child of last child of ... of last top-level node
+            var grandparent = parent.getParent();
+            return this.moveTo({
+                parent: grandparent,
+                index: grandparent.findChild(parent.id) + 1,
+            });
+        } else {
+            return undefined; // Last top-level node
+        }
     },
     indent: function() {
         // Last child of previous sibling, then nothing
index 1a774b8833fdbcb1737b7f2bcde43cb2cc32b42a..f0806d5cf5ac318d2c50d6fbdc570bb7b6ba1f7d 100644 (file)
@@ -22,8 +22,8 @@ var TodoView = Backbone.View.extend({
     'Shortcut("expand", "Not Done - Expand / collapse", "ctrl+space") > .text': 'expand',
     'Shortcut("indent", "Indent", "tab,alt+shift+right") > .text': 'indent',
     'Shortcut("outdent", "Outdent", "shift+tab,alt+shift+left") > .text': 'outdent',
-    'Shortcut("moveDown", "Not Done - Move", "alt+shift+down") > .text': 'moveDown',
-    'Shortcut("moveUp", "Not Done - Move", "alt+shift+up") > .text': 'moveUp',
+    'Shortcut("moveDown", "Move", "alt+shift+down") > .text': 'moveDown',
+    'Shortcut("moveUp", "Move", "alt+shift+up") > .text': 'moveUp',
     'Shortcut("toggleComplete", "Complete", "ctrl+enter") > .text': "toggleComplete",
     'Shortcut("delete", "Delete", "ctrl+shift+backspace") > .text': 'delete',
   },