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 / bookmarksProperties.js < prev    next >
Encoding:
JavaScript  |  2005-07-29  |  12.5 KB  |  373 lines

  1.  
  2. // This is the RDF Resource we're dealing with.
  3. var gResource;
  4. // This is the set of fields that are visible in the window.
  5. var gFields;
  6. // ...and this is a parallel array that contains the RDF properties
  7. // that they are associated with.
  8. var gProperties;
  9.  
  10. function showDescription()
  11. {
  12.   initServices();
  13.   initBMService();
  14.  
  15.   gResource = RDF.GetResource(window.arguments[0]);
  16.  
  17.   if (gResource == BMSVC.getBookmarksToolbarFolder()) {
  18.     var description = BookmarksUtils.getLocaleString("description_PersonalToolbarFolder");
  19.     var box = document.getElementById("description-box");
  20.     box.hidden = false;
  21.     var textNode = document.createTextNode(description);
  22.     document.getElementById("bookmarkDescription").appendChild(textNode);
  23.   }
  24. }
  25.  
  26. function Init()
  27. {
  28.  
  29.   // assume the user will press cancel (only used when creating new resources)
  30.   window.arguments[1].ok = false;
  31.  
  32.   // This is the set of fields that are visible in the window.
  33.   gFields     = ["name", "url", "shortcut", "description", "webpanel", "feedurl"];
  34.  
  35.   // ...and this is a parallel array that contains the RDF properties
  36.   // that they are associated with.
  37.   gProperties = [RDF.GetResource(NC_NS+"Name"),
  38.                  RDF.GetResource(NC_NS+"URL"),
  39.                  RDF.GetResource(NC_NS+"ShortcutURL"),
  40.                  RDF.GetResource(NC_NS+"Description"),
  41.                  RDF.GetResource(NC_NS+"WebPanel"),
  42.                  RDF.GetResource(NC_NS+"FeedURL")];
  43.  
  44.   var x;
  45.   // Initialize the properties panel by copying the values from the
  46.   // RDF graph into the fields on screen.
  47.  
  48.   for (var i=0; i<gFields.length; ++i) {
  49.     var field = document.getElementById(gFields[i]);
  50.     var value = BMDS.GetTarget(gResource, gProperties[i], true);
  51.     
  52.     if (value)
  53.       value = value.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  54.  
  55.     if (gFields[i] == "webpanel")
  56.       field.checked = (value != undefined);
  57.     else if (value) //make sure were aren't stuffing null into any fields
  58.       field.value = value;
  59.   }
  60.  
  61.   var propsWindow = document.getElementById("bmPropsWindow");
  62.   var nameNode = document.getElementById("name");
  63.   var title = propsWindow.getAttribute("title");
  64.   title = title.replace(/\*\*bm_title\*\*/gi, nameNode.value);
  65.   propsWindow.setAttribute("title", title);
  66.  
  67.   // check bookmark schedule
  68.   var scheduleArc = RDF.GetResource(WEB_NS+"Schedule");
  69.   value = BMDS.GetTarget(gResource, scheduleArc, true);
  70.  
  71.   if (value) {
  72.     value = value.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  73.  
  74.     if (value) {
  75.       var values = value.split("|");
  76.       if (values.length == 4) {
  77.         // get day range
  78.         var days = values[0];
  79.         var dayNode = document.getElementById("dayRange");
  80.         var dayItems = dayNode.childNodes[0].childNodes;
  81.         for (x=0; x < dayItems.length; ++x) {
  82.           if (dayItems[x].getAttribute("value") == days) {
  83.             dayNode.selectedItem = dayItems[x];
  84.             break;
  85.           }
  86.         }
  87.  
  88.         // get hour range
  89.         var hours = values[1].split("-");
  90.         var startHour = "";
  91.         var endHour = "";
  92.  
  93.         if (hours.length == 2) {
  94.           startHour = hours[0];
  95.           endHour = hours[1];
  96.         }
  97.  
  98.         // set start hour
  99.         var startHourNode = document.getElementById("startHourRange");
  100.         var startHourItems = startHourNode.childNodes[0].childNodes;
  101.         for (x=0; x < startHourItems.length; ++x) {
  102.           if (startHourItems[x].getAttribute("value") == startHour) {
  103.             startHourNode.selectedItem = startHourItems[x];
  104.             break;
  105.           }
  106.         }
  107.  
  108.         // set end hour
  109.         var endHourNode = document.getElementById("endHourRange");
  110.         var endHourItems = endHourNode.childNodes[0].childNodes;
  111.         for (x=0; x < endHourItems.length; ++x) {
  112.           if (endHourItems[x].getAttribute("value") == endHour) {
  113.             endHourNode.selectedItem = endHourItems[x];
  114.             break;
  115.           }
  116.         }
  117.  
  118.         // get duration
  119.         var duration = values[2];
  120.         var durationNode = document.getElementById("duration");
  121.         durationNode.value = duration;
  122.  
  123.         // get notification method
  124.         var method = values[3];
  125.         if (method.indexOf("icon") >= 0)
  126.           document.getElementById("bookmarkIcon").checked = true;
  127.  
  128.         if (method.indexOf("sound") >= 0)
  129.           document.getElementById("playSound").checked = true;
  130.  
  131.         if (method.indexOf("alert") >= 0)
  132.           document.getElementById("showAlert").checked = true;
  133.  
  134.         if (method.indexOf("open") >= 0)
  135.           document.getElementById("openWindow").checked = true;
  136.       }
  137.     }
  138.   }
  139.  
  140.   // if its a container, disable some things
  141.   var isContainerFlag = RDFCU.IsContainer(BMDS, gResource);
  142.   if (!isContainerFlag) {
  143.     // XXX To do: the "RDFCU.IsContainer" call above only works for RDF sequences;
  144.     //            if its not a RDF sequence, we should to more checking to see if
  145.     //            the item in question is really a container of not.  A good example
  146.     //            of this is the "File System" container.
  147.   }
  148.  
  149.   var isLivemark = BookmarksUtils.resolveType(gResource) == "Livemark";
  150.   var isSeparator = BookmarksUtils.resolveType(gResource) == "BookmarkSeparator";
  151.  
  152.   if (isContainerFlag || isSeparator) {
  153.     // Hide the "Load in sidebar" checkbox unless it's a bookmark.
  154.     var webpanelCheckbox = document.getElementById("webpanel");
  155.     webpanelCheckbox.hidden = true;
  156.  
  157.     // If it is a folder, it has no URL or Keyword
  158.     document.getElementById("locationrow").setAttribute("hidden", "true");
  159.     document.getElementById("shortcutrow").setAttribute("hidden", "true");
  160.     if (isSeparator) {
  161.       document.getElementById("descriptionrow").setAttribute("hidden", "true");
  162.     }
  163.   }
  164.  
  165.   if (isLivemark) {
  166.     document.getElementById("locationrow").hidden = true;
  167.     document.getElementById("shortcutrow").hidden = true;
  168.   } else {
  169.     document.getElementById("feedurlrow").hidden = true;
  170.   }
  171.  
  172.   var showScheduling = false;
  173.   var url = BMDS.GetTarget(gResource, gProperties[1], true);
  174.   if (url) {
  175.     url = url.QueryInterface(Components.interfaces.nsIRDFLiteral).Value;
  176.     if (!url                                       || 
  177.         url.substr(0,7).toLowerCase() == "http://" ||
  178.         url.substr(0,8).toLowerCase() == "https://")
  179.       showScheduling = true;
  180.   }
  181.  
  182.   // always hide the scheduling/notification tabs;
  183.   // we're diabling scheduling for 1.0 (bug 253478)
  184.   //if (!showScheduling || isLivemark) {
  185.     // only allow scheduling of http/https URLs that are not livemarks
  186.     document.getElementById("ScheduleTab").setAttribute("hidden", "true");
  187.     document.getElementById("NotifyTab").setAttribute("hidden", "true");
  188.   //}
  189.  
  190.   sizeToContent();
  191.   
  192.   // Set up the enabled of controls on the scheduling panels
  193.   dayRangeChange(document.getElementById("dayRange"));
  194.  
  195.   // set initial focus
  196.   nameNode.focus();
  197.   nameNode.select();
  198. }
  199.  
  200.  
  201. function Commit()
  202. {
  203.   var changed = false;
  204.  
  205.   // Grovel through the fields to see if any of the values have
  206.   // changed. If so, update the RDF graph and force them to be saved
  207.   // to disk.
  208.   for (var i=0; i<gFields.length; ++i) {
  209.     var field = document.getElementById(gFields[i]);
  210.  
  211.     if (! field)
  212.       continue;
  213.  
  214.     // Get the new value as a literal, using 'null' if the value is empty.
  215.     var newValue = field.value;
  216.     if (gFields[i] == "webpanel")
  217.       newValue = field.checked ? "true" : undefined;
  218.  
  219.     var oldValue = BMDS.GetTarget(gResource, gProperties[i], true);
  220.  
  221.     if (oldValue)
  222.       oldValue = oldValue.QueryInterface(Components.interfaces.nsIRDFLiteral);
  223.  
  224.     if (newValue && gFields[i] == "shortcut")
  225.       // shortcuts are always lowercased internally
  226.       newValue = newValue.toLowerCase();
  227.     else if (newValue && gFields[i] == "url") {
  228.       if (newValue.indexOf(":") < 0)
  229.         // we're dealing with the URL attribute;
  230.         // if a scheme isn't specified, use "http://"
  231.         newValue = "http://" + newValue;
  232.     }
  233.  
  234.     if (newValue)
  235.       newValue = RDF.GetLiteral(newValue);
  236.  
  237.     changed |= updateAttribute(gProperties[i], oldValue, newValue);
  238.  
  239.     if (gFields[i] == "url" && oldValue && oldValue.Value != newValue.Value) {
  240.       // if the URL was updated, clear out the favicon
  241.       var icon = BMDS.GetTarget(gResource, RDF.GetResource(NC_NS+"Icon"), true);
  242.       if (icon) BMDS.Unassert(gResource, RDF.GetResource(NC_NS+"Icon"), icon);
  243.     }
  244.   }
  245.  
  246.   // Update bookmark schedule if necessary;
  247.   // if the tab was removed, just skip it
  248.   var scheduleTab = document.getElementById("ScheduleTab");
  249.   if (scheduleTab) {
  250.     var scheduleArc = RDF.GetResource(WEB_NS+"Schedule");
  251.     oldValue = BMDS.GetTarget(gResource, scheduleArc, true);
  252.     newValue = null;
  253.     var dayRangeNode = document.getElementById("dayRange");
  254.     var dayRange = dayRangeNode.selectedItem.getAttribute("value");
  255.  
  256.     if (dayRange) {
  257.       var startHourRangeNode = document.getElementById("startHourRange");
  258.       var startHourRange = startHourRangeNode.selectedItem.getAttribute("value");
  259.  
  260.       var endHourRangeNode = document.getElementById("endHourRange");
  261.       var endHourRange = endHourRangeNode.selectedItem.getAttribute("value");
  262.  
  263.       if (parseInt(startHourRange) > parseInt(endHourRange)) {
  264.         var temp = startHourRange;
  265.         startHourRange = endHourRange;
  266.         endHourRange = temp;
  267.       }
  268.  
  269.       var duration = document.getElementById("duration").value;
  270.       if (!duration) {
  271.         alert(BookmarksUtils.getLocaleString("pleaseEnterADuration"));
  272.         return false;
  273.       }
  274.  
  275.       var methods = [];
  276.       if (document.getElementById("bookmarkIcon").checked)
  277.         methods.push("icon");
  278.       if (document.getElementById("playSound").checked)
  279.         methods.push("sound");
  280.       if (document.getElementById("showAlert").checked)
  281.         methods.push("alert");
  282.       if (document.getElementById("openWindow").checked)
  283.         methods.push("open");
  284.  
  285.       if (methods.length == 0) {
  286.         alert(BookmarksUtils.getLocaleString("pleaseSelectANotification"));
  287.         return false;
  288.       }
  289.  
  290.       var method = methods.join(); // join string in array with ","
  291.  
  292.       newValue = dayRange + "|" + startHourRange + "-" + endHourRange + "|" + duration + "|" + method;
  293.     }
  294.  
  295.     if (newValue)
  296.       newValue = RDF.GetLiteral(newValue);
  297.  
  298.     changed |= updateAttribute(scheduleArc, oldValue, newValue);   
  299.   }
  300.  
  301.   if (changed) {
  302.     var remote = BMDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  303.     if (remote)
  304.       remote.Flush();
  305.   }
  306.  
  307.   window.arguments[1].ok = true;
  308.   window.close();
  309.   return true;
  310. }
  311.  
  312. function updateAttribute(aProperty, aOldValue, aNewValue)
  313. {
  314.   if ((aOldValue || aNewValue) && aOldValue != aNewValue) {
  315.     if (aOldValue && !aNewValue)
  316.       BMDS.Unassert(gResource, aProperty, aOldValue);
  317.     else if (!aOldValue && aNewValue)
  318.       BMDS.Assert(gResource, aProperty, aNewValue, true);
  319.     else /* if (aOldValue && aNewValue) */
  320.       BMDS.Change(gResource, aProperty, aOldValue, aNewValue);
  321.     return true;
  322.   }
  323.   return false;
  324. }
  325.  
  326. function setEndHourRange()
  327. {
  328.   // Get the values of the start-time and end-time as ints
  329.   var startHourRangeNode = document.getElementById("startHourRange");
  330.   var startHourRange = startHourRangeNode.selectedItem.getAttribute("value");
  331.   var startHourRangeInt = parseInt(startHourRange);
  332.  
  333.   var endHourRangeNode = document.getElementById("endHourRange");
  334.   var endHourRange = endHourRangeNode.selectedItem.getAttribute("value");
  335.   var endHourRangeInt = parseInt(endHourRange);
  336.  
  337.   var endHourItemNode = endHourRangeNode.firstChild.firstChild;
  338.  
  339.   // disable all those end-times before the start-time
  340.   for (var index=0; index<startHourRangeInt; ++index) {
  341.     endHourItemNode.setAttribute("disabled", "true");
  342.     endHourItemNode = endHourItemNode.nextSibling;
  343.   }
  344.  
  345.   // update the selected value if it's out of the allowed range
  346.   if (startHourRangeInt >= endHourRangeInt)
  347.     endHourRangeNode.selectedItem = endHourItemNode;
  348.  
  349.   // make sure all the end-times after the start-time are enabled
  350.   for (; index < 24; ++index) {
  351.     endHourItemNode.removeAttribute("disabled");
  352.     endHourItemNode = endHourItemNode.nextSibling;
  353.   }
  354. }
  355.  
  356. function dayRangeChange (aMenuList)
  357. {
  358.   var controls = ["startHourRange", "endHourRange", "duration", "bookmarkIcon", 
  359.                   "showAlert", "openWindow", "playSound", "durationSubLabel", 
  360.                   "durationLabel", "startHourRangeLabel", "endHourRangeLabel"];
  361.   for (var i=0; i<controls.length; ++i)
  362.     document.getElementById(controls[i]).disabled = !aMenuList.value;
  363. }
  364.  
  365. // MERC - JCH: This function was added to address BLT# 149920. Need to explicitly
  366. // indicate that the user cancelled the create new folder action.
  367. function cancelAction()
  368. {
  369.     // Set the variable that the opener checks to determine whether 
  370.     // 'accept' or 'cancel' was selected.
  371.     window.arguments[1].ok = false;
  372. }
  373.