home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Komunik / phoenix / chrome / browser.jar / content / browser / bookmarks / addBookmark2.js < prev    next >
Text File  |  2002-10-13  |  6KB  |  173 lines

  1.  
  2. /**
  3.  * Add Bookmark Dialog. 
  4.  * ====================
  5.  * 
  6.  * This is a generic bookmark dialog that allows for bookmark addition
  7.  * and folder selection. It can be opened with various parameters that 
  8.  * result in appearance/purpose differences and initial state. 
  9.  * 
  10.  * Use: Open with 'openDialog', with the flags 
  11.  *        'centerscreen,chrome,dialog=no,resizable=yes'
  12.  * 
  13.  * Parameters: 
  14.  *   Apart from the standard openDialog parameters, this dialog can 
  15.  *   be passed additional information, which gets mapped to the 
  16.  *   window.arguments array:
  17.  * 
  18.  *   window.arguments[0]: Bookmark Name. The value to be prefilled
  19.  *                        into the "Name: " field (if visible).
  20.  *   window.arguments[1]: Bookmark URL: The location of the bookmark.
  21.  *                        The value to be filled in the "Location: "
  22.  *                        field (if visible).
  23.  *   window.arguments[2]: Bookmark Folder. The RDF Resource URI of the
  24.  *                        folder that this bookmark should be created in.
  25.  *   window.arguments[3]: Bookmark Charset. The charset that should be
  26.  *                        used when adding a bookmark to the specified
  27.  *                        URL. (Usually the charset of the current 
  28.  *                        document when launching this window). 
  29.  *   window.arguments[4]: The mode of operation. See notes for details.
  30.  *   window.arguments[5]: If the mode is "addGroup", this is an array
  31.  *                        of objects with name, URL and charset
  32.  *                        properties, one for each group member.
  33.  */
  34.  
  35. var gSelectedFolder;
  36. var gName;
  37. var gGroup;
  38. var gList;
  39. var gIndentation; // temporary hack to indent the folders
  40. var gNameArc;
  41.  
  42. function Startup()
  43. {
  44.   initServices();
  45.   initBMService();
  46.   gNameArc = RDF.GetResource(NC_NS+"Name");
  47.   gName  = document.getElementById("name");
  48.   gGroup = document.getElementById("addgroup");
  49.   gList  = document.getElementById("select-menu");
  50.   gName.value = window.arguments[0];
  51.   gName.select();
  52.   gName.focus();
  53.   onFieldInput();
  54.   setTimeout(fillSelectFolderMenupopup, 0);
  55.   gIndentation = Array(16);
  56.   gIndentation[0] = "";
  57.   for (var i=1; i<16; ++i)
  58.     gIndentation[i]=gIndentation[i-1]+"  "; 
  59.  
  60.  
  61. function onFieldInput()
  62. {
  63.   const ok = document.documentElement.getButton("accept");
  64.   ok.disabled = gName.value == "";
  65. }    
  66.  
  67. function onOK()
  68. {
  69.   document.getElementById("addBookmarkDialog").setAttribute("selectedFolder",gSelectedFolder);
  70.   var rFolder = RDF.GetResource(gSelectedFolder);
  71.   RDFC.Init(BMDS, rFolder);
  72.  
  73.   var url, rSource;
  74.   if (gGroup && gGroup.checked) {
  75.     rSource = BMDS.createFolder(gName.value);
  76.     const groups = window.arguments[5];
  77.     for (var i = 0; i < groups.length; ++i) {
  78.       url = getNormalizedURL(groups[i].url);
  79.       BMDS.createBookmarkInContainer(groups[i].name, url,
  80.                                      groups[i].charset, rSource, -1);
  81.     }
  82.   } else {
  83.     url = getNormalizedURL(window.arguments[1]);
  84.     rSource = BMDS.createBookmark(gName.value, url, window.arguments[3]);
  85.   }
  86.   if (!gBMtxmgr)
  87.     gBMtxmgr= BookmarksUtils.getTransactionManager();
  88.  
  89.   var selection, target;
  90.   selection = BookmarksUtils.getSelectionFromResource(rSource);
  91.   target    = BookmarksUtils.getSelectionFromResource(rFolder);
  92.   target    = BookmarksUtils.getTargetFromSelection(target);
  93.   BookmarksUtils.insertSelection("newbookmark", selection, target);
  94.   // in insertSelection, the ds flush is delayed. It will never be performed,
  95.   // since this dialog is destroyed before.
  96.   // We have to flush manually
  97.   var remoteDS = BMDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  98.   remoteDS.Flush();
  99. }
  100.  
  101. function getNormalizedURL(url)
  102. {
  103.   // Check to see if the item is a local directory path, and if so, convert
  104.   // to a file URL so that aggregation with rdf:files works
  105.   try {
  106.     const kLF = Components.classes["@mozilla.org/file/local;1"]
  107.                           .createInstance(Components.interfaces.nsILocalFile);
  108.     kLF.initWithPath(url);
  109.     if (kLF.exists()) {
  110.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  111.                                 .getService(Components.interfaces.nsIIOService);
  112.       var fileHandler = ioService.getProtocolHandler("file")
  113.                                  .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  114.  
  115.       url = fileHandler.getURLSpecFromFile(kLF);
  116.     }
  117.   }
  118.   catch (e) {
  119.   }
  120.  
  121.   return url;
  122. }
  123.  
  124. function fillFolder(aPopup, aFolder, aDepth)
  125. {
  126.   RDFC.Init(BMDS, aFolder);
  127.   var children = RDFC.GetElements();
  128.   while (children.hasMoreElements()) {
  129.     var curr = children.getNext();
  130.     if (RDFCU.IsContainer(BMDS, curr)) {
  131.       curr = curr.QueryInterface(Components.interfaces.nsIRDFResource);
  132.       var element = document.createElementNS(XUL_NS, "menuitem");
  133.       var name = BMDS.GetTarget(curr, gNameArc, true).QueryInterface(kRDFLITIID).Value;
  134.       element.setAttribute("label", gIndentation[aDepth]+name);
  135.       element.setAttribute("id", curr.Value);
  136.       aPopup.appendChild(element);
  137.       if (curr.Value == gSelectedFolder)
  138.         gList.selectedItem = element;
  139.       fillFolder(aPopup, curr, ++aDepth);
  140.       --aDepth;
  141.     }
  142.   }
  143. }
  144.  
  145. function fillSelectFolderMenupopup ()
  146. {
  147.  
  148.   gSelectedFolder = document.getElementById("addBookmarkDialog").getAttribute("selectedFolder");
  149.   var popup = document.getElementById("select-folder");
  150.   // clearing the old menupopup
  151.   while (popup.hasChildNodes()) 
  152.     popup.removeChild(popup.firstChild);
  153.  
  154.   // to be removed once I checkin the top folder
  155.   var element = document.createElementNS(XUL_NS, "menuitem");
  156.   element.setAttribute("label", "Bookmarks");
  157.   element.setAttribute("id", "NC:BookmarksRoot");
  158.   popup.appendChild(element);
  159.  
  160.   var folder = RDF.GetResource("NC:BookmarksRoot");
  161.   fillFolder(popup, folder, 1);
  162.   if (gList.selectedIndex == -1) {
  163.     gList.selectedIndex = 0;
  164.     gSelectedFolder = "NC:BookmarksRoot";
  165.   }
  166. }
  167.  
  168. function selectFolder(aEvent)
  169. {
  170.   gSelectedFolder = aEvent.target.id;
  171. }
  172.