From: Zachary Vance Date: Mon, 25 May 2015 23:46:51 +0000 (-0700) Subject: Add delete functionality and shortcut. Competing item should never focus root. X-Git-Url: https://git.za3k.com/?a=commitdiff_plain;h=59f03126a8aba52358ba0f073ec0621f2d750063;p=flowy.git Add delete functionality and shortcut. Competing item should never focus root. --- diff --git a/dist/flowy.js b/dist/flowy.js index f4d420e..2382082 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -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); diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index ffb2d2e..dd1e013 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -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); diff --git a/src/models/todo.js b/src/models/todo.js index c33a2cf..d5d366a 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -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); diff --git a/src/views/todo.js b/src/views/todo.js index f7816a1..67a850a 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -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