mobile/android/chrome/content/InputWidgetHelper.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial