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 / bm-props.js < prev    next >
Text File  |  2003-06-08  |  15KB  |  414 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the NPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the NPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. var NC_NAMESPACE_URI = "http://home.netscape.com/NC-rdf#";
  39.  
  40. // XXX MAKE SURE that the "url" field is LAST!
  41. // This is important for what happens if/when the URL itself is changed.
  42. // Ask rjc@netscape.com if you want to know why exactly this is.
  43.  
  44. // This is the set of fields that are visible in the window.
  45. var gFields     = ["name", "shortcut", "description", "url"];
  46.  
  47. // ...and this is a parallel array that contains the RDF properties
  48. // that they are associated with.
  49. var gProperties = [NC_NAMESPACE_URI + "Name",
  50.                    NC_NAMESPACE_URI + "ShortcutURL",
  51.                    NC_NAMESPACE_URI + "Description",
  52.                    NC_NAMESPACE_URI + "URL"];
  53.  
  54. var RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"]
  55.                     .getService(Components.interfaces.nsIRDFService);
  56.  
  57. var RDFCU = Components.classes["@mozilla.org/rdf/container-utils;1"]
  58.                       .getService(Components.interfaces.nsIRDFContainerUtils);
  59.  
  60. var Bookmarks = RDF.GetDataSource("rdf:bookmarks");
  61.  
  62. var gBookmarkURL = "";
  63.  
  64. function showDescription()
  65. {
  66.   gBookmarkURL = window.arguments[0];
  67.   var resource = RDF.GetResource(gBookmarkURL);
  68.  
  69.   // Check the description
  70.   var primaryType = BookmarksUtils.resolveType(resource).split("#")[1];
  71.   if (primaryType == "Folder") {
  72.     if (resource.Value == "NC:PersonalToolbarFolder")
  73.       primaryType = "PersonalToolbarFolder";
  74.     else {
  75.       var folderGroupArc = RDF.GetResource(NC_NAMESPACE_URI+"FolderGroup");
  76.       var isFolderGroup  = Bookmarks.GetTarget(resource, folderGroupArc, true);
  77.       if (isFolderGroup)
  78.         primaryType = "FolderGroup";
  79.     }
  80.   }
  81.   var description = BookmarksUtils.getLocaleString("description_"+primaryType);
  82.   
  83.   var newBookmarkFolder = BookmarksUtils.getNewBookmarkFolder();
  84.   var newSearchFolder   = BookmarksUtils.getNewSearchFolder();
  85.  
  86.   if (resource == newBookmarkFolder && resource == newSearchFolder)
  87.     description = description+" "+BookmarksUtils.getLocaleString("description_NewBookmarkAndSearchFolder")
  88.   else if (resource == newBookmarkFolder )
  89.     description = description+" "+BookmarksUtils.getLocaleString("description_NewBookmarkFolder")
  90.   else if (resource == newSearchFolder)
  91.     description = description+" "+BookmarksUtils.getLocaleString("description_NewSearchFolder");
  92.  
  93.   var textNode = document.createTextNode(description);
  94.   document.getElementById("bookmarkDescription").appendChild(textNode);
  95.  
  96. }
  97.  
  98. function Init()
  99. {
  100.   var x;
  101.   var resource = RDF.GetResource(gBookmarkURL);
  102.   // Initialize the properties panel by copying the values from the
  103.   // RDF graph into the fields on screen.
  104.  
  105.   for (var i = 0; i < gFields.length; ++i) {
  106.     var field = document.getElementById(gFields[i]);
  107.  
  108.     var value = Bookmarks.GetTarget(resource, RDF.GetResource(gProperties[i]), true);
  109.  
  110.     if (value)
  111.       value = value.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  112.  
  113.     if (value) //make sure were aren't stuffing null into any fields
  114.       field.value = value;
  115.   }
  116.  
  117.   var propsWindow = document.getElementById("bmPropsWindow");
  118.   var nameNode = document.getElementById("name");
  119.   var title = propsWindow.getAttribute("title");
  120.   title = title.replace(/\*\*bm_title\*\*/gi, nameNode.value);
  121.   propsWindow.setAttribute("title", title);
  122.  
  123.   // check bookmark schedule
  124.   var scheduleArc = RDF.GetResource("http://home.netscape.com/WEB-rdf#Schedule");
  125.   value = Bookmarks.GetTarget(resource, scheduleArc, true);
  126.  
  127.   if (value) {
  128.     value = value.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  129.  
  130.     if (value) {
  131.       var values = value.split("|");
  132.       if (values.length == 4) {
  133.         // get day range
  134.         var days = values[0];
  135.         var dayNode = document.getElementById("dayRange");
  136.         var dayItems = dayNode.childNodes[0].childNodes;
  137.         for (x=0; x < dayItems.length; ++x) {
  138.           if (dayItems[x].getAttribute("value") == days) {
  139.             dayNode.selectedItem = dayItems[x];
  140.             break;
  141.           }
  142.         }
  143.  
  144.         // get hour range
  145.         var hours = values[1].split("-");
  146.         var startHour = "";
  147.         var endHour = "";
  148.  
  149.         if (hours.length == 2) {
  150.           startHour = hours[0];
  151.           endHour = hours[1];
  152.         }
  153.  
  154.         // set start hour
  155.         var startHourNode = document.getElementById("startHourRange");
  156.         var startHourItems = startHourNode.childNodes[0].childNodes;
  157.         for (x=0; x < startHourItems.length; ++x) {
  158.           if (startHourItems[x].getAttribute("value") == startHour) {
  159.             startHourNode.selectedItem = startHourItems[x];
  160.             break;
  161.           }
  162.         }
  163.  
  164.         // set end hour
  165.         var endHourNode = document.getElementById("endHourRange");
  166.         var endHourItems = endHourNode.childNodes[0].childNodes;
  167.         for (x=0; x < endHourItems.length; ++x) {
  168.           if (endHourItems[x].getAttribute("value") == endHour) {
  169.             endHourNode.selectedItem = endHourItems[x];
  170.             break;
  171.           }
  172.         }
  173.  
  174.         // get duration
  175.         var duration = values[2];
  176.         var durationNode = document.getElementById("duration");
  177.         durationNode.value = duration;
  178.  
  179.         // get notification method
  180.         var method = values[3];
  181.         if (method.indexOf("icon") >= 0)
  182.           document.getElementById("bookmarkIcon").checked = true;
  183.  
  184.         if (method.indexOf("sound") >= 0)
  185.           document.getElementById("playSound").checked = true;
  186.  
  187.         if (method.indexOf("alert") >= 0)
  188.           document.getElementById("showAlert").checked = true;
  189.  
  190.         if (method.indexOf("open") >= 0)
  191.           document.getElementById("openWindow").checked = true;
  192.       }
  193.     }
  194.   }
  195.  
  196.   // if its a container, disable some things
  197.   var isContainerFlag = RDFCU.IsContainer(Bookmarks, RDF.GetResource(gBookmarkURL));
  198.   if (!isContainerFlag) {
  199.     // XXX To do: the "RDFCU.IsContainer" call above only works for RDF sequences;
  200.     //            if its not a RDF sequence, we should to more checking to see if
  201.     //            the item in question is really a container of not.  A good example
  202.     //            of this is the "File System" container.
  203.   }
  204.  
  205.   if (isContainerFlag) {
  206.     // If it is a folder, it has no URL or Keyword
  207.     document.getElementById("locationrow").setAttribute("hidden", "true");
  208.     document.getElementById("shortcutrow").setAttribute("hidden", "true");
  209.   }
  210.  
  211.   if (gBookmarkURL.substr(0, 7).toLowerCase() != "http://" &&
  212.       gBookmarkURL.substr(0, 8).toLowerCase() != "https://") {
  213.     // only allow scheduling of http/https URLs
  214.     document.getElementById("ScheduleTab").setAttribute("hidden", "true");
  215.     document.getElementById("NotifyTab").setAttribute("hidden", "true");
  216.   }
  217.  
  218.   sizeToContent();
  219.   
  220.   // Set up the enabled of controls on the scheduling panels
  221.   dayRangeChange(document.getElementById("dayRange"));
  222.  
  223.   // set initial focus
  224.   nameNode.focus();
  225.   nameNode.select();
  226.  
  227. }
  228.  
  229.  
  230. function Commit()
  231. {
  232.   var changed = false;
  233.  
  234.   // Grovel through the fields to see if any of the values have
  235.   // changed. If so, update the RDF graph and force them to be saved
  236.   // to disk.
  237.   for (var i = 0; i < gFields.length; ++i) {
  238.     var field = document.getElementById(gFields[i]);
  239.  
  240.     if (field) {
  241.       // Get the new value as a literal, using 'null' if the value is empty.
  242.       var newvalue = field.value;
  243.  
  244.       var oldvalue = Bookmarks.GetTarget(RDF.GetResource(gBookmarkURL),
  245.                                          RDF.GetResource(gProperties[i]),
  246.                                          true);
  247.  
  248.       if (oldvalue)
  249.         oldvalue = oldvalue.QueryInterface(Components.interfaces.nsIRDFLiteral);
  250.  
  251.       if (newvalue && gProperties[i] == (NC_NAMESPACE_URI + "ShortcutURL")) {
  252.         // shortcuts are always lowercased internally
  253.         newvalue = newvalue.toLowerCase();
  254.       }
  255.       else if (newvalue && gProperties[i] == (NC_NAMESPACE_URI + "URL")) {
  256.         // we're dealing with the URL attribute;
  257.         // if a scheme isn't specified, use "http://"
  258.         if (newvalue.indexOf(":") < 0)
  259.           newvalue = "http://" + newvalue;
  260.       }
  261.  
  262.       if (newvalue)
  263.         newvalue = RDF.GetLiteral(newvalue);
  264.  
  265.       if (updateAttribute(gProperties[i], oldvalue, newvalue)) {
  266.         // Update gBookmarkURL if the url changed
  267.         if (newvalue && gProperties[i] == NC_NAMESPACE_URI + "URL")
  268.           gBookmarkURL = newvalue.Value;
  269.  
  270.         changed = true;
  271.       }
  272.     }
  273.   }
  274.  
  275.   // Update bookmark schedule if necessary;
  276.   // if the tab was removed, just skip it
  277.   var scheduleTab = document.getElementById("ScheduleTab");
  278.   if (scheduleTab) {
  279.     var scheduleRes = "http://home.netscape.com/WEB-rdf#Schedule";
  280.     oldvalue = Bookmarks.GetTarget(RDF.GetResource(gBookmarkURL),
  281.                                    RDF.GetResource(scheduleRes), true);
  282.     newvalue = "";
  283.     var dayRangeNode = document.getElementById("dayRange");
  284.     var dayRange = dayRangeNode.selectedItem.getAttribute("value");
  285.  
  286.     if (dayRange) {
  287.       var startHourRangeNode = document.getElementById("startHourRange");
  288.       var startHourRange = startHourRangeNode.selectedItem.getAttribute("value");
  289.  
  290.       var endHourRangeNode = document.getElementById("endHourRange");
  291.       var endHourRange = endHourRangeNode.selectedItem.getAttribute("value");
  292.  
  293.       if (parseInt(startHourRange) > parseInt(endHourRange)) {
  294.         var temp = startHourRange;
  295.         startHourRange = endHourRange;
  296.         endHourRange = temp;
  297.       }
  298.  
  299.       var duration = document.getElementById("duration").value;
  300.       if (!duration) {
  301.         alert(BookmarksUtils.getLocaleString("pleaseEnterADuration"));
  302.         return false;
  303.       }
  304.  
  305.       var methods = [];
  306.       if (document.getElementById("bookmarkIcon").checked)
  307.         methods.push("icon");
  308.       if (document.getElementById("playSound").checked)
  309.         methods.push("sound");
  310.       if (document.getElementById("showAlert").checked)
  311.         methods.push("alert");
  312.       if (document.getElementById("openWindow").checked)
  313.         methods.push("open");
  314.  
  315.       if (methods.length == 0) {
  316.         alert(BookmarksUtils.getLocaleString("pleaseSelectANotification"));
  317.         return false;
  318.       }
  319.  
  320.       var method = methods.join(); // join string in array with ","
  321.  
  322.       newvalue = dayRange + "|" + startHourRange + "-" + endHourRange + "|" + duration + "|" + method;
  323.     }
  324.  
  325.     if (newvalue)
  326.       newvalue = RDF.GetLiteral(newvalue);
  327.  
  328.     if (updateAttribute(scheduleRes, oldvalue, newvalue))
  329.       changed = true;
  330.   }
  331.  
  332.   if (changed) {
  333.     var remote = Bookmarks.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  334.     if (remote)
  335.       remote.Flush();
  336.   }
  337.  
  338.   window.close();
  339.   return true;
  340. }
  341.  
  342. function updateAttribute(prop, oldvalue, newvalue)
  343. {
  344.   var changed = false;
  345.  
  346.   if (prop && (oldvalue || newvalue) && oldvalue != newvalue) {
  347.  
  348.     if (oldvalue && !newvalue) {
  349.       Bookmarks.Unassert(RDF.GetResource(gBookmarkURL),
  350.                          RDF.GetResource(prop),
  351.                          oldvalue);
  352.     }
  353.     else if (!oldvalue && newvalue) {
  354.       Bookmarks.Assert(RDF.GetResource(gBookmarkURL),
  355.                        RDF.GetResource(prop),
  356.                        newvalue,
  357.                        true);
  358.     }
  359.     else /* if (oldvalue && newvalue) */ {
  360.       Bookmarks.Change(RDF.GetResource(gBookmarkURL),
  361.                        RDF.GetResource(prop),
  362.                        oldvalue,
  363.                        newvalue);
  364.     }
  365.  
  366.     changed = true;
  367.   }
  368.  
  369.   return changed;
  370. }
  371.  
  372. function setEndHourRange()
  373. {
  374.   // Get the values of the start-time and end-time as ints
  375.   var startHourRangeNode = document.getElementById("startHourRange");
  376.   var startHourRange = startHourRangeNode.selectedItem.getAttribute("value");
  377.   var startHourRangeInt = parseInt(startHourRange);
  378.  
  379.   var endHourRangeNode = document.getElementById("endHourRange");
  380.   var endHourRange = endHourRangeNode.selectedItem.getAttribute("value");
  381.   var endHourRangeInt = parseInt(endHourRange);
  382.  
  383.   var endHourItemNode = endHourRangeNode.firstChild.firstChild;
  384.  
  385.   var index = 0;
  386.  
  387.   // disable all those end-times before the start-time
  388.   for (; index < startHourRangeInt; ++index) {
  389.     endHourItemNode.setAttribute("disabled", "true");
  390.     endHourItemNode = endHourItemNode.nextSibling;
  391.   }
  392.  
  393.   // update the selected value if it's out of the allowed range
  394.   if (startHourRangeInt >= endHourRangeInt)
  395.     endHourRangeNode.selectedItem = endHourItemNode;
  396.  
  397.   // make sure all the end-times after the start-time are enabled
  398.   for (; index < 24; ++index) {
  399.     endHourItemNode.removeAttribute("disabled");
  400.     endHourItemNode = endHourItemNode.nextSibling;
  401.   }
  402. }
  403.  
  404. function dayRangeChange (aMenuList)
  405. {
  406.   var controls = ["startHourRange", "endHourRange", "duration", "bookmarkIcon", 
  407.                   "showAlert", "openWindow", "playSound", "durationSubLabel", 
  408.                   "durationLabel", "startHourRangeLabel", "endHourRangeLabel"];
  409.   for (var i = 0; i < controls.length; ++i)
  410.     document.getElementById(controls[i]).disabled = !aMenuList.value;
  411. }
  412.  
  413.  
  414.