home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / bookmarks / addBookmark.js next >
Encoding:
Text File  |  2005-07-29  |  12.7 KB  |  339 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.  *   window.arguments[6]: If the bookmark should become a web panel.
  71.  *
  72.  * Mode of Operation Notes:
  73.  * ------------------------
  74.  * This dialog can be opened in four different ways by using a parameter
  75.  * passed through the call to openDialog. The 'mode' of operation
  76.  * of the window is expressed in window.arguments[4]. The valid modes are:
  77.  *
  78.  * 1) <default> (no fifth open parameter).
  79.  *      Opens this dialog with the bookmark Name, URL and folder selection
  80.  *      components visible. 
  81.  * 2) "newBookmark" (fifth open parameter = String("newBookmark"))
  82.  *      Opens the dialog as in (1) above except the folder selection tree
  83.  *      is hidden. This type of mode is useful when the creation folder 
  84.  *      is pre-determined.
  85.  * 3) "selectFolder" (fifth open parameter = String("selectFolder"))
  86.  *      Opens the dialog as in (1) above except the Name/Location section
  87.  *      is hidden, and the dialog takes on the utility of a Folder chooser.
  88.  *      Used when the user must select a Folder for some purpose. 
  89.  * 4) "addGroup" (fifth open parameter = String("addGroup"))
  90.  *      Opens the dialog like <default>, with a checkbox to select between
  91.  *      filing a single bookmark or a group. For the single bookmark the
  92.  *      values are taken from the name, URL and charset arguments.
  93.  *      For the group, the values are taken from the sixth argument.
  94.  *      This parameter can also be String("addGroup,group") where "group"
  95.  *      specifies that the dialog starts in filing as a group.
  96.  */
  97.  
  98. var gFld_Name   = null;
  99. var gFld_URL    = null; 
  100. var gFolderTree = null;
  101. var gCB_AddGroup = null;
  102.  
  103. var gBookmarkCharset = null;
  104.  
  105. var gSelectItemObserver = null;
  106.  
  107. var gCreateInFolder = "NC:NewBookmarkFolder";
  108.  
  109. function Startup()
  110. {
  111.   initServices();
  112.   initBMService();
  113.   gFld_Name = document.getElementById("name");
  114.   gFld_URL = document.getElementById("url");
  115.   gCB_AddGroup = document.getElementById("addgroup");
  116.   var bookmarkView = document.getElementById("bookmarks-view");
  117.  
  118.   var shouldSetOKButton = true;
  119.   var dialogElement = document.documentElement;
  120.   if ("arguments" in window) {
  121.     var ind;
  122.     var folderItem = null;
  123.     var arg;
  124.     if (window.arguments.length < 5)
  125.       arg = null;
  126.     else
  127.       arg = window.arguments[4];
  128.     switch (arg) {
  129.     case "selectFolder":
  130.       // If we're being opened as a folder selection window
  131.       document.getElementById("bookmarknamegrid").setAttribute("hidden", "true");
  132.       document.getElementById("createinseparator").setAttribute("hidden", "true");
  133.       document.getElementById("nameseparator").setAttribute("hidden", "true");
  134.       sizeToContent();
  135.       dialogElement.setAttribute("title", dialogElement.getAttribute("title-selectFolder"));
  136.       shouldSetOKButton = false;
  137.       if (window.arguments[2])
  138.         folderItem = 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.       setupFields();
  146.       if (window.arguments[2])
  147.         gCreateInFolder = window.arguments[2];
  148.       document.getElementById("folderbox").setAttribute("hidden", "true");
  149.       sizeToFit();
  150.       break;
  151.     case "addGroup":
  152.       document.getElementById("showaddgroup").setAttribute("hidden", "false");
  153.       setupFields();
  154.       sizeToFit();
  155.       break;
  156.     case "addGroup,group":
  157.       document.getElementById("showaddgroup").setAttribute("hidden", "false");
  158.       gCB_AddGroup.setAttribute("checked", "true");
  159.       setupFields();
  160.       toggleGroup();
  161.       sizeToFit();
  162.       break;
  163.     default:
  164.       // Regular Add Bookmark
  165.       setupFields();
  166.       if (window.arguments[2]) {
  167.         gCreateInFolder = window.arguments[2];
  168.         folderItem = bookmarkView.rdf.GetResource(gCreateInFolder);
  169.         if (folderItem) {
  170.           ind = bookmarkView.treeBuilder.getIndexOfResource(folderItem);
  171.           bookmarkView.treeBoxObject.selection.select(ind);
  172.         }
  173.       }
  174.     }
  175.   }
  176.   
  177.   if (shouldSetOKButton)
  178.     onFieldInput();
  179.   if (document.getElementById("bookmarknamegrid").hasAttribute("hidden")) {
  180.     bookmarkView.tree.focus();
  181.     if (bookmarkView.currentIndex == -1)
  182.       bookmarkView.treeBoxObject.selection.select(0);
  183.   }
  184.   else {
  185.     gFld_Name.select();
  186.     gFld_Name.focus();
  187.   }
  188.  
  189. function sizeToFit()
  190. {
  191.   var dialogElement = document.documentElement;
  192.   dialogElement.removeAttribute("persist");
  193.   dialogElement.removeAttribute("height");
  194.   dialogElement.removeAttribute("width");
  195.   dialogElement.setAttribute("style", dialogElement.getAttribute("style"));
  196.   sizeToContent();
  197. }
  198.  
  199. function setupFields()
  200. {
  201.   // New bookmark in predetermined folder. 
  202.   gFld_Name.value = window.arguments[0] || "";
  203.   gFld_URL.value = window.arguments[1] || "";
  204.   onFieldInput();
  205.   gFld_Name.select();
  206.   gFld_Name.focus();
  207.   gBookmarkCharset = window.arguments[3] || null;
  208. }
  209.  
  210. function onFieldInput()
  211. {
  212.   const ok = document.documentElement.getButton("accept");
  213.   ok.disabled = gFld_URL.value == "" && !addingGroup() ||
  214.                 gFld_Name.value == "";
  215. }    
  216.  
  217. function onOK()
  218. {
  219.   if (!document.getElementById("folderbox").hasAttribute("hidden")) {
  220.     var bookmarkView = document.getElementById("bookmarks-view");
  221.     var currentIndex = bookmarkView.currentIndex;
  222.     if (currentIndex != -1)
  223.       gCreateInFolder = bookmarkView.treeBuilder.getResourceAtIndex(currentIndex).Value;
  224.   }
  225.   // In Select Folder Mode, do nothing but tell our caller what
  226.   // folder was selected. 
  227.   if (window.arguments.length > 4 && window.arguments[4] == "selectFolder")
  228.     window.arguments[5].target = BookmarksUtils.getTargetFromFolder(bookmarkView.treeBuilder.getResourceAtIndex(currentIndex));
  229.   else {
  230.     // Otherwise add a bookmark to the selected folder. 
  231.     var rFolder = RDF.GetResource(gCreateInFolder);
  232.     try {
  233.       RDFC.Init(BMDS, rFolder);
  234.     }
  235.     catch (e) {
  236.       // No "NC:NewBookmarkFolder" exists, just append to the root.
  237.       rFolder = RDF.GetResource("NC:BookmarksRoot");
  238.       RDFC.Init(BMDS, rFolder);
  239.     }
  240.  
  241.     // if no URL was provided and we're not filing as a group, do nothing
  242.     if (!gFld_URL.value && !addingGroup())
  243.       return;
  244.  
  245.     var url, rSource;
  246.     if (addingGroup()) {
  247.       rSource = BMDS.createFolder(gFld_Name.value);
  248.       const groups  = window.arguments[5];
  249.       for (var i = 0; i < groups.length; ++i) {
  250.         url = getNormalizedURL(groups[i].url);
  251.         BMDS.createBookmarkInContainer(groups[i].name, url, null, null,
  252.                                        groups[i].charset, null, rSource, -1);
  253.       }
  254.     } else {
  255.       url = getNormalizedURL(gFld_URL.value);
  256.       rSource = BMDS.createBookmark(gFld_Name.value, url, null, null, gBookmarkCharset, false, "");
  257.       if (window.arguments.length > 4 && window.arguments[4] == "newBookmark") {
  258.         window.arguments[5].newBookmark = rSource;
  259.       }
  260.     }
  261.     var selection = BookmarksUtils.getSelectionFromResource(rSource);
  262.     var target    = BookmarksUtils.getTargetFromFolder(rFolder);
  263.     BookmarksUtils.insertAndCheckSelection("newbookmark", selection, target);
  264.   }
  265. }
  266.  
  267. function getNormalizedURL(url)
  268. {
  269.   // Check to see if the item is a local directory path, and if so, convert
  270.   // to a file URL so that aggregation with rdf:files works
  271.   try {
  272.     const kLF = Components.classes["@mozilla.org/file/local;1"]
  273.                           .createInstance(Components.interfaces.nsILocalFile);
  274.     kLF.initWithPath(url);
  275.     if (kLF.exists()) {
  276.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  277.                                 .getService(Components.interfaces.nsIIOService);
  278.       var fileHandler = ioService.getProtocolHandler("file")
  279.                                  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  280.  
  281.       url = fileHandler.getURLSpecFromFile(kLF);
  282.     }
  283.   }
  284.   catch (e) {
  285.   }
  286.  
  287.   return url;
  288. }
  289.  
  290. function createNewFolder ()
  291. {
  292.   var bookmarksView = document.getElementById("bookmarks-view");
  293.   var resource = bookmarksView.treeBuilder.getResourceAtIndex(bookmarksView.currentIndex);
  294.   var target = BookmarksUtils.getTargetFromFolder(resource);
  295.   BookmarksCommand.createNewFolder(target);
  296. }
  297.  
  298. function useDefaultFolder ()
  299. {
  300.   var bookmarkView = document.getElementById("bookmarks-view");
  301.   var folder = BookmarksUtils.getNewBookmarkFolder();
  302.   var ind = bookmarkView.treeBuilder.getIndexOfResource(folder);
  303.   if (ind != -1) {
  304.     bookmarkView.tree.focus();
  305.     bookmarkView.treeBoxObject.selection.select(ind);
  306.   } else {
  307.     bookmarkView.treeBoxObject.selection.clearSelection();
  308.   }
  309.   gCreateInFolder = folder.Value;
  310. }
  311.  
  312. var gOldNameValue = "";
  313. var gOldURLValue = "";
  314.  
  315. function toggleGroup()
  316. {
  317.   // swap between single bookmark and group name
  318.   var temp = gOldNameValue;
  319.   gOldNameValue = gFld_Name.value;
  320.   gFld_Name.value = temp;
  321.  
  322.   // swap between single bookmark and group url
  323.   temp = gOldURLValue;
  324.   gOldURLValue = gFld_URL.value;
  325.   gFld_URL.value = temp;
  326.   gFld_URL.disabled = gCB_AddGroup.getAttribute("checked") == "true";
  327.  
  328.   gFld_Name.select();
  329.   gFld_Name.focus();
  330.   onFieldInput();
  331. }
  332.  
  333. function addingGroup()
  334. {
  335.   const showAddGroup = document.getElementById("showaddgroup");
  336.   return showAddGroup.getAttribute("hidden") != "true" && gCB_AddGroup.getAttribute("checked") == "true";
  337. }
  338.