|
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 file, |
|
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 "use strict"; |
|
5 |
|
6 var InputWidgetHelper = { |
|
7 _uiBusy: false, |
|
8 |
|
9 handleEvent: function(aEvent) { |
|
10 this.handleClick(aEvent.target); |
|
11 }, |
|
12 |
|
13 handleClick: function(aTarget) { |
|
14 // if we're busy looking at a InputWidget we want to eat any clicks that |
|
15 // come to us, but not to process them |
|
16 if (this._uiBusy || !this.hasInputWidget(aTarget)) |
|
17 return; |
|
18 |
|
19 this._uiBusy = true; |
|
20 this.show(aTarget); |
|
21 this._uiBusy = false; |
|
22 }, |
|
23 |
|
24 show: function(aElement) { |
|
25 let type = aElement.getAttribute('type'); |
|
26 let p = new Prompt({ |
|
27 window: aElement.ownerDocument.defaultView, |
|
28 title: Strings.browser.GetStringFromName("inputWidgetHelper." + aElement.getAttribute('type')), |
|
29 buttons: [ |
|
30 Strings.browser.GetStringFromName("inputWidgetHelper.set"), |
|
31 Strings.browser.GetStringFromName("inputWidgetHelper.clear"), |
|
32 Strings.browser.GetStringFromName("inputWidgetHelper.cancel") |
|
33 ], |
|
34 }).addDatePicker({ |
|
35 value: aElement.value, |
|
36 type: type, |
|
37 }).show((function(data) { |
|
38 let changed = false; |
|
39 if (data.button == -1) { |
|
40 // This type is not supported with this android version. |
|
41 return; |
|
42 } |
|
43 if (data.button == 1) { |
|
44 // The user cleared the value. |
|
45 if (aElement.value != "") { |
|
46 aElement.value = ""; |
|
47 changed = true; |
|
48 } |
|
49 } else if (data.button == 0) { |
|
50 // Commit the new value. |
|
51 if (aElement.value != data[type]) { |
|
52 aElement.value = data[type + "0"]; |
|
53 changed = true; |
|
54 } |
|
55 } |
|
56 // Else the user canceled the input. |
|
57 |
|
58 if (changed) |
|
59 this.fireOnChange(aElement); |
|
60 }).bind(this)); |
|
61 }, |
|
62 |
|
63 hasInputWidget: function(aElement) { |
|
64 if (!aElement instanceof HTMLInputElement) |
|
65 return false; |
|
66 |
|
67 let type = aElement.getAttribute('type'); |
|
68 if (type == "date" || type == "datetime" || type == "datetime-local" || |
|
69 type == "week" || type == "month" || type == "time") { |
|
70 return true; |
|
71 } |
|
72 |
|
73 return false; |
|
74 }, |
|
75 |
|
76 fireOnChange: function(aElement) { |
|
77 let evt = aElement.ownerDocument.createEvent("Events"); |
|
78 evt.initEvent("change", true, true, aElement.defaultView, 0, |
|
79 false, false, |
|
80 false, false, null); |
|
81 setTimeout(function() { |
|
82 aElement.dispatchEvent(evt); |
|
83 }, 0); |
|
84 } |
|
85 }; |