]> git.za3k.com Git - flowy.git/commitdiff
When you delete an empty bullet, focus the previous bullet. You can't delete bullets...
authorZachary Vance <vanceza@gmail.com>
Thu, 14 May 2015 02:44:14 +0000 (19:44 -0700)
committerZachary Vance <vanceza@gmail.com>
Thu, 14 May 2015 02:44:14 +0000 (19:44 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
src/models/todo.js
src/views/app.js
src/views/todo.js

index 99d7cf9dca4d4f8280353098bc8740826f3f4f65..a9c1e73e119cf3d7566e29cdace6be7160e3616a 100644 (file)
@@ -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;
index e9564258db0dd91d16c29266b88b07ede6bb7832..52df4b366f4daab1f00aa1f39e9fb63d9709b5f9 100644 (file)
@@ -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;
index 9a78d119fb835c9c80c01e80140172ab0ee0906e..f5c3b63bea23e9812d22f5fb05aefc4e902fadd1 100644 (file)
@@ -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;
     },
index 202c746853030265d81f2d45df6e7f469584482e..d8d900b796028cbc3bb37b863a9ee5c47aca8670 100644 (file)
@@ -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;
index 145ab7b388324d5be9232e34f37d3c98adb436e6..3c5d35ececae595ee632640474d3208a989cb48c 100644 (file)
@@ -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;