]> git.za3k.com Git - flowy.git/commitdiff
Control toggles whether a node is complete or not
authorZachary Vance <vanceza@gmail.com>
Fri, 15 May 2015 06:59:03 +0000 (23:59 -0700)
committerZachary Vance <vanceza@gmail.com>
Fri, 15 May 2015 06:59:03 +0000 (23:59 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
src/models/todo.js
src/views/todo.js

index 5d057d3da041d3a832f59a8e259b56cff7728594..3d81e669878f3beb8aa9d68fc1fd5cf9a60ee379 100644 (file)
@@ -72,7 +72,7 @@ var TodoView = Backbone.View.extend({
     //"click > .checkbox": "toggleComplete",
     "input > .text": "textChange",
     "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end
-    "keydown > .text": "backspace",
+    "keydown > .text": "keydown",
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -110,10 +110,22 @@ var TodoView = Backbone.View.extend({
   decodeText: function(encodedText) {
     return $("<div/>").html(encodedText).text();
   },
-  backspace: function(e) {
-    if (e.keyCode !== 46 /* backspace */ && e.keyCode !== 8 /* delete */) {
-        return;
+  keydown: function(e) {
+    if (e.keyCode == 46 /* backspace */ || e.keyCode == 8 /* delete */) {
+        return this.backspace(e);
+    } else if (e.keyCode == 17) {
+        return this.control(e);
+    } else {
+        console.log(e.keyCode + " key pressed");
     }
+  },
+  control: function(e) {
+    this.stopEditingText();
+    this.model.toggleComplete();
+    var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false});
+    next.getView().startEditingText();
+  },
+  backspace: function(e) {
     if (this.model.hasChildren()) {
         return;
     }
@@ -232,6 +244,9 @@ var TodoModel = Backbone.Model.extend({
     hasChildren: function() {
         return this.get("bullets").length > 0;
     },
+    getChildrenCount: function() {
+        return this.get("bullets").length;
+    },
     getChildren: function(collection) {
         return _.map(this.get("bullets"), function(id) {
             return collection.get(id);
@@ -246,8 +261,28 @@ var TodoModel = Backbone.Model.extend({
     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);
+        if (index < 0 || index === 0) return undefined;
+        return parent.getChild(collection, index - 1);
+    },
+    getNextSibling: function(collection) {
+        var parent = this.getParent(collection);
+        var index = parent.findChild(this.id);
+        var numChildren = parent.getChildrenCount();
+        if (index < 0 || index === numChildren - 1) return undefined;
+        return parent.getChild(collection, index + 1);
+    },
+    nextNode: function(collection, options) {
+        options = _.defaults({}, options, {"childrenAllowed": true});
+        if (options.childrenAllowed) {
+            if (this.hasChildren()) {
+                return this.getChild(collection, 0);
+            }
+        }
+        for (var node = this; node && node.id !== collection.rootId; node = node.getParent(collection)) {
+            var next = node.getNextSibling(collection);
+            if (next) return next;
+        }
+        return undefined;
     },
     previousNode: function(collection) {
         var previous = this.getPreviousSibling(collection) || this.getParent(collection);
index 64cdaa71d3536041eb42cc5f3b28b97efdf79a9c..1d2033a637993512d5419b35debe2d9802486ecd 100644 (file)
@@ -71,7 +71,7 @@ var TodoView = Backbone.View.extend({
     //"click > .checkbox": "toggleComplete",
     "input > .text": "textChange",
     "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end
-    "keydown > .text": "backspace",
+    "keydown > .text": "keydown",
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -109,10 +109,22 @@ var TodoView = Backbone.View.extend({
   decodeText: function(encodedText) {
     return $("<div/>").html(encodedText).text();
   },
-  backspace: function(e) {
-    if (e.keyCode !== 46 /* backspace */ && e.keyCode !== 8 /* delete */) {
-        return;
+  keydown: function(e) {
+    if (e.keyCode == 46 /* backspace */ || e.keyCode == 8 /* delete */) {
+        return this.backspace(e);
+    } else if (e.keyCode == 17) {
+        return this.control(e);
+    } else {
+        console.log(e.keyCode + " key pressed");
     }
+  },
+  control: function(e) {
+    this.stopEditingText();
+    this.model.toggleComplete();
+    var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false});
+    next.getView().startEditingText();
+  },
+  backspace: function(e) {
     if (this.model.hasChildren()) {
         return;
     }
@@ -231,6 +243,9 @@ var TodoModel = Backbone.Model.extend({
     hasChildren: function() {
         return this.get("bullets").length > 0;
     },
+    getChildrenCount: function() {
+        return this.get("bullets").length;
+    },
     getChildren: function(collection) {
         return _.map(this.get("bullets"), function(id) {
             return collection.get(id);
@@ -245,8 +260,28 @@ var TodoModel = Backbone.Model.extend({
     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);
+        if (index < 0 || index === 0) return undefined;
+        return parent.getChild(collection, index - 1);
+    },
+    getNextSibling: function(collection) {
+        var parent = this.getParent(collection);
+        var index = parent.findChild(this.id);
+        var numChildren = parent.getChildrenCount();
+        if (index < 0 || index === numChildren - 1) return undefined;
+        return parent.getChild(collection, index + 1);
+    },
+    nextNode: function(collection, options) {
+        options = _.defaults({}, options, {"childrenAllowed": true});
+        if (options.childrenAllowed) {
+            if (this.hasChildren()) {
+                return this.getChild(collection, 0);
+            }
+        }
+        for (var node = this; node && node.id !== collection.rootId; node = node.getParent(collection)) {
+            var next = node.getNextSibling(collection);
+            if (next) return next;
+        }
+        return undefined;
     },
     previousNode: function(collection) {
         var previous = this.getPreviousSibling(collection) || this.getParent(collection);
index f5c3b63bea23e9812d22f5fb05aefc4e902fadd1..8c031099b041d41a77e28366a4c8968d1c3517e5 100644 (file)
@@ -20,6 +20,9 @@ var TodoModel = Backbone.Model.extend({
     hasChildren: function() {
         return this.get("bullets").length > 0;
     },
+    getChildrenCount: function() {
+        return this.get("bullets").length;
+    },
     getChildren: function(collection) {
         return _.map(this.get("bullets"), function(id) {
             return collection.get(id);
@@ -34,8 +37,28 @@ var TodoModel = Backbone.Model.extend({
     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);
+        if (index < 0 || index === 0) return undefined;
+        return parent.getChild(collection, index - 1);
+    },
+    getNextSibling: function(collection) {
+        var parent = this.getParent(collection);
+        var index = parent.findChild(this.id);
+        var numChildren = parent.getChildrenCount();
+        if (index < 0 || index === numChildren - 1) return undefined;
+        return parent.getChild(collection, index + 1);
+    },
+    nextNode: function(collection, options) {
+        options = _.defaults({}, options, {"childrenAllowed": true});
+        if (options.childrenAllowed) {
+            if (this.hasChildren()) {
+                return this.getChild(collection, 0);
+            }
+        }
+        for (var node = this; node && node.id !== collection.rootId; node = node.getParent(collection)) {
+            var next = node.getNextSibling(collection);
+            if (next) return next;
+        }
+        return undefined;
     },
     previousNode: function(collection) {
         var previous = this.getPreviousSibling(collection) || this.getParent(collection);
index 298a45a5b60ac42ffa52a3b59ae4e2cce1421486..81cbb5c02e83fef944b1da9cec1d2d9cea2f0f29 100644 (file)
@@ -10,7 +10,7 @@ var TodoView = Backbone.View.extend({
     //"click > .checkbox": "toggleComplete",
     "input > .text": "textChange",
     "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end
-    "keydown > .text": "backspace",
+    "keydown > .text": "keydown",
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -48,10 +48,22 @@ var TodoView = Backbone.View.extend({
   decodeText: function(encodedText) {
     return $("<div/>").html(encodedText).text();
   },
-  backspace: function(e) {
-    if (e.keyCode !== 46 /* backspace */ && e.keyCode !== 8 /* delete */) {
-        return;
+  keydown: function(e) {
+    if (e.keyCode == 46 /* backspace */ || e.keyCode == 8 /* delete */) {
+        return this.backspace(e);
+    } else if (e.keyCode == 17) {
+        return this.control(e);
+    } else {
+        console.log(e.keyCode + " key pressed");
     }
+  },
+  control: function(e) {
+    this.stopEditingText();
+    this.model.toggleComplete();
+    var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false});
+    next.getView().startEditingText();
+  },
+  backspace: function(e) {
     if (this.model.hasChildren()) {
         return;
     }