]> git.za3k.com Git - flowy.git/commitdiff
Shortcut library does its own binding so you don't have to update it all the time.
authorZachary Vance <vanceza@gmail.com>
Fri, 22 May 2015 01:23:46 +0000 (18:23 -0700)
committerZachary Vance <vanceza@gmail.com>
Fri, 22 May 2015 01:23:46 +0000 (18:23 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
src/library/shortcut.js
src/views/app.js

index 9f314505a6eeb115f21cee72b19f70b1941e2847..f389129cab7b249809151fdefa8a816243ec26f4 100644 (file)
@@ -95,6 +95,8 @@ var Shortcut = (function(document, _) {
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
         shortcuts: {},
+        onRebindCallbacks: [],
+        onNewShortcutCallbacks: [],
         registerShortcut: function(shortcut) {
             if (arguments.length > 1) {
                 shortcut = {
@@ -127,6 +129,9 @@ var Shortcut = (function(document, _) {
             if (this.shortcuts[shortcut.id]) return this.shortcuts[shortcut.id];
             this.shortcuts[shortcut.id] = shortcut;
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
+            _.each(this.onNewShortcutCallbacks, function(callback) {
+                callback.call(this, shortcut);
+            }, this);
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
@@ -154,7 +159,11 @@ var Shortcut = (function(document, _) {
             shortcut.unbind();
             shortcut._keybinding = keybinding;
             _.each(objects, shortcut.bind, shortcut);
-            if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding);
+            if (this.onRebindCallbacks) {
+                _.each(this.onRebindCallbacks, function(f) {
+                    f.call(this, shortcut, keybinding, oldKeybinding);
+                });
+            }
         },
         userRebindShortcut: function(shortcut, options) {
             // Cancels on focus loss or if the user presses the escape key
@@ -203,7 +212,12 @@ var Shortcut = (function(document, _) {
         shortcutPressed: function(shortcut, object) {
             if (shortcut.action) return shortcut.action(object.element, shortcut, object.type, object.options);
         },
-        onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
+        onNewShortcut: function(f) {
+            this.onNewShortcutCallbacks.push(f);
+        },
+        onRebindShortcut: function(f) {
+            this.onRebindCallbacks.push(f);
+            // 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
         },
@@ -250,6 +264,16 @@ var Shortcut = (function(document, _) {
                 object.mousetrap.unpause();
             });
         },
+        bindShortcutsDisplay: function(element, options) {
+            element.innerHTML = this.displayShortcuts(options);
+            var self = this;
+            this.onRebindShortcut(function() {
+                element.innerHTML = self.displayShortcuts(options);
+            });
+            this.onNewShortcut(function() {
+                element.innerHTML = self.displayShortcuts(options);
+            });
+        },
         displayShortcuts: function(options) {
             options = _.defaults({}, options, {
                 objectGrouping: "default", // Display one list, or group under headers by object type? "true" "false", or "default"
@@ -262,8 +286,8 @@ var Shortcut = (function(document, _) {
                 var multipleObjectTypes = _.chain(shortcuts).pluck('object').unique().size().value() > 1;
                 options.objectGrouping = multipleObjectTypes;
             }
-            return '<h3>Shortcuts</h3><table>' + _.map(options.shortcuts, function(shortcut) {
-                return _.template("<tr><td><%= description %></td><td><%= keybindingHtml %></td></tr>")({
+            return '<table class="shortcuts">' + _.map(options.shortcuts, function(shortcut) {
+                return _.template('<tr><td class="shortcut-description"><%= description %></td><td class="shortcut-keybinding"><%= keybindingHtml %></td></tr>')({
                     description: shortcut.description,
                     keybindingHtml: this._displayKeybinding(shortcut, options)
                 });
@@ -778,20 +802,14 @@ var AppView = Backbone.View.extend({
                 e.save();
             });
         });
-        Shortcut.onRebindShortcut = function() {
-            self.renderShortcuts();
-        };
+        Shortcut.bindShortcutsDisplay(this.$("#shortcuts")[0], {allowRebind: true});
         this.views = {}; // A list of views for each element in the collection
         this.list.fetch();
         this.render();
     },
     render: function() {
-        this.renderShortcuts();
         return this;
     },
-    renderShortcuts: function() {
-        this.$("#shortcuts").html(Shortcut.displayShortcuts({allowRebind: true}));
-    },
     addOne: function(todo) {
         this.renderTodo(todo);
     },
index 5ba3ba087e5b2fe3cab3b189ce077d07a2c53771..4f3edee42b54056dcd721bbc9b3211e63027bada 100644 (file)
@@ -94,6 +94,8 @@ var Shortcut = (function(document, _) {
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
         shortcuts: {},
+        onRebindCallbacks: [],
+        onNewShortcutCallbacks: [],
         registerShortcut: function(shortcut) {
             if (arguments.length > 1) {
                 shortcut = {
@@ -126,6 +128,9 @@ var Shortcut = (function(document, _) {
             if (this.shortcuts[shortcut.id]) return this.shortcuts[shortcut.id];
             this.shortcuts[shortcut.id] = shortcut;
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
+            _.each(this.onNewShortcutCallbacks, function(callback) {
+                callback.call(this, shortcut);
+            }, this);
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
@@ -153,7 +158,11 @@ var Shortcut = (function(document, _) {
             shortcut.unbind();
             shortcut._keybinding = keybinding;
             _.each(objects, shortcut.bind, shortcut);
-            if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding);
+            if (this.onRebindCallbacks) {
+                _.each(this.onRebindCallbacks, function(f) {
+                    f.call(this, shortcut, keybinding, oldKeybinding);
+                });
+            }
         },
         userRebindShortcut: function(shortcut, options) {
             // Cancels on focus loss or if the user presses the escape key
@@ -202,7 +211,12 @@ var Shortcut = (function(document, _) {
         shortcutPressed: function(shortcut, object) {
             if (shortcut.action) return shortcut.action(object.element, shortcut, object.type, object.options);
         },
-        onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
+        onNewShortcut: function(f) {
+            this.onNewShortcutCallbacks.push(f);
+        },
+        onRebindShortcut: function(f) {
+            this.onRebindCallbacks.push(f);
+            // 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
         },
@@ -249,6 +263,16 @@ var Shortcut = (function(document, _) {
                 object.mousetrap.unpause();
             });
         },
+        bindShortcutsDisplay: function(element, options) {
+            element.innerHTML = this.displayShortcuts(options);
+            var self = this;
+            this.onRebindShortcut(function() {
+                element.innerHTML = self.displayShortcuts(options);
+            });
+            this.onNewShortcut(function() {
+                element.innerHTML = self.displayShortcuts(options);
+            });
+        },
         displayShortcuts: function(options) {
             options = _.defaults({}, options, {
                 objectGrouping: "default", // Display one list, or group under headers by object type? "true" "false", or "default"
@@ -261,8 +285,8 @@ var Shortcut = (function(document, _) {
                 var multipleObjectTypes = _.chain(shortcuts).pluck('object').unique().size().value() > 1;
                 options.objectGrouping = multipleObjectTypes;
             }
-            return '<h3>Shortcuts</h3><table>' + _.map(options.shortcuts, function(shortcut) {
-                return _.template("<tr><td><%= description %></td><td><%= keybindingHtml %></td></tr>")({
+            return '<table class="shortcuts">' + _.map(options.shortcuts, function(shortcut) {
+                return _.template('<tr><td class="shortcut-description"><%= description %></td><td class="shortcut-keybinding"><%= keybindingHtml %></td></tr>')({
                     description: shortcut.description,
                     keybindingHtml: this._displayKeybinding(shortcut, options)
                 });
@@ -777,20 +801,14 @@ var AppView = Backbone.View.extend({
                 e.save();
             });
         });
-        Shortcut.onRebindShortcut = function() {
-            self.renderShortcuts();
-        };
+        Shortcut.bindShortcutsDisplay(this.$("#shortcuts")[0], {allowRebind: true});
         this.views = {}; // A list of views for each element in the collection
         this.list.fetch();
         this.render();
     },
     render: function() {
-        this.renderShortcuts();
         return this;
     },
-    renderShortcuts: function() {
-        this.$("#shortcuts").html(Shortcut.displayShortcuts({allowRebind: true}));
-    },
     addOne: function(todo) {
         this.renderTodo(todo);
     },
index dfa58decf9ba4617779dc19a3e40db31a0401f77..0a6f075afe365ff73c396cba825e4ec42f9b6766 100644 (file)
@@ -33,6 +33,8 @@ var Shortcut = (function(document, _) {
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
         shortcuts: {},
+        onRebindCallbacks: [],
+        onNewShortcutCallbacks: [],
         registerShortcut: function(shortcut) {
             if (arguments.length > 1) {
                 shortcut = {
@@ -65,6 +67,9 @@ var Shortcut = (function(document, _) {
             if (this.shortcuts[shortcut.id]) return this.shortcuts[shortcut.id];
             this.shortcuts[shortcut.id] = shortcut;
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
+            _.each(this.onNewShortcutCallbacks, function(callback) {
+                callback.call(this, shortcut);
+            }, this);
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
@@ -92,7 +97,11 @@ var Shortcut = (function(document, _) {
             shortcut.unbind();
             shortcut._keybinding = keybinding;
             _.each(objects, shortcut.bind, shortcut);
-            if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding);
+            if (this.onRebindCallbacks) {
+                _.each(this.onRebindCallbacks, function(f) {
+                    f.call(this, shortcut, keybinding, oldKeybinding);
+                });
+            }
         },
         userRebindShortcut: function(shortcut, options) {
             // Cancels on focus loss or if the user presses the escape key
@@ -141,7 +150,12 @@ var Shortcut = (function(document, _) {
         shortcutPressed: function(shortcut, object) {
             if (shortcut.action) return shortcut.action(object.element, shortcut, object.type, object.options);
         },
-        onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
+        onNewShortcut: function(f) {
+            this.onNewShortcutCallbacks.push(f);
+        },
+        onRebindShortcut: function(f) {
+            this.onRebindCallbacks.push(f);
+            // 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
         },
@@ -188,6 +202,16 @@ var Shortcut = (function(document, _) {
                 object.mousetrap.unpause();
             });
         },
+        bindShortcutsDisplay: function(element, options) {
+            element.innerHTML = this.displayShortcuts(options);
+            var self = this;
+            this.onRebindShortcut(function() {
+                element.innerHTML = self.displayShortcuts(options);
+            });
+            this.onNewShortcut(function() {
+                element.innerHTML = self.displayShortcuts(options);
+            });
+        },
         displayShortcuts: function(options) {
             options = _.defaults({}, options, {
                 objectGrouping: "default", // Display one list, or group under headers by object type? "true" "false", or "default"
@@ -200,8 +224,8 @@ var Shortcut = (function(document, _) {
                 var multipleObjectTypes = _.chain(shortcuts).pluck('object').unique().size().value() > 1;
                 options.objectGrouping = multipleObjectTypes;
             }
-            return '<h3>Shortcuts</h3><table>' + _.map(options.shortcuts, function(shortcut) {
-                return _.template("<tr><td><%= description %></td><td><%= keybindingHtml %></td></tr>")({
+            return '<table class="shortcuts">' + _.map(options.shortcuts, function(shortcut) {
+                return _.template('<tr><td class="shortcut-description"><%= description %></td><td class="shortcut-keybinding"><%= keybindingHtml %></td></tr>')({
                     description: shortcut.description,
                     keybindingHtml: this._displayKeybinding(shortcut, options)
                 });
index 31d06ddf1d10d0bf0f51c7c38ede6fd58659324f..9639bf2766f33540135a23e307c6824750b9b479 100644 (file)
@@ -79,20 +79,14 @@ var AppView = Backbone.View.extend({
                 e.save();
             });
         });
-        Shortcut.onRebindShortcut = function() {
-            self.renderShortcuts();
-        };
+        Shortcut.bindShortcutsDisplay(this.$("#shortcuts")[0], {allowRebind: true});
         this.views = {}; // A list of views for each element in the collection
         this.list.fetch();
         this.render();
     },
     render: function() {
-        this.renderShortcuts();
         return this;
     },
-    renderShortcuts: function() {
-        this.$("#shortcuts").html(Shortcut.displayShortcuts({allowRebind: true}));
-    },
     addOne: function(todo) {
         this.renderTodo(todo);
     },