UNPKG

10.2 kBJavaScriptView Raw
1function _typeof(obj) {
2 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
3 _typeof = function _typeof(obj) {
4 return typeof obj;
5 };
6 } else {
7 _typeof = function _typeof(obj) {
8 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
9 };
10 }
11
12 return _typeof(obj);
13}
14
15function _classCallCheck(instance, Constructor) {
16 if (!(instance instanceof Constructor)) {
17 throw new TypeError("Cannot call a class as a function");
18 }
19}
20
21function _defineProperties(target, props) {
22 for (var i = 0; i < props.length; i++) {
23 var descriptor = props[i];
24 descriptor.enumerable = descriptor.enumerable || false;
25 descriptor.configurable = true;
26 if ("value" in descriptor) descriptor.writable = true;
27 Object.defineProperty(target, descriptor.key, descriptor);
28 }
29}
30
31function _createClass(Constructor, protoProps, staticProps) {
32 if (protoProps) _defineProperties(Constructor.prototype, protoProps);
33 if (staticProps) _defineProperties(Constructor, staticProps);
34 return Constructor;
35}
36
37function _possibleConstructorReturn(self, call) {
38 if (call && (_typeof(call) === "object" || typeof call === "function")) {
39 return call;
40 }
41
42 return _assertThisInitialized(self);
43}
44
45function _assertThisInitialized(self) {
46 if (self === void 0) {
47 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
48 }
49
50 return self;
51}
52
53function _getPrototypeOf(o) {
54 _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
55 return o.__proto__ || Object.getPrototypeOf(o);
56 };
57 return _getPrototypeOf(o);
58}
59
60function _inherits(subClass, superClass) {
61 if (typeof superClass !== "function" && superClass !== null) {
62 throw new TypeError("Super expression must either be null or a function");
63 }
64
65 subClass.prototype = Object.create(superClass && superClass.prototype, {
66 constructor: {
67 value: subClass,
68 writable: true,
69 configurable: true
70 }
71 });
72 if (superClass) _setPrototypeOf(subClass, superClass);
73}
74
75function _setPrototypeOf(o, p) {
76 _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
77 o.__proto__ = p;
78 return o;
79 };
80
81 return _setPrototypeOf(o, p);
82}
83/**
84 * Copyright IBM Corp. 2016, 2018
85 *
86 * This source code is licensed under the Apache-2.0 license found in the
87 * LICENSE file in the root directory of this source tree.
88 */
89
90
91import settings from '../../globals/js/settings';
92import mixin from '../../globals/js/misc/mixin';
93import createComponent from '../../globals/js/mixins/create-component';
94import initComponentBySearch from '../../globals/js/mixins/init-component-by-search';
95import handles from '../../globals/js/mixins/handles';
96import on from '../../globals/js/misc/on';
97var stateChangeTypes = {
98 true: 'true',
99 false: 'false',
100 mixed: 'mixed'
101};
102
103var Checkbox =
104/*#__PURE__*/
105function (_mixin) {
106 _inherits(Checkbox, _mixin);
107 /**
108 * Checkbox UI.
109 * @extends CreateComponent
110 * @extends InitComponentBySearch
111 * @extends Handles
112 * @param {HTMLElement} element The element working as a checkbox UI.
113 */
114
115
116 function Checkbox(element, options) {
117 var _this;
118
119 _classCallCheck(this, Checkbox);
120
121 _this = _possibleConstructorReturn(this, _getPrototypeOf(Checkbox).call(this, element, options));
122
123 _this.manage(on(_this.element, 'click', function (event) {
124 _this._handleClick(event);
125 }));
126
127 _this.manage(on(_this.element, 'focus', function (event) {
128 _this._handleFocus(event);
129 }));
130
131 _this.manage(on(_this.element, 'blur', function (event) {
132 _this._handleBlur(event);
133 }));
134
135 _this._indeterminateCheckbox();
136
137 _this._initCheckbox();
138
139 return _this;
140 }
141
142 _createClass(Checkbox, [{
143 key: "_handleClick",
144 value: function _handleClick() {
145 if (this.element.checked === true) {
146 this.element.setAttribute('checked', '');
147 this.element.setAttribute('aria-checked', 'true');
148 this.element.checked = true; // nested checkboxes inside labels
149
150 if (this.element.parentElement.classList.contains(this.options.classLabel)) {
151 this.element.parentElement.setAttribute(this.options.attribContainedCheckboxState, 'true');
152 }
153 } else if (this.element.checked === false) {
154 this.element.removeAttribute('checked');
155 this.element.setAttribute('aria-checked', 'false');
156 this.element.checked = false; // nested checkboxes inside labels
157
158 if (this.element.parentElement.classList.contains(this.options.classLabel)) {
159 this.element.parentElement.setAttribute(this.options.attribContainedCheckboxState, 'false');
160 }
161 }
162 }
163 }, {
164 key: "_handleFocus",
165 value: function _handleFocus() {
166 if (this.element.parentElement.classList.contains(this.options.classLabel)) {
167 this.element.parentElement.classList.add(this.options.classLabelFocused);
168 }
169 }
170 }, {
171 key: "_handleBlur",
172 value: function _handleBlur() {
173 if (this.element.parentElement.classList.contains(this.options.classLabel)) {
174 this.element.parentElement.classList.remove(this.options.classLabelFocused);
175 }
176 }
177 /**
178 * Sets the new checkbox state.
179 * @param {boolean|string} [state]
180 * The new checkbox state to set. `mixed` to put checkbox in indeterminate state.
181 * If omitted, this method simply makes the style reflect `aria-checked` attribute.
182 */
183
184 }, {
185 key: "setState",
186 value: function setState(state) {
187 if (state === undefined || stateChangeTypes[state] === undefined) {
188 throw new TypeError('setState expects a value of true, false or mixed.');
189 }
190
191 this.element.setAttribute('aria-checked', state);
192 this.element.indeterminate = state === stateChangeTypes.mixed;
193 this.element.checked = state === stateChangeTypes.true;
194 var container = this.element.closest(this.options.selectorContainedCheckboxState);
195
196 if (container) {
197 container.setAttribute(this.options.attribContainedCheckboxState, state);
198 }
199 }
200 }, {
201 key: "setDisabled",
202 value: function setDisabled(value) {
203 if (value === undefined) {
204 throw new TypeError('setDisabled expects a boolean value of true or false');
205 }
206
207 if (value === true) {
208 this.element.setAttribute('disabled', true);
209 } else if (value === false) {
210 this.element.removeAttribute('disabled');
211 }
212
213 var container = this.element.closest(this.options.selectorContainedCheckboxDisabled);
214
215 if (container) {
216 container.setAttribute(this.options.attribContainedCheckboxDisabled, value);
217 }
218 }
219 }, {
220 key: "_indeterminateCheckbox",
221 value: function _indeterminateCheckbox() {
222 if (this.element.getAttribute('aria-checked') === 'mixed') {
223 this.element.indeterminate = true;
224 }
225
226 if (this.element.indeterminate === true) {
227 this.element.setAttribute('aria-checked', 'mixed');
228 }
229
230 if (this.element.parentElement.classList.contains(this.options.classLabel) && this.element.indeterminate === true) {
231 this.element.parentElement.setAttribute(this.options.attribContainedCheckboxState, 'mixed');
232 }
233 }
234 }, {
235 key: "_initCheckbox",
236 value: function _initCheckbox() {
237 if (this.element.checked === true) {
238 this.element.setAttribute('aria-checked', 'true');
239 }
240
241 if (this.element.parentElement.classList.contains(this.options.classLabel) && this.element.checked) {
242 this.element.parentElement.setAttribute(this.options.attribContainedCheckboxState, 'true');
243 }
244
245 if (this.element.parentElement.classList.contains(this.options.classLabel)) {
246 this.element.parentElement.setAttribute(this.options.attribContainedCheckboxDisabled, 'false');
247 }
248
249 if (this.element.parentElement.classList.contains(this.options.classLabel) && this.element.disabled) {
250 this.element.parentElement.setAttribute(this.options.attribContainedCheckboxDisabled, 'true');
251 }
252 }
253 /**
254 * The map associating DOM element and copy button UI instance.
255 * @member Checkbox.components
256 * @type {WeakMap}
257 */
258
259 }], [{
260 key: "options",
261
262 /**
263 * The component options.
264 * If `options` is specified in the constructor, {@linkcode Checkbox.create .create()}, or {@linkcode Checkbox.init .init()},
265 * properties in this object are overriden for the instance being create and how {@linkcode Checkbox.init .init()} works.
266 * @member Checkbox.options
267 * @type {Object}
268 * @property {string} selectorInit The data attribute to find copy button UIs.
269 * @property {string} selectorContainedCheckboxState The CSS selector to find a container of checkbox preserving checked state.
270 * @property {string} selectorContainedCheckboxDisabled
271 * The CSS selector to find a container of checkbox preserving disabled state.
272 * @property {string} classLabel The CSS class for the label.
273 * @property {string} classLabelFocused The CSS class for the focused label.
274 * @property {string} attribContainedCheckboxState The attribute name for the checked state of contained checkbox.
275 * @property {string} attribContainedCheckboxDisabled The attribute name for the disabled state of contained checkbox.
276 */
277 get: function get() {
278 var prefix = settings.prefix;
279 return {
280 selectorInit: ".".concat(prefix, "--checkbox"),
281 selectorContainedCheckboxState: '[data-contained-checkbox-state]',
282 selectorContainedCheckboxDisabled: '[data-contained-checkbox-disabled]',
283 classLabel: "".concat(prefix, "--checkbox-label"),
284 classLabelFocused: "".concat(prefix, "--checkbox-label__focus"),
285 attribContainedCheckboxState: 'data-contained-checkbox-state',
286 attribContainedCheckboxDisabled: 'data-contained-checkbox-disabled'
287 };
288 }
289 }]);
290
291 Checkbox.components = new WeakMap();
292 Checkbox.stateChangeTypes = stateChangeTypes;
293 return Checkbox;
294}(mixin(createComponent, initComponentBySearch, handles));
295
296export default Checkbox;
\No newline at end of file