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 / embed-sample.jar / content / embed / mini-nav.js < prev    next >
Text File  |  2003-06-08  |  7KB  |  261 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.         iid.equals(Components.interfaces.nsISupports))
  54.     {
  55.       return this;
  56.     }
  57.     throw Components.results.NS_NOINTERFACE;
  58.   },
  59.  
  60.   exec : function(command, params)
  61.   {
  62.   },
  63.   query : function(command, params, result)
  64.   {
  65.     result = "";
  66.   }
  67. }
  68.  
  69. //
  70.  
  71. function nsBrowserStatusHandler()
  72. {
  73.   this.init();
  74. }
  75.  
  76. nsBrowserStatusHandler.prototype = 
  77. {
  78.   QueryInterface : function(aIID)
  79.   {
  80.     if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  81.         aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  82.         aIID.equals(Components.interfaces.nsISupports))
  83.     {
  84.       return this;
  85.     }
  86.     throw Components.results.NS_NOINTERFACE;
  87.   },
  88.  
  89.   init : function()
  90.   {
  91.     this.urlBar = document.getElementById("urlbar");
  92.   },
  93.  
  94.   destroy : function()
  95.   {
  96.     this.urlBar = null;
  97.   },
  98.  
  99.   onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
  100.   {
  101.   },
  102.  
  103.   onProgressChange : function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress)
  104.   {
  105.   },
  106.  
  107.   onLocationChange : function(aWebProgress, aRequest, aLocation)
  108.   {
  109.     domWindow = aWebProgress.DOMWindow;
  110.     // Update urlbar only if there was a load on the root docshell
  111.     if (domWindow == domWindow.top) {
  112.       this.urlBar.value = location;
  113.     }
  114.   },
  115.  
  116.   onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage)
  117.   {
  118.   },
  119.  
  120.   onSecurityChange : function(aWebProgress, aRequest, aState)
  121.   {
  122.   }
  123. }
  124.  
  125. var gBrowserStatusHandler;
  126. function MiniNavStartup()
  127. {
  128.   dump("*** MiniNavStartup\n");
  129.  
  130.   try {
  131.     gBrowserStatusHandler = new nsBrowserStatusHandler();
  132.     var webNavigation = getWebNavigation();
  133.     webNavigation.sessionHistory = Components.classes["@mozilla.org/browser/shistory;1"]
  134.                                              .createInstance(Components.interfaces.nsISHistory);
  135.  
  136.     var interfaceRequestor = getBrowser().docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor);
  137.     var webProgress = interfaceRequestor.getInterface(Components.interfaces.nsIWebProgress);
  138.     webProgress.addProgressListener(gBrowserStatusHandler, Components.interfaces.nsIWebProgress.NOTIFY_LOCATION);
  139.   } catch (e) {
  140.     alert("Error opening a mini-nav window"); 
  141.     dump(e+"\n");
  142.     window.close();
  143.     return;
  144.   }
  145.  
  146.   // create the embedding command handler
  147.   netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
  148.   var commandHandlerInit = Components
  149.       .classes["@mozilla.org/embedding/browser/nsCommandHandler;1"]
  150.       .createInstance(Components.interfaces.nsICommandHandlerInit);
  151.  
  152.   // Attach it to the window
  153.   commandHandlerInit.window = window;
  154.   commandHandler = commandHandlerInit.QueryInterface(Components.interfaces.nsICommandHandler);
  155.  
  156.   gURLBar = document.getElementById("urlbar");
  157.   dump("gURLBar " + gURLBar + "\n");
  158. }
  159.  
  160. function MiniNavShutdown()
  161. {
  162.   dump("*** MiniNavShutdown\n");
  163.   if (gBrowserStatusHandler)
  164.     gBrowserStatusHandler.destroy();
  165. }
  166.  
  167. function getBrowser()
  168. {
  169.   return document.getElementById("content");
  170. }
  171.  
  172. function getWebNavigation()
  173. {
  174.   return getBrowser().webNavigation;
  175. }
  176.  
  177. function CHExecTest()
  178. {
  179.   if (commandHandler != null)
  180.   {
  181.     commandHandler.exec("hello", "xxx");
  182.   }
  183. }
  184.  
  185. function CHQueryTest()
  186. {
  187.   if (commandHandler != null)
  188.   {
  189.     var result = commandHandler.query("hello", "xxx");
  190.   }
  191. }
  192.  
  193. function InitContextMenu(xulMenu)
  194. {
  195.   // Back determined by canGoBack broadcaster.
  196.   InitMenuItemAttrFromNode( "context-back", "disabled", "canGoBack" );
  197.  
  198.   // Forward determined by canGoForward broadcaster.
  199.   InitMenuItemAttrFromNode( "context-forward", "disabled", "canGoForward" );
  200. }
  201.  
  202. function InitMenuItemAttrFromNode( item_id, attr, other_id )
  203. {
  204.   var elem = document.getElementById( other_id );
  205.   if ( elem && elem.getAttribute( attr ) == "true" ) {
  206.     SetMenuItemAttr( item_id, attr, "true" );
  207.   } else {
  208.     SetMenuItemAttr( item_id, attr, null );
  209.   }
  210. }
  211.  
  212. function SetMenuItemAttr( id, attr, val )
  213. {
  214.   var elem = document.getElementById( id );
  215.   if ( elem ) {
  216.     if ( val == null ) {
  217.       // null indicates attr should be removed.
  218.       elem.removeAttribute( attr );
  219.     } else {
  220.       // Set attr=val.
  221.       elem.setAttribute( attr, val );
  222.     }
  223.   }
  224. }
  225.  
  226. function loadURI(uri)
  227. {
  228.   getWebNavigation().loadURI(uri, nsIWebNavigation.LOAD_FLAGS_NONE, null, null, null);
  229. }
  230.  
  231. function BrowserLoadURL()
  232. {
  233.   dump("browserloadurl: " + gURLBar.value + '\n');
  234.   try {
  235.     loadURI(gURLBar.value);
  236.   }
  237.   catch(e) {
  238.   }
  239. }
  240.  
  241. function BrowserBack()
  242. {
  243.   getWebNavigation().goBack();
  244. }
  245.  
  246. function BrowserForward()
  247. {
  248.   getWebNavigation().goForward();
  249. }
  250.  
  251. function BrowserStop()
  252. {
  253.   getWebNavigation().stop(nsIWebNavigation.STOP_ALL);
  254. }
  255.  
  256. function BrowserReload()
  257. {
  258.   getWebNavigation().reload(nsIWebNavigation.LOAD_FLAGS_NONE);
  259. }
  260.  
  261.