home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 2001 December / VPR0112B.ISO / NETSCAPE6 / N6Setup.exe / bin / chrome / comm.jar / content / communicator / bookmarks / bm-props.js < prev    next >
Text File  |  2001-05-22  |  13KB  |  376 lines

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