From: Zachary Vance Date: Wed, 20 May 2015 03:44:23 +0000 (-0700) Subject: Shortcuts work correctly X-Git-Url: https://git.za3k.com/?a=commitdiff_plain;h=4a68422bf22e4909124cca63c62a966451309c81;p=flowy.git Shortcuts work correctly --- diff --git a/Gruntfile.js b/Gruntfile.js index 61210f9..af913ed 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -8,7 +8,7 @@ module.exports = function(grunt) { files: ['Gruntfile.js', 'src/**/*.js', 'test/**/*.js'], options: { globals: { - jQuery: true + jQuery: true, } } }, diff --git a/dist/flowy.js b/dist/flowy.js index 3563e04..c0885a0 100644 --- a/dist/flowy.js +++ b/dist/flowy.js @@ -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 $("
").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 = $(''); previousNode.setText(previousNode.get("text") + '' + 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) { diff --git a/dist/flowy.unwrapped.js b/dist/flowy.unwrapped.js index 86ed1df..4ceec4b 100644 --- a/dist/flowy.unwrapped.js +++ b/dist/flowy.unwrapped.js @@ -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 $("
").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 = $(''); previousNode.setText(previousNode.get("text") + '' + 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) { diff --git a/src/library/shortcut.js b/src/library/shortcut.js index 29f9d9f..a990480 100644 --- a/src/library/shortcut.js +++ b/src/library/shortcut.js @@ -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); diff --git a/src/library/viewShortcuts.js b/src/library/viewShortcuts.js index e4f90ed..659c59f 100644 --- a/src/library/viewShortcuts.js +++ b/src/library/viewShortcuts.js @@ -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) { @@ -19,19 +18,21 @@ }); } 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) { diff --git a/src/views/todo.js b/src/views/todo.js index dc9451f..6d3f45f 100644 --- a/src/views/todo.js +++ b/src/views/todo.js @@ -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 $("
").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 = $(''); previousNode.setText(previousNode.get("text") + '' + 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) {