1 /* See license.txt for terms of usage */
  2 
  3 FBL.ns( function() { with (FBL) {
  4 
  5 // ************************************************************************************************
  6 // Constants
  7 
  8 const Cc = Components.classes;
  9 const Ci = Components.interfaces;
 10 const prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch2);
 11 
 12 /**
 13  * ShortcutsModel object implements keyboard shortcuts logic.
 14  */
 15 Firebug.ShortcutsModel = extend(Firebug.Module,
 16 {
 17     dispatchName: "shortcuts",
 18 
 19     initializeUI: function()
 20     {
 21         if (FBTrace.DBG_SHORTCUTS)
 22             FBTrace.sysout("shortcuts.initializeUI; Shortcuts module initialization.");
 23 
 24         this.initShortcuts();
 25     },
 26 
 27     initShortcuts : function()
 28     {
 29         var branch = prefs.getBranch("extensions.firebug.key.shortcut.");
 30         var shortcutNames = branch.getChildList("", {});
 31         shortcutNames.forEach(this.initShortcut);
 32     },
 33 
 34     initShortcut : function(element, index, array)
 35     {
 36         var branch = prefs.getBranch("extensions.firebug.key.");
 37         var shortcut = branch.getCharPref("shortcut." + element);
 38         var tokens = shortcut.split(' ');
 39         var key = tokens.pop();
 40         var modifiers = tokens.join(',')
 41 
 42         var keyElem = document.getElementById("key_" + element);
 43         if (!keyElem)
 44         {
 45             //if key is not defined in xul, add it
 46             keyElem = document.createElement('key');
 47             keyElem.className = "fbOnlyKey";
 48             keyElem.id = "key_" + element;
 49             keyElem.command = "cmd_" + element;
 50             $('mainKeyset').appendChild(keyElem);
 51         }
 52 
 53         //choose between key or keycode attribute
 54         if (key.length == 1)
 55         {
 56             keyElem.setAttribute('modifiers', modifiers);
 57             keyElem.setAttribute('key', key);
 58             keyElem.removeAttribute('keycode');
 59         }
 60         else if (KeyEvent['DOM_' + key]) //only set valid keycodes
 61         {
 62             keyElem.setAttribute('modifiers', modifiers);
 63             keyElem.setAttribute('keycode', key);
 64             keyElem.removeAttribute('key'); //in case default shortcut uses key rather than keycode
 65         }
 66     },
 67 
 68     // UI Commands
 69     customizeShortcuts: function()
 70     {
 71         var args = {
 72             FBL: FBL,
 73             FBTrace: FBTrace
 74         };
 75 
 76         // Open customize shortcuts dialog. Pass FBL into the XUL window so,
 77         // common APIs can be used (e.g. localization).
 78         window.openDialog("chrome://firebug/content/customizeShortcuts.xul", "", 
 79             "chrome,centerscreen,dialog,modal,resizable=yes", args);
 80     }
 81 });
 82 
 83 // ************************************************************************************************
 84 // Registration
 85 
 86 Firebug.registerModule(Firebug.ShortcutsModel);
 87 
 88 // ************************************************************************************************
 89 }});