mobile/android/chrome/content/InputWidgetHelper.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/chrome/content/InputWidgetHelper.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,85 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     1.6 + * You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +var InputWidgetHelper = {
    1.10 +  _uiBusy: false,
    1.11 +
    1.12 +  handleEvent: function(aEvent) {
    1.13 +    this.handleClick(aEvent.target);
    1.14 +  },
    1.15 +
    1.16 +  handleClick: function(aTarget) {
    1.17 +    // if we're busy looking at a InputWidget we want to eat any clicks that
    1.18 +    // come to us, but not to process them
    1.19 +    if (this._uiBusy || !this.hasInputWidget(aTarget))
    1.20 +      return;
    1.21 +
    1.22 +    this._uiBusy = true;
    1.23 +    this.show(aTarget);
    1.24 +    this._uiBusy = false;
    1.25 +  },
    1.26 +
    1.27 +  show: function(aElement) {
    1.28 +    let type = aElement.getAttribute('type');
    1.29 +    let p = new Prompt({
    1.30 +      window: aElement.ownerDocument.defaultView,
    1.31 +      title: Strings.browser.GetStringFromName("inputWidgetHelper." + aElement.getAttribute('type')),
    1.32 +      buttons: [
    1.33 +        Strings.browser.GetStringFromName("inputWidgetHelper.set"),
    1.34 +        Strings.browser.GetStringFromName("inputWidgetHelper.clear"),
    1.35 +        Strings.browser.GetStringFromName("inputWidgetHelper.cancel")
    1.36 +      ],
    1.37 +    }).addDatePicker({
    1.38 +      value: aElement.value,
    1.39 +      type: type,
    1.40 +    }).show((function(data) {
    1.41 +      let changed = false;
    1.42 +      if (data.button == -1) {
    1.43 +        // This type is not supported with this android version.
    1.44 +        return;
    1.45 +      }
    1.46 +      if (data.button == 1) {
    1.47 +        // The user cleared the value.
    1.48 +        if (aElement.value != "") {
    1.49 +          aElement.value = "";
    1.50 +          changed = true;
    1.51 +        }
    1.52 +      } else if (data.button == 0) {
    1.53 +        // Commit the new value.
    1.54 +        if (aElement.value != data[type]) {
    1.55 +          aElement.value = data[type + "0"];
    1.56 +          changed = true;
    1.57 +        }
    1.58 +      }
    1.59 +      // Else the user canceled the input.
    1.60 +
    1.61 +      if (changed)
    1.62 +        this.fireOnChange(aElement);
    1.63 +    }).bind(this));
    1.64 +  },
    1.65 +
    1.66 +  hasInputWidget: function(aElement) {
    1.67 +    if (!aElement instanceof HTMLInputElement)
    1.68 +      return false;
    1.69 +
    1.70 +    let type = aElement.getAttribute('type');
    1.71 +    if (type == "date" || type == "datetime" || type == "datetime-local" ||
    1.72 +        type == "week" || type == "month" || type == "time") {
    1.73 +      return true;
    1.74 +    }
    1.75 +
    1.76 +    return false;
    1.77 +  },
    1.78 +
    1.79 +  fireOnChange: function(aElement) {
    1.80 +    let evt = aElement.ownerDocument.createEvent("Events");
    1.81 +    evt.initEvent("change", true, true, aElement.defaultView, 0,
    1.82 +                  false, false,
    1.83 +                  false, false, null);
    1.84 +    setTimeout(function() {
    1.85 +      aElement.dispatchEvent(evt);
    1.86 +    }, 0);
    1.87 +  }
    1.88 +};

mercurial