summaryrefslogtreecommitdiffstats
path: root/mobile/android/chrome/content/InputWidgetHelper.js
blob: 9c753bd7bb66bc7fb9a8c1873ba7a62cb066ab2f (plain)
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
/* 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/. */
"use strict";

var InputWidgetHelper = {
  _uiBusy: false,

  handleEvent: function(aEvent) {
    this.handleClick(aEvent.target);
  },

  handleClick: function(aTarget) {
    // if we're busy looking at a InputWidget we want to eat any clicks that
    // come to us, but not to process them
    if (this._uiBusy || !this.hasInputWidget(aTarget) || this._isDisabledElement(aTarget))
      return;

    this._uiBusy = true;
    this.show(aTarget);
    this._uiBusy = false;
  },

  show: function(aElement) {
    let type = aElement.getAttribute('type');
    let p = new Prompt({
      window: aElement.ownerDocument.defaultView,
      title: Strings.browser.GetStringFromName("inputWidgetHelper." + aElement.getAttribute('type')),
      buttons: [
        Strings.browser.GetStringFromName("inputWidgetHelper.set"),
        Strings.browser.GetStringFromName("inputWidgetHelper.clear"),
        Strings.browser.GetStringFromName("inputWidgetHelper.cancel")
      ],
    }).addDatePicker({
      value: aElement.value,
      type: type,
      min: aElement.min,
      max: aElement.max,
    }).show((function(data) {
      let changed = false;
      if (data.button == -1) {
        // This type is not supported with this android version.
        return;
      }
      if (data.button == 1) {
        // The user cleared the value.
        if (aElement.value != "") {
          aElement.value = "";
          changed = true;
        }
      } else if (data.button == 0) {
        // Commit the new value.
        if (aElement.value != data[type]) {
          aElement.value = data[type + "0"];
          changed = true;
        }
      }
      // Else the user canceled the input.

      if (changed)
        this.fireOnChange(aElement);
    }).bind(this));
  },

  hasInputWidget: function(aElement) {
    if (!aElement instanceof HTMLInputElement)
      return false;

    let type = aElement.getAttribute('type');
    if (type == "date" || type == "datetime" || type == "datetime-local" ||
        type == "week" || type == "month" || type == "time") {
      return true;
    }

    return false;
  },

  fireOnChange: function(aElement) {
    let evt = aElement.ownerDocument.createEvent("Events");
    evt.initEvent("change", true, true, aElement.defaultView, 0,
                  false, false,
                  false, false, null);
    setTimeout(function() {
      aElement.dispatchEvent(evt);
    }, 0);
  },

  _isDisabledElement : function(aElement) {
    let currentElement = aElement;
    while (currentElement) {
      if (currentElement.disabled)
	return true;

      currentElement = currentElement.parentElement;
    }
    return false;
  }
};