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

  1.  
  2. // XXX MAKE SURE that the "url" field is LAST!
  3. // This is important for what happens if/when the URL itself is changed.
  4. // Ask rjc@netscape.com if you want to know why exactly this is.
  5.  
  6. // This is the set of fields that are visible in the window.
  7. var gFields;
  8.  
  9. // ...and this is a parallel array that contains the RDF properties
  10. // that they are associated with.
  11. var gProperties;
  12.  
  13. var Bookmarks;
  14. var gBookmarkURL;
  15.  
  16. function showDescription()
  17. {
  18.   initServices();
  19.   initBMService();
  20.  
  21.   gBookmarkURL = window.arguments[0];
  22.   var resource = RDF.GetResource(gBookmarkURL);
  23.  
  24.   // Check the description
  25.   var primaryType = BookmarksUtils.resolveType(resource);
  26.   var description = BookmarksUtils.getLocaleString("description_"+primaryType);
  27.   
  28.   var newBookmarkFolder = BookmarksUtils.getNewBookmarkFolder();
  29.   var newSearchFolder   = BookmarksUtils.getNewSearchFolder();
  30.  
  31.   if (resource == newBookmarkFolder && resource == newSearchFolder)
  32.     description = description+" "+BookmarksUtils.getLocaleString("description_NewBookmarkAndSearchFolder")
  33.   else if (resource == newBookmarkFolder )
  34.     description = description+" "+BookmarksUtils.getLocaleString("description_NewBookmarkFolder")
  35.   else if (resource == newSearchFolder)
  36.     description = description+" "+BookmarksUtils.getLocaleString("description_NewSearchFolder");
  37.  
  38.   var textNode = document.createTextNode(description);
  39.   document.getElementById("bookmarkDescription").appendChild(textNode);
  40.  
  41. }
  42.  
  43. function Init()
  44. {
  45.   // This is the set of fields that are visible in the window.
  46.   gFields     = ["name", "shortcut", "description", "url"];
  47.  
  48.   // ...and this is a parallel array that contains the RDF properties
  49.   // that they are associated with.
  50.  
  51.   gProperties = [NC_NS + "Name",
  52.                  NC_NS + "ShortcutURL",
  53.                  NC_NS + "Description",
  54.                  NC_NS + "URL"];
  55.  
  56.   Bookmarks = RDF.GetDataSource("rdf:bookmarks");
  57.  
  58.   var x;
  59.   var resource = RDF.GetResource(gBookmarkURL);
  60.   // Initialize the properties panel by copying the values from the
  61.   // RDF graph into the fields on screen.
  62.  
  63.   for (var i = 0; i < gFields.length; ++i) {
  64.     var field = document.getElementById(gFields[i]);
  65.  
  66.     var value = Bookmarks.GetTarget(resource, RDF.GetResource(gProperties[i]), true);
  67.  
  68.     if (value)
  69.       value = value.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  70.  
  71.     if (value) //make sure were aren't stuffing null into any fields
  72.       field.value = value;
  73.   }
  74.  
  75.   var propsWindow = document.getElementById("bmPropsWindow");
  76.   var nameNode = document.getElementById("name");
  77.   var title = propsWindow.getAttribute("title");
  78.   title = title.replace(/\*\*bm_title\*\*/gi, nameNode.value);
  79.   propsWindow.setAttribute("title", title);
  80.  
  81.   // check bookmark schedule
  82.   var scheduleArc = RDF.GetResource("http://home.netscape.com/WEB-rdf#Schedule");
  83.   value = Bookmarks.GetTarget(resource, scheduleArc, 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 = RDFCU.IsContainer(Bookmarks, RDF.GetResource(gBookmarkURL));
  156.   if (!isContainerFlag) {
  157.     // XXX To do: the "RDFCU.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.   nameNode.focus();
  183.   nameNode.select();
  184.  
  185. }
  186.  
  187.  
  188. function Commit()
  189. {
  190.   var changed = false;
  191.  
  192.   // Grovel through the fields to see if any of the values have
  193.   // changed. If so, update the RDF graph and force them to be saved
  194.   // to disk.
  195.   for (var i = 0; i < gFields.length; ++i) {
  196.     var field = document.getElementById(gFields[i]);
  197.  
  198.     if (field) {
  199.       // Get the new value as a literal, using 'null' if the value is empty.
  200.       var newvalue = field.value;
  201.  
  202.       var oldvalue = Bookmarks.GetTarget(RDF.GetResource(gBookmarkURL),
  203.                                          RDF.GetResource(gProperties[i]),
  204.                                          true);
  205.  
  206.       if (oldvalue)
  207.         oldvalue = oldvalue.QueryInterface(Components.interfaces.nsIRDFLiteral);
  208.  
  209.       if (newvalue && gProperties[i] == (NC_NS + "ShortcutURL")) {
  210.         // shortcuts are always lowercased internally
  211.         newvalue = newvalue.toLowerCase();
  212.       }
  213.       else if (newvalue && gProperties[i] == (NC_NS + "URL")) {
  214.         // we're dealing with the URL attribute;
  215.         // if a scheme isn't specified, use "http://"
  216.         if (newvalue.indexOf(":") < 0)
  217.           newvalue = "http://" + newvalue;
  218.       }
  219.  
  220.       if (newvalue)
  221.         newvalue = RDF.GetLiteral(newvalue);
  222.  
  223.       if (updateAttribute(gProperties[i], oldvalue, newvalue)) {
  224.         // Update gBookmarkURL if the url changed
  225.         if (newvalue && gProperties[i] == NC_NS + "URL")
  226.           gBookmarkURL = newvalue.Value;
  227.  
  228.         changed = true;
  229.       }
  230.     }
  231.   }
  232.  
  233.   // Update bookmark schedule if necessary;
  234.   // if the tab was removed, just skip it
  235.   var scheduleTab = document.getElementById("ScheduleTab");
  236.   if (scheduleTab) {
  237.     var scheduleRes = "http://home.netscape.com/WEB-rdf#Schedule";
  238.     oldvalue = Bookmarks.GetTarget(RDF.GetResource(gBookmarkURL),
  239.                                    RDF.GetResource(scheduleRes), true);
  240.     newvalue = "";
  241.     var dayRangeNode = document.getElementById("dayRange");
  242.     var dayRange = dayRangeNode.selectedItem.getAttribute("value");
  243.  
  244.     if (dayRange) {
  245.       var startHourRangeNode = document.getElementById("startHourRange");
  246.       var startHourRange = startHourRangeNode.selectedItem.getAttribute("value");
  247.  
  248.       var endHourRangeNode = document.getElementById("endHourRange");
  249.       var endHourRange = endHourRangeNode.selectedItem.getAttribute("value");
  250.  
  251.       if (parseInt(startHourRange) > parseInt(endHourRange)) {
  252.         var temp = startHourRange;
  253.         startHourRange = endHourRange;
  254.         endHourRange = temp;
  255.       }
  256.  
  257.       var duration = document.getElementById("duration").value;
  258.       if (!duration) {
  259.         alert(BookmarksUtils.getLocaleString("pleaseEnterADuration"));
  260.         return false;
  261.       }
  262.  
  263.       var methods = [];
  264.       if (document.getElementById("bookmarkIcon").checked)
  265.         methods.push("icon");
  266.       if (document.getElementById("playSound").checked)
  267.         methods.push("sound");
  268.       if (document.getElementById("showAlert").checked)
  269.         methods.push("alert");
  270.       if (document.getElementById("openWindow").checked)
  271.         methods.push("open");
  272.  
  273.       if (methods.length == 0) {
  274.         alert(BookmarksUtils.getLocaleString("pleaseSelectANotification"));
  275.         return false;
  276.       }
  277.  
  278.       var method = methods.join(); // join string in array with ","
  279.  
  280.       newvalue = dayRange + "|" + startHourRange + "-" + endHourRange + "|" + duration + "|" + method;
  281.     }
  282.  
  283.     if (newvalue)
  284.       newvalue = RDF.GetLiteral(newvalue);
  285.  
  286.     if (updateAttribute(scheduleRes, oldvalue, newvalue))
  287.       changed = true;
  288.   }
  289.  
  290.   if (changed) {
  291.     var remote = Bookmarks.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  292.     if (remote)
  293.       remote.Flush();
  294.   }
  295.  
  296.   window.close();
  297.   return true;
  298. }
  299.  
  300. function updateAttribute(prop, oldvalue, newvalue)
  301. {
  302.   var changed = false;
  303.  
  304.   if (prop && (oldvalue || newvalue) && oldvalue != newvalue) {
  305.  
  306.     if (oldvalue && !newvalue) {
  307.       Bookmarks.Unassert(RDF.GetResource(gBookmarkURL),
  308.                          RDF.GetResource(prop),
  309.                          oldvalue);
  310.     }
  311.     else if (!oldvalue && newvalue) {
  312.       Bookmarks.Assert(RDF.GetResource(gBookmarkURL),
  313.                        RDF.GetResource(prop),
  314.                        newvalue,
  315.                        true);
  316.     }
  317.     else /* if (oldvalue && newvalue) */ {
  318.       Bookmarks.Change(RDF.GetResource(gBookmarkURL),
  319.                        RDF.GetResource(prop),
  320.                        oldvalue,
  321.                        newvalue);
  322.     }
  323.  
  324.     changed = true;
  325.   }
  326.  
  327.   return changed;
  328. }
  329.  
  330. function setEndHourRange()
  331. {
  332.   // Get the values of the start-time and end-time as ints
  333.   var startHourRangeNode = document.getElementById("startHourRange");
  334.   var startHourRange = startHourRangeNode.selectedItem.getAttribute("value");
  335.   var startHourRangeInt = parseInt(startHourRange);
  336.  
  337.   var endHourRangeNode = document.getElementById("endHourRange");
  338.   var endHourRange = endHourRangeNode.selectedItem.getAttribute("value");
  339.   var endHourRangeInt = parseInt(endHourRange);
  340.  
  341.   var endHourItemNode = endHourRangeNode.firstChild.firstChild;
  342.  
  343.   var index = 0;
  344.  
  345.   // disable all those end-times before the start-time
  346.   for (; index < startHourRangeInt; ++index) {
  347.     endHourItemNode.setAttribute("disabled", "true");
  348.     endHourItemNode = endHourItemNode.nextSibling;
  349.   }
  350.  
  351.   // update the selected value if it's out of the allowed range
  352.   if (startHourRangeInt >= endHourRangeInt)
  353.     endHourRangeNode.selectedItem = endHourItemNode;
  354.  
  355.   // make sure all the end-times after the start-time are enabled
  356.   for (; index < 24; ++index) {
  357.     endHourItemNode.removeAttribute("disabled");
  358.     endHourItemNode = endHourItemNode.nextSibling;
  359.   }
  360. }
  361.  
  362. function dayRangeChange (aMenuList)
  363. {
  364.   var controls = ["startHourRange", "endHourRange", "duration", "bookmarkIcon", 
  365.                   "showAlert", "openWindow", "playSound", "durationSubLabel", 
  366.                   "durationLabel", "startHourRangeLabel", "endHourRangeLabel"];
  367.   for (var i = 0; i < controls.length; ++i)
  368.     document.getElementById(controls[i]).disabled = !aMenuList.value;
  369. }
  370.  
  371.  
  372.