1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 export var DateTimePickerPanel = class {
7 this.element = element;
9 this.TIME_PICKER_WIDTH = "13em";
10 this.TIME_PICKER_HEIGHT = "22em";
11 this.DATE_PICKER_WIDTH = "24em";
12 this.DATE_PICKER_HEIGHT = "27em";
15 get dateTimePopupFrame() {
16 let frame = this.element.querySelector("#dateTimePopupFrame");
18 frame = this.element.ownerDocument.createXULElement("iframe");
19 frame.id = "dateTimePopupFrame";
20 this.element.appendChild(frame);
25 openPicker(type, rect, detail) {
26 if (type == "datetime-local") {
30 this.pickerState = {};
31 // TODO: Resize picker according to content zoom level
32 this.element.style.fontSize = "10px";
36 this.dateTimePopupFrame.addEventListener("load", this, true);
37 this.dateTimePopupFrame.setAttribute(
39 "chrome://global/content/timepicker.xhtml"
41 this.dateTimePopupFrame.style.width = this.TIME_PICKER_WIDTH;
42 this.dateTimePopupFrame.style.height = this.TIME_PICKER_HEIGHT;
47 this.dateTimePopupFrame.addEventListener("load", this, true);
48 this.dateTimePopupFrame.setAttribute(
50 "chrome://global/content/datepicker.xhtml"
52 this.dateTimePopupFrame.style.width = this.DATE_PICKER_WIDTH;
53 this.dateTimePopupFrame.style.height = this.DATE_PICKER_HEIGHT;
57 this.element.openPopupAtScreenRect(
70 this.element.dispatchEvent(new CustomEvent("DateTimePickerValueCleared"));
72 this.setInputBoxValue(true);
74 this.pickerState = {};
75 this.type = undefined;
76 this.dateTimePopupFrame.removeEventListener("load", this, true);
77 this.dateTimePopupFrame.contentDocument.removeEventListener(
81 this.dateTimePopupFrame.setAttribute("src", "");
82 this.element.hidePopup();
88 this.postMessageToPicker({
89 name: "PickerSetValue",
95 const { year, month, day } = data.value;
96 this.postMessageToPicker({
97 name: "PickerSetValue",
100 // Month value from input box starts from 1 instead of 0
101 month: month == undefined ? undefined : month - 1,
111 let locale = new Services.intl.Locale(
112 Services.locale.webExposedLocales[0],
118 // Workaround for bug 1418061, while we wait for resolution of
119 // http://bugs.icu-project.org/trac/ticket/13592: drop the PT region code,
120 // because it results in "abbreviated" day names that are too long;
121 // the region-less "pt" locale has shorter forms that are better here.
122 locale = locale.replace(/^pt-PT/i, "pt");
124 const dir = Services.locale.isAppLocaleRTL ? "rtl" : "ltr";
128 const { hour, minute } = detail.value;
129 const format = detail.format || "12";
131 this.postMessageToPicker({
146 const { year, month, day } = detail.value;
147 const { firstDayOfWeek, weekends } = this.getCalendarInfo(locale);
149 const monthDisplayNames = new Services.intl.DisplayNames(locale, {
154 const monthStrings = [
167 ].map(month => monthDisplayNames.of(month));
169 const weekdayDisplayNames = new Services.intl.DisplayNames(locale, {
171 style: "abbreviated",
174 const weekdayStrings = [
175 // Weekdays starting Sunday (7) to Saturday (6).
183 ].map(weekday => weekdayDisplayNames.of(weekday));
185 this.postMessageToPicker({
189 // Month value from input box starts from 1 instead of 0
190 month: month == undefined ? undefined : month - 1,
201 stepBase: detail.stepBase,
210 * @param {Boolean} passAllValues: Pass spinner values regardless if they've been set/changed or not
212 setInputBoxValue(passAllValues) {
221 } = this.pickerState;
222 const isAnyValueSet = isHourSet || isMinuteSet || isDayPeriodSet;
223 if (passAllValues && isAnyValueSet) {
224 this.sendPickerValueChanged({ hour, minute });
226 this.sendPickerValueChanged({
227 hour: isHourSet || isDayPeriodSet ? hour : undefined,
228 minute: isMinuteSet ? minute : undefined,
234 this.sendPickerValueChanged(this.pickerState);
240 sendPickerValueChanged(value) {
243 this.element.dispatchEvent(
244 new CustomEvent("DateTimePickerValueChanged", {
247 minute: value.minute,
254 this.element.dispatchEvent(
255 new CustomEvent("DateTimePickerValueChanged", {
258 // Month value from input box starts from 1 instead of 0
259 month: value.month == undefined ? undefined : value.month + 1,
269 getCalendarInfo(locale) {
270 const calendarInfo = Services.intl.getCalendarInfo(locale);
272 // Day of week from calendarInfo starts from 1 as Monday to 7 as Sunday,
273 // so they need to be mapped to JavaScript convention with 0 as Sunday
275 function toDateWeekday(day) {
276 return day === 7 ? 0 : day;
279 let firstDayOfWeek = toDateWeekday(calendarInfo.firstDayOfWeek),
280 weekend = calendarInfo.weekend;
282 let weekends = weekend.map(toDateWeekday);
290 handleEvent(aEvent) {
291 switch (aEvent.type) {
293 this.initPicker(this.detail);
294 this.dateTimePopupFrame.contentWindow.addEventListener("message", this);
298 this.handleMessage(aEvent);
304 handleMessage(aEvent) {
306 !this.dateTimePopupFrame.contentDocument.nodePrincipal.isSystemPrincipal
311 switch (aEvent.data.name) {
312 case "PickerPopupChanged": {
313 this.pickerState = aEvent.data.detail;
314 this.setInputBoxValue();
318 this.closePicker(aEvent.data.detail);
324 postMessageToPicker(data) {
326 this.dateTimePopupFrame.contentDocument.nodePrincipal.isSystemPrincipal
328 this.dateTimePopupFrame.contentWindow.postMessage(data, "*");