Bustage/Regression fixes for bug 475715 - Create a binding for the calendar list. r=ssitter
authorPhilipp Kewisch <mozilla@kewis.ch>
Tue, 03 Mar 2009 08:55:26 +0100
changeset 2115 c92e3a7a0e1ccc23ff662dcca936aa47b619a890
parent 2114 ddaacd907768fbaf206aa9927cf48ef9d832b64d
child 2116 d8e86c232bbcc20b2ab605ed4c76db37a1e0f417
push idunknown
push userunknown
push dateunknown
reviewersssitter
bugs475715
Bustage/Regression fixes for bug 475715 - Create a binding for the calendar list. r=ssitter
calendar/base/content/calendar-management.js
calendar/base/content/calendar-views.js
calendar/base/content/widgets/calendar-list-tree.xml
calendar/sunbird/base/content/calendar.xul
--- a/calendar/base/content/calendar-management.js
+++ b/calendar/base/content/calendar-management.js
@@ -76,31 +76,33 @@ function promptDeleteCalendar(aCalendar)
         calMgr.deleteCalendar(aCalendar);
     }
 }
 
 /**
  * Called to initialize the calendar manager for a window.
  */
 function loadCalendarManager() {
+    // Set up the composite calendar in the calendar list widget.
+    let tree = document.getElementById("calendar-list-tree-widget");
+    tree.compositeCalendar = getCompositeCalendar();
+
     // Create the home calendar if no calendar exists.
     let calendars = getCalendarManager().getCalendars({});
     if (!calendars.length) {
         initHomeCalendar();
     }
-
-    // Set up the composite calendar in the calendar list widget.
-    let tree = document.getElementById("calendar-list-tree-widget");
-    tree.compositeCalendar = getCompositeCalendar();
 }
 
 /**
  * Creates the initial "Home" calendar if no calendar exists.
  */
 function initHomeCalendar() {
+    let calMgr = cal.getCalendarManager();
+    let composite = getCompositeCalendar();
     let url = makeURL("moz-profile-calendar://");
     let homeCalendar = calMgr.createCalendar("storage", url);
 
     calMgr.registerCalendar(homeCalendar);
     let name = calGetString("calendar", "homeCalendarName");
 
     homeCalendar.name = name;
     cal.setPref("calendar.list.sortOrder", homeCalendar.id);
--- a/calendar/base/content/calendar-views.js
+++ b/calendar/base/content/calendar-views.js
@@ -539,46 +539,43 @@ var categoryManagement = {
                   categories.splice(i, 1); // remove illegal name
               }
           }
       }
 
       // Add color information to the stylesheets.
       categories.forEach(categoryManagement.updateStyleSheetForCategory,
                          categoryManagement);
-
-      categoryPrefBranch.addObserver("", this, false);
+      categoryPrefBranch.addObserver("", categoryManagement, false);
     },
 
     cleanupCategories: function cM_cleanupCategories() {
       let prefService = Components.classes["@mozilla.org/preferences-service;1"]
                                   .getService(Components.interfaces.nsIPrefService);
-      let categoryPrefBranch = prefService.getBranch("calendar.category.color.");
-      categoryPrefBranch.removeObserver("", this, false);
+      let categoryPrefBranch = prefService.getBranch("calendar.category.color.")
+                                          .QueryInterface(Components.interfaces.nsIPrefBranch2);
+      categoryPrefBranch.removeObserver("", categoryManagement, false);
     },
 
     observe: function cM_observe(aSubject, aTopic, aPrefName) {
-        if (aPrefName.substring(0, 24) == "calendar.category.color.") {
-            this.updateStyleSheetForCateogry(aPrefName.substring(24));
-        }
+        this.updateStyleSheetForCategory(aPrefName);
         // TODO Currently, the only way to find out if categories are removed is
         // to initially grab the calendar.categories.names preference and then
         // observe changes to it. it would be better if we had hooks for this,
         // so we could delete the rule from our style cache and also remove its
         // color preference.
-        
     },
 
     categoryStyleCache: {},
 
     updateStyleSheetForCategory: function cM_updateStyleSheetForCategory(aCatName) {
         if (!(aCatName in this.categoryStyleCache)) {
             // We haven't created a rule for this category yet, do so now.
             let sheet = getViewStyleSheet();
-            let ruleString = '.category-color-box[categories~="' + aCalendar.id + '"] {} ';
+            let ruleString = '.category-color-box[categories~="' + aCatName + '"] {} ';
             let ruleIndex = sheet.insertRule(ruleString, sheet.cssRules.length);
 
             this.categoryStyleCache[aCatName] = sheet.cssRules[ruleIndex];
         }
 
         let color = cal.getPrefSafe("calendar.category.color." + aCatName) || "";
         this.categoryStyleCache[aCatName].style.backgroundColor = color;
     }
--- a/calendar/base/content/widgets/calendar-list-tree.xml
+++ b/calendar/base/content/widgets/calendar-list-tree.xml
@@ -53,32 +53,36 @@
         calMgr.addObserver(this.calMgrObserver);
       ]]></constructor>
       <destructor><![CDATA[
         let calMgr = cal.getCalendarManager();
         calMgr.removeObserver(this.calMgrObserver);
         this.calMgrObserver.listTree = null;
       ]]></destructor>
 
+      <field name="mAddingFromComposite">false</field>
+
       <property name="compositeCalendar">
         <getter><![CDATA[
           if (!this.mCompositeCalendar) {
               throw Components.Exception("Calendar list has no composite calendar yet",
                                          Components.results.NS_ERROR_NOT_INITIALIZED);
           }
           return this.mCompositeCalendar;
         ]]></getter>
         <setter><![CDATA[
           this.mCompositeCalendar = val;
           this.mCompositeCalendar.addObserver(this.compositeObserver);
 
           // Now that we have a composite calendar, we can get all calendars
           // from the calendar manager.
-          let calendars = cal.getCalendarManager().getCalendars({});
+          this.mAddingFromComposite = true;
+          let calendars = sortCalendarArray(getCalendarManager().getCalendars({}));
           calendars.forEach(this.addCalendar, this);
+          this.mAddingFromComposite = false;
 
           return val;
         ]]></setter>
       </property>
 
       <property name="calendars">
         <getter><![CDATA[
           return this.mCalendarList;
@@ -258,17 +262,16 @@
         }
 
         this.tree.view = null;
         this.calObserver.listTree = null;
 
         if (this.mCompositeCalendar) {
             this.mCompositeCalendar.removeObserver(this.compositeObserver);
         }
-
       ]]></destructor>
 
       <field name="calObserver"><![CDATA[
       ({ listTree: this,
 
         // calIObserver. Note that each registered calendar uses this observer
         onStartBatch: function cMO_onStartBatch() { },
         onEndBatch: function cMO_onEndBatch() { },
@@ -413,20 +416,29 @@
                 onset="this.setAttribute('seltype', val); return val;"/>
       <property name="ignoreDisabledState"
                 onget="return (this.getAttribute('ignoredisabledstate') == 'true');"
                 onset="return setBooleanAttribute(this, 'ignoredisabledstate', val);"/>
 
       <method name="sortOrderChanged">
         <parameter name=""/>
         <body><![CDATA[
+          if (this.mAddingFromComposite) {
+              return;
+          }
           let event = document.createEvent('Events');
           event.initEvent("SortOrderChanged", true, false);
           event.sortOrder = this.sortOrder
           this.dispatchEvent(event);
+
+          let handler = this.getAttribute("onSortOrderChanged");
+          if (handler) {
+              // Call the given code in a function
+              (new Function("event", handler))(event);
+          }
         ]]></body>
       </method>
       <method name="displayScrollbarSpacer">
         <parameter name="aShouldDisplay"/>
         <body><![CDATA[
             let spacer = document.getAnonymousElementByAttribute(this, "anonid", "scrollbar-spacer");
             spacer.collapsed = !aShouldDisplay;
         ]]></body>
--- a/calendar/sunbird/base/content/calendar.xul
+++ b/calendar/sunbird/base/content/calendar.xul
@@ -169,17 +169,17 @@
                          freebusy="true"
                          onchange="minimonthPick(this.value);"/>
               <spacer id="spacebelowleftminimonth" flex="1" orient="vertical"/>
             </vbox>
           </tabpanel>
       
           <tabpanel id="tablist-calendarlist">
             <!-- This will be overlayed by calendar-calendars-list.xul -->
-            <tree id="calendar-list-tree-widget" flex="1"/>
+            <calendar-list-tree id="calendar-list-tree-widget" flex="1"/>
           </tabpanel>
         </tabpanels>
       </tabbox>
 
       <splitter id="calendar-todo-splitter" collapse="before"
                 persist="state,collapsed" orient="vertical">
         <grippy/>
       </splitter>