home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 December / VPR0112B.ISO / NETSCAPE6 / N6Setup.exe / bin / chrome / comm.jar / content / communicator / bookmarks / addBookmark.js next >
Text File  |  2001-06-27  |  9KB  |  240 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  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.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s): 
  21.  *   Ben Goodger <ben@netscape.com> (Original Author)
  22.  */
  23.  
  24. /**
  25.  * Add Bookmark Dialog. 
  26.  * ====================
  27.  * 
  28.  * This is a generic bookmark dialog that allows for bookmark addition
  29.  * and folder selection. It can be opened with various parameters that 
  30.  * result in appearance/purpose differences and initial state. 
  31.  * 
  32.  * Use: Open with 'openDialog', with the flags 
  33.  *        'centerscreen,chrome,dialog=no,resizable=yes'
  34.  * 
  35.  * Parameters: 
  36.  *   Apart from the standard openDialog parameters, this dialog can 
  37.  *   be passed additional information, which gets mapped to the 
  38.  *   window.arguments array:
  39.  * 
  40.  *   window.arguments[0]: Bookmark Name. The value to be prefilled
  41.  *                        into the "Name: " field (if visible).
  42.  *   window.arguments[1]: Bookmark URL: The location of the bookmark.
  43.  *                        The value to be filled in the "Location: "
  44.  *                        field (if visible).
  45.  *   window.arguments[2]: Bookmark Folder. The RDF Resource URI of the
  46.  *                        folder that this bookmark should be created in.
  47.  *   window.arguments[3]: Bookmark Charset. The charset that should be
  48.  *                        used when adding a bookmark to the specified
  49.  *                        URL. (Usually the charset of the current 
  50.  *                        document when launching this window). 
  51.  *   window.arguments[4]: The mode of operation. See notes for details.
  52.  *
  53.  * Mode of Operation Notes:
  54.  * ------------------------
  55.  * This dialog can be opened in four different ways by using a parameter
  56.  * passed through the call to openDialog. The 'mode' of operation
  57.  * of the window is expressed in window.arguments[4]. The valid modes are:
  58.  *
  59.  * 1) <default> (no fifth open parameter).
  60.  *      Opens this dialog with the bookmark Name, URL and folder selection
  61.  *      components visible. 
  62.  * 2) "newBookmark" (fifth open parameter = String("newBookmark"))
  63.  *      Opens the dialog as in (1) above except the folder selection tree
  64.  *      is hidden. This type of mode is useful when the creation folder 
  65.  *      is pre-determined.
  66.  * 3) "selectFolder" (fifth open parameter = String("selectFolder"))
  67.  *      Opens the dialog as in (1) above except the Name/Location section
  68.  *      is hidden, and the dialog takes on the utility of a Folder chooser.
  69.  *      Used when the user must select a Folder for some purpose. 
  70.  */
  71.  
  72. var gFld_Name   = null;
  73. var gFld_URL    = null; 
  74. var gFolderTree = null;
  75.  
  76. var gBookmarkCharset = null;
  77.  
  78. const kRDFSContractID = "@mozilla.org/rdf/rdf-service;1";
  79. const kRDFSIID = Components.interfaces.nsIRDFService;
  80. const kRDF = Components.classes[kRDFSContractID].getService(kRDFSIID);
  81.  
  82. var gSelectItemObserver = null;
  83.  
  84. var gCreateInFolder = "NC:NewBookmarkFolder";
  85.  
  86. function Startup()
  87. {
  88.   doSetOKCancel(onOK);
  89.   gFld_Name = document.getElementById("name");
  90.   gFld_URL = document.getElementById("url");
  91.   gFolderTree = document.getElementById("folders");
  92.   
  93.   var shouldSetOKButton = true;
  94.   if ("arguments" in window) {
  95.     switch (window.arguments[4]) {
  96.     case "selectFolder":
  97.       // If we're being opened as a folder selection window
  98.       document.getElementById("bookmarknamegrid").setAttribute("hidden", "true");
  99.       document.getElementById("createinseparator").setAttribute("hidden", "true");
  100.       document.getElementById("nameseparator").setAttribute("hidden", "true");
  101.       sizeToContent();
  102.       var windowNode = document.getElementById("newBookmarkWindow");
  103.       windowNode.setAttribute("title", windowNode.getAttribute("title-selectFolder"));
  104.       shouldSetOKButton = false;
  105.       var folderItem = document.getElementById(window.arguments[2]);
  106.       if (folderItem)
  107.         gFolderTree.selectItem(folderItem);
  108.       break;
  109.     case "newBookmark":
  110.       setupFields();
  111.       if (window.arguments[2])
  112.         gCreateInFolder = window.arguments[2];
  113.       document.getElementById("folderbox").setAttribute("hidden", "true");
  114.       windowNode = document.getElementById("newBookmarkWindow");
  115.       windowNode.removeAttribute("persist");
  116.       windowNode.removeAttribute("height");
  117.       windowNode.removeAttribute("width");
  118.       windowNode.setAttribute("style", windowNode.getAttribute("style"));
  119.       sizeToContent();
  120.       break;
  121.     default:
  122.       // Regular Add Bookmark
  123.       setupFields();
  124.       if (window.arguments[2]) {
  125.         gCreateInFolder = window.arguments[2];
  126.         var folderItem = document.getElementById(gCreateInFolder);
  127.         if (folderItem)
  128.           gFolderTree.selectItem(folderItem);
  129.       }
  130.     }
  131.   }
  132.   
  133.   if (shouldSetOKButton)
  134.     onLocationInput();
  135.   gFld_Name.select();
  136.   gFld_Name.focus();
  137.  
  138. function setupFields()
  139. {
  140.   // New bookmark in predetermined folder. 
  141.   gFld_Name.value = window.arguments[0] || "";
  142.   gFld_URL.value = window.arguments[1] || "";
  143.   onLocationInput();
  144.   gFld_Name.select();
  145.   gFld_Name.focus();
  146.   gBookmarkCharset = window.arguments [3] || null;
  147. }
  148.  
  149. function onLocationInput ()
  150. {
  151.   var ok = document.getElementById("ok");
  152.   ok.disabled = gFld_URL.value == "";
  153. }
  154.  
  155. function onOK()
  156. {
  157.   // In Select Folder Mode, do nothing but tell our caller what
  158.   // folder was selected. 
  159.   if (window.arguments[4] == "selectFolder")
  160.     window.arguments[5].selectedFolder = gCreateInFolder;
  161.   else {
  162.     // Otherwise add a bookmark to the selected folder. 
  163.  
  164.     const kBMDS = kRDF.GetDataSource("rdf:bookmarks");
  165.     const kBMSContractID = "@mozilla.org/browser/bookmarks-service;1";
  166.     const kBMSIID = Components.interfaces.nsIBookmarksService;
  167.     const kBMS = Components.classes[kBMSContractID].getService(kBMSIID);
  168.     var rFolder = kRDF.GetResource(gCreateInFolder, true);
  169.     const kRDFCContractID = "@mozilla.org/rdf/container;1";
  170.     const kRDFIID = Components.interfaces.nsIRDFContainer;
  171.     const kRDFC = Components.classes[kRDFCContractID].getService(kRDFIID);
  172.     try {
  173.       kRDFC.Init(kBMDS, rFolder);
  174.     }
  175.     catch (e) {
  176.       // No "NC:NewBookmarkFolder" exists, just append to the root.
  177.       rFolder = kRDF.GetResource("NC:BookmarksRoot", true);
  178.       kRDFC.Init(kBMDS, rFolder);
  179.     }
  180.     if (!gFld_URL.value) return;
  181.     
  182.     // Check to see if the item is a local directory path, and if so, convert
  183.     // to a file URL so that aggregation with rdf:files works
  184.     var url = gFld_URL.value;
  185.     try {
  186.       const kLFContractID = "@mozilla.org/file/local;1";
  187.       const kLFIID = Components.interfaces.nsILocalFile;
  188.       const kLF = Components.classes[kLFContractID].createInstance(kLFIID);
  189.       kLF.initWithUnicodePath(url);
  190.       if (kLF.exists()) 
  191.       url = kLF.URL;
  192.     }
  193.     catch (e) {
  194.     }
  195.     
  196.     kBMS.AddBookmarkToFolder(url, rFolder, gFld_Name.value, gBookmarkCharset);
  197.   }
  198.   close();
  199. }
  200.  
  201. function onTreeSelect ()
  202. {
  203.   if (gFolderTree.selectedItems.length < 1) 
  204.     gCreateInFolder = "NC:NewBookmarkFolder";
  205.   else {
  206.     var selectedItem = gFolderTree.selectedItems[0];
  207.     gCreateInFolder = selectedItem.id;
  208.   }
  209. }
  210.  
  211. var gBookmarksShell = null;
  212. function createNewFolder ()
  213. {
  214.   // ick. 
  215.   gBookmarksShell = new BookmarksTree("folders");
  216.   var item = null;
  217.   var folderKids = document.getElementById("folderKids");
  218.   if (gFolderTree.selectedItems.length < 1)
  219.     item = folderKids.firstChild;
  220.   item = gFolderTree.selectedItems.length < 1 ? folderKids.firstChild : gFolderTree.selectedItems[0];
  221.   gBookmarksShell.commands.createBookmarkItem("folder", item);
  222. }
  223.  
  224. function useDefaultFolder ()
  225. {
  226.   const kBMDS = kRDF.GetDataSource("rdf:bookmarks");
  227.   var newBookmarkFolder = document.getElementById("NC:NewBookmarkFolder");
  228.   if (newBookmarkFolder) {
  229.     gFolderTree.selectItem(newBookmarkFolder);
  230.     gCreateInFolder = "NC:NewBookmarkFolder";
  231.   }
  232.   else {
  233.     gFolderTree.clearItemSelection();
  234.     gCreateInFolder = "NC:BookmarksRoot";
  235.   }
  236. }
  237.  
  238.  
  239.