]> git.za3k.com Git - flowy.git/commitdiff
Make indent work, minus focus
authorZachary Vance <vanceza@gmail.com>
Wed, 27 May 2015 03:10:26 +0000 (20:10 -0700)
committerZachary Vance <vanceza@gmail.com>
Wed, 27 May 2015 03:10:26 +0000 (20:10 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
src/models/todo.js
src/views/app.js
src/views/todo.js

index c41bc863cecc7533cae55df4b7cfbe293a3593e2..36a70234c8fa39c2871dd965a347c9fd9aa0f7a7 100644 (file)
@@ -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(/<br\\?>/);
@@ -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();
index 512f6d9f1f64db3a44c005ac5f0ed2cbf9f2d6c1..eec138a1e3e8586614cee0c75df0a7f76f3f90fd 100644 (file)
@@ -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(/<br\\?>/);
@@ -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();
index dc28a52d58774884ed38a44e19179e808b142306..72415724f17607de03c73e6bd5acc2f3953b6de3 100644 (file)
@@ -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() {
index 2d8f3670de2d8f51ae4751644edfa2602334b9b0..d641806d98aeb62ac2c096175d19e86c3c0726ef 100644 (file)
@@ -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();
index 5c0b41dfc32e1d82b118a3bd5d8c7c898bc4d064..2c7307a09258b94329c624cae2609cb149b475a8 100644 (file)
@@ -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(/<br\\?>/);
@@ -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