]> git.za3k.com Git - flowy.git/commitdiff
Use getters and setters to make code cleaner
authorZachary Vance <vanceza@gmail.com>
Wed, 20 May 2015 00:24:16 +0000 (17:24 -0700)
committerZachary Vance <vanceza@gmail.com>
Wed, 20 May 2015 00:24:16 +0000 (17:24 -0700)
dist/flowy.js
dist/flowy.unwrapped.js
flowy.tmux.conf
src/library/shortcut.js
src/views/todo.js

index 83ad047d9bfc960d96554de415651a45e4750b0f..9d8e6d260f08643ec1d9ecc0be56c1ee6dfa8f83 100644 (file)
@@ -72,7 +72,7 @@ var TodoView = Backbone.View.extend({
     //"click > .checkbox": "toggleComplete",
     "input > .text": "textChange",
     "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end
-    "keydown > .text": "keydown",
+    //"keydown > .text": "keydown",
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -377,8 +377,8 @@ var FlowyDocModel = Backbone.Collection.extend({
     comparator: 'id',
 });
 
-// TODO: None of this code has ever been run and doesn't work
 // TODO: More documentation
+// TODO: Removing objects/shortcuts/rebinding won't work until this bug is fixed in mousetrap: https://github.com/ccampbell/mousetrap/issues/267
 
 // firstShortcut = Shortcut.registerShortcut("Control + k", function() { console.log("Shortcut pressed."); });
 // firstShortcut.rebind("Control + m");
@@ -390,7 +390,7 @@ var Shortcut = (function(document, _) {
     var globalMousetrap = new Mousetrap();
     var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap};
     var Shortcut = {
-        globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
+        _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
         shortcuts: [],
@@ -411,21 +411,28 @@ var Shortcut = (function(document, _) {
                 unbind: function(object) { manager.unbindShortcut(this, object); },
                 rebind: function(keybinding) { manager.rebindShortcut(this, keybinding); },
                 object: "global",
-                keybinding: "",
+
+                _keybinding: shortcut.keybinding || "",
                 action: function() {},
                 boundObjects: [],
                 ownedByUs: true
             });
+            Object.defineProperty(shortcut, "keybinding", {
+                set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); },
+                get: function () { return shortcut._keybinding; },
+            });
             this.shortcuts.push(shortcut);
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
-            _.each(shortcut.boundObjects, _.partial(this.unbindShortcut, shortcut, _), this); // TODO: This will die under mutation, find a safer 'each' method
+            _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this);
         },
         bindShortcut: function(shortcut, object) { // Do not call directly
-            object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object));
-            shortcut.boundObjects.push(object);
+            if (!_.contains(shortcut.boundObjects, object)) {
+                object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object));
+                shortcut.boundObjects.push(object);
+            }
         },
         unbindShortcut: function(shortcut, object) { // Do not call directly with an object
             if (!object){
@@ -435,12 +442,11 @@ var Shortcut = (function(document, _) {
             shortcut.boundObjects = _.without(shortcut.boundObjects, object);
         },
         rebindShortcut: function(shortcut, keybinding) {
-            // TODO: figure out a way to do this with 'shorcut.keybinding=' binding
             if (keybinding === shortcut.keybinding) return;
             var oldKeybinding = shortcut.keybinding;
             var objects = shortcut.boundObjects;
             shortcut.unbind();
-            shorcut.keybinding = keybinding;
+            shorcut._keybinding = keybinding;
             _.each(objects, shortcut.bind, shortcut);
             if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding);
         },
@@ -469,7 +475,7 @@ var Shortcut = (function(document, _) {
             });
         },
         shortcutPressed: function(shortcut, object) {
-            if (shortcut.action) shortcut.action(object, object, shortcut);
+            if (shortcut.action) shortcut.action(object.element, shortcut, object.type);
         },
         onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
             // NOTE: You may want to hook into this if you want to save/load user preferences
@@ -481,7 +487,8 @@ var Shortcut = (function(document, _) {
                 element:element,
                 type:type,
                 mousetrap: new Mousetrap(element),
-                remove: function() { manager.removeObject(this); }
+                remove: function() { manager.removeObject(this); },
+                add: function() { manager.addObject(this.element, this.type); },
             };
             _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value();
             if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
@@ -491,17 +498,19 @@ var Shortcut = (function(document, _) {
             _.each(this.shortcuts, _.partial(this.unbindShortcut, _, object), this);
             this.boundObjects = _.without(this.boundObjects, object);
         },
-        changeGlobalObject: function(newObject) {
-            // TODO: Figure out how to do this with 'this.globalObject=' binding
+        get globalObject() {
+            return this._globalObject;
+        },
+        set globalObject(newElement) {
             this.removeObject(this.globalObject, "global");
-            this.globalObject = {
-                element: element,
-                type: type,
+            this._globalObject = {
+                element: newElement,
+                type: "global",
                 isGlobal: true,
                 mousetrap: this.globalMousetrap,
             };
-            _.chain(this.shortcuts).where({'object': type}).each(function(shortcut) { this.bindShortcut(shortcut, object); }, this).value();
-            if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
+            _.chain(this.shortcuts).where({'object': "global"}).each(_.partial(this.bindShortcut, _, this.globalObject), this).value();
+            if (!_.contains(this.boundObjects)) this.boundObjects.push(this.globalObject);
             return this.globalObject;
         },
         pause: function() { // TODO: These require plugin, test
index 1c1923078554e34547d07ed4c3f9511f6e51c0eb..ccb133be78b1b7cec0aac8549e103ddc8c14f6bd 100644 (file)
@@ -71,7 +71,7 @@ var TodoView = Backbone.View.extend({
     //"click > .checkbox": "toggleComplete",
     "input > .text": "textChange",
     "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end
-    "keydown > .text": "keydown",
+    //"keydown > .text": "keydown",
   },
   initialize: function() {
     this.childViewPositions = [];
@@ -376,8 +376,8 @@ var FlowyDocModel = Backbone.Collection.extend({
     comparator: 'id',
 });
 
-// TODO: None of this code has ever been run and doesn't work
 // TODO: More documentation
+// TODO: Removing objects/shortcuts/rebinding won't work until this bug is fixed in mousetrap: https://github.com/ccampbell/mousetrap/issues/267
 
 // firstShortcut = Shortcut.registerShortcut("Control + k", function() { console.log("Shortcut pressed."); });
 // firstShortcut.rebind("Control + m");
@@ -389,7 +389,7 @@ var Shortcut = (function(document, _) {
     var globalMousetrap = new Mousetrap();
     var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap};
     var Shortcut = {
-        globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
+        _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
         shortcuts: [],
@@ -410,21 +410,28 @@ var Shortcut = (function(document, _) {
                 unbind: function(object) { manager.unbindShortcut(this, object); },
                 rebind: function(keybinding) { manager.rebindShortcut(this, keybinding); },
                 object: "global",
-                keybinding: "",
+
+                _keybinding: shortcut.keybinding || "",
                 action: function() {},
                 boundObjects: [],
                 ownedByUs: true
             });
+            Object.defineProperty(shortcut, "keybinding", {
+                set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); },
+                get: function () { return shortcut._keybinding; },
+            });
             this.shortcuts.push(shortcut);
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
-            _.each(shortcut.boundObjects, _.partial(this.unbindShortcut, shortcut, _), this); // TODO: This will die under mutation, find a safer 'each' method
+            _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this);
         },
         bindShortcut: function(shortcut, object) { // Do not call directly
-            object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object));
-            shortcut.boundObjects.push(object);
+            if (!_.contains(shortcut.boundObjects, object)) {
+                object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object));
+                shortcut.boundObjects.push(object);
+            }
         },
         unbindShortcut: function(shortcut, object) { // Do not call directly with an object
             if (!object){
@@ -434,12 +441,11 @@ var Shortcut = (function(document, _) {
             shortcut.boundObjects = _.without(shortcut.boundObjects, object);
         },
         rebindShortcut: function(shortcut, keybinding) {
-            // TODO: figure out a way to do this with 'shorcut.keybinding=' binding
             if (keybinding === shortcut.keybinding) return;
             var oldKeybinding = shortcut.keybinding;
             var objects = shortcut.boundObjects;
             shortcut.unbind();
-            shorcut.keybinding = keybinding;
+            shorcut._keybinding = keybinding;
             _.each(objects, shortcut.bind, shortcut);
             if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding);
         },
@@ -468,7 +474,7 @@ var Shortcut = (function(document, _) {
             });
         },
         shortcutPressed: function(shortcut, object) {
-            if (shortcut.action) shortcut.action(object, object, shortcut);
+            if (shortcut.action) shortcut.action(object.element, shortcut, object.type);
         },
         onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
             // NOTE: You may want to hook into this if you want to save/load user preferences
@@ -480,7 +486,8 @@ var Shortcut = (function(document, _) {
                 element:element,
                 type:type,
                 mousetrap: new Mousetrap(element),
-                remove: function() { manager.removeObject(this); }
+                remove: function() { manager.removeObject(this); },
+                add: function() { manager.addObject(this.element, this.type); },
             };
             _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value();
             if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
@@ -490,17 +497,19 @@ var Shortcut = (function(document, _) {
             _.each(this.shortcuts, _.partial(this.unbindShortcut, _, object), this);
             this.boundObjects = _.without(this.boundObjects, object);
         },
-        changeGlobalObject: function(newObject) {
-            // TODO: Figure out how to do this with 'this.globalObject=' binding
+        get globalObject() {
+            return this._globalObject;
+        },
+        set globalObject(newElement) {
             this.removeObject(this.globalObject, "global");
-            this.globalObject = {
-                element: element,
-                type: type,
+            this._globalObject = {
+                element: newElement,
+                type: "global",
                 isGlobal: true,
                 mousetrap: this.globalMousetrap,
             };
-            _.chain(this.shortcuts).where({'object': type}).each(function(shortcut) { this.bindShortcut(shortcut, object); }, this).value();
-            if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
+            _.chain(this.shortcuts).where({'object': "global"}).each(_.partial(this.bindShortcut, _, this.globalObject), this).value();
+            if (!_.contains(this.boundObjects)) this.boundObjects.push(this.globalObject);
             return this.globalObject;
         },
         pause: function() { // TODO: These require plugin, test
index 68d57efb5fe03f72d99d50f3c1f948b051c65266..d3424c49853b61b04669b355c3a8360d21506a32 100755 (executable)
@@ -5,7 +5,7 @@ new-window -n "source" -c ~/flowy/src "vim -p *.html */*"
 new-window -n timelog "vim ~/documents/paul-timelog"
 
 # 'b' runs a browser
-bind-key b run-shell -b "chromium file:///home/zachary/flowy/dist/main.html"
+bind-key b run-shell -b "chromium file:///home/zachary/flowy/dist/index.html"
 # 'd' runs a browser and opens documentation
 bind-key d run-shell -b "chromium http://backbonejs.org/"
 # 'r' reloads this file (doesn't work because of the sessions and windows at the top)
index 6bd08604b963b521f1b6ebca3443211ebcf7e263..75f46ee79147009d8295c65411d6524932dcee8e 100644 (file)
@@ -1,5 +1,5 @@
-// TODO: None of this code has ever been run and doesn't work
 // TODO: More documentation
+// TODO: Removing objects/shortcuts/rebinding won't work until this bug is fixed in mousetrap: https://github.com/ccampbell/mousetrap/issues/267
 
 // firstShortcut = Shortcut.registerShortcut("Control + k", function() { console.log("Shortcut pressed."); });
 // firstShortcut.rebind("Control + m");
@@ -11,7 +11,7 @@ var Shortcut = (function(document, _) {
     var globalMousetrap = new Mousetrap();
     var globalObject = {element: document, type: "global", isGlobal: true, mousetrap: globalMousetrap};
     var Shortcut = {
-        globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
+        _globalObject: globalObject, // The object passed to callbacks when a global keybinding is invoked.
         globalMousetrap: globalMousetrap,
         boundObjects: [globalObject],
         shortcuts: [],
@@ -32,21 +32,28 @@ var Shortcut = (function(document, _) {
                 unbind: function(object) { manager.unbindShortcut(this, object); },
                 rebind: function(keybinding) { manager.rebindShortcut(this, keybinding); },
                 object: "global",
-                keybinding: "",
+
+                _keybinding: shortcut.keybinding || "",
                 action: function() {},
                 boundObjects: [],
                 ownedByUs: true
             });
+            Object.defineProperty(shortcut, "keybinding", {
+                set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); },
+                get: function () { return shortcut._keybinding; },
+            });
             this.shortcuts.push(shortcut);
             _.chain(this.boundObjects).where({'type':shortcut.object}).each(_.partial(this.bindShortcut, shortcut, _), this).value();
             return shortcut;
         },
         unregisterShortcut: function(shortcut) {
-            _.each(shortcut.boundObjects, _.partial(this.unbindShortcut, shortcut, _), this); // TODO: This will die under mutation, find a safer 'each' method
+            _.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this);
         },
         bindShortcut: function(shortcut, object) { // Do not call directly
-            object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object));
-            shortcut.boundObjects.push(object);
+            if (!_.contains(shortcut.boundObjects, object)) {
+                object.mousetrap.bind(shortcut.keybinding, _.bind(this.shortcutPressed, this, shortcut, object));
+                shortcut.boundObjects.push(object);
+            }
         },
         unbindShortcut: function(shortcut, object) { // Do not call directly with an object
             if (!object){
@@ -56,12 +63,11 @@ var Shortcut = (function(document, _) {
             shortcut.boundObjects = _.without(shortcut.boundObjects, object);
         },
         rebindShortcut: function(shortcut, keybinding) {
-            // TODO: figure out a way to do this with 'shorcut.keybinding=' binding
             if (keybinding === shortcut.keybinding) return;
             var oldKeybinding = shortcut.keybinding;
             var objects = shortcut.boundObjects;
             shortcut.unbind();
-            shorcut.keybinding = keybinding;
+            shorcut._keybinding = keybinding;
             _.each(objects, shortcut.bind, shortcut);
             if (this.onRebindShortcut) this.onRebindShortcut(shortcut, keybinding, oldKeybinding);
         },
@@ -90,7 +96,7 @@ var Shortcut = (function(document, _) {
             });
         },
         shortcutPressed: function(shortcut, object) {
-            if (shortcut.action) shortcut.action(object, object, shortcut);
+            if (shortcut.action) shortcut.action(object.element, shortcut, object.type);
         },
         onRebindShortcut: function(shortcut, keybinding, oldKeybinding) {
             // NOTE: You may want to hook into this if you want to save/load user preferences
@@ -102,7 +108,8 @@ var Shortcut = (function(document, _) {
                 element:element,
                 type:type,
                 mousetrap: new Mousetrap(element),
-                remove: function() { manager.removeObject(this); }
+                remove: function() { manager.removeObject(this); },
+                add: function() { manager.addObject(this.element, this.type); },
             };
             _.chain(this.shortcuts).where({'object': type}).each(_.partial(this.bindShortcut, _, object), this).value();
             if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
@@ -112,17 +119,19 @@ var Shortcut = (function(document, _) {
             _.each(this.shortcuts, _.partial(this.unbindShortcut, _, object), this);
             this.boundObjects = _.without(this.boundObjects, object);
         },
-        changeGlobalObject: function(newObject) {
-            // TODO: Figure out how to do this with 'this.globalObject=' binding
+        get globalObject() {
+            return this._globalObject;
+        },
+        set globalObject(newElement) {
             this.removeObject(this.globalObject, "global");
-            this.globalObject = {
-                element: element,
-                type: type,
+            this._globalObject = {
+                element: newElement,
+                type: "global",
                 isGlobal: true,
                 mousetrap: this.globalMousetrap,
             };
-            _.chain(this.shortcuts).where({'object': type}).each(function(shortcut) { this.bindShortcut(shortcut, object); }, this).value();
-            if (!_.contains(this.boundObjects)) this.boundObjects.push(object);
+            _.chain(this.shortcuts).where({'object': "global"}).each(_.partial(this.bindShortcut, _, this.globalObject), this).value();
+            if (!_.contains(this.boundObjects)) this.boundObjects.push(this.globalObject);
             return this.globalObject;
         },
         pause: function() { // TODO: These require plugin, test
index 81cbb5c02e83fef944b1da9cec1d2d9cea2f0f29..e1a2d3aecb62cd834b823f29d5bdba49f58314f4 100644 (file)
@@ -10,7 +10,7 @@ var TodoView = Backbone.View.extend({
     //"click > .checkbox": "toggleComplete",
     "input > .text": "textChange",
     "blur > .text": "render", // Because the model shouldn't update the view during active editing, add a re-render at the end
-    "keydown > .text": "keydown",
+    //"keydown > .text": "keydown",
   },
   initialize: function() {
     this.childViewPositions = [];