home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / navigator / linkToolbarOverlay.js < prev    next >
Text File  |  2003-12-20  |  9KB  |  258 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.deactivate();
  88.   linkToolbarUI.fullSlowRefresh();
  89. }
  90.  
  91. LinkToolbarUI.prototype.fullSlowRefresh =
  92. function()
  93. {
  94.   var currentNode = getBrowser().contentDocument.documentElement;
  95.   if (!(currentNode instanceof Components.interfaces.nsIDOMHTMLHtmlElement))
  96.     return;
  97.   currentNode = currentNode.firstChild;
  98.   
  99.   while(currentNode)
  100.   {
  101.     if (currentNode instanceof Components.interfaces.nsIDOMHTMLHeadElement) {
  102.       currentNode = currentNode.firstChild;
  103.       
  104.       while(currentNode)
  105.       {
  106.         if (currentNode instanceof Components.interfaces.nsIDOMHTMLLinkElement)
  107.           linkToolbarUI.linkAdded({originalTarget: currentNode});
  108.         currentNode = currentNode.nextSibling;
  109.       }
  110.     }
  111.     else if (currentNode instanceof Components.interfaces.nsIDOMElement)
  112.     {
  113.       // head is supposed to be the first element inside html.
  114.       // Got something else instead. returning
  115.        return;
  116.     }
  117.     else
  118.     {
  119.       // Got a comment node or something like that. Moving on.
  120.       currentNode = currentNode.nextSibling;
  121.     }
  122.   }  
  123. }
  124.  
  125. LinkToolbarUI.prototype.toolbarActive = false;
  126.  
  127. LinkToolbarUI.prototype.activate =
  128. function()
  129. {
  130.   if (!linkToolbarUI.toolbarActive) {
  131.     linkToolbarUI.toolbarActive = true;
  132.     document.getElementById("linktoolbar").setAttribute("hasitems", "true");
  133.     var contentArea = document.getElementById("appcontent");
  134.     contentArea.addEventListener("unload", linkToolbarUI.clear, true);
  135.     contentArea.addEventListener("load", linkToolbarUI.deactivate, true);
  136.     contentArea.addEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
  137.                                  true);
  138.   }
  139. }
  140.  
  141. LinkToolbarUI.prototype.deactivate =
  142. function()
  143. {
  144.   // This function can never be called unless the toolbar is active, because
  145.   // it's a handler that's only activated in that situation, so there's no need
  146.   // to check toolbarActive. On the other hand, by the time this method is
  147.   // called the toolbar might have been populated again already, in which case
  148.   // we don't want to deactivate.
  149.   if (!linkToolbarHandler.hasItems) {
  150.     linkToolbarUI.toolbarActive = false;
  151.     document.getElementById("linktoolbar").setAttribute("hasitems", "false");
  152.     var contentArea = document.getElementById("appcontent");
  153.     contentArea.removeEventListener("unload", linkToolbarUI.clear, true);
  154.     contentArea.removeEventListener("load", linkToolbarUI.deactivate, true);
  155.     contentArea.removeEventListener("DOMHeadLoaded", linkToolbarUI.deactivate,
  156.                                     true);
  157.   }
  158. }
  159.  
  160. /* called whenever something on the toolbar gets an oncommand event */
  161. LinkToolbarUI.prototype.commanded =
  162. function(event)
  163. {
  164.   // Return if this is one of the menubuttons.
  165.   if (event.target.getAttribute("type") == "menu") return;
  166.   
  167.   if (!event.target.getAttribute("href")) return;
  168.  
  169.   var destURL = event.target.getAttribute("href");
  170.   
  171.   // We have to do a security check here, because we are loading URIs given
  172.   // to us by a web page from chrome, which is privileged.
  173.   try {
  174.     var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"].getService().
  175.                     QueryInterface(Components.interfaces.nsIScriptSecurityManager);
  176.       ssm.checkLoadURIStr(window.content.location.href, destURL, 0);
  177.     var referrer =
  178.         Components.classes["@mozilla.org/network/standard-url;1"].
  179.           createInstance(Components.interfaces.nsIURI);
  180.     referrer.spec = window.content.location.href;
  181.     loadURI(destURL, referrer);
  182.   } catch (e) {
  183.     dump("Error: it is not permitted to load this URI from a <link> element: " + e);
  184.   }
  185. }
  186.  
  187. // functions for twiddling XUL elements in the toolbar
  188.  
  189. LinkToolbarUI.prototype.toggleLinkToolbar =
  190. function(checkedItem)
  191. {
  192.   this.goToggleTristateToolbar("linktoolbar", checkedItem);
  193.   this.initHandlers();
  194.   if (this.isLinkToolbarEnabled())
  195.     this.fullSlowRefresh();
  196.   else
  197.     linkToolbarHandler.clearAllItems();
  198. }
  199.  
  200. LinkToolbarUI.prototype.initLinkbarVisibilityMenu = 
  201. function()
  202. {
  203.   var state = document.getElementById("linktoolbar").getAttribute("hidden");
  204.   if (!state)
  205.     state = "maybe";
  206.   var checkedItem = document.getElementById("cmd_viewlinktoolbar_" + state);
  207.   checkedItem.setAttribute("checked", true);
  208.   checkedItem.checked = true;
  209. }
  210.  
  211. LinkToolbarUI.prototype.goToggleTristateToolbar =
  212. function(id, checkedItem)
  213. {
  214.   var toolbar = document.getElementById(id);
  215.   if (toolbar)
  216.   {
  217.     toolbar.setAttribute("hidden", checkedItem.value);
  218.     document.persist(id, "hidden");
  219.   }
  220. }
  221.  
  222. LinkToolbarUI.prototype.addHandlerActive = false;
  223.  
  224. LinkToolbarUI.prototype.initialized = false;
  225.  
  226. LinkToolbarUI.prototype.initHandlers =
  227. function()
  228. {
  229.   var contentArea = document.getElementById("appcontent");
  230.   if (linkToolbarUI.isLinkToolbarEnabled())
  231.   {
  232.     if (!linkToolbarUI.addHandlerActive) {
  233.       contentArea.addEventListener("select", linkToolbarUI.tabSelected,
  234.                                    false);
  235.       contentArea.addEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
  236.                                    true);
  237.       linkToolbarUI.addHandlerActive = true;
  238.     }
  239.   } else
  240.   {
  241.     if (linkToolbarUI.addHandlerActive) {
  242.       contentArea.removeEventListener("select", linkToolbarUI.tabSelected,
  243.                                       false);
  244.       contentArea.removeEventListener("DOMLinkAdded", linkToolbarUI.linkAdded,
  245.                                       true);
  246.       linkToolbarUI.addHandlerActive = false;
  247.     }
  248.   }
  249.   if (!linkToolbarUI.initialized)
  250.   {
  251.     linkToolbarUI.initialized = true;
  252.     document.removeEventListener("load", linkToolbarUI.initHandlers, true);
  253.   }
  254. }
  255.  
  256. const linkToolbarUI = new LinkToolbarUI;
  257.  
  258.