]> git.za3k.com Git - flowy.git/commitdiff
Add delete functionality and shortcut. Competing item should never focus root.
authorZachary Vance <vanceza@gmail.com>
Mon, 25 May 2015 23:46:51 +0000 (16:46 -0700)
committerZachary Vance <vanceza@gmail.com>
Mon, 25 May 2015 23:46:51 +0000 (16:46 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
src/models/todo.js
src/views/todo.js

index f4d420ec7ff7b78ee905d6526a4204cb000a4b3e..2382082ba62e2c52ee35a9574492f944520b8c33 100644 (file)
@@ -403,8 +403,8 @@ var TodoView = Backbone.View.extend({
     "keydown > .text": "keydown",
     'Shortcut("combinePrevious", "Combine an item with the previous item", "backspace") > .text': "combinePrevious",
     'Shortcut("combineNext", "Combine an item with the next item", "del") > .text': "combineNext",
-    'Shortcut("next", "Next", "down") > .text': 'next',
-    'Shortcut("previous", "Previous", "up") > .text': 'previous',
+    'Shortcut("next", "Next", "down") > .text': 'focusNext',
+    'Shortcut("previous", "Previous", "up") > .text': 'focusPrevious',
     'Shortcut("zoomIn", "Not Done - Zoom in", "alt+right") > .text': 'zoomIn',
     'Shortcut("zoomOut", "Not Done - Zoom out", "alt+left") > .text': 'zoomOut',
     'Shortcut("expand", "Not Done - Expand / collapse", "ctrl+space") > .text': 'expand',
@@ -413,7 +413,7 @@ var TodoView = Backbone.View.extend({
     'Shortcut("moveDown", "Not Done - Move", "alt+shift+down") > .text': 'moveDown',
     'Shortcut("moveUp", "Not Done - Move", "alt+shift+up") > .text': 'moveUp',
     'Shortcut("toggleComplete", "Complete", "ctrl+enter") > .text': "toggleComplete",
-    'Shortcut("delete", "Not Done - Delete", "ctrl+shift+del") > .text': 'delete',
+    'Shortcut("delete", "Delete", "ctrl+shift+backspace") > .text': 'delete',
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -457,20 +457,20 @@ var TodoView = Backbone.View.extend({
     } else {
         this.stopEditingText();
         this.model.toggleComplete();
-        var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false}) || this.model.getParent(this.model.collection);
+        var next = this.model.nextNode(this.model.collection, { childrenAllowed: false}) || this.model.getParent(this.model.collection, { rootAllowed: false});
         if (!next) return false;
         next.getView().startEditingText();
     }
     return false;
   },
-  next: function() {
+  focusNext: function() {
     var nextNode = this.model.nextNode(this.model.collection, { childrenAllowed: true });
     if (!nextNode) return false;
     this.stopEditingText();
     nextNode.getView().startEditingText();
     return false;
   },
-  previous: function() {
+  focusPrevious: function() {
     var previousNode = this.model.previousNode(this.model.collection, { childrenAllowed: true });
     if (!previousNode) return false;
     this.stopEditingText();
@@ -520,7 +520,9 @@ var TodoView = Backbone.View.extend({
   },
   "delete": function() {
     // Delete node and its entire subtree
-    console.log("Delete not implemented"); // TODO
+    this.focusPrevious();
+    this.model.removeAll(this.model.collection);
+    return false;
   },
   indent: function() {
     // Last child of previous sibling, then nothing
@@ -672,8 +674,11 @@ var TodoModel = Backbone.Model.extend({
     getChild: function(collection, index) {
         return collection.get(this.get("bullets")[index]);
     },
-    getParent: function(collection) {
-        return collection.get(this.get("parent"));
+    getParent: function(collection, options) {
+        options = _.defaults({}, options, { rootAllowed: true });
+        var parent = collection.get(this.get("parent"));
+        if (parent && parent.id == collection.rootId && !options.rootAllowed) return undefined;
+        return parent;
     },
     getPreviousSibling: function(collection) {
         var parent = this.getParent(collection);
@@ -759,6 +764,19 @@ var TodoModel = Backbone.Model.extend({
     remove: function(collection) {
         this.getParent(collection).removeChild(this, collection);
     },
+    removeAll: function(collection) {
+        if (collection.rootId == this.id) {
+            console.log("Cannot remove root node of collection");
+            return;
+        }
+        if (this.hasChildren()) {
+            var children = _.clone(this.getChildren(collection));
+            _.each(children, function(child) {
+                child.removeAll(collection);
+            });
+        }
+        this.remove(collection);
+    },
     addTodoBefore: function(todo, collection) {
         // Todo always goes as the previous sibling
         var parent = this.getParent(collection);
index ffb2d2ee5f881797c26cebe73300a40cb1cff1db..dd1e01361e8bdfe945061accd0d0b8968c752eb6 100644 (file)
@@ -402,8 +402,8 @@ var TodoView = Backbone.View.extend({
     "keydown > .text": "keydown",
     'Shortcut("combinePrevious", "Combine an item with the previous item", "backspace") > .text': "combinePrevious",
     'Shortcut("combineNext", "Combine an item with the next item", "del") > .text': "combineNext",
-    'Shortcut("next", "Next", "down") > .text': 'next',
-    'Shortcut("previous", "Previous", "up") > .text': 'previous',
+    'Shortcut("next", "Next", "down") > .text': 'focusNext',
+    'Shortcut("previous", "Previous", "up") > .text': 'focusPrevious',
     'Shortcut("zoomIn", "Not Done - Zoom in", "alt+right") > .text': 'zoomIn',
     'Shortcut("zoomOut", "Not Done - Zoom out", "alt+left") > .text': 'zoomOut',
     'Shortcut("expand", "Not Done - Expand / collapse", "ctrl+space") > .text': 'expand',
@@ -412,7 +412,7 @@ var TodoView = Backbone.View.extend({
     'Shortcut("moveDown", "Not Done - Move", "alt+shift+down") > .text': 'moveDown',
     'Shortcut("moveUp", "Not Done - Move", "alt+shift+up") > .text': 'moveUp',
     'Shortcut("toggleComplete", "Complete", "ctrl+enter") > .text': "toggleComplete",
-    'Shortcut("delete", "Not Done - Delete", "ctrl+shift+del") > .text': 'delete',
+    'Shortcut("delete", "Delete", "ctrl+shift+backspace") > .text': 'delete',
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -456,20 +456,20 @@ var TodoView = Backbone.View.extend({
     } else {
         this.stopEditingText();
         this.model.toggleComplete();
-        var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false}) || this.model.getParent(this.model.collection);
+        var next = this.model.nextNode(this.model.collection, { childrenAllowed: false}) || this.model.getParent(this.model.collection, { rootAllowed: false});
         if (!next) return false;
         next.getView().startEditingText();
     }
     return false;
   },
-  next: function() {
+  focusNext: function() {
     var nextNode = this.model.nextNode(this.model.collection, { childrenAllowed: true });
     if (!nextNode) return false;
     this.stopEditingText();
     nextNode.getView().startEditingText();
     return false;
   },
-  previous: function() {
+  focusPrevious: function() {
     var previousNode = this.model.previousNode(this.model.collection, { childrenAllowed: true });
     if (!previousNode) return false;
     this.stopEditingText();
@@ -519,7 +519,9 @@ var TodoView = Backbone.View.extend({
   },
   "delete": function() {
     // Delete node and its entire subtree
-    console.log("Delete not implemented"); // TODO
+    this.focusPrevious();
+    this.model.removeAll(this.model.collection);
+    return false;
   },
   indent: function() {
     // Last child of previous sibling, then nothing
@@ -671,8 +673,11 @@ var TodoModel = Backbone.Model.extend({
     getChild: function(collection, index) {
         return collection.get(this.get("bullets")[index]);
     },
-    getParent: function(collection) {
-        return collection.get(this.get("parent"));
+    getParent: function(collection, options) {
+        options = _.defaults({}, options, { rootAllowed: true });
+        var parent = collection.get(this.get("parent"));
+        if (parent && parent.id == collection.rootId && !options.rootAllowed) return undefined;
+        return parent;
     },
     getPreviousSibling: function(collection) {
         var parent = this.getParent(collection);
@@ -758,6 +763,19 @@ var TodoModel = Backbone.Model.extend({
     remove: function(collection) {
         this.getParent(collection).removeChild(this, collection);
     },
+    removeAll: function(collection) {
+        if (collection.rootId == this.id) {
+            console.log("Cannot remove root node of collection");
+            return;
+        }
+        if (this.hasChildren()) {
+            var children = _.clone(this.getChildren(collection));
+            _.each(children, function(child) {
+                child.removeAll(collection);
+            });
+        }
+        this.remove(collection);
+    },
     addTodoBefore: function(todo, collection) {
         // Todo always goes as the previous sibling
         var parent = this.getParent(collection);
index c33a2cfa9107711aacf308e2fbbe625863812951..d5d366a8581ad6018cb29ed1b3fb8f159860603e 100644 (file)
@@ -31,8 +31,11 @@ var TodoModel = Backbone.Model.extend({
     getChild: function(collection, index) {
         return collection.get(this.get("bullets")[index]);
     },
-    getParent: function(collection) {
-        return collection.get(this.get("parent"));
+    getParent: function(collection, options) {
+        options = _.defaults({}, options, { rootAllowed: true });
+        var parent = collection.get(this.get("parent"));
+        if (parent && parent.id == collection.rootId && !options.rootAllowed) return undefined;
+        return parent;
     },
     getPreviousSibling: function(collection) {
         var parent = this.getParent(collection);
@@ -118,6 +121,19 @@ var TodoModel = Backbone.Model.extend({
     remove: function(collection) {
         this.getParent(collection).removeChild(this, collection);
     },
+    removeAll: function(collection) {
+        if (collection.rootId == this.id) {
+            console.log("Cannot remove root node of collection");
+            return;
+        }
+        if (this.hasChildren()) {
+            var children = _.clone(this.getChildren(collection));
+            _.each(children, function(child) {
+                child.removeAll(collection);
+            });
+        }
+        this.remove(collection);
+    },
     addTodoBefore: function(todo, collection) {
         // Todo always goes as the previous sibling
         var parent = this.getParent(collection);
index f7816a141f3dd44c638f14cfc88e0ece730a95f7..67a850ab80fd26432c5486bc786c92540ab2de12 100644 (file)
@@ -13,8 +13,8 @@ var TodoView = Backbone.View.extend({
     "keydown > .text": "keydown",
     'Shortcut("combinePrevious", "Combine an item with the previous item", "backspace") > .text': "combinePrevious",
     'Shortcut("combineNext", "Combine an item with the next item", "del") > .text': "combineNext",
-    'Shortcut("next", "Next", "down") > .text': 'next',
-    'Shortcut("previous", "Previous", "up") > .text': 'previous',
+    'Shortcut("next", "Next", "down") > .text': 'focusNext',
+    'Shortcut("previous", "Previous", "up") > .text': 'focusPrevious',
     'Shortcut("zoomIn", "Not Done - Zoom in", "alt+right") > .text': 'zoomIn',
     'Shortcut("zoomOut", "Not Done - Zoom out", "alt+left") > .text': 'zoomOut',
     'Shortcut("expand", "Not Done - Expand / collapse", "ctrl+space") > .text': 'expand',
@@ -23,7 +23,7 @@ var TodoView = Backbone.View.extend({
     'Shortcut("moveDown", "Not Done - Move", "alt+shift+down") > .text': 'moveDown',
     'Shortcut("moveUp", "Not Done - Move", "alt+shift+up") > .text': 'moveUp',
     'Shortcut("toggleComplete", "Complete", "ctrl+enter") > .text': "toggleComplete",
-    'Shortcut("delete", "Not Done - Delete", "ctrl+shift+del") > .text': 'delete',
+    'Shortcut("delete", "Delete", "ctrl+shift+backspace") > .text': 'delete',
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -67,20 +67,20 @@ var TodoView = Backbone.View.extend({
     } else {
         this.stopEditingText();
         this.model.toggleComplete();
-        var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false}) || this.model.getParent(this.model.collection);
+        var next = this.model.nextNode(this.model.collection, { childrenAllowed: false}) || this.model.getParent(this.model.collection, { rootAllowed: false});
         if (!next) return false;
         next.getView().startEditingText();
     }
     return false;
   },
-  next: function() {
+  focusNext: function() {
     var nextNode = this.model.nextNode(this.model.collection, { childrenAllowed: true });
     if (!nextNode) return false;
     this.stopEditingText();
     nextNode.getView().startEditingText();
     return false;
   },
-  previous: function() {
+  focusPrevious: function() {
     var previousNode = this.model.previousNode(this.model.collection, { childrenAllowed: true });
     if (!previousNode) return false;
     this.stopEditingText();
@@ -130,7 +130,9 @@ var TodoView = Backbone.View.extend({
   },
   "delete": function() {
     // Delete node and its entire subtree
-    console.log("Delete not implemented"); // TODO
+    this.focusPrevious();
+    this.model.removeAll(this.model.collection);
+    return false;
   },
   indent: function() {
     // Last child of previous sibling, then nothing