// o.remove() // Once we lose textbox focus, stop keeping track of it, for example
var Shortcut = (function(document, _) {
+ _.mixin({
+ guid : function(){
+ return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
+ var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
+ return v.toString(16);
+ });
+ }
+ });
+
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.
globalMousetrap: globalMousetrap,
boundObjects: [globalObject],
- shortcuts: [],
+ shortcuts: {},
registerShortcut: function(shortcut) {
if (arguments.length > 1) {
shortcut = {
_keybinding: shortcut.keybinding || "",
action: function() {},
boundObjects: [],
- ownedByUs: true
+ ownedByUs: true,
+ id: _.guid(),
});
Object.defineProperty(shortcut, "keybinding", {
set: function (newKeybinding) { manager.rebindShortcut(shortcut, newKeybinding); },
get: function () { return shortcut._keybinding; },
});
- this.shortcuts.push(shortcut);
+ 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();
return shortcut;
},
unregisterShortcut: function(shortcut) {
_.each(_.clone(shortcut.boundObjects), _.partial(this.unbindShortcut, shortcut, _), this);
+ delete this.shortcuts[shortcut.id];
},
bindShortcut: function(shortcut, object) { // Do not call directly
if (!_.contains(shortcut.boundObjects, object)) {
object = {
element:element,
type:type,
- mousetrap: new Mousetrap(element),
+ mousetrap: Mousetrap(element),
remove: function() { manager.removeObject(this); },
add: function() { manager.addObject(this.element, this.type); },
};