Bug 1329589 - Set calendar variables for date picker using Intl.getCalendarInfo draft
authorScott Wu <scottcwwu@gmail.com>
Tue, 10 Jan 2017 16:41:36 +0800
changeset 465577 7e98278ba8f0421f303b47d7c5986f926d130abb
parent 464862 d5e37c0a776f1f2c21ddac4612529f819e13733b
child 543177 8eba9c5a036bb367c7985188fe6ba02c83ea4e5f
push id42631
push userbmo:scwwu@mozilla.com
push dateTue, 24 Jan 2017 10:08:59 +0000
bugs1329589
milestone53.0a1
Bug 1329589 - Set calendar variables for date picker using Intl.getCalendarInfo MozReview-Commit-ID: KmKVSAyNgeW
toolkit/content/widgets/datepicker.js
toolkit/content/widgets/datetimepopup.xml
--- a/toolkit/content/widgets/datepicker.js
+++ b/toolkit/content/widgets/datepicker.js
@@ -33,26 +33,25 @@ function DatePicker(context) {
     /*
      * Set initial date picker states.
      */
     _setDefaultState() {
       const now = new Date();
       const { year = now.getFullYear(),
               month = now.getMonth(),
               day = now.getDate(),
+              firstDayOfWeek,
+              weekends,
               locale } = this.props;
-
-      // TODO: Use calendar info API to get first day of week & weekends
-      //       (Bug 1287503)
       const dateKeeper = new DateKeeper({
         year, month, day
       }, {
-        calViewSize: CAL_VIEW_SIZE,
-        firstDayOfWeek: 0,
-        weekends: [0]
+        firstDayOfWeek,
+        weekends,
+        calViewSize: CAL_VIEW_SIZE
       });
 
       this.state = {
         dateKeeper,
         locale,
         isMonthPickerVisible: false,
         isYearSet: false,
         isMonthSet: false,
--- a/toolkit/content/widgets/datetimepopup.xml
+++ b/toolkit/content/widgets/datetimepopup.xml
@@ -110,23 +110,28 @@
                   max: detail.max,
                   step: detail.step,
                 }
               });
               break;
             }
             case "date": {
               const { year, month, day } = detail.value;
+              const { firstDayOfWeek, weekends } =
+                this.getCalendarInfo(locale);
+
               this.postMessageToPicker({
                 name: "PickerInit",
                 detail: {
                   year,
                   // Month value from input box starts from 1 instead of 0
                   month: month == undefined ? undefined : month - 1,
                   day,
+                  firstDayOfWeek,
+                  weekends,
                   locale
                 }
               });
               break;
             }
           }
         ]]></body>
       </method>
@@ -179,16 +184,51 @@
                   day: value.day
                 }
               }));
               break;
             }
           }
         ]]></body>
       </method>
+      <method name="getCalendarInfo">
+        <parameter name="locale"/>
+        <body><![CDATA[
+          const l10n = {};
+          const mozIntl = Components.classes["@mozilla.org/mozintl;1"]
+                            .getService(Components.interfaces.mozIMozIntl);
+          mozIntl.addGetCalendarInfo(l10n);
+          const calendarInfo = l10n.getCalendarInfo(locale);
+
+          // Day of week from calendarInfo starts from 1 as Sunday to 7 as Saturday,
+          // so they need to be mapped to JavaScript convention with 0 as Sunday
+          // and 6 as Saturday
+          let firstDayOfWeek = calendarInfo.firstDayOfWeek - 1,
+              weekendStart = calendarInfo.weekendStart - 1,
+              weekendEnd = calendarInfo.weekendEnd - 1;
+
+          let weekends = [];
+
+          // Make sure weekendEnd is greater than weekendStart
+          if (weekendEnd < weekendStart) {
+            weekendEnd += 7;
+          }
+
+          // We get the weekends by incrementing weekendStart up to weekendEnd.
+          // If the start and end is the same day, then weekends only has one day.
+          for (let day = weekendStart; day <= weekendEnd; day++) {
+            weekends.push(day % 7);
+          }
+
+          return {
+            firstDayOfWeek,
+            weekends
+          }
+        ]]></body>
+      </method>
       <method name="handleEvent">
         <parameter name="aEvent"/>
         <body><![CDATA[
           switch (aEvent.type) {
             case "load": {
               this.initPicker(this.detail);
               this.dateTimePopupFrame.contentWindow.addEventListener("message", this);
               break;