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 / linkToolbarOverlay.js < prev    next >
Text File  |  2003-06-08  |  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.  *      Henri Sivonen <henris@clinet.fi>
  25.  *      Stuart Ballard <sballard@netreach.net>
  26.  *
  27.  * Alternatively, the contents of this file may be used under the terms of
  28.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  29.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30.  * in which case the provisions of the GPL or the LGPL are applicable instead
  31.  * of those above. If you wish to allow use of your version of this file only
  32.  * under the terms of either the GPL or the LGPL, and not to allow others to
  33.  * use your version of this file under the terms of the MPL, indicate your
  34.  * decision by deleting the provisions above and replace them with the notice
  35.  * and other provisions required by the GPL or the LGPL. If you do not delete
  36.  * the provisions above, a recipient may use your version of this file under
  37.  * the terms of any one of the MPL, the GPL or the LGPL.
  38.  *
  39.  * ***** END LICENSE BLOCK ***** */
  40.  
  41. var LinkToolbarUI = function()
  42. {
  43. }
  44.  
  45. LinkToolbarUI.prototype.linkAdded =
  46. function(event)
  47. {
  48.   var element = event.originalTarget;
  49.  
  50.   if (element.ownerDocument != getBrowser().contentDocument ||
  51.       !linkToolbarUI.isLinkToolbarEnabled() ||
  52.       !element instanceof Components.interfaces.nsIDOMHTMLLinkElement ||
  53.       !element.href || (!element.rel && !element.rev))
  54.     return;
  55.  
  56.   linkToolbarHandler.handle(element);
  57. }
  58.  
  59. LinkToolbarUI.prototype.isLinkToolbarEnabled =
  60. function()
  61. {
  62.   if (document.getElementById("linktoolbar").getAttribute("hidden") == "true")
  63.     return false;
  64.   else
  65.     return true;
  66. }
  67.  
  68. LinkToolbarUI.prototype.clear =
  69. function(event)
  70. {
  71.   if (event.originalTarget != getBrowser().contentDocument ||
  72.       !linkToolbarUI.isLinkToolbarEnabled() ||
  73.       !linkToolbarHandler.hasItems)
  74.     return;
  75.  
  76.   linkToolbarHandler.clearAllItems();
  77. }
  78.  
  79. LinkToolbarUI.prototype.tabSelected =
  80. function(event)
  81. {
  82.   if (event.originalTarget.localName != "tabs" ||
  83.       !linkToolbarUI.isLinkToolbarEnabled())
  84.     return;
  85.  
  86.   linkToolbarHandler.clearAllItems();
  87.   linkToolbarUI.fullSlowRefresh();
  88. }
  89.  
  90. LinkToolbarUI.prototype.fullSlowRefresh =
  91. function()
  92. {
  93.   var currentNode = getBrowser().contentDocument.documentElement;
  94.   if (!(currentNode instanceof Components.interfaces.nsIDOMHTMLHtmlElement))
  95.     return;
  96.   currentNode = currentNode.firstChild;
  97.   
  98.   while(currentNode)
  99.   {
  100.     if (currentNode instanceof Components.interfaces.nsIDOMHTMLHeadElement) {
  101.       currentNode = currentNode.firstChild;
  102.       
  103.       while(currentNode)
  104.       {
  105.         if (currentNode instanceof Components.interfaces.nsIDOMHTMLLinkElement)
  106.           linkToolbarUI.linkAdded({originalTarget: currentNode});
  107.         currentNode = currentNode.nextSibling;
  108.       }
  109.     }
  110.     else if (currentNode instanceof Components.interfaces.nsIDOMElement)
  111.     {
  112.       // head is supposed to be the first element inside html.
  113.       // Got something else instead. returning
  114.        return;
  115.     }
  116.     else
  117.     {
  118.       // Got a comment node or something like that. Moving on.
  119.       currentNode = currentNode.nextSibling;
  120.     }
  121.   }  
  122. }
  123.  
  124. LinkToolbarUI.prototype.toolbarActive = false;
  125.  
  126. LinkToolbarUI.prototype.activate =
  127. function()
  128. {
  129.   if (!linkToolbarUI.toolbarActive) {
  130.     linkToolbarUI.toolbarActive = true;
  131.     document.getElementById("linktoolbar").setAttribute("hasitems", "true");
  132.     var contentArea = document.getElementById("appcontent");
  133.     contentArea.addEventListener("unload", linkToolbarUI.clear, true);
  134.     contentArea.addEventListener("load", linkToolbarUI.deactivate, true);
  135.     contentArea.addEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
  136.                                  true);
  137.   }
  138. }
  139.  
  140. LinkToolbarUI.prototype.deactivate =
  141. function()
  142. {
  143.   // This function can never be called unless the toolbar is active, because
  144.   // it's a handler that's only activated in that situation, so there's no need
  145.   // to check toolbarActive. On the other hand, by the time this method is
  146.   // called the toolbar might have been populated again already, in which case
  147.   // we don't want to deactivate.
  148.   if (!linkToolbarHandler.hasItems) {
  149.     linkToolbarUI.toolbarActive = false;
  150.     document.getElementById("linktoolbar").setAttribute("hasitems", "false");
  151.     var contentArea = document.getElementById("appcontent");
  152.     contentArea.removeEventListener("unload", linkToolbarUI.clear, true);
  153.     contentArea.removeEventListener("load", linkToolbarUI.deactivate, true);
  154.     contentArea.removeEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
  155.                                     true);
  156.   }
  157. }
  158.  
  159. /* called whenever something on the toolbar gets an oncommand event */
  160. LinkToolbarUI.prototype.commanded =
  161. function(event)
  162. {
  163.   // Return if this is one of the menubuttons.
  164.   if (event.target.getAttribute("type") == "menu") return;
  165.   
  166.   if (!event.target.getAttribute("href")) return;
  167.  
  168.   var destURL = event.target.getAttribute("href");
  169.   
  170.   // We have to do a security check here, because we are loading URIs given
  171.   // to us by a web page from chrome, which is privileged.
  172.   try {
  173.     var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"].getService().
  174.                     QueryInterface(Components.interfaces.nsIScriptSecurityManager);
  175.       ssm.checkLoadURIStr(window.content.location.href, destURL, 0);
  176.     var referrer =
  177.         Components.classes["@mozilla.org/network/standard-url;1"].
  178.           createInstance(Components.interfaces.nsIURI);
  179.     referrer.spec = window.content.location.href;
  180.     loadURI(destURL, referrer);
  181.   } catch (e) {
  182.     dump("Error: it is not permitted to load this URI from a <link> element: " + e);
  183.   }
  184. }
  185.  
  186. // functions for twiddling XUL elements in the toolbar
  187.  
  188. LinkToolbarUI.prototype.toggleLinkToolbar =
  189. function(checkedItem)
  190. {
  191.   this.goToggleTristateToolbar("linktoolbar", checkedItem);
  192.   this.initHandlers();
  193.   if (this.isLinkToolbarEnabled())
  194.     this.fullSlowRefresh();
  195.   else
  196.     linkToolbarHandler.clearAllItems();
  197. }
  198.  
  199. LinkToolbarUI.prototype.initLinkbarVisibilityMenu = 
  200. function()
  201. {
  202.   var state = document.getElementById("linktoolbar").getAttribute("hidden");
  203.   if (!state)
  204.     state = "maybe";
  205.   var checkedItem = document.getElementById("cmd_viewlinktoolbar_" + state);
  206.   checkedItem.setAttribute("checked", true);
  207.   checkedItem.checked = true;
  208. }
  209.  
  210. LinkToolbarUI.prototype.goToggleTristateToolbar =
  211. function(id, checkedItem)
  212. {
  213.   var toolbar = document.getElementById(id);
  214.   if (toolbar)
  215.   {
  216.     toolbar.setAttribute("hidden", checkedItem.value);
  217.     document.persist(id, "hidden");
  218.   }
  219. }
  220.  
  221. LinkToolbarUI.prototype.addHandlerActive = false;
  222.  
  223. LinkToolbarUI.prototype.initHandlers =
  224. function()
  225. {
  226.   var contentArea = document.getElementById("appcontent");
  227.   if (linkToolbarUI.isLinkToolbarEnabled())
  228.   {
  229.     if (!linkToolbarUI.addHandlerActive) {
  230.       contentArea.addEventListener("select", linkToolbarUI.tabSelected,
  231.                                    false);
  232.       contentArea.addEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
  233.                                    true);
  234.       linkToolbarUI.addHandlerActive = true;
  235.     }
  236.   } else
  237.   {
  238.     if (linkToolbarUI.addHandlerActive) {
  239.       contentArea.removeEventListener("select", linkToolbarUI.tabSelected,
  240.                                       false);
  241.       contentArea.removeEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
  242.                                       true);
  243.       linkToolbarUI.addHandlerActive = false;
  244.     }
  245.   }
  246.   if (!linkToolbarUI.initialized)
  247.   {
  248.     linkToolbarUI.initialized = true;
  249.     document.removeEventListener("load", linkToolbarUI.initHandlers, true);
  250.   }
  251. }
  252.  
  253. const linkToolbarUI = new LinkToolbarUI;
  254.  
  255.