Bug 794585 - unnecessary additional empty week row printed in monthly layout; Bug 792061 - incorrect dates printed in weekly layout r=philipp a=philipp BETA_BASE_20121008
authorStefan Sitter <ssitter@gmail.com>
Mon, 08 Oct 2012 19:50:21 +0200
changeset 13462 1f1c1c48c3b3ba8157a897468291380ab417ceb9
parent 13461 9fa5ba04901846be12e30fe1ab30d033792dce65
child 13463 791b6b3c1f80df2be4d8b5baef464c90fad0dc52
push id701
push userbugzilla@standard8.plus.com
push dateMon, 08 Oct 2012 19:06:59 +0000
treeherdercomm-beta@c9696e33af8e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersphilipp, philipp
bugs794585, 792061
Bug 794585 - unnecessary additional empty week row printed in monthly layout; Bug 792061 - incorrect dates printed in weekly layout r=philipp a=philipp
calendar/base/modules/calUtils.jsm
calendar/import-export/calMonthGridPrinter.js
calendar/import-export/calWeekPrinter.js
--- a/calendar/base/modules/calUtils.jsm
+++ b/calendar/base/modules/calUtils.jsm
@@ -424,30 +424,16 @@ let cal = {
                             aDate.getSeconds(),
                             aTimezone);
         } else {
             newDate.jsDate = aDate;
         }
         return newDate;
     },
 
-    userWeekStart: function userWeekStart(dt) {
-        let wkst = cal.getPrefSafe("calendar.week.start", 0);
-        let wkstDate = dt.clone();
-        wkstDate.day -= (wkstDate.weekday - wkst + 7) % 7;
-        return wkstDate;
-    },
-
-    userWeekEnd: function userWeekEnd(dt) {
-        let wkst = cal.getPrefSafe("calendar.week.start", 0);
-        let wkendDate = dt.clone();
-        wkendDate.day += (7 - wkendDate.weekday) + wkst;
-        return wkendDate;
-    },
-
     sortEntry: function cal_sortEntry(aItem) {
         let key = cal.getItemSortKey(aItem, this.mSortKey, this.mSortStartedDate);
         return { mSortKey : key, mItem: aItem };
     },
 
     sortEntryItem: function cal_sortEntryItem(sortEntry) {
         return sortEntry.mItem;
     },
--- a/calendar/import-export/calMonthGridPrinter.js
+++ b/calendar/import-export/calMonthGridPrinter.js
@@ -33,48 +33,25 @@ calMonthPrinter.prototype = {
         // Set page title
         document.getElementById("title").textContent = aTitle;
 
         // Table that maps YYYY-MM-DD to the DOM node container where items are to be added
         let dayTable = {};
 
         // Make sure to create tables from start to end, if passed
         if (aStart && aEnd) {
-            // Make sure the start date is really a date.
-            let startDate = aStart.clone();
-            startDate.isDate = true;
-
-            // Copy end date, which is exclusive. For our calculations, we will
-            // only be handling dates and the below code is much cleaner with
-            // the range being inclusive.
-            let endDate = aEnd.clone();
-            endDate.isDate = true;
-
-            // Find out if the start date is also shown in the first week of the
-            // following month. This means we can spare a month printout.
-            let probeDate = startDate.clone();
-            probeDate.month++;
-            probeDate.day = 1;
-            if (cal.userWeekStart(probeDate).compare(startDate) <= 0) {
-                startDate = probeDate;
-            } else {
-                startDate = startDate.startOfMonth;
-            }
-
-            // Find out if the end date is also shown in the last week of the
-            // previous month. This also means we can spare a month printout.
-            probeDate = endDate.clone();
-            probeDate.month--;
-            probeDate = probeDate.endOfMonth;
-            if (cal.userWeekEnd(probeDate).compare(endDate) >= 0) {
-                endDate = probeDate;
-            }
+            let startDate = this.normalizeStartDate(aStart);
+            let endDate = this.normalizeEndDate(aEnd);
+            let weekInfoService = cal.getWeekInfoService();
 
             // Now set up all the months we need to
-            for (let current = startDate.clone(); cal.userWeekEnd(current).compare(endDate) <= 0; current.month += 1) {
+            for (let current = startDate.clone();
+                 weekInfoService.getEndOfWeek(current.endOfMonth).compare(endDate) < 0;
+                 current.month += 1)
+            {
                 this.setupMonth(document, current, dayTable);
             }
         }
 
         for each (let item in aItems) {
             let itemStartDate = item[cal.calGetStartDateProp(item)] || item[cal.calGetEndDateProp(item)];
             let itemEndDate = item[cal.calGetEndDateProp(item)] || item[cal.calGetStartDateProp(item)];
 
@@ -119,17 +96,54 @@ calMonthPrinter.prototype = {
         // Stream out the resulting HTML
         let html = cal.xml.serializeDOM(document);
         let convStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
                                    .createInstance(Components.interfaces.nsIConverterOutputStream);
         convStream.init(aStream, 'UTF-8', 0, 0x0000);
         convStream.writeString(html);
     },
 
-    setupMonth: function setupMonth(document, startOfMonth, dayTable) {
+    normalizeStartDate: function monthPrint_normalizeStartDate(aStart) {
+        // Make sure the start date is really a date.
+        let startDate = aStart.clone();
+        startDate.isDate = true;
+
+        // Find out if the start date is also shown in the first week of the
+        // following month. This means we can spare a month printout.
+        let firstDayOfNextMonth = startDate.clone();
+        firstDayOfNextMonth.day = 1;
+        firstDayOfNextMonth.month++;
+        if (cal.getWeekInfoService().getStartOfWeek(firstDayOfNextMonth).compare(startDate) <= 0) {
+            startDate = firstDayOfNextMonth;
+        } else {
+            startDate = startDate.startOfMonth;
+        }
+        return startDate;
+    },
+
+    normalizeEndDate: function monthPrint_normalizeEndDate(aEnd) {
+        // Copy end date, which is exclusive. For our calculations, we will
+        // only be handling dates and the formatToHtml() code is much cleaner with
+        // the range being inclusive.
+        let endDate = aEnd.clone();
+        endDate.isDate = true;
+
+        // Find out if the end date is also shown in the last week of the
+        // previous month. This also means we can spare a month printout.
+        lastDayOfPreviousMonth = endDate.clone();
+        lastDayOfPreviousMonth.month--;
+        lastDayOfPreviousMonth = lastDayOfPreviousMonth.endOfMonth;
+        if (cal.getWeekInfoService().getEndOfWeek(lastDayOfPreviousMonth).compare(endDate) >= 0) {
+            endDate = lastDayOfPreviousMonth;
+        }
+
+        return endDate;
+    },
+
+    setupMonth: function monthPrint_setupMonth(document, startOfMonth, dayTable) {
         let monthTemplate = document.getElementById("month-template");
         let monthContainer = document.getElementById("month-container");
 
         // Clone the template month and make sure it doesn't have an id
         let currentMonth = monthTemplate.cloneNode(true);
         currentMonth.removeAttribute("id");
         currentMonth.item = startOfMonth.clone();
 
@@ -142,18 +156,19 @@ calMonthPrinter.prototype = {
         let wkst = cal.getPrefSafe("calendar.week.start", 0);
         for (let i = 1; i <= 7; i++) {
             let dayNumber = ((i + wkst - 1) % 7) + 1;
             let dayTitle = currentMonth.querySelector(".day" + i + "-title");
             dayTitle.textContent = cal.calGetString("dateFormat", "day." + dayNumber + ".Mmm");
         }
 
         // Set up each week
-        let endOfMonthView = cal.userWeekEnd(startOfMonth.endOfMonth);
-        let startOfMonthView = cal.userWeekStart(startOfMonth);
+        let weekInfoService = cal.getWeekInfoService();
+        let endOfMonthView = weekInfoService.getEndOfWeek(startOfMonth.endOfMonth);
+        let startOfMonthView = weekInfoService.getStartOfWeek(startOfMonth);
         let mainMonth = startOfMonth.month;
         let weekContainer = currentMonth.querySelector(".week-container");
 
         for (let weekStart = startOfMonthView; weekStart.compare(endOfMonthView) < 0; weekStart.day += 7) {
             this.setupWeek(document, weekContainer, weekStart, mainMonth, dayTable);
         }
 
         // Now insert the month into the page container, sorting by date (and therefore by month)
@@ -161,17 +176,17 @@ calMonthPrinter.prototype = {
             if (!a || !b) return -1;
             let res = a.compare(b);
             return res;
         }
 
         cal.binaryInsertNode(monthContainer, currentMonth, currentMonth.item, compareDates);
     },
 
-    setupWeek: function setupWeek(document, weekContainer, startOfWeek, mainMonth, dayTable) {
+    setupWeek: function monthPrint_setupWeek(document, weekContainer, startOfWeek, mainMonth, dayTable) {
         const weekdayMap = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
         let weekTemplate = document.getElementById("week-template");
 
         // Clone the template week and make sure it doesn't have an id
         let currentWeek = weekTemplate.cloneNode(true);
         currentWeek.removeAttribute("id");
 
         // Set up day numbers for all days in this week
--- a/calendar/import-export/calWeekPrinter.js
+++ b/calendar/import-export/calWeekPrinter.js
@@ -23,31 +23,28 @@ calWeekPrinter.prototype = {
         contractID: "@mozilla.org/calendar/printformatter;1?type=weekplan",
         classDescription: "Calendar Week Print Formatter",
         interfaces: [Components.interfaces.calIPrintFormatter]
     }),
 
     get name() cal.calGetString("calendar", "weekPrinterName"),
 
     formatToHtml: function weekPrint_format(aStream, aStart, aEnd, aCount, aItems, aTitle) {
-        let dateFormatter = cal.getDateFormatter();
         let document = cal.xml.parseFile("chrome://calendar/skin/printing/calWeekPrinter.html");
 
         // Set page title
         document.getElementById("title").textContent = aTitle;
 
         // Table that maps YYYY-MM-DD to the DOM node container where items are to be added
         let dayTable = {};
+        let weekInfoService = cal.getWeekInfoService();
 
         // Make sure to create tables from start to end, if passed
         if (aStart && aEnd) {
-            let startDate = aStart.clone();
-            startDate.isDate = true;
-
-            for (let current = cal.userWeekStart(startDate); current.compare(aEnd) < 0; current.day += 7) {
+            for (let current = weekInfoService.getStartOfWeek(aStart); current.compare(aEnd) < 0; current.day += 7) {
                 this.setupWeek(document, current, dayTable);
             }
         }
 
         for each (let item in aItems) {
             let itemStartDate = item[cal.calGetStartDateProp(item)] || item[cal.calGetEndDateProp(item)];
             let itemEndDate = item[cal.calGetEndDateProp(item)] || item[cal.calGetStartDateProp(item)];
 
@@ -66,17 +63,17 @@ calWeekPrinter.prototype = {
                     (boxDate.compare(aStart) < 0 || boxDate.compare(aEnd) >= 0)) {
                     continue;
                 }
 
                 let boxDateKey = cal.print.getDateKey(boxDate);
 
                 if (!(boxDateKey in dayTable)) {
                     // Doesn't exist, we need to create a new table for it
-                    let startOfWeek = boxDate.startOfWeek;
+                    let startOfWeek = weekInfoService.getStartOfWeek(boxDate);
                     this.setupWeek(document, startOfWeek, dayTable);
                 }
 
                 cal.print.addItemToDaybox(document, item, dayTable[boxDateKey]);
             }
         }
 
         // Remove templates from HTML, no longer needed
@@ -86,40 +83,38 @@ calWeekPrinter.prototype = {
         // Stream out the resulting HTML
         let html = cal.xml.serializeDOM(document);
         let convStream = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
                                    .createInstance(Components.interfaces.nsIConverterOutputStream);
         convStream.init(aStream, 'UTF-8', 0, 0x0000);
         convStream.writeString(html);
     },
 
-    setupWeek: function setupWeek(document, startOfWeek, dayTable) {
+    setupWeek: function weekPrint_setupWeek(document, startOfWeek, dayTable) {
         const weekdayMap = ["sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"];
 
         let weekTemplate = document.getElementById("week-template");
         let weekContainer = document.getElementById("week-container");
         let defaultTimezone = cal.calendarDefaultTimezone();
 
         // Clone the template week and make sure it doesn't have an id
         let currentPage = weekTemplate.cloneNode(true);
-        let startOfWeekKey = cal.print.getDateKey(startOfWeek);
         currentPage.removeAttribute("id");
         currentPage.item = startOfWeek.clone();
 
         // Set up the week number title
         let weekInfo = cal.getWeekInfoService();
         let dateFormatter = cal.getDateFormatter();
         let weekno = weekInfo.getWeekTitle(startOfWeek);
         let weekTitle = cal.calGetString("calendar", 'WeekTitle', [weekno]);
         currentPage.querySelector(".week-number").textContent = weekTitle;
 
-
         // Set up the day boxes
-        let endOfWeek = cal.userWeekEnd(startOfWeek);
-        for (let currentDate = startOfWeek; currentDate.compare(endOfWeek) <= 0; currentDate.day++) {
+        let endOfWeek = weekInfo.getEndOfWeek(startOfWeek);
+        for (let currentDate = startOfWeek.clone(); currentDate.compare(endOfWeek) <= 0; currentDate.day++) {
             let weekday = currentDate.weekday;
             let weekdayName = weekdayMap[weekday];
             let dayOffPrefName = "calendar.week.d" +  weekday + weekdayName + "soff";
             dayTable[cal.print.getDateKey(currentDate)] = currentPage.querySelector("." + weekdayName + "-container");
 
             let titleNode = currentPage.querySelector("." + weekdayName + "-title");
             titleNode.textContent = dateFormatter.formatDateLong(currentDate.getInTimezone(defaultTimezone));