From 67f6a271cb5f3d8f136fe090d333037f253dff73 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Thu, 14 May 2015 23:59:03 -0700 Subject: [PATCH] Control toggles whether a node is complete or not --- dist/flowy.js | 47 +++++++++++++++++++++++++++++++++++------ dist/flowy.unwrapped.js | 47 +++++++++++++++++++++++++++++++++++------ src/models/todo.js | 27 +++++++++++++++++++++-- src/views/todo.js | 20 ++++++++++++++---- 4 files changed, 123 insertions(+), 18 deletions(-) diff --git a/dist/flowy.js b/dist/flowy.js index 5d057d3..3d81e66 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -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 $("
").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); diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index 64cdaa7..1d2033a 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -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 $("
").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); diff --git a/src/models/todo.js b/src/models/todo.js index f5c3b63..8c03109 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -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); diff --git a/src/views/todo.js b/src/views/todo.js index 298a45a..81cbb5c 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -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 $("
").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; } -- 2.47.3