home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / comm.jar / content / communicator / openLocation.js < prev    next >
Text File  |  2004-01-22  |  6KB  |  183 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   <blaker@netscape.com>
  23.  */
  24.  
  25. var browser;
  26. var dialog = {};
  27. var gNavigatorBundle;
  28. var pref = null;
  29. try {
  30.   pref = Components.classes["@mozilla.org/preferences-service;1"]
  31.                    .getService(Components.interfaces.nsIPrefBranch);
  32. } catch (ex) {
  33.   // not critical, remain silent
  34. }
  35.  
  36. function onLoad()
  37. {
  38.   dialog.input          = document.getElementById("dialog.input");
  39.   dialog.open           = document.documentElement.getButton("accept");
  40.   dialog.openAppList    = document.getElementById("openAppList");
  41.   dialog.openTopWindow  = document.getElementById("currentWindow");
  42.   dialog.openEditWindow = document.getElementById("editWindow");
  43.   dialog.bundle         = document.getElementById("openLocationBundle");
  44.   gNavigatorBundle      = document.getElementById("navigatorBundle");
  45.  
  46.   if ("arguments" in window && window.arguments.length >= 1)
  47.     browser = window.arguments[0];
  48.    
  49.   if (!browser) {
  50.     // No browser supplied - we are calling from Composer
  51.     dialog.openAppList.selectedItem = dialog.openEditWindow;
  52.  
  53.     // Change string to make more sense for Composer
  54.     dialog.openTopWindow.setAttribute("label", dialog.bundle.getString("existingNavigatorWindow"));
  55.  
  56.     // Find most recent browser window
  57.     var windowManager = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  58.     var windowManagerInterface = windowManager.QueryInterface( Components.interfaces.nsIWindowMediator);
  59.     if (windowManagerInterface)
  60.       browser = windowManagerInterface.getMostRecentWindow( "navigator:browser" );
  61.  
  62.     // Disable "current browser" item if no browser is open
  63.     if (!browser)
  64.       dialog.openTopWindow.setAttribute("disabled", "true");
  65.   }
  66.   else {
  67.     dialog.openAppList.selectedItem = dialog.openTopWindow;
  68.   }
  69.  
  70.   // change OK button text to 'open'
  71.   dialog.open.label = dialog.bundle.getString("openButtonLabel");
  72.  
  73.   if (pref) {
  74.     try {
  75.       var value = pref.getIntPref("general.open_location.last_window_choice");
  76.       var element = dialog.openAppList.getElementsByAttribute("value", value)[0];
  77.       if (element)
  78.         dialog.openAppList.selectedItem = element;
  79.       dialog.input.value = pref.getComplexValue("general.open_location.last_url",
  80.                                                 Components.interfaces.nsISupportsString).data;
  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 editorApplicationOverlay.js 
  114.         // 3rd param tells editPage to use "delayedOpenWindow"
  115.         if ("editPage" in window.opener)
  116.           window.opener.editPage(url, window.opener, true);
  117.         break;
  118.       case "3":
  119.         if (browser.getBrowser && browser.getBrowser().localName == "tabbrowser")
  120.           browser.delayedOpenTab(url);
  121.         else
  122.           browser.loadURI(url); // Just do a normal load.
  123.         break;
  124.     }
  125.   }
  126.   catch(exception) {
  127.   }
  128.  
  129.   if (pref) {
  130.     var str = Components.classes["@mozilla.org/supports-string;1"]
  131.                         .createInstance(Components.interfaces.nsISupportsString);
  132.     str.data = dialog.input.value;
  133.     pref.setComplexValue("general.open_location.last_url",
  134.                          Components.interfaces.nsISupportsString, str);
  135.     pref.setIntPref("general.open_location.last_window_choice", dialog.openAppList.value);
  136.   }
  137.  
  138.   // Delay closing slightly to avoid timing bug on Linux.
  139.   window.close();
  140.   return false;
  141. }
  142.  
  143. function createInstance(contractid, iidName)
  144. {
  145.   var iid = Components.interfaces[iidName];
  146.   return Components.classes[contractid].createInstance(iid);
  147. }
  148.  
  149. const nsIFilePicker = Components.interfaces.nsIFilePicker;
  150. function onChooseFile()
  151. {
  152.   try {
  153.     var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  154.     fp.init(window, dialog.bundle.getString("chooseFileDialogTitle"), nsIFilePicker.modeOpen);
  155.     if (dialog.openAppList.value == "2") {
  156.       // When loading into Composer, direct user to prefer HTML files and text files,
  157.       // so we call separately to control the order of the filter list
  158.       fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText);
  159.       fp.appendFilters(nsIFilePicker.filterText);
  160.       fp.appendFilters(nsIFilePicker.filterAll);
  161.     }
  162.     else {
  163.       fp.appendFilters(nsIFilePicker.filterHTML | nsIFilePicker.filterText |
  164.                        nsIFilePicker.filterAll | nsIFilePicker.filterImages | nsIFilePicker.filterXML);
  165.     }
  166.  
  167.     if (fp.show() == nsIFilePicker.returnOK && fp.fileURL.spec && fp.fileURL.spec.length > 0)
  168.       dialog.input.value = fp.fileURL.spec;
  169.   }
  170.   catch(ex) {
  171.   }
  172.   doEnabling();
  173. }
  174.  
  175. function useUBHistoryItem(aMenuItem)
  176. {
  177.   var urlbar = document.getElementById("dialog.input");
  178.   urlbar.value = aMenuItem.getAttribute("label");
  179.   urlbar.focus();
  180.   doEnabling();
  181. }
  182.  
  183.