home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / browser.xpi / bin / chrome / comm.jar / content / communicator / openLocation.js < prev    next >
Encoding:
JavaScript  |  2001-09-24  |  5.6 KB  |  170 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released March
  14.  * 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications
  17.  * Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s): Michael Lowe <michael.lowe@bigfoot.com>
  22.  *                 Blake Ross   <blakeross@telocity.com>
  23.  */
  24.  
  25. var browser;
  26. var dialog = {};
  27. var pref = null;
  28. try {
  29.   pref = Components.classes["@mozilla.org/preferences;1"]
  30.                    .getService(Components.interfaces.nsIPref);
  31. } catch (ex) {
  32.   // not critical, remain silent
  33. }
  34.  
  35. function onLoad()
  36. {
  37.   dialog.input          = document.getElementById("dialog.input");
  38.   dialog.open           = document.getElementById("ok");
  39.   dialog.openAppList    = document.getElementById("openAppList");
  40.   dialog.openTopWindow  = document.getElementById("currentWindow");
  41.   dialog.openEditWindow = document.getElementById("editWindow");
  42.   dialog.bundle         = document.getElementById("openLocationBundle");
  43.  
  44.   if ("arguments" in window && window.arguments.length >= 1)
  45.     browser = window.arguments[0];
  46.  
  47.   if (!browser) {
  48.     // No browser supplied - we are calling from Composer
  49.     dialog.openAppList.selectedItem = dialog.openEditWindow;
  50.  
  51.     // Change string to make more sense for Composer
  52.     dialog.openTopWindow.setAttribute("label", dialog.bundle.getString("existingNavigatorWindow"));
  53.  
  54.     // Find most recent browser window
  55.     var windowManager = Components.classes['@mozilla.org/rdf/datasource;1?name=window-mediator'].getService();
  56.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  57.     if (windowManagerInterface)
  58.       browser = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  59.  
  60.     // Disable "current browser" item if no browser is open
  61.     if (!browser)
  62.       dialog.openTopWindow.setAttribute("disabled", "true");
  63.   }
  64.   else {
  65.     dialog.openAppList.selectedItem = dialog.openTopWindow;
  66.   }
  67.  
  68.   // change OK button text to 'open'
  69.   dialog.open.label = dialog.bundle.getString("openButtonLabel");
  70.  
  71.   doSetOKCancel(open, 0, 0, 0);
  72.  
  73.   dialog.input.focus();
  74.   if (pref) {
  75.     try {
  76.       var value = pref.GetIntPref("general.open_location.last_window_choice");
  77.       var element = dialog.openAppList.getElementsByAttribute("value", value)[0];
  78.       if (element)
  79.         dialog.openAppList.selectedItem = element;
  80.       dialog.input.value = pref.CopyUnicharPref("general.open_location.last_url");
  81.     }
  82.     catch(ex) {
  83.     }
  84.     if (dialog.input.value)
  85.       dialog.input.select(); // XXX should probably be done automatically
  86.   }
  87.  
  88.   doEnabling();
  89. }
  90.  
  91. function doEnabling()
  92. {
  93.     dialog.open.disabled = !dialog.input.value;
  94. }
  95.  
  96. function open()
  97. {
  98.   var url;
  99.   if (browser)
  100.     url = browser.getShortcutOrURI(dialog.input.value);
  101.   else
  102.     url = dialog.input.value;
  103.  
  104.   try {
  105.     switch (dialog.openAppList.value) {
  106.       case "0":
  107.         browser.loadURI(url);
  108.         break;
  109.       case "1":
  110.         window.opener.delayedOpenWindow(getBrowserURL(), "all,dialog=no", url);
  111.         break;
  112.       case "2":
  113.         // editPage is in utilityOverlay.js (all editor openers with URL should use this)
  114.         // 3rd param tells editPage to use "delayedOpenWindow"
  115.         editPage(url, window.opener, true);
  116.         break;
  117.     }
  118.   }
  119.   catch(exception) {
  120.   }
  121.  
  122.   if (pref) {
  123.     pref.SetUnicharPref("general.open_location.last_url", dialog.input.value);
  124.     pref.SetIntPref("general.open_location.last_window_choice", dialog.openAppList.value);
  125.   }
  126.  
  127.   // Delay closing slightly to avoid timing bug on Linux.
  128.   window.close();
  129.   return false;
  130. }
  131.  
  132. function createInstance(contractid, iidName)
  133. {
  134.   var iid = Components.interfaces[iidName];
  135.   return Components.classes[contractid].createInstance(iid);
  136. }
  137.  
  138. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  139. function onChooseFile()
  140. {
  141.   try {
  142.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  143.     fp.init(window, dialog.bundle.getString("chooseFileDialogTitle"), nsIFilePicker.modeOpen);
  144.     if (dialog.openAppList.value == "2") {
  145.       // When loading into Composer, direct user to prefer HTML files and text files,
  146.       // so we call separately to control the order of the filter list
  147.       fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText);
  148.       fp.appendFilters(nsIFilePicker.filterText);
  149.       fp.appendFilters(nsIFilePicker.filterAll);
  150.     }
  151.     else {
  152.       fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText |
  153.                        nsIFilePicker.filterAll | nsIFilePicker.filterImages | nsIFilePicker.filterXML);
  154.     }
  155.  
  156.     if (fp.show() == nsIFilePicker.returnOK && fp.fileURL.spec && fp.fileURL.spec.length > 0)
  157.       dialog.input.value = fp.fileURL.spec;
  158.   }
  159.   catch(ex) {
  160.   }
  161.   doEnabling();
  162. }
  163.  
  164. function useUBHistoryItem(aMenuItem)
  165. {
  166.   var urlbar = document.getElementById("dialog.input");
  167.   urlbar.value = aMenuItem.getAttribute("label");
  168. }
  169.  
  170.