home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / navigator / linkToolbarItem.js < prev    next >
Text File  |  2003-02-22  |  8KB  |  255 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Eric Hodel's <drbrain@segment7.net> code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Eric Hodel.
  18.  * Portions created by the Initial Developer are Copyright (C) 2001
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *      Christopher Hoess <choess@force.stwing.upenn.edu>
  23.  *      Tim Taylor <tim@tool-man.org>
  24.  *      Stuart Ballard <sballard@netreach.net>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /* 
  41.  * LinkToolbarItem and its subclasses represent the buttons, menuitems, 
  42.  * and menus that handle the various link types.
  43.  */
  44. function LinkToolbarItem (linkType) {
  45.   this.linkType = linkType;
  46.   this.xulElementId = "link-" + linkType;
  47.   this.xulPopupId = this.xulElementId + "-popup";
  48.   this.parentMenuButton = null;
  49.  
  50.   this.getXULElement = function() {
  51.     return document.getElementById(this.xulElementId);
  52.   }
  53.  
  54.   this.clear = function() {
  55.     this.disableParentMenuButton();
  56.     this.getXULElement().setAttribute("disabled", "true");
  57.     this.getXULElement().removeAttribute("href");
  58.   }
  59.  
  60.   this.displayLink = function(linkElement) {
  61.     if (this.getXULElement().hasAttribute("href")) return false;
  62.  
  63.     this.setItem(linkElement);
  64.     this.enableParentMenuButton();
  65.     return true;
  66.   }
  67.  
  68.   this.setItem = function(linkElement) {
  69.     this.getXULElement().setAttribute("href", linkElement.href);
  70.     this.getXULElement().removeAttribute("disabled");
  71.   }
  72.  
  73.   this.enableParentMenuButton = function() {
  74.     if(this.getParentMenuButton())
  75.       this.getParentMenuButton().removeAttribute("disabled");
  76.   }
  77.  
  78.   this.disableParentMenuButton = function() {
  79.     if (!this.parentMenuButton) return;
  80.  
  81.     this.parentMenuButton.setAttribute("disabled", "true");
  82.     this.parentMenuButton = null;
  83.   }
  84.  
  85.   this.getParentMenuButton = function() {
  86.     if (!this.parentMenuButton)
  87.       this.parentMenuButton = getParentMenuButtonRecursive(
  88.           this.getXULElement());
  89.  
  90.     return this.parentMenuButton;
  91.   }
  92.  
  93.   function getParentMenuButtonRecursive(xulElement) {
  94.     if (!xulElement) return null;
  95.  
  96.     if (xulElement.tagName == "toolbarbutton") 
  97.       return xulElement;
  98.  
  99.     return getParentMenuButtonRecursive(xulElement.parentNode)
  100.   }
  101. }
  102.  
  103.  
  104. function LinkToolbarButton (linkType) {
  105.   this.constructor(linkType);
  106.  
  107.   this.clear = function() {
  108.     this.__proto__.clear.apply(this);
  109.  
  110.     this.getXULElement().removeAttribute("tooltiptext");
  111.   }
  112.  
  113.   this.setItem = function(linkElement) {
  114.     this.__proto__.setItem.apply(this, [linkElement]);
  115.  
  116.     this.getXULElement().setAttribute("tooltiptext", linkElement.getTooltip());
  117.   }
  118.  
  119.   this.enableParentMenuButton = function() { /* do nothing */ }
  120.   this.disableParentMenuButton = function() { /* do nothing */ }
  121. }
  122. LinkToolbarButton.prototype = new LinkToolbarItem;
  123.  
  124.  
  125. function LinkToolbarMenu (linkType) {
  126.   this.constructor(linkType);
  127.  
  128.   this.clear = function() {
  129.     this.disableParentMenuButton();
  130.     this.getXULElement().setAttribute("disabled", "true");
  131.     clearPopup(this.getPopup());
  132.   }
  133.  
  134.   function clearPopup(popup) {
  135.     while (popup.hasChildNodes())
  136.       popup.removeChild(popup.lastChild);
  137.   }
  138.  
  139.   this.getPopup = function() {
  140.     return document.getElementById(this.xulPopupId);
  141.   }
  142.  
  143.   this.displayLink = function(linkElement) {
  144.     this.addMenuItem(linkElement);
  145.     this.getXULElement().removeAttribute("disabled");
  146.     this.enableParentMenuButton();
  147.     return true;
  148.   }
  149.  
  150.   function match(first, second) {
  151.     if (!first && !second) return true;
  152.     if (!first || !second) return false;
  153.  
  154.     return first == second;
  155.   }
  156.  
  157.   this.addMenuItem = function(linkElement) {
  158.     this.getPopup().appendChild(this.createMenuItem(linkElement));
  159.   }
  160.  
  161.   this.createMenuItem = function(linkElement) {
  162.     // XXX: clone a prototypical XUL element instead of hardcoding these
  163.     //   attributes
  164.     var menuitem = document.createElement("menuitem");
  165.     menuitem.setAttribute("label", linkElement.getLabel());
  166.     menuitem.setAttribute("href", linkElement.href);
  167.     menuitem.setAttribute("class", "menuitem-iconic bookmark-item");
  168.     menuitem.setAttribute("rdf:type", 
  169.         "rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#linkType");
  170.  
  171.     return menuitem;
  172.   }
  173. }
  174. LinkToolbarMenu.prototype = new LinkToolbarItem;
  175.  
  176.  
  177. function LinkToolbarTransientMenu (linkType) {
  178.   this.constructor(linkType);
  179.  
  180.   this.getXULElement = function() {
  181.     if (this.__proto__.getXULElement.apply(this)) 
  182.       return this.__proto__.getXULElement.apply(this);
  183.     else
  184.       return this.createXULElement();
  185.   }
  186.  
  187.   this.createXULElement = function() {
  188.     // XXX: clone a prototypical XUL element instead of hardcoding these
  189.     //   attributes
  190.     var menu = document.createElement("menu");
  191.     menu.setAttribute("id", this.xulElementId);
  192.     menu.setAttribute("label", this.linkType);
  193.     menu.setAttribute("disabled", "true");
  194.     menu.setAttribute("class", "menu-iconic bookmark-item");
  195.     menu.setAttribute("container", "true");
  196.     menu.setAttribute("type", "rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
  197.  
  198.     document.getElementById("more-menu-popup").appendChild(menu);
  199.  
  200.     return menu;
  201.   }
  202.  
  203.   this.getPopup = function() {
  204.     if (!this.__proto__.getPopup.apply(this))
  205.       this.getXULElement().appendChild(this.createPopup());
  206.  
  207.     return this.__proto__.getPopup.apply(this) 
  208.   }
  209.  
  210.   this.createPopup = function() {
  211.     var popup = document.createElement("menupopup");
  212.     popup.setAttribute("id", this.xulPopupId);
  213.  
  214.     return popup;
  215.   }
  216.  
  217.   this.clear = function() {
  218.     this.__proto__.clear.apply(this);
  219.  
  220.     // XXX: we really want to use this instead of removeXULElement
  221.     //this.hideXULElement();
  222.     this.removeXULElement();
  223.   }
  224.  
  225.   this.hideXULElement = function() {
  226.     /*
  227.      * XXX: using "hidden" or "collapsed" leads to a crash when you 
  228.      *        open the More menu under certain circumstances.  Maybe
  229.      *        related to bug 83906.  As of 0.9.2 I it doesn't seem
  230.      *        to crash anymore.
  231.      */
  232.     this.getXULElement().setAttribute("collapsed", "true");
  233.   }
  234.  
  235.   this.removeXULElement = function() {
  236.     // XXX: stop using this method once it's safe to use hideXULElement
  237.     if (this.__proto__.getXULElement.apply(this))
  238.       this.__proto__.getXULElement.apply(this).parentNode.removeChild(
  239.           this.__proto__.getXULElement.apply(this));
  240.   }
  241.  
  242.   this.displayLink = function(linkElement) {
  243.     if(!this.__proto__.displayLink.apply(this, [linkElement])) return false;
  244.  
  245.     this.getXULElement().removeAttribute("collapsed");
  246.  
  247.     // Show the 'miscellaneous' separator
  248.     document.getElementById("misc-separator").removeAttribute("collapsed");
  249.     return true;
  250.   }
  251. }
  252.  
  253. LinkToolbarTransientMenu.prototype = new LinkToolbarMenu;
  254.  
  255.