home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 March / PCpro_2006_03.ISO / files / freeware / greasemonkey-0.6.4-fx.xpi / chrome / chromeFiles / content / menucommander.js < prev    next >
Encoding:
Text File  |  2005-11-30  |  4.3 KB  |  149 lines

  1.  
  2.  
  3. function GM_MenuCommander() {
  4.   GM_log("> GM_MenuCommander")
  5.   
  6.   this.menu = document.getElementById("userscript-commands-sb");
  7.   this.keyset = document.getElementById("mainKeyset");
  8.   this.menuPopup = this.menu.firstChild;
  9.  
  10.   this.menuItems = [];
  11.   this.keys = [];
  12.   this.attached = false;
  13.   
  14.   GM_log("< GM_MenuCommander")
  15. }
  16.  
  17. GM_MenuCommander.prototype.registerMenuCommand = 
  18. function(commandName, commandFunc, accelKey, accelModifiers, accessKey) {
  19.   GM_log("> GM_MenuCommander.registerMenuCommand");
  20.   
  21.   GM_log('accelKey: ' + accelKey);
  22.   GM_log('modifiers: ' + accelModifiers); 
  23.   GM_log('accessKey: ' + accessKey); 
  24.  
  25.   var menuItem = this.createMenuItem(commandName, commandFunc, accessKey);
  26.   this.menuItems.push(menuItem);  
  27.  
  28.   if (accelKey) {
  29.     var key = this.createKey(commandFunc, accelKey, accelModifiers, menuItem);
  30.     this.keys.push(key);
  31.   }
  32.  
  33.   // if this menucommander is for the current document, we should add the 
  34.   // elements immediately. otherwise it will be added in attach()
  35.   if (this.attached) {
  36.     this.menuPopup.appendChild(menuItem);
  37.   
  38.     if (accelKey) {
  39.       this.keyset.appendChild(key);
  40.     }
  41.   
  42.     this.setDisabled(false);
  43.   }
  44.   
  45.   GM_log("< GM_MenuCommmander.registerMenuCommand")
  46. }
  47.  
  48. GM_MenuCommander.prototype.attach = function() {
  49.   GM_log("> GM_MenuCommander.attach")
  50.  
  51.   for (var i = 0; i < this.menuItems.length; i++) {
  52.     this.menuPopup.appendChild(this.menuItems[i]);
  53.   }
  54.   
  55.   for (var i = 0; i < this.keys.length; i++) {
  56.     this.keyset.appendChild(this.keys[i]);
  57.   }
  58.  
  59.   this.setDisabled(this.menuItems.length == 0);
  60.   this.attached = true;
  61.  
  62.   GM_log("< GM_MenuCommander.attach")
  63. }
  64.  
  65. GM_MenuCommander.prototype.detach = function() {
  66.   GM_log("> GM_MenuCommander.detach")
  67.   GM_log("* this.menuPopup: " + this.menuPopup);
  68.  
  69.   for (var i = 0; i < this.menuItems.length; i++) {
  70.     this.menuPopup.removeChild(this.menuItems[i]);
  71.   }
  72.   
  73.   for (var i = 0; i < this.keys.length; i++) {
  74.     this.keyset.removeChild(this.keys[i]);
  75.   }
  76.  
  77.   this.setDisabled(true);
  78.   this.attached = false;
  79.  
  80.   GM_log("< GM_MenuCommander.detach")
  81. }
  82.  
  83. //TODO: restructure accel/access validation to be at register time.  
  84. //Should throw when called, not when building menu.  
  85. //This has side effect of one script's bad reg affecting another script's.
  86. GM_MenuCommander.prototype.createMenuItem = 
  87. function(commandName, commandFunc, accessKey) {
  88.   GM_log("> GM_MenuCommander.createMenuItem");
  89.   
  90.   var menuItem = document.createElement("menuitem");
  91.   menuItem._commandFunc = commandFunc;
  92.   menuItem.setAttribute("label", commandName);
  93.   menuItem.setAttribute("oncommand", "this._commandFunc()");
  94.  
  95.   if (accessKey) {
  96.     if (typeof(accessKey) == "string" && accessKey.length == 1) {
  97.       menuItem.setAttribute("accesskey", accessKey);
  98.     } else {
  99.       throw "accessKey must be a single character";
  100.     }
  101.   }
  102.  
  103.   GM_log("< GM_MenuCommander.createMenuItem");
  104.   return menuItem;
  105. }
  106.  
  107. GM_MenuCommander.prototype.createKey = 
  108. function(commandFunc, accelKey, modifiers, menuItem) {
  109.   GM_log("> GM_MenuCommander.createKey");
  110.   
  111.   var key = document.createElement("key");
  112.  
  113.   if ((typeof accelKey) == "number") {
  114.     GM_log("keycode: " + accelKey);
  115.     key.setAttribute("keycode", accelKey);
  116.   } else if ((typeof accelKey) == "string" && accelKey.length == 1) {
  117.     GM_log("key: " + accelKey);
  118.     key.setAttribute("key", accelKey);
  119.   } else {
  120.     throw "accelKey must be a numerical keycode or a single character";
  121.   }
  122.  
  123.   GM_log("modifiers: " + modifiers);
  124.   key.setAttribute("modifiers", modifiers);
  125.  
  126.   // hack, because listen("oncommand", commandFunc) does not work!
  127.   // this is ok because .detach() gets called when the document is unloaded
  128.   // and this key is destroyed
  129.   key._commandFunc = commandFunc;
  130.   key.setAttribute("oncommand", "this._commandFunc()");
  131.   
  132.   var id = "userscript-command-" + this.keys.length;
  133.   key.setAttribute("id", id);
  134.   menuItem.setAttribute("key", id);
  135.   
  136.   GM_log("< GM_MenuCommander.createKey");
  137.   return key;  
  138. }
  139.  
  140. GM_MenuCommander.prototype.setDisabled = function(disabled) {
  141.   var menu = this.menu;
  142.   var marker = menu.nextSibling;
  143.   var parent = menu.parentNode;
  144.   
  145.   menu.setAttribute("disabled", disabled);
  146.  
  147.   parent.removeChild(menu);
  148.   parent.insertBefore(menu, marker);
  149. }