Bug 1329589 - Set calendar variables for date picker using Intl.getCalendarInfo
MozReview-Commit-ID: KmKVSAyNgeW
--- 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;