From 7d5b36d983059e14ca56dc746bcd8b941c5848cd Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Wed, 13 May 2015 19:44:14 -0700 Subject: [PATCH] When you delete an empty bullet, focus the previous bullet. You can't delete bullets with children even if blank --- dist/flowy.js | 44 +++++++++++++++++++++++++++++++++-------- dist/flowy.unwrapped.js | 44 +++++++++++++++++++++++++++++++++-------- src/models/todo.js | 23 +++++++++++++++++++++ src/views/app.js | 1 + src/views/todo.js | 20 +++++++++++-------- 5 files changed, 108 insertions(+), 24 deletions(-) diff --git a/dist/flowy.js b/dist/flowy.js index 99d7cf9..a9c1e73 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -40,15 +40,19 @@ var TodoView = Backbone.View.extend({ if (e.keyCode !== 8 /* backspace */ && e.keyCode !== 46 /* delete */) { return; } - if (this.model.get("text") !== "") { - return; + if (this.model.get("text") === "") { // Or focus is at the beginning + if (this.model.hasChildren()) { + return; + } + // TODO: Focus end of previous node + var previousNode = this.model.previousNode(this.model.collection); + if (!previousNode) { + return; + } + e.preventDefault(); + this.model.remove(this.model.collection); + previousNode.getView().startEditingText({"atEnd":true}); } - e.preventDefault(); - // TODO: Focus end of previous node - //var previousNode = this.model.previousNode(this.model.collection); - this.model.remove(this.model.collection); - //var previousNodeView = this.collectionView.getViewFor(previousNode); - //var previousNodeView.startEditingText({"atEnd":true}); }, textChange: function(e) { var collection = this.model.collection; @@ -150,14 +154,37 @@ var TodoModel = Backbone.Model.extend({ setText: function(text) { this.save({text: text}); }, + hasChildren: function() { + return this.get("bullets").length > 0; + }, getChildren: function(collection) { return _.map(this.get("bullets"), function(id) { return collection.get(id); }, this); }, + getChild: function(collection, index) { + return collection.get(this.get("bullets")[index]); + }, getParent: function(collection) { return collection.get(this.get("parent")); }, + getPreviousSibling: function(collection) { + var parent = this.getParent(collection); + var index = parent.findChild(this.id); + if (index <= 0) return undefined; + return parent.getChild(collection, index-1); + }, + previousNode: function(collection) { + var previous = this.getPreviousSibling(collection) || this.getParent(collection); + if (previous.id === collection.rootId) { + return undefined; + } else { + return previous; + } + }, + getView: function() { //TODO: remove + return this.collection.app.getView(this); + }, isTopLevel: function(collection) { return this.get("parent") === collection.rootId; }, @@ -305,6 +332,7 @@ var AppView = Backbone.View.extend({ initialize: function(options) { options = _.defaults({}, options, appDefaults); this.list = options.list || new FlowyDocModel(); + this.list.app = this; this.listenTo(this.list, 'add', this.addOne); this.listenTo(this.list, 'reset', this.addAll); var self = this; diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index e956425..52df4b3 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -39,15 +39,19 @@ var TodoView = Backbone.View.extend({ if (e.keyCode !== 8 /* backspace */ && e.keyCode !== 46 /* delete */) { return; } - if (this.model.get("text") !== "") { - return; + if (this.model.get("text") === "") { // Or focus is at the beginning + if (this.model.hasChildren()) { + return; + } + // TODO: Focus end of previous node + var previousNode = this.model.previousNode(this.model.collection); + if (!previousNode) { + return; + } + e.preventDefault(); + this.model.remove(this.model.collection); + previousNode.getView().startEditingText({"atEnd":true}); } - e.preventDefault(); - // TODO: Focus end of previous node - //var previousNode = this.model.previousNode(this.model.collection); - this.model.remove(this.model.collection); - //var previousNodeView = this.collectionView.getViewFor(previousNode); - //var previousNodeView.startEditingText({"atEnd":true}); }, textChange: function(e) { var collection = this.model.collection; @@ -149,14 +153,37 @@ var TodoModel = Backbone.Model.extend({ setText: function(text) { this.save({text: text}); }, + hasChildren: function() { + return this.get("bullets").length > 0; + }, getChildren: function(collection) { return _.map(this.get("bullets"), function(id) { return collection.get(id); }, this); }, + getChild: function(collection, index) { + return collection.get(this.get("bullets")[index]); + }, getParent: function(collection) { return collection.get(this.get("parent")); }, + getPreviousSibling: function(collection) { + var parent = this.getParent(collection); + var index = parent.findChild(this.id); + if (index <= 0) return undefined; + return parent.getChild(collection, index-1); + }, + previousNode: function(collection) { + var previous = this.getPreviousSibling(collection) || this.getParent(collection); + if (previous.id === collection.rootId) { + return undefined; + } else { + return previous; + } + }, + getView: function() { //TODO: remove + return this.collection.app.getView(this); + }, isTopLevel: function(collection) { return this.get("parent") === collection.rootId; }, @@ -304,6 +331,7 @@ var AppView = Backbone.View.extend({ initialize: function(options) { options = _.defaults({}, options, appDefaults); this.list = options.list || new FlowyDocModel(); + this.list.app = this; this.listenTo(this.list, 'add', this.addOne); this.listenTo(this.list, 'reset', this.addAll); var self = this; diff --git a/src/models/todo.js b/src/models/todo.js index 9a78d11..f5c3b63 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -17,14 +17,37 @@ var TodoModel = Backbone.Model.extend({ setText: function(text) { this.save({text: text}); }, + hasChildren: function() { + return this.get("bullets").length > 0; + }, getChildren: function(collection) { return _.map(this.get("bullets"), function(id) { return collection.get(id); }, this); }, + getChild: function(collection, index) { + return collection.get(this.get("bullets")[index]); + }, getParent: function(collection) { return collection.get(this.get("parent")); }, + getPreviousSibling: function(collection) { + var parent = this.getParent(collection); + var index = parent.findChild(this.id); + if (index <= 0) return undefined; + return parent.getChild(collection, index-1); + }, + previousNode: function(collection) { + var previous = this.getPreviousSibling(collection) || this.getParent(collection); + if (previous.id === collection.rootId) { + return undefined; + } else { + return previous; + } + }, + getView: function() { //TODO: remove + return this.collection.app.getView(this); + }, isTopLevel: function(collection) { return this.get("parent") === collection.rootId; }, diff --git a/src/views/app.js b/src/views/app.js index 202c746..d8d900b 100644 --- a/src/views/app.js +++ b/src/views/app.js @@ -63,6 +63,7 @@ var AppView = Backbone.View.extend({ initialize: function(options) { options = _.defaults({}, options, appDefaults); this.list = options.list || new FlowyDocModel(); + this.list.app = this; this.listenTo(this.list, 'add', this.addOne); this.listenTo(this.list, 'reset', this.addAll); var self = this; diff --git a/src/views/todo.js b/src/views/todo.js index 145ab7b..3c5d35e 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -39,15 +39,19 @@ var TodoView = Backbone.View.extend({ if (e.keyCode !== 8 /* backspace */ && e.keyCode !== 46 /* delete */) { return; } - if (this.model.get("text") !== "") { - return; + if (this.model.get("text") === "") { // Or focus is at the beginning + if (this.model.hasChildren()) { + return; + } + // TODO: Focus end of previous node + var previousNode = this.model.previousNode(this.model.collection); + if (!previousNode) { + return; + } + e.preventDefault(); + this.model.remove(this.model.collection); + previousNode.getView().startEditingText({"atEnd":true}); } - e.preventDefault(); - // TODO: Focus end of previous node - //var previousNode = this.model.previousNode(this.model.collection); - this.model.remove(this.model.collection); - //var previousNodeView = this.collectionView.getViewFor(previousNode); - //var previousNodeView.startEditingText({"atEnd":true}); }, textChange: function(e) { var collection = this.model.collection; -- 2.47.3