home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / 01_03.iso / software / ghostzilla_hit / files / ghostzilla-1.0-plus-install.exe / chrome / embed-sample.jar / content / embed / mini-nav.js < prev    next >
Encoding:
Text File  |  2002-05-21  |  6.5 KB  |  257 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const nsIWebNavigation = Components.interfaces.nsIWebNavigation;
  40.  
  41. var commandHandler = null;
  42. var gURLBar = null;
  43.  
  44. function nsCommandHandler()
  45. {
  46. }
  47.  
  48. nsCommandHandler.prototype = 
  49. {
  50.   QueryInterface : function(iid)
  51.     {
  52.       if (iid.equals(Components.interfaces.nsICommandHandler))
  53.       {
  54.         return this;
  55.       }
  56.       throw Components.results.NS_NOINTERFACE;
  57.     },
  58.  
  59.     exec : function(command, params)
  60.       {
  61.       },
  62.     query : function(command, params, result)
  63.       {
  64.         result = "";
  65.       }
  66. }
  67.  
  68. //
  69.  
  70. function nsBrowserStatusHandler()
  71. {
  72.   this.init();
  73. }
  74.  
  75. nsBrowserStatusHandler.prototype = 
  76. {
  77.   QueryInterface : function(aIID)
  78.   {
  79.     if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  80.         aIID.equals(Components.interfaces.nsISupportsWeakReference))
  81.       return this;
  82.     throw Components.results.NS_NOINTERFACE;
  83.   },
  84.  
  85.   init : function()
  86.   {
  87.     this.urlBar = document.getElementById("urlbar");
  88.   },
  89.  
  90.   destroy : function()
  91.   {
  92.     this.urlBar = null;
  93.   },
  94.  
  95.   onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
  96.   {
  97.   },
  98.  
  99.   onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  100.   {
  101.   },
  102.  
  103.   onLocationChange : function(aWebProgress, aRequest, aLocation)
  104.   {
  105.     domWindow = aWebProgress.DOMWindow;
  106.     // Update urlbar only if there was a load on the root docshell
  107.     if (domWindow == domWindow.top) {
  108.       this.urlBar.value = location;
  109.     }
  110.   },
  111.  
  112.   onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage)
  113.   {
  114.   },
  115.  
  116.   onSecurityChange : function(aWebProgress, aRequest, aState)
  117.   {
  118.   }
  119. }
  120.  
  121. var gBrowserStatusHandler;
  122. function MiniNavStartup()
  123. {
  124.   dump("*** MiniNavStartup\n");
  125.  
  126.   try {
  127.     gBrowserStatusHandler = new nsBrowserStatusHandler();
  128.     var webNavigation = getWebNavigation();
  129.     webNavigation.sessionHistory = Components.classes["@mozilla.org/browser/shistory;1"]
  130.                                              .createInstance(Components.interfaces.nsISHistory);
  131.  
  132.     var interfaceRequestor = getBrowser().docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  133.     var webProgress = interfaceRequestor.getInterface(Components.interfaces.nsIWebProgress);
  134.     webProgress.addProgressListener(gBrowserStatusHandler, Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
  135.   } catch (e) {
  136.     alert("Error opening a mini-nav window"); 
  137.     dump(e+"\n");
  138.     window.close();
  139.     return;
  140.   }
  141.  
  142.   // create the embedding command handler
  143.   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  144.   var commandHandlerInit = Components
  145.       .classes["@mozilla.org/embedding/browser/nsCommandHandler;1"]
  146.       .createInstance(Components.interfaces.nsICommandHandlerInit);
  147.  
  148.   // Attach it to the window
  149.   commandHandlerInit.window = window;
  150.   commandHandler = commandHandlerInit.QueryInterface(Components.interfaces.nsICommandHandler);
  151.  
  152.   gURLBar = document.getElementById("urlbar");
  153.   dump("gURLBar " + gURLBar + "\n");
  154. }
  155.  
  156. function MiniNavShutdown()
  157. {
  158.   dump("*** MiniNavShutdown\n");
  159.   if (gBrowserStatusHandler)
  160.     gBrowserStatusHandler.destroy();
  161. }
  162.  
  163. function getBrowser()
  164. {
  165.   return document.getElementById("content");
  166. }
  167.  
  168. function getWebNavigation()
  169. {
  170.   return getBrowser().webNavigation;
  171. }
  172.  
  173. function CHExecTest()
  174. {
  175.   if (commandHandler != null)
  176.   {
  177.     commandHandler.exec("hello", "xxx");
  178.   }
  179. }
  180.  
  181. function CHQueryTest()
  182. {
  183.   if (commandHandler != null)
  184.   {
  185.     var result = commandHandler.query("hello", "xxx");
  186.   }
  187. }
  188.  
  189. function InitContextMenu(xulMenu)
  190. {
  191.   // Back determined by canGoBack broadcaster.
  192.   InitMenuItemAttrFromNode( "context-back", "disabled", "canGoBack" );
  193.  
  194.   // Forward determined by canGoForward broadcaster.
  195.   InitMenuItemAttrFromNode( "context-forward", "disabled", "canGoForward" );
  196. }
  197.  
  198. function InitMenuItemAttrFromNode( item_id, attr, other_id )
  199. {
  200.   var elem = document.getElementById( other_id );
  201.   if ( elem && elem.getAttribute( attr ) == "true" ) {
  202.     SetMenuItemAttr( item_id, attr, "true" );
  203.   } else {
  204.     SetMenuItemAttr( item_id, attr, null );
  205.   }
  206. }
  207.  
  208. function SetMenuItemAttr( id, attr, val )
  209. {
  210.   var elem = document.getElementById( id );
  211.   if ( elem ) {
  212.     if ( val == null ) {
  213.       // null indicates attr should be removed.
  214.       elem.removeAttribute( attr );
  215.     } else {
  216.       // Set attr=val.
  217.       elem.setAttribute( attr, val );
  218.     }
  219.   }
  220. }
  221.  
  222. function loadURI(uri)
  223. {
  224.   getWebNavigation().loadURI(uri, nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
  225. }
  226.  
  227. function BrowserLoadURL()
  228. {
  229.   dump("browserloadurl: " + gURLBar.value + '\n');
  230.   try {
  231.     loadURI(gURLBar.value);
  232.   }
  233.   catch(e) {
  234.   }
  235. }
  236.  
  237. function BrowserBack()
  238. {
  239.   getWebNavigation().goBack();
  240. }
  241.  
  242. function BrowserForward()
  243. {
  244.   getWebNavigation().goForward();
  245. }
  246.  
  247. function BrowserStop()
  248. {
  249.   getWebNavigation().stop(nsIWebNavigation.STOP_ALL);
  250. }
  251.  
  252. function BrowserReload()
  253. {
  254.   getWebNavigation().reload(nsIWebNavigation.LOAD_FLAGS_NONE);
  255. }
  256.  
  257.