forked from ibm-js/deliteful
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Checkbox.js
62 lines (55 loc) · 1.95 KB
/
Checkbox.js
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
/** @module deliteful/Checkbox */
define([
"dcl/dcl",
"delite/register",
"delite/FormWidget",
"./Toggle",
"delite/handlebars!./Checkbox/Checkbox.html",
"delite/theme!./Checkbox/themes/{{theme}}/Checkbox.css"
], function (dcl, register, FormWidget, Toggle, template) {
var labelClickHandler;
/**
* A 2-state checkbox widget similar to an HTML5 input type="checkbox" element.
* @example
* <d-checkbox checked="true"></d-checkbox>
* @class module:deliteful/Checkbox
* @augments module:delite/FormWidget
* @augments module:deliteful/Toggle
*/
return register("d-checkbox", [HTMLElement, FormWidget, Toggle], /** @lends module:deliteful/Checkbox# */ {
/**
* The component css base class.
* @member {string}
* @default "d-checkbox"
*/
baseClass: "d-checkbox",
template: template,
postRender: function () {
this._lbl4 = null;
this.on("click", this._inputClickHandler.bind(this), this.focusNode);
this.on("change", this._inputClickHandler.bind(this), this.focusNode);
},
_inputClickHandler: function () {
this.checked = this.focusNode.checked;
},
attachedCallback: function () {
// The fact that deliteful/Checkbox is not an HTMLInputElement seems not being compatible with the default
// "<label for" behavior of the browser. So it needs to explicitly listen to click on associated
// <label for=...> elements.
if (!labelClickHandler) {
// set a global listener that listens to click events on label elt
labelClickHandler = function (e) {
var forId;
if (/label/i.test(e.target.tagName) && (forId = e.target.getAttribute("for"))) {
var elt = document.getElementById(forId);
if (elt && elt.render && elt._lbl4 !== undefined) { //_lbl4: to check it's a checkbox
// call click() on the input instead of this.toggle() to get the 'change' event for free
elt.focusNode.click();
}
}
};
this.ownerDocument.addEventListener("click", labelClickHandler);
}
}
});
});