From: Zachary Vance Date: Thu, 28 May 2015 02:09:19 +0000 (-0700) Subject: Add move up and down X-Git-Url: https://git.za3k.com/?a=commitdiff_plain;h=d8715a7ce01c7d7566f67f461f73fc92742015f4;p=flowy.git Add move up and down --- diff --git a/dist/flowy.js b/dist/flowy.js index 547ad42..5594612 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -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 diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index c168b12..ae7f181 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -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 diff --git a/src/models/todo.js b/src/models/todo.js index b58241c..ab6a810 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -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 diff --git a/src/views/todo.js b/src/views/todo.js index 1a774b8..f0806d5 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -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', },