]> git.za3k.com Git - flowy.git/commitdiff
Shortcuts work correctly
authorZachary Vance <vanceza@gmail.com>
Wed, 20 May 2015 03:44:23 +0000 (20:44 -0700)
committerZachary Vance <vanceza@gmail.com>
Wed, 20 May 2015 03:44:23 +0000 (20:44 -0700)
Gruntfile.js
dist/flowy.js
dist/flowy.unwrapped.js
src/library/shortcut.js
src/library/viewShortcuts.js
src/views/todo.js

index 61210f9e97bb4f1f3d1cc16dc277a65a539a4b82..af913ed39c0cf4eb217d84b3fb76836261e4124c 100644 (file)
@@ -8,7 +8,7 @@ module.exports = function(grunt) {
       files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'],
       options: {
         globals: {
-          jQuery: true
+          jQuery: true,
         }
       }
     },
index 3563e04c3bc3e6c84694ce529361e8b433128933..c0885a02608755791c7512c216726f9fdab2e128 100644 (file)
@@ -170,13 +170,13 @@ var Shortcut = (function(document, _) {
             });
         },
         shortcutPressed: function(shortcut, object) {
-            if (shortcut.action) shortcut.action(object.element, shortcut, object.type);
+            if (shortcut.action) return shortcut.action(object.element, shortcut, object.type, object.options);
         },
         onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
             // NOTE: You may want to hook into this if you want to save/load user preferences
             // shortcut.id is probably what you want and not the whole shortcut
         },
-        addObject: function(element, type) {
+        addObject: function(element, type, options) {
             var manager = this;
             object = {
                 element:element,
@@ -184,6 +184,7 @@ var Shortcut = (function(document, _) {
                 mousetrap: Mousetrap(element),
                 remove: function() { manager.removeObject(this); },
                 add: function() { manager.addObject(this.element, this.type); },
+                options: options,
             };
             _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value();
             if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
@@ -246,7 +247,6 @@ var Shortcut = (function(document, _) {
  * @depend ../library/shortcut.js
  */
 
-
 // Integrates "Shortcuts" library with backbone views, to allow events in the form "Shortcut(id, description, default keycombo)": action
 // Shortcut selectors are not supported
 (function (View, Shortcut) {
@@ -263,19 +263,21 @@ var Shortcut = (function(document, _) {
         });
     }
     function delegateMousetrapEvents(events, objectType) {
-        for (var key in events) {
-            var method = events[key];
-            if (!_.isFunction(method)) method = this[events[key]];
-            if (!method) continue;
+        _.each(events, function(method, key) {
+            if (!_.isFunction(method)) method = this[method];
+            if (!method) return;
             var match = key.match(ShortcutRegex);
-            delegate.call(this, match[1], match[2], match[3], objectType, _.bind(method, this));
-        }
+            /* jshint loopfunc: true */
+            delegate.call(this, match[1], match[2], match[3], objectType, function(element, shortcut, type, options) { 
+                return method.call(options.view, element, shortcut, type, options);  
+            } ); 
+        }, this);
         this.shortcutObjects = this.shortcutObjects || [];
         if (objectType === "global") {
-            Shortcut.globalObject = this.$el[0];
+            Shortcut.globalObject = this.el;
             this._shortcutObject = Shortcut.globalObject;
         } else {
-            this._shortcutObject = Shortcut.addObject(this.$el[0], objectType);
+            this._shortcutObject = Shortcut.addObject(this.el, objectType, { view: this });
         }
     }
     View.delegateEvents = function(events) {
@@ -308,7 +310,7 @@ var Shortcut = (function(document, _) {
 
 /**
  * @depend ../library/cursorToEnd.js
- * @depend ../library/viewShortcuts.js
//* @depend ../library/viewShortcuts.js
  */
 
 
@@ -354,13 +356,15 @@ var TodoView = Backbone.View.extend({
   decodeText: function(encodedText) {
     return $("<div/>").html(encodedText).text();
   },
-  toggleComplete: function(e) {
+  toggleComplete: function() {
     this.stopEditingText();
     this.model.toggleComplete();
     var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false});
+    if (!next) return false;
     next.getView().startEditingText();
+    return false; // Don't propogate
   },
-  backspace: function(e) {
+  backspace: function() {
     if (this.model.hasChildren()) {
         return;
     }
@@ -369,30 +373,30 @@ var TodoView = Backbone.View.extend({
         return;
     }
     if (this.model.get("text") === "") {
-        e.preventDefault();
         this.model.remove(this.model.collection);
         previousNode.getView().startEditingText({"atEnd":true});
+        return false;
     } else if (this.isFocusAtBeginning()) {
-        e.preventDefault();
         var text = this.model.get("text");
         this.model.remove(this.model.collection);
         var focusReminderElement = $('<span class="focus"></span>');
         previousNode.setText(previousNode.get("text") + '<span class="focus"></span>' + text);
         previousNode.getView().startEditingText({"atMarker": ".focus"});
+        return false;
     }
   },
   "delete": function(e) {
     if (this.model.hasChildren()) {
         return;
     }
-    var previousNode = this.model.previousNode(this.model.collection);
-    if (!previousNode) {
+    var nextNode = this.model.nextNode(this.model.collection);
+    if (!nextNode) {
         return;
     }
     if (this.model.get("text") === "") {
-        e.preventDefault();
         this.model.remove(this.model.collection);
-        previousNode.getView().startEditingText({"atEnd":true});
+        nextNode.getView().startEditingText();
+        return false;
     }
   },
   textChange: function(e) {
index 86ed1df617c89bea26a7ce880f5345d8693d28f0..4ceec4bb329b9cadd35c7d7fd7a73c549357df7b 100644 (file)
@@ -169,13 +169,13 @@ var Shortcut = (function(document, _) {
             });
         },
         shortcutPressed: function(shortcut, object) {
-            if (shortcut.action) shortcut.action(object.element, shortcut, object.type);
+            if (shortcut.action) return shortcut.action(object.element, shortcut, object.type, object.options);
         },
         onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
             // NOTE: You may want to hook into this if you want to save/load user preferences
             // shortcut.id is probably what you want and not the whole shortcut
         },
-        addObject: function(element, type) {
+        addObject: function(element, type, options) {
             var manager = this;
             object = {
                 element:element,
@@ -183,6 +183,7 @@ var Shortcut = (function(document, _) {
                 mousetrap: Mousetrap(element),
                 remove: function() { manager.removeObject(this); },
                 add: function() { manager.addObject(this.element, this.type); },
+                options: options,
             };
             _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value();
             if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
@@ -245,7 +246,6 @@ var Shortcut = (function(document, _) {
  * @depend ../library/shortcut.js
  */
 
-
 // Integrates "Shortcuts" library with backbone views, to allow events in the form "Shortcut(id, description, default keycombo)": action
 // Shortcut selectors are not supported
 (function (View, Shortcut) {
@@ -262,19 +262,21 @@ var Shortcut = (function(document, _) {
         });
     }
     function delegateMousetrapEvents(events, objectType) {
-        for (var key in events) {
-            var method = events[key];
-            if (!_.isFunction(method)) method = this[events[key]];
-            if (!method) continue;
+        _.each(events, function(method, key) {
+            if (!_.isFunction(method)) method = this[method];
+            if (!method) return;
             var match = key.match(ShortcutRegex);
-            delegate.call(this, match[1], match[2], match[3], objectType, _.bind(method, this));
-        }
+            /* jshint loopfunc: true */
+            delegate.call(this, match[1], match[2], match[3], objectType, function(element, shortcut, type, options) { 
+                return method.call(options.view, element, shortcut, type, options);  
+            } ); 
+        }, this);
         this.shortcutObjects = this.shortcutObjects || [];
         if (objectType === "global") {
-            Shortcut.globalObject = this.$el[0];
+            Shortcut.globalObject = this.el;
             this._shortcutObject = Shortcut.globalObject;
         } else {
-            this._shortcutObject = Shortcut.addObject(this.$el[0], objectType);
+            this._shortcutObject = Shortcut.addObject(this.el, objectType, { view: this });
         }
     }
     View.delegateEvents = function(events) {
@@ -307,7 +309,7 @@ var Shortcut = (function(document, _) {
 
 /**
  * @depend ../library/cursorToEnd.js
- * @depend ../library/viewShortcuts.js
//* @depend ../library/viewShortcuts.js
  */
 
 
@@ -353,13 +355,15 @@ var TodoView = Backbone.View.extend({
   decodeText: function(encodedText) {
     return $("<div/>").html(encodedText).text();
   },
-  toggleComplete: function(e) {
+  toggleComplete: function() {
     this.stopEditingText();
     this.model.toggleComplete();
     var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false});
+    if (!next) return false;
     next.getView().startEditingText();
+    return false; // Don't propogate
   },
-  backspace: function(e) {
+  backspace: function() {
     if (this.model.hasChildren()) {
         return;
     }
@@ -368,30 +372,30 @@ var TodoView = Backbone.View.extend({
         return;
     }
     if (this.model.get("text") === "") {
-        e.preventDefault();
         this.model.remove(this.model.collection);
         previousNode.getView().startEditingText({"atEnd":true});
+        return false;
     } else if (this.isFocusAtBeginning()) {
-        e.preventDefault();
         var text = this.model.get("text");
         this.model.remove(this.model.collection);
         var focusReminderElement = $('<span class="focus"></span>');
         previousNode.setText(previousNode.get("text") + '<span class="focus"></span>' + text);
         previousNode.getView().startEditingText({"atMarker": ".focus"});
+        return false;
     }
   },
   "delete": function(e) {
     if (this.model.hasChildren()) {
         return;
     }
-    var previousNode = this.model.previousNode(this.model.collection);
-    if (!previousNode) {
+    var nextNode = this.model.nextNode(this.model.collection);
+    if (!nextNode) {
         return;
     }
     if (this.model.get("text") === "") {
-        e.preventDefault();
         this.model.remove(this.model.collection);
-        previousNode.getView().startEditingText({"atEnd":true});
+        nextNode.getView().startEditingText();
+        return false;
     }
   },
   textChange: function(e) {
index 29f9d9fc44224e8b5a3ce21f4cb03fdc7725e156..a990480dab0671346614d7545c3415c5b8e2213f 100644 (file)
@@ -108,13 +108,13 @@ var Shortcut = (function(document, _) {
             });
         },
         shortcutPressed: function(shortcut, object) {
-            if (shortcut.action) shortcut.action(object.element, shortcut, object.type);
+            if (shortcut.action) return shortcut.action(object.element, shortcut, object.type, object.options);
         },
         onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
             // NOTE: You may want to hook into this if you want to save/load user preferences
             // shortcut.id is probably what you want and not the whole shortcut
         },
-        addObject: function(element, type) {
+        addObject: function(element, type, options) {
             var manager = this;
             object = {
                 element:element,
@@ -122,6 +122,7 @@ var Shortcut = (function(document, _) {
                 mousetrap: Mousetrap(element),
                 remove: function() { manager.removeObject(this); },
                 add: function() { manager.addObject(this.element, this.type); },
+                options: options,
             };
             _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value();
             if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
index e4f90ed5e03de4294d1fc36dc66f650257b8e083..659c59f35d9b1b936dedcb6fb33e411d354f507d 100644 (file)
@@ -2,7 +2,6 @@
  * @depend ../library/shortcut.js
  */
 
-
 // Integrates "Shortcuts" library with backbone views, to allow events in the form "Shortcut(id, description, default keycombo)": action
 // Shortcut selectors are not supported
 (function (View, Shortcut) {
         });
     }
     function delegateMousetrapEvents(events, objectType) {
-        for (var key in events) {
-            var method = events[key];
-            if (!_.isFunction(method)) method = this[events[key]];
-            if (!method) continue;
+        _.each(events, function(method, key) {
+            if (!_.isFunction(method)) method = this[method];
+            if (!method) return;
             var match = key.match(ShortcutRegex);
-            delegate.call(this, match[1], match[2], match[3], objectType, _.bind(method, this));
-        }
+            /* jshint loopfunc: true */
+            delegate.call(this, match[1], match[2], match[3], objectType, function(element, shortcut, type, options) { 
+                return method.call(options.view, element, shortcut, type, options);  
+            } ); 
+        }, this);
         this.shortcutObjects = this.shortcutObjects || [];
         if (objectType === "global") {
-            Shortcut.globalObject = this.$el[0];
+            Shortcut.globalObject = this.el;
             this._shortcutObject = Shortcut.globalObject;
         } else {
-            this._shortcutObject = Shortcut.addObject(this.$el[0], objectType);
+            this._shortcutObject = Shortcut.addObject(this.el, objectType, { view: this });
         }
     }
     View.delegateEvents = function(events) {
index dc9451f9a39068254f9a4e989d8c53a6df242db3..6d3f45fa16c8ade0b78c63b3a9bfd7a18cf759d0 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * @depend ../library/cursorToEnd.js
- * @depend ../library/viewShortcuts.js
//* @depend ../library/viewShortcuts.js
  */
 
 
@@ -46,13 +46,15 @@ var TodoView = Backbone.View.extend({
   decodeText: function(encodedText) {
     return $("<div/>").html(encodedText).text();
   },
-  toggleComplete: function(e) {
+  toggleComplete: function() {
     this.stopEditingText();
     this.model.toggleComplete();
     var next = this.model.nextNode(this.model.collection, {"childrenAllowed":false});
+    if (!next) return false;
     next.getView().startEditingText();
+    return false; // Don't propogate
   },
-  backspace: function(e) {
+  backspace: function() {
     if (this.model.hasChildren()) {
         return;
     }
@@ -61,30 +63,30 @@ var TodoView = Backbone.View.extend({
         return;
     }
     if (this.model.get("text") === "") {
-        e.preventDefault();
         this.model.remove(this.model.collection);
         previousNode.getView().startEditingText({"atEnd":true});
+        return false;
     } else if (this.isFocusAtBeginning()) {
-        e.preventDefault();
         var text = this.model.get("text");
         this.model.remove(this.model.collection);
         var focusReminderElement = $('<span class="focus"></span>');
         previousNode.setText(previousNode.get("text") + '<span class="focus"></span>' + text);
         previousNode.getView().startEditingText({"atMarker": ".focus"});
+        return false;
     }
   },
   "delete": function(e) {
     if (this.model.hasChildren()) {
         return;
     }
-    var previousNode = this.model.previousNode(this.model.collection);
-    if (!previousNode) {
+    var nextNode = this.model.nextNode(this.model.collection);
+    if (!nextNode) {
         return;
     }
     if (this.model.get("text") === "") {
-        e.preventDefault();
         this.model.remove(this.model.collection);
-        previousNode.getView().startEditingText({"atEnd":true});
+        nextNode.getView().startEditingText();
+        return false;
     }
   },
   textChange: function(e) {