1 | function _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 |
|
15 | function _classCallCheck(instance, Constructor) {
|
16 | if (!(instance instanceof Constructor)) {
|
17 | throw new TypeError("Cannot call a class as a function");
|
18 | }
|
19 | }
|
20 |
|
21 | function _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 |
|
31 | function _createClass(Constructor, protoProps, staticProps) {
|
32 | if (protoProps) _defineProperties(Constructor.prototype, protoProps);
|
33 | if (staticProps) _defineProperties(Constructor, staticProps);
|
34 | return Constructor;
|
35 | }
|
36 |
|
37 | function _possibleConstructorReturn(self, call) {
|
38 | if (call && (_typeof(call) === "object" || typeof call === "function")) {
|
39 | return call;
|
40 | }
|
41 |
|
42 | return _assertThisInitialized(self);
|
43 | }
|
44 |
|
45 | function _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 |
|
53 | function _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 |
|
60 | function _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 |
|
75 | function _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 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | import settings from '../../globals/js/settings';
|
92 | import mixin from '../../globals/js/misc/mixin';
|
93 | import createComponent from '../../globals/js/mixins/create-component';
|
94 | import initComponentBySearch from '../../globals/js/mixins/init-component-by-search';
|
95 | import handles from '../../globals/js/mixins/handles';
|
96 | import on from '../../globals/js/misc/on';
|
97 | var stateChangeTypes = {
|
98 | true: 'true',
|
99 | false: 'false',
|
100 | mixed: 'mixed'
|
101 | };
|
102 |
|
103 | var Checkbox =
|
104 |
|
105 | function (_mixin) {
|
106 | _inherits(Checkbox, _mixin);
|
107 | |
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
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;
|
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;
|
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 |
|
179 |
|
180 |
|
181 |
|
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 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | }], [{
|
260 | key: "options",
|
261 |
|
262 | |
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
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 |
|
296 | export default Checkbox; |
\ | No newline at end of file |