1 /** 2 * Creates a new ShortcutController. This object takes care of all keyboard 3 * shortcuts. 4 * 5 * @constructor 6 */ 7 mindmaps.ShortcutController = function() { 8 // set to save shortcuts in 9 /** 10 * @private 11 */ 12 this.shortcuts = {}; 13 14 /** 15 * Set the event type and add namespace for later removal. 16 * 17 * @param {String} shortcut the key combination 18 * @param {String} [type="keydown"] 19 * @returns {String} 20 */ 21 function getType(shortcut, type) { 22 type = type || "keydown"; 23 return type + "." + shortcut; 24 } 25 26 /** 27 * Registers a new application wide shortcut. 28 * 29 * @param {String} shortcut 30 * @param {Function} handler 31 * @param {String} [type="keydown"] 32 */ 33 this.register = function(shortcut, handler, type) { 34 type = getType(shortcut, type); 35 $(document).bind(type, shortcut, function(e) { 36 // try best to cancel default actions on shortcuts like ctrl+n 37 e.stopImmediatePropagation(); 38 e.stopPropagation(); 39 e.preventDefault(); 40 handler(); 41 return false; 42 }); 43 this.shortcuts[type] = true; 44 }; 45 46 /** 47 * Unregisters a application shortcut. 48 * 49 * @param {String} shortcut 50 * @param {String} [type="keydown"] 51 */ 52 this.unregister = function(shortcut, type) { 53 type = getType(shortcut, type); 54 $(document).unbind(type); 55 delete this.shortcuts[type]; 56 }; 57 58 /** 59 * Removes all shortcuts. 60 */ 61 this.unregisterAll = function() { 62 for ( var shortcut in shortcuts) { 63 $(document).unbind(shortcut); 64 } 65 }; 66 }; 67