michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: "use strict"; michael@0: michael@0: var InputWidgetHelper = { michael@0: _uiBusy: false, michael@0: michael@0: handleEvent: function(aEvent) { michael@0: this.handleClick(aEvent.target); michael@0: }, michael@0: michael@0: handleClick: function(aTarget) { michael@0: // if we're busy looking at a InputWidget we want to eat any clicks that michael@0: // come to us, but not to process them michael@0: if (this._uiBusy || !this.hasInputWidget(aTarget)) michael@0: return; michael@0: michael@0: this._uiBusy = true; michael@0: this.show(aTarget); michael@0: this._uiBusy = false; michael@0: }, michael@0: michael@0: show: function(aElement) { michael@0: let type = aElement.getAttribute('type'); michael@0: let p = new Prompt({ michael@0: window: aElement.ownerDocument.defaultView, michael@0: title: Strings.browser.GetStringFromName("inputWidgetHelper." + aElement.getAttribute('type')), michael@0: buttons: [ michael@0: Strings.browser.GetStringFromName("inputWidgetHelper.set"), michael@0: Strings.browser.GetStringFromName("inputWidgetHelper.clear"), michael@0: Strings.browser.GetStringFromName("inputWidgetHelper.cancel") michael@0: ], michael@0: }).addDatePicker({ michael@0: value: aElement.value, michael@0: type: type, michael@0: }).show((function(data) { michael@0: let changed = false; michael@0: if (data.button == -1) { michael@0: // This type is not supported with this android version. michael@0: return; michael@0: } michael@0: if (data.button == 1) { michael@0: // The user cleared the value. michael@0: if (aElement.value != "") { michael@0: aElement.value = ""; michael@0: changed = true; michael@0: } michael@0: } else if (data.button == 0) { michael@0: // Commit the new value. michael@0: if (aElement.value != data[type]) { michael@0: aElement.value = data[type + "0"]; michael@0: changed = true; michael@0: } michael@0: } michael@0: // Else the user canceled the input. michael@0: michael@0: if (changed) michael@0: this.fireOnChange(aElement); michael@0: }).bind(this)); michael@0: }, michael@0: michael@0: hasInputWidget: function(aElement) { michael@0: if (!aElement instanceof HTMLInputElement) michael@0: return false; michael@0: michael@0: let type = aElement.getAttribute('type'); michael@0: if (type == "date" || type == "datetime" || type == "datetime-local" || michael@0: type == "week" || type == "month" || type == "time") { michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: }, michael@0: michael@0: fireOnChange: function(aElement) { michael@0: let evt = aElement.ownerDocument.createEvent("Events"); michael@0: evt.initEvent("change", true, true, aElement.defaultView, 0, michael@0: false, false, michael@0: false, false, null); michael@0: setTimeout(function() { michael@0: aElement.dispatchEvent(evt); michael@0: }, 0); michael@0: } michael@0: };