home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / browser.jar / content / browser / bookmarks / addBookmark.js next >
Text File  |  2002-10-13  |  13KB  |  340 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. var gSelectItemObserver = null;
  105.  
  106. var gCreateInFolder = "NC:NewBookmarkFolder";
  107.  
  108. function Startup()
  109. {
  110.   initServices();
  111.   initBMService();
  112.   gFld_Name = document.getElementById("name");
  113.   gFld_URL = document.getElementById("url");
  114.   gCB_AddGroup = document.getElementById("addgroup");
  115.   var bookmarkView = document.getElementById("bookmarks-view");
  116.  
  117.   var shouldSetOKButton = true;
  118.   var dialogElement = document.documentElement;
  119.   if ("arguments" in window) {
  120.     var ind;
  121.     var folderItem = null;
  122.     var arg;
  123.     if (window.arguments.length < 5)
  124.       arg = null;
  125.     else
  126.       arg = window.arguments[4];
  127.     switch (arg) {
  128.     case "selectFolder":
  129.       // If we're being opened as a folder selection window
  130.       document.getElementById("bookmarknamegrid").setAttribute("hidden", "true");
  131.       document.getElementById("createinseparator").setAttribute("hidden", "true");
  132.       document.getElementById("nameseparator").setAttribute("hidden", "true");
  133.       sizeToContent();
  134.       dialogElement.setAttribute("title", dialogElement.getAttribute("title-selectFolder"));
  135.       shouldSetOKButton = false;
  136.       if (window.arguments[2])
  137.         folderItem = RDF.GetResource(window.arguments[2]);
  138.       if (folderItem) {
  139.         ind = bookmarkView.treeBuilder.getIndexOfResource(folderItem);
  140.         bookmarkView.treeBoxObject.selection.select(ind);
  141.       }
  142.       break;
  143.     case "newBookmark":
  144.       setupFields();
  145.       if (window.arguments[2])
  146.         gCreateInFolder = window.arguments[2];
  147.       document.getElementById("folderbox").setAttribute("hidden", "true");
  148.       sizeToFit();
  149.       break;
  150.     case "addGroup":
  151.       document.getElementById("showaddgroup").setAttribute("hidden", "false");
  152.       setupFields();
  153.       sizeToFit();
  154.       break;
  155.     case "addGroup,group":
  156.       document.getElementById("showaddgroup").setAttribute("hidden", "false");
  157.       gCB_AddGroup.setAttribute("checked", "true");
  158.       setupFields();
  159.       toggleGroup();
  160.       sizeToFit();
  161.       break;
  162.     default:
  163.       // Regular Add Bookmark
  164.       setupFields();
  165.       if (window.arguments[2]) {
  166.         gCreateInFolder = window.arguments[2];
  167.         folderItem = bookmarkView.rdf.GetResource(gCreateInFolder);
  168.         if (folderItem) {
  169.           ind = bookmarkView.treeBuilder.getIndexOfResource(folderItem);
  170.           bookmarkView.treeBoxObject.selection.select(ind);
  171.         }
  172.       }
  173.     }
  174.   }
  175.   
  176.   if (shouldSetOKButton)
  177.     onFieldInput();
  178.   if (document.getElementById("bookmarknamegrid").hasAttribute("hidden")) {
  179.     bookmarkView.tree.focus();
  180.     if (bookmarkView.currentIndex == -1)
  181.       bookmarkView.treeBoxObject.selection.select(0);
  182.   }
  183.   else {
  184.     gFld_Name.select();
  185.     gFld_Name.focus();
  186.   }
  187.  
  188. function sizeToFit()
  189. {
  190.   var dialogElement = document.documentElement;
  191.   dialogElement.removeAttribute("persist");
  192.   dialogElement.removeAttribute("height");
  193.   dialogElement.removeAttribute("width");
  194.   dialogElement.setAttribute("style", dialogElement.getAttribute("style"));
  195.   sizeToContent();
  196. }
  197.  
  198. function setupFields()
  199. {
  200.   // New bookmark in predetermined folder. 
  201.   gFld_Name.value = window.arguments[0] || "";
  202.   gFld_URL.value = window.arguments[1] || "";
  203.   onFieldInput();
  204.   gFld_Name.select();
  205.   gFld_Name.focus();
  206.   gBookmarkCharset = window.arguments[3] || null;
  207. }
  208.  
  209. function onFieldInput()
  210. {
  211.   const ok = document.documentElement.getButton("accept");
  212.   ok.disabled = gFld_URL.value == "" && !addingGroup() ||
  213.                 gFld_Name.value == "";
  214. }    
  215.  
  216. function onOK()
  217. {
  218.   if (!document.getElementById("folderbox").hasAttribute("hidden")) {
  219.     var bookmarkView = document.getElementById("bookmarks-view");
  220.     var currentIndex = bookmarkView.currentIndex;
  221.     if (currentIndex != -1)
  222.       gCreateInFolder = bookmarkView.treeBuilder.getResourceAtIndex(currentIndex).Value;
  223.   }
  224.   // In Select Folder Mode, do nothing but tell our caller what
  225.   // folder was selected. 
  226.   if (window.arguments.length > 4 && window.arguments[4] == "selectFolder")
  227.     window.arguments[5].selectedFolder = gCreateInFolder;
  228.   else {
  229.     // Otherwise add a bookmark to the selected folder. 
  230.     var rFolder = RDF.GetResource(gCreateInFolder);
  231.     try {
  232.       RDFC.Init(BMDS, rFolder);
  233.     }
  234.     catch (e) {
  235.       // No "NC:NewBookmarkFolder" exists, just append to the root.
  236.       rFolder = RDF.GetResource("NC:BookmarksRoot");
  237.       RDFC.Init(BMDS, rFolder);
  238.     }
  239.  
  240.     // if no URL was provided and we're not filing as a group, do nothing
  241.     if (!gFld_URL.value && !addingGroup())
  242.       return;
  243.  
  244.     var url, rSource;
  245.     if (addingGroup()) {
  246.       rSource = BMDS.createFolder(gFld_Name.value);
  247.       const groups  = window.arguments[5];
  248.       for (var i = 0; i < groups.length; ++i) {
  249.         url = getNormalizedURL(groups[i].url);
  250.         BMDS.createBookmarkInContainer(groups[i].name, url,
  251.                                        groups[i].charset, rSource, -1);
  252.       }
  253.     } else {
  254.       url = getNormalizedURL(gFld_URL.value);
  255.       rSource = BMDS.createBookmark(gFld_Name.value, url, gBookmarkCharset);
  256.       if (window.arguments.length > 4 && window.arguments[4] == "newBookmark") {
  257.         window.arguments[5].newBookmark = rSource;
  258.       }
  259.     }
  260.     var selection, target;
  261.     selection = BookmarksUtils.getSelectionFromResource(rSource);
  262.     target    = BookmarksUtils.getSelectionFromResource(rFolder);
  263.     target    = BookmarksUtils.getTargetFromSelection(target);
  264.     BookmarksUtils.insertSelection("newbookmark", selection, target);
  265.   }
  266. }
  267.  
  268. function getNormalizedURL(url)
  269. {
  270.   // Check to see if the item is a local directory path, and if so, convert
  271.   // to a file URL so that aggregation with rdf:files works
  272.   try {
  273.     const kLF = Components.classes["@mozilla.org/file/local;1"]
  274.                           .createInstance(Components.interfaces.nsILocalFile);
  275.     kLF.initWithPath(url);
  276.     if (kLF.exists()) {
  277.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  278.                                 .getService(Components.interfaces.nsIIOService);
  279.       var fileHandler = ioService.getProtocolHandler("file")
  280.                                  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  281.  
  282.       url = fileHandler.getURLSpecFromFile(kLF);
  283.     }
  284.   }
  285.   catch (e) {
  286.   }
  287.  
  288.   return url;
  289. }
  290.  
  291. function createNewFolder ()
  292. {
  293.   var bookmarksView = document.getElementById("bookmarks-view");
  294.   var selection = bookmarksView.getTreeSelection();
  295.   var target    = BookmarksUtils.getTargetFromSelection(selection)
  296.   BookmarksCommand.createNewFolder(target);
  297. }
  298.  
  299. function useDefaultFolder ()
  300. {
  301.   var bookmarkView = document.getElementById("bookmarks-view");
  302.   var folder = BookmarksUtils.getNewBookmarkFolder();
  303.   var ind = bookmarkView.treeBuilder.getIndexOfResource(folder);
  304.   if (ind != -1) {
  305.     bookmarkView.tree.focus();
  306.     bookmarkView.treeBoxObject.selection.select(ind);
  307.   } else {
  308.     bookmarkView.treeBoxObject.selection.clearSelection();
  309.   }
  310.   gCreateInFolder = folder.Value;
  311. }
  312.  
  313. var gOldNameValue = "";
  314. var gOldURLValue = "";
  315.  
  316. function toggleGroup()
  317. {
  318.   // swap between single bookmark and group name
  319.   var temp = gOldNameValue;
  320.   gOldNameValue = gFld_Name.value;
  321.   gFld_Name.value = temp;
  322.  
  323.   // swap between single bookmark and group url
  324.   temp = gOldURLValue;
  325.   gOldURLValue = gFld_URL.value;
  326.   gFld_URL.value = temp;
  327.   gFld_URL.disabled = gCB_AddGroup.getAttribute("checked") == "true";
  328.  
  329.   gFld_Name.select();
  330.   gFld_Name.focus();
  331.   onFieldInput();
  332. }
  333.  
  334. function addingGroup()
  335. {
  336.   const showAddGroup = document.getElementById("showaddgroup");
  337.   return showAddGroup.getAttribute("hidden") != "true" && gCB_AddGroup.getAttribute("checked") == "true";
  338. }
  339.