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 / comm.jar / content / communicator / bookmarks / addBookmark.js next >
Text File  |  2003-06-08  |  13KB  |  336 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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.  *   Ben Goodger <ben@netscape.com> (Original Author)
  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. /**
  40.  * Add Bookmark Dialog. 
  41.  * ====================
  42.  * 
  43.  * This is a generic bookmark dialog that allows for bookmark addition
  44.  * and folder selection. It can be opened with various parameters that 
  45.  * result in appearance/purpose differences and initial state. 
  46.  * 
  47.  * Use: Open with 'openDialog', with the flags 
  48.  *        'centerscreen,chrome,dialog=no,resizable=yes'
  49.  * 
  50.  * Parameters: 
  51.  *   Apart from the standard openDialog parameters, this dialog can 
  52.  *   be passed additional information, which gets mapped to the 
  53.  *   window.arguments array:
  54.  * 
  55.  *   window.arguments[0]: Bookmark Name. The value to be prefilled
  56.  *                        into the "Name: " field (if visible).
  57.  *   window.arguments[1]: Bookmark URL: The location of the bookmark.
  58.  *                        The value to be filled in the "Location: "
  59.  *                        field (if visible).
  60.  *   window.arguments[2]: Bookmark Folder. The RDF Resource URI of the
  61.  *                        folder that this bookmark should be created in.
  62.  *   window.arguments[3]: Bookmark Charset. The charset that should be
  63.  *                        used when adding a bookmark to the specified
  64.  *                        URL. (Usually the charset of the current 
  65.  *                        document when launching this window). 
  66.  *   window.arguments[4]: The mode of operation. See notes for details.
  67.  *   window.arguments[5]: If the mode is "addGroup", this is an array
  68.  *                        of objects with name, URL and charset
  69.  *                        properties, one for each group member.
  70.  *
  71.  * Mode of Operation Notes:
  72.  * ------------------------
  73.  * This dialog can be opened in four different ways by using a parameter
  74.  * passed through the call to openDialog. The 'mode' of operation
  75.  * of the window is expressed in window.arguments[4]. The valid modes are:
  76.  *
  77.  * 1) <default> (no fifth open parameter).
  78.  *      Opens this dialog with the bookmark Name, URL and folder selection
  79.  *      components visible. 
  80.  * 2) "newBookmark" (fifth open parameter = String("newBookmark"))
  81.  *      Opens the dialog as in (1) above except the folder selection tree
  82.  *      is hidden. This type of mode is useful when the creation folder 
  83.  *      is pre-determined.
  84.  * 3) "selectFolder" (fifth open parameter = String("selectFolder"))
  85.  *      Opens the dialog as in (1) above except the Name/Location section
  86.  *      is hidden, and the dialog takes on the utility of a Folder chooser.
  87.  *      Used when the user must select a Folder for some purpose. 
  88.  * 4) "addGroup" (fifth open parameter = String("addGroup"))
  89.  *      Opens the dialog like <default>, with a checkbox to select between
  90.  *      filing a single bookmark or a group. For the single bookmark the
  91.  *      values are taken from the name, URL and charset arguments.
  92.  *      For the group, the values are taken from the sixth argument.
  93.  *      This parameter can also be String("addGroup,group") where "group"
  94.  *      specifies that the dialog starts in filing as a group.
  95.  */
  96.  
  97. var gFld_Name   = null;
  98. var gFld_URL    = null; 
  99. var gFolderTree = null;
  100. var gCB_AddGroup = null;
  101.  
  102. var gBookmarkCharset = null;
  103.  
  104. const kRDFSContractID = "@mozilla.org/rdf/rdf-service;1";
  105. const kRDFSIID = Components.interfaces.nsIRDFService;
  106. const kRDF = Components.classes[kRDFSContractID].getService(kRDFSIID);
  107.  
  108. var gSelectItemObserver = null;
  109.  
  110. var gCreateInFolder = "NC:NewBookmarkFolder";
  111.  
  112. function Startup()
  113. {
  114.   gFld_Name = document.getElementById("name");
  115.   gFld_URL = document.getElementById("url");
  116.   gCB_AddGroup = document.getElementById("addgroup");
  117.   var bookmarkView = document.getElementById("bookmarks-view");
  118.  
  119.   var shouldSetOKButton = true;
  120.   var dialogElement = document.documentElement;
  121.   if ("arguments" in window) {
  122.     var ind;
  123.     var folderItem = null;
  124.     var arg;
  125.     if (window.arguments.length < 5)
  126.       arg = null;
  127.     else
  128.       arg = window.arguments[4];
  129.     switch (arg) {
  130.     case "selectFolder":
  131.       // If we're being opened as a folder selection window
  132.       document.getElementById("bookmarknamegrid").hidden = true;
  133.       document.getElementById("createinseparator").hidden = true;
  134.       document.getElementById("nameseparator").hidden = true;
  135.       dialogElement.setAttribute("title", dialogElement.getAttribute("title-selectFolder"));
  136.       shouldSetOKButton = false;
  137.       if (window.arguments[2])
  138.         folderItem = bookmarkView.rdf.GetResource(window.arguments[2]);
  139.       if (folderItem) {
  140.         ind = bookmarkView.treeBuilder.getIndexOfResource(folderItem);
  141.         bookmarkView.treeBoxObject.selection.select(ind);
  142.       }
  143.       break;
  144.     case "newBookmark":
  145.       document.getElementById("showaddgroup").hidden = true;
  146.       setupFields();
  147.       if (window.arguments[2])
  148.         gCreateInFolder = window.arguments[2];
  149.       document.getElementById("folderbox").hidden = true;
  150.       break;
  151.     case "addGroup":
  152.       setupFields();
  153.       break;
  154.     case "addGroup,group":
  155.       gCB_AddGroup.checked = true;
  156.       setupFields();
  157.       toggleGroup();
  158.       break;
  159.     default:
  160.       // Regular Add Bookmark
  161.       document.getElementById("showaddgroup").hidden = true;
  162.       setupFields();
  163.       if (window.arguments[2]) {
  164.         gCreateInFolder = window.arguments[2];
  165.         folderItem = bookmarkView.rdf.GetResource(gCreateInFolder);
  166.         if (folderItem) {
  167.           ind = bookmarkView.treeBuilder.getIndexOfResource(folderItem);
  168.           bookmarkView.treeBoxObject.selection.select(ind);
  169.         }
  170.       }
  171.     }
  172.   }
  173.   
  174.   if (shouldSetOKButton)
  175.     onFieldInput();
  176.   if (document.getElementById("bookmarknamegrid").hidden) {
  177.     bookmarkView.tree.focus();
  178.     if (bookmarkView.currentIndex == -1)
  179.       bookmarkView.treeBoxObject.selection.select(0);
  180.   }
  181.   else {
  182.     gFld_Name.select();
  183.     gFld_Name.focus();
  184.   }
  185.  
  186.   // XXX fix old profiles
  187.   dialogElement.removeAttribute("height");
  188.   dialogElement.removeAttribute("width");
  189.   sizeToContent();
  190. }
  191.  
  192. function setupFields()
  193. {
  194.   // New bookmark in predetermined folder. 
  195.   gFld_Name.value = window.arguments[0] || "";
  196.   gFld_URL.value = window.arguments[1] || "";
  197.   onFieldInput();
  198.   gFld_Name.select();
  199.   gFld_Name.focus();
  200.   gBookmarkCharset = window.arguments[3] || null;
  201. }
  202.  
  203. function onFieldInput()
  204. {
  205.   const ok = document.documentElement.getButton("accept");
  206.   ok.disabled = gFld_URL.value == "" && !gCB_AddGroup.checked ||
  207.                 gFld_Name.value == "";
  208. }    
  209.  
  210. function onOK()
  211. {
  212.   if (!document.getElementById("folderbox").hidden) {
  213.     var bookmarkView = document.getElementById("bookmarks-view");
  214.     var currentIndex = bookmarkView.currentIndex;
  215.     if (currentIndex != -1)
  216.       gCreateInFolder = bookmarkView.treeBuilder.getResourceAtIndex(currentIndex).Value;
  217.   }
  218.   // In Select Folder Mode, do nothing but tell our caller what
  219.   // folder was selected. 
  220.   if (window.arguments.length > 4 && window.arguments[4] == "selectFolder")
  221.     window.arguments[5].selectedFolder = gCreateInFolder;
  222.   else {
  223.     // Otherwise add a bookmark to the selected folder. 
  224.  
  225.     const kBMDS = kRDF.GetDataSource("rdf:bookmarks");
  226.     const kBMSContractID = "@mozilla.org/browser/bookmarks-service;1";
  227.     const kBMSIID = Components.interfaces.nsIBookmarksService;
  228.     const kBMS = Components.classes[kBMSContractID].getService(kBMSIID);
  229.     var rFolder = kRDF.GetResource(gCreateInFolder, true);
  230.     const kRDFCContractID = "@mozilla.org/rdf/container;1";
  231.     const kRDFIID = Components.interfaces.nsIRDFContainer;
  232.     const kRDFC = Components.classes[kRDFCContractID].getService(kRDFIID);
  233.     try {
  234.       kRDFC.Init(kBMDS, rFolder);
  235.     }
  236.     catch (e) {
  237.       // No "NC:NewBookmarkFolder" exists, just append to the root.
  238.       rFolder = kRDF.GetResource("NC:BookmarksRoot", true);
  239.       kRDFC.Init(kBMDS, rFolder);
  240.     }
  241.  
  242.     var url;
  243.     if (gCB_AddGroup.checked) {
  244.       const group = kBMS.createGroupInContainer(gFld_Name.value, rFolder, -1);
  245.       const groups = window.arguments[5];
  246.       for (var i = 0; i < groups.length; ++i) {
  247.         url = getNormalizedURL(groups[i].url);
  248.         kBMS.createBookmarkInContainer(groups[i].name, url,
  249.                                        groups[i].charset, group, -1);
  250.       }
  251.     } else if (gFld_URL.value) {
  252.       url = getNormalizedURL(gFld_URL.value);
  253.       var newBookmark = kBMS.createBookmarkInContainer(gFld_Name.value, url, gBookmarkCharset, rFolder, -1);
  254.       if (window.arguments.length > 4 && window.arguments[4] == "newBookmark") {
  255.         window.arguments[5].newBookmark = newBookmark;
  256.       }
  257.     }
  258.   }
  259. }
  260.  
  261. function getNormalizedURL(url)
  262. {
  263.   // Check to see if the item is a local directory path, and if so, convert
  264.   // to a file URL so that aggregation with rdf:files works
  265.   try {
  266.     const kLF = Components.classes["@mozilla.org/file/local;1"]
  267.                           .createInstance(Components.interfaces.nsILocalFile);
  268.     kLF.initWithPath(url);
  269.     if (kLF.exists()) {
  270.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  271.                                 .getService(Components.interfaces.nsIIOService);
  272.       var fileHandler = ioService.getProtocolHandler("file")
  273.                                  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  274.  
  275.       url = fileHandler.getURLSpecFromFile(kLF);
  276.     }
  277.   }
  278.   catch (e) {
  279.   }
  280.  
  281.   return url;
  282. }
  283.  
  284. var gBookmarksShell = null;
  285. function createNewFolder ()
  286. {
  287.   var bookmarksView = document.getElementById("bookmarks-view");
  288.   bookmarksView.createNewFolder();
  289. }
  290.  
  291. function useDefaultFolder ()
  292. {
  293.   var bookmarkView = document.getElementById("bookmarks-view");
  294.   var folder = BookmarksUtils.getNewBookmarkFolder();
  295.   var ind = bookmarkView.treeBuilder.getIndexOfResource(folder);
  296.   if (ind != -1) {
  297.     bookmarkView.tree.focus();
  298.     bookmarkView.treeBoxObject.selection.select(ind);
  299.   } else {
  300.     bookmarkView.treeBoxObject.selection.clearSelection();
  301.   }
  302.   gCreateInFolder = folder.Value;
  303. }
  304.  
  305. var gOldNameValue = "";
  306. var gOldURLValue = "";
  307.  
  308. function toggleGroup()
  309. {
  310.   // swap between single bookmark and group name
  311.   var temp = gOldNameValue;
  312.   gOldNameValue = gFld_Name.value;
  313.   gFld_Name.value = temp;
  314.  
  315.   // swap between single bookmark and group url
  316.   temp = gOldURLValue;
  317.   gOldURLValue = gFld_URL.value;
  318.   gFld_URL.value = temp;
  319.   gFld_URL.disabled = gCB_AddGroup.checked;
  320.  
  321.   gFld_Name.select();
  322.   gFld_Name.focus();
  323.   onFieldInput();
  324. }
  325.  
  326. function persistTreeSize()
  327. {
  328.   if (!document.getElementById("folderbox").hidden) {
  329.     var bookmarkView = document.getElementById("bookmarks-view");
  330.     bookmarkView.setAttribute("height", bookmarkView.boxObject.height);
  331.     document.persist("bookmarks-view", "height");
  332.     bookmarkView.setAttribute("width", bookmarkView.boxObject.width);
  333.     document.persist("bookmarks-view", "width");
  334.   }
  335. }
  336.