1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
ChromeUtils.defineModuleGetter(
this,
"BrowserUtils",
"resource://gre/modules/BrowserUtils.jsm"
);
var EXPORTED_SYMBOLS = ["DateTimePickerChild"];
/**
* DateTimePickerChild is the communication channel between the input box
* (content) for date/time input types and its picker (chrome).
*/
class DateTimePickerChild extends JSWindowActorChild {
/**
* On init, just listen for the event to open the picker, once the picker is
* opened, we'll listen for update and close events.
*/
constructor() {
super();
this._inputElement = null;
}
/**
* Cleanup function called when picker is closed.
*/
close() {
this.removeListeners(this._inputElement);
let dateTimeBoxElement = this._inputElement.dateTimeBoxElement;
if (!dateTimeBoxElement) {
this._inputElement = null;
return;
}
if (this._inputElement.openOrClosedShadowRoot) {
// dateTimeBoxElement is within UA Widget Shadow DOM.
// An event dispatch to it can't be accessed by document.
let win = this._inputElement.ownerGlobal;
dateTimeBoxElement.dispatchEvent(
new win.CustomEvent("MozSetDateTimePickerState", { detail: false })
);
}
this._inputElement = null;
}
/**
* Called after picker is opened to start listening for input box update
* events.
*/
addListeners(aElement) {
aElement.ownerGlobal.addEventListener("pagehide", this);
}
/**
* Stop listeneing for events when picker is closed.
*/
removeListeners(aElement) {
aElement.ownerGlobal.removeEventListener("pagehide", this);
}
/**
* Helper function that returns the CSS direction property of the element.
*/
getComputedDirection(aElement) {
return aElement.ownerGlobal
.getComputedStyle(aElement)
.getPropertyValue("direction");
}
/**
* Helper function that returns the rect of the element, which is the position
* relative to the left/top of the content area.
*/
getBoundingContentRect(aElement) {
return BrowserUtils.getElementBoundingScreenRect(aElement);
}
getTimePickerPref() {
return Services.prefs.getBoolPref("dom.forms.datetime.timepicker");
}
/**
* nsIMessageListener.
*/
receiveMessage(aMessage) {
switch (aMessage.name) {
case "FormDateTime:PickerClosed": {
this.close();
break;
}
case "FormDateTime:PickerValueChanged": {
if (!this._inputElement) {
return;
}
let dateTimeBoxElement = this._inputElement.dateTimeBoxElement;
if (!dateTimeBoxElement) {
return;
}
let win = this._inputElement.ownerGlobal;
if (this._inputElement.openOrClosedShadowRoot) {
// dateTimeBoxElement is within UA Widget Shadow DOM.
// An event dispatch to it can't be accessed by document.
dateTimeBoxElement.dispatchEvent(
new win.CustomEvent("MozPickerValueChanged", {
detail: Cu.cloneInto(aMessage.data, win),
})
);
}
break;
}
default:
break;
}
}
/**
* nsIDOMEventListener, for chrome events sent by the input element and other
* DOM events.
*/
handleEvent(aEvent) {
switch (aEvent.type) {
case "MozOpenDateTimePicker": {
// Time picker is disabled when preffed off
if (
!(
aEvent.originalTarget instanceof
aEvent.originalTarget.ownerGlobal.HTMLInputElement
) ||
(aEvent.originalTarget.type == "time" && !this.getTimePickerPref())
) {
return;
}
if (this._inputElement) {
// This happens when we're trying to open a picker when another picker
// is still open. We ignore this request to let the first picker
// close gracefully.
return;
}
this._inputElement = aEvent.originalTarget;
let dateTimeBoxElement = this._inputElement.dateTimeBoxElement;
if (!dateTimeBoxElement) {
throw new Error(
"How do we get this event without a UA Widget or XBL binding?"
);
}
if (this._inputElement.openOrClosedShadowRoot) {
// dateTimeBoxElement is within UA Widget Shadow DOM.
// An event dispatch to it can't be accessed by document, because
// the event is not composed.
let win = this._inputElement.ownerGlobal;
dateTimeBoxElement.dispatchEvent(
new win.CustomEvent("MozSetDateTimePickerState", { detail: true })
);
}
this.addListeners(this._inputElement);
let value = this._inputElement.getDateTimeInputBoxValue();
this.sendAsyncMessage("FormDateTime:OpenPicker", {
rect: this.getBoundingContentRect(this._inputElement),
dir: this.getComputedDirection(this._inputElement),
type: this._inputElement.type,
detail: {
// Pass partial value if it's available, otherwise pass input
// element's value.
value: Object.keys(value).length ? value : this._inputElement.value,
min: this._inputElement.getMinimum(),
max: this._inputElement.getMaximum(),
step: this._inputElement.getStep(),
stepBase: this._inputElement.getStepBase(),
},
});
break;
}
case "MozUpdateDateTimePicker": {
let value = this._inputElement.getDateTimeInputBoxValue();
value.type = this._inputElement.type;
this.sendAsyncMessage("FormDateTime:UpdatePicker", { value });
break;
}
case "MozCloseDateTimePicker": {
this.sendAsyncMessage("FormDateTime:ClosePicker", {});
this.close();
break;
}
case "pagehide": {
if (
this._inputElement &&
this._inputElement.ownerDocument == aEvent.target
) {
this.sendAsyncMessage("FormDateTime:ClosePicker", {});
this.close();
}
break;
}
default:
break;
}
}
}
|