home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / navigator / linkToolbarItem.js < prev    next >
Text File  |  2003-06-08  |  8KB  |  271 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.knownLinks = null;
  129.  
  130.   this.clear = function() {
  131.     this.disableParentMenuButton();
  132.     this.getXULElement().setAttribute("disabled", "true");
  133.     clearPopup(this.getPopup());
  134.     this.knownLinks = null;
  135.   }
  136.  
  137.   function clearPopup(popup) {
  138.     while (popup.hasChildNodes())
  139.       popup.removeChild(popup.lastChild);
  140.   }
  141.  
  142.   this.getPopup = function() {
  143.     return document.getElementById(this.xulPopupId);
  144.   }
  145.  
  146.   this.displayLink = function(linkElement) {
  147.     if (this.isAlreadyAdded(linkElement)) return false;
  148.  
  149.     this.getKnownLinks()[linkElement.href] = true;
  150.     this.addMenuItem(linkElement);
  151.     this.getXULElement().removeAttribute("disabled");
  152.     this.enableParentMenuButton();
  153.     return true;
  154.   }
  155.  
  156.   this.isAlreadyAdded = function(linkElement) {
  157.     return this.getKnownLinks()[linkElement.href];
  158.   }
  159.  
  160.   this.getKnownLinks = function() {
  161.     if (!this.knownLinks)
  162.       this.knownLinks = new Array();
  163.     return this.knownLinks;
  164.   }
  165.   
  166.   function match(first, second) {
  167.     if (!first && !second) return true;
  168.     if (!first || !second) return false;
  169.  
  170.     return first == second;
  171.   }
  172.  
  173.   this.addMenuItem = function(linkElement) {
  174.     this.getPopup().appendChild(this.createMenuItem(linkElement));
  175.   }
  176.  
  177.   this.createMenuItem = function(linkElement) {
  178.     // XXX: clone a prototypical XUL element instead of hardcoding these
  179.     //   attributes
  180.     var menuitem = document.createElement("menuitem");
  181.     menuitem.setAttribute("label", linkElement.getLabel());
  182.     menuitem.setAttribute("href", linkElement.href);
  183.     menuitem.setAttribute("class", "menuitem-iconic bookmark-item");
  184.     menuitem.setAttribute("rdf:type", 
  185.         "rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#linkType");
  186.  
  187.     return menuitem;
  188.   }
  189. }
  190. LinkToolbarMenu.prototype = new LinkToolbarItem;
  191.  
  192.  
  193. function LinkToolbarTransientMenu (linkType) {
  194.   this.constructor(linkType);
  195.  
  196.   this.getXULElement = function() {
  197.     if (this.__proto__.getXULElement.apply(this)) 
  198.       return this.__proto__.getXULElement.apply(this);
  199.     else
  200.       return this.createXULElement();
  201.   }
  202.  
  203.   this.createXULElement = function() {
  204.     // XXX: clone a prototypical XUL element instead of hardcoding these
  205.     //   attributes
  206.     var menu = document.createElement("menu");
  207.     menu.setAttribute("id", this.xulElementId);
  208.     menu.setAttribute("label", this.linkType);
  209.     menu.setAttribute("disabled", "true");
  210.     menu.setAttribute("class", "menu-iconic bookmark-item");
  211.     menu.setAttribute("container", "true");
  212.     menu.setAttribute("type", "rdf:http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
  213.  
  214.     document.getElementById("more-menu-popup").appendChild(menu);
  215.  
  216.     return menu;
  217.   }
  218.  
  219.   this.getPopup = function() {
  220.     if (!this.__proto__.getPopup.apply(this))
  221.       this.getXULElement().appendChild(this.createPopup());
  222.  
  223.     return this.__proto__.getPopup.apply(this) 
  224.   }
  225.  
  226.   this.createPopup = function() {
  227.     var popup = document.createElement("menupopup");
  228.     popup.setAttribute("id", this.xulPopupId);
  229.  
  230.     return popup;
  231.   }
  232.  
  233.   this.clear = function() {
  234.     this.__proto__.clear.apply(this);
  235.  
  236.     // XXX: we really want to use this instead of removeXULElement
  237.     //this.hideXULElement();
  238.     this.removeXULElement();
  239.   }
  240.  
  241.   this.hideXULElement = function() {
  242.     /*
  243.      * XXX: using "hidden" or "collapsed" leads to a crash when you 
  244.      *        open the More menu under certain circumstances.  Maybe
  245.      *        related to bug 83906.  As of 0.9.2 I it doesn't seem
  246.      *        to crash anymore.
  247.      */
  248.     this.getXULElement().setAttribute("collapsed", "true");
  249.   }
  250.  
  251.   this.removeXULElement = function() {
  252.     // XXX: stop using this method once it's safe to use hideXULElement
  253.     if (this.__proto__.getXULElement.apply(this))
  254.       this.__proto__.getXULElement.apply(this).parentNode.removeChild(
  255.           this.__proto__.getXULElement.apply(this));
  256.   }
  257.  
  258.   this.displayLink = function(linkElement) {
  259.     if(!this.__proto__.displayLink.apply(this, [linkElement])) return false;
  260.  
  261.     this.getXULElement().removeAttribute("collapsed");
  262.  
  263.     // Show the 'miscellaneous' separator
  264.     document.getElementById("misc-separator").removeAttribute("collapsed");
  265.     return true;
  266.   }
  267. }
  268.  
  269. LinkToolbarTransientMenu.prototype = new LinkToolbarMenu;
  270.  
  271.