From 41beb64f295a6cf1c67d9a857f688851379d4d27 Mon Sep 17 00:00:00 2001 From: Zachary Vance Date: Tue, 26 May 2015 20:10:26 -0700 Subject: [PATCH] Make indent work, minus focus --- dist/flowy.js | 52 +++++++++++++++++++++++++++-------------- dist/flowy.unwrapped.js | 52 +++++++++++++++++++++++++++-------------- src/models/todo.js | 2 +- src/views/app.js | 16 ++++++------- src/views/todo.js | 34 ++++++++++++++++++++------- 5 files changed, 102 insertions(+), 54 deletions(-) diff --git a/dist/flowy.js b/dist/flowy.js index c41bc86..36a7023 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -521,6 +521,14 @@ var TodoView = Backbone.View.extend({ return false; } }, + _maintainingFocus: function(f) { + var el = this.$el.find(".text")[0]; + var start = el.selectionStart, end = el.selectionEnd; + var retVal = f(); + el.focus(); + el.setSelectionRange(start, end); + return retVal; + }, "delete": function() { // Delete node and its entire subtree this.focusPrevious(); @@ -528,13 +536,16 @@ var TodoView = Backbone.View.extend({ return false; }, indent: function() { - // TODO: maintain focus - this.model.indent(); + this._maintainingFocus(_.bind(function() { + this.model.indent(); + }, this)); return false; }, outdent: function() { - // TODO: maintain focus - this.model.outdent(); + this._maintainingFocus(_.bind(function() { + this.model.outdent(); + }, this)); + return false; }, expand: function() { console.log("Expand not implemented"); // TODO @@ -548,12 +559,16 @@ var TodoView = Backbone.View.extend({ console.log("Zoom not implemented"); // TODO }, moveDown: function() { - // TODO: maintain focus - this.model.moveDown(); + this._maintainingFocus(_.bind(function() { + this.model.moveDown(); + }, this)); + return false; }, moveUp: function() { - // TODO: maintain focus - this.model.moveUp(); + this._maintainingFocus(_.bind(function() { + this.model.moveUp(); + }, this)); + return false; }, textChange: function(e) { var lines = $(e.target).html().split(//); @@ -618,6 +633,9 @@ var TodoView = Backbone.View.extend({ } return this; }, + removeChild: function(position) { + return this.$el.find("> .bullets > *").eq(position).detach(); + }, render: function() { if (this.$el.find("> .text").is(":focus")) { // Don't re-render during editing @@ -766,7 +784,7 @@ var TodoModel = Backbone.Model.extend({ newLocation.parent.save(); this.save(); } - this.collection.trigger("move", this, existingLocation); + this.collection.trigger("move", this, existingLocation, newLocation); return this; }, moveUp: function() { @@ -967,17 +985,15 @@ var AppView = Backbone.View.extend({ addOne: function(todo) { this.renderTodo(todo); }, - moveOne: function(todo, oldLocation) { - var view = this.getView(todo); - - var oldParent = oldLocation.parent; - var oldParentView; - if (oldParent) oldParentView = this.getView(oldParent); - if (oldParentView) { + moveOne: function(todo, oldLocation, newLocation) { + if (oldLocation && oldLocation.parent) { // Remove from old parent + var el = oldLocation.parent.getView().removeChild(oldLocation.index); + newLocation.parent.getView().addChild(el, newLocation.index); + } else { + // Not even on screen somehow, just render + this.renderTodo(todo, { rerender: true }); // Don't remove existing view since it's still valid. } - - this.renderTodo(todo, { rerender: true }); // Don't remove existing view since it's still valid. }, toggleShortcuts: function() { this.$(".shortcuts").toggle(); diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index 512f6d9..eec138a 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -520,6 +520,14 @@ var TodoView = Backbone.View.extend({ return false; } }, + _maintainingFocus: function(f) { + var el = this.$el.find(".text")[0]; + var start = el.selectionStart, end = el.selectionEnd; + var retVal = f(); + el.focus(); + el.setSelectionRange(start, end); + return retVal; + }, "delete": function() { // Delete node and its entire subtree this.focusPrevious(); @@ -527,13 +535,16 @@ var TodoView = Backbone.View.extend({ return false; }, indent: function() { - // TODO: maintain focus - this.model.indent(); + this._maintainingFocus(_.bind(function() { + this.model.indent(); + }, this)); return false; }, outdent: function() { - // TODO: maintain focus - this.model.outdent(); + this._maintainingFocus(_.bind(function() { + this.model.outdent(); + }, this)); + return false; }, expand: function() { console.log("Expand not implemented"); // TODO @@ -547,12 +558,16 @@ var TodoView = Backbone.View.extend({ console.log("Zoom not implemented"); // TODO }, moveDown: function() { - // TODO: maintain focus - this.model.moveDown(); + this._maintainingFocus(_.bind(function() { + this.model.moveDown(); + }, this)); + return false; }, moveUp: function() { - // TODO: maintain focus - this.model.moveUp(); + this._maintainingFocus(_.bind(function() { + this.model.moveUp(); + }, this)); + return false; }, textChange: function(e) { var lines = $(e.target).html().split(//); @@ -617,6 +632,9 @@ var TodoView = Backbone.View.extend({ } return this; }, + removeChild: function(position) { + return this.$el.find("> .bullets > *").eq(position).detach(); + }, render: function() { if (this.$el.find("> .text").is(":focus")) { // Don't re-render during editing @@ -765,7 +783,7 @@ var TodoModel = Backbone.Model.extend({ newLocation.parent.save(); this.save(); } - this.collection.trigger("move", this, existingLocation); + this.collection.trigger("move", this, existingLocation, newLocation); return this; }, moveUp: function() { @@ -966,17 +984,15 @@ var AppView = Backbone.View.extend({ addOne: function(todo) { this.renderTodo(todo); }, - moveOne: function(todo, oldLocation) { - var view = this.getView(todo); - - var oldParent = oldLocation.parent; - var oldParentView; - if (oldParent) oldParentView = this.getView(oldParent); - if (oldParentView) { + moveOne: function(todo, oldLocation, newLocation) { + if (oldLocation && oldLocation.parent) { // Remove from old parent + var el = oldLocation.parent.getView().removeChild(oldLocation.index); + newLocation.parent.getView().addChild(el, newLocation.index); + } else { + // Not even on screen somehow, just render + this.renderTodo(todo, { rerender: true }); // Don't remove existing view since it's still valid. } - - this.renderTodo(todo, { rerender: true }); // Don't remove existing view since it's still valid. }, toggleShortcuts: function() { this.$(".shortcuts").toggle(); diff --git a/src/models/todo.js b/src/models/todo.js index dc28a52..7241572 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -132,7 +132,7 @@ var TodoModel = Backbone.Model.extend({ newLocation.parent.save(); this.save(); } - this.collection.trigger("move", this, existingLocation); + this.collection.trigger("move", this, existingLocation, newLocation); return this; }, moveUp: function() { diff --git a/src/views/app.js b/src/views/app.js index 2d8f367..d641806 100644 --- a/src/views/app.js +++ b/src/views/app.js @@ -96,17 +96,15 @@ var AppView = Backbone.View.extend({ addOne: function(todo) { this.renderTodo(todo); }, - moveOne: function(todo, oldLocation) { - var view = this.getView(todo); - - var oldParent = oldLocation.parent; - var oldParentView; - if (oldParent) oldParentView = this.getView(oldParent); - if (oldParentView) { + moveOne: function(todo, oldLocation, newLocation) { + if (oldLocation && oldLocation.parent) { // Remove from old parent + var el = oldLocation.parent.getView().removeChild(oldLocation.index); + newLocation.parent.getView().addChild(el, newLocation.index); + } else { + // Not even on screen somehow, just render + this.renderTodo(todo, { rerender: true }); // Don't remove existing view since it's still valid. } - - this.renderTodo(todo, { rerender: true }); // Don't remove existing view since it's still valid. }, toggleShortcuts: function() { this.$(".shortcuts").toggle(); diff --git a/src/views/todo.js b/src/views/todo.js index 5c0b41d..2c7307a 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -129,6 +129,14 @@ var TodoView = Backbone.View.extend({ return false; } }, + _maintainingFocus: function(f) { + var el = this.$el.find(".text")[0]; + var start = el.selectionStart, end = el.selectionEnd; + var retVal = f(); + el.focus(); + el.setSelectionRange(start, end); + return retVal; + }, "delete": function() { // Delete node and its entire subtree this.focusPrevious(); @@ -136,13 +144,16 @@ var TodoView = Backbone.View.extend({ return false; }, indent: function() { - // TODO: maintain focus - this.model.indent(); + this._maintainingFocus(_.bind(function() { + this.model.indent(); + }, this)); return false; }, outdent: function() { - // TODO: maintain focus - this.model.outdent(); + this._maintainingFocus(_.bind(function() { + this.model.outdent(); + }, this)); + return false; }, expand: function() { console.log("Expand not implemented"); // TODO @@ -156,12 +167,16 @@ var TodoView = Backbone.View.extend({ console.log("Zoom not implemented"); // TODO }, moveDown: function() { - // TODO: maintain focus - this.model.moveDown(); + this._maintainingFocus(_.bind(function() { + this.model.moveDown(); + }, this)); + return false; }, moveUp: function() { - // TODO: maintain focus - this.model.moveUp(); + this._maintainingFocus(_.bind(function() { + this.model.moveUp(); + }, this)); + return false; }, textChange: function(e) { var lines = $(e.target).html().split(//); @@ -226,6 +241,9 @@ var TodoView = Backbone.View.extend({ } return this; }, + removeChild: function(position) { + return this.$el.find("> .bullets > *").eq(position).detach(); + }, render: function() { if (this.$el.find("> .text").is(":focus")) { // Don't re-render during editing -- 2.47.3