Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* globals module, require */ | 1 /* |
Thomas Greiner
2018/04/25 17:29:14
I noticed that none of the new files in this revie
Thomas Greiner
2018/04/25 17:29:14
Detail: You don't use `module` in this module.
a.giammarchi
2018/04/26 10:44:52
To be honest, I feel like we should have module an
a.giammarchi
2018/04/26 10:44:52
Will do.
Thomas Greiner
2018/04/26 17:00:52
Wouldn't .eslintrc.json be a more suitable place t
a.giammarchi
2018/04/27 10:59:04
absolutely!
| |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
3 * Copyright (C) 2006-present eyeo GmbH | |
4 * | |
5 * Adblock Plus is free software: you can redistribute it and/or modify | |
6 * it under the terms of the GNU General Public License version 3 as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * Adblock Plus is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License | |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
16 */ | |
2 | 17 |
3 "use strict"; | 18 "use strict"; |
4 | 19 |
5 const IOElement = require("./io-element"); | 20 const IOElement = require("./io-element"); |
21 const {boolean} = IOElement.utils; | |
6 | 22 |
7 class IOToggle extends IOElement | 23 class IOToggle extends IOElement |
8 { | 24 { |
9 // action, checked, and disabled should be reflected down the button | 25 // action, checked, and disabled should be reflected down the button |
10 static get observedAttributes() | 26 static get observedAttributes() |
11 { | 27 { |
12 return ["action", "checked", "disabled"]; | 28 return ["action", "checked", "disabled"]; |
13 } | 29 } |
14 | 30 |
15 created() | 31 created() |
16 { | 32 { |
17 this.addEventListener("click", this); | 33 this.addEventListener("click", this); |
18 this.render(); | 34 this.render(); |
19 } | 35 } |
20 | 36 |
21 get checked() | 37 get checked() |
22 { | 38 { |
23 return this.hasAttribute("checked"); | 39 return this.hasAttribute("checked"); |
24 } | 40 } |
25 | 41 |
26 set checked(value) | 42 set checked(value) |
27 { | 43 { |
28 booleanAttribute.call(this, "checked", value); | 44 boolean.attribute(this, "checked", value); |
29 this.render(); | 45 this.render(); |
30 } | 46 } |
31 | 47 |
32 get disabled() | 48 get disabled() |
33 { | 49 { |
34 return this.hasAttribute("disabled"); | 50 return this.hasAttribute("disabled"); |
35 } | 51 } |
36 | 52 |
37 set disabled(value) | 53 set disabled(value) |
38 { | 54 { |
39 booleanAttribute.call(this, "disabled", value); | 55 boolean.attribute(this, "disabled", value); |
40 this.firstElementChild.disabled = this._disabled; | 56 this.render(); |
Thomas Greiner
2018/04/25 17:29:14
Why don't you call `this.render()` here?
a.giammarchi
2018/04/26 10:44:52
it's not needed, the disabled is the native DOM ac
Thomas Greiner
2018/04/26 17:00:52
I mean instead of setting `this.firstElementChild.
a.giammarchi
2018/04/27 10:59:04
I'll try that. The issue here is that disabled is
Thomas Greiner
2018/05/02 11:53:28
Thanks, looking good!
| |
41 } | 57 } |
42 | 58 |
43 onclick(event) | 59 onclick(event) |
44 { | 60 { |
45 if (!this.disabled) | 61 if (!this.disabled) |
46 { | 62 { |
47 this.checked = !this.checked; | 63 this.checked = !this.checked; |
48 if (this.ownerDocument.activeElement !== this.firstElementChild) | 64 if (this.ownerDocument.activeElement !== this.child) |
49 { | 65 { |
50 this.firstElementChild.focus(); | 66 this.child.focus(); |
51 } | 67 } |
52 this.dispatchEvent(new CustomEvent("change", { | 68 this.dispatchEvent(new CustomEvent("change", { |
53 bubbles: true, | 69 bubbles: true, |
54 cancelable: true, | 70 cancelable: true, |
55 detail: this.checked | 71 detail: this.checked |
56 })); | 72 })); |
57 } | 73 } |
58 } | 74 } |
59 | 75 |
60 render() | 76 render() |
61 { | 77 { |
62 this.html` | 78 this.html` |
63 <button | 79 <button |
64 role="checkbox" | 80 role="checkbox" |
81 disabled="${this.disabled}" | |
65 data-action="${this.action}" | 82 data-action="${this.action}" |
66 aria-checked="${this.checked}" | 83 aria-checked="${this.checked}" |
67 aria-disabled="${this.disabled}" | 84 aria-disabled="${this.disabled}" |
68 />`; | 85 />`; |
69 } | 86 } |
70 } | 87 } |
71 | 88 |
72 IOToggle.define("io-toggle"); | 89 IOToggle.define("io-toggle"); |
73 | |
74 function asBoolean(value) | |
Thomas Greiner
2018/04/25 17:29:14
Suggestion: "asBoolean" is quite close to "isBoole
a.giammarchi
2018/04/26 10:44:51
will do
| |
75 { | |
76 return typeof value === "string" ? JSON.parse(value) : !!value; | |
Thomas Greiner
2018/04/25 17:29:14
It's a bit unexpected that this function accepts e
a.giammarchi
2018/04/26 10:44:52
only strings can be JSON, and in this case we are
Thomas Greiner
2018/04/26 17:00:52
I can follow your line or argument right until the
a.giammarchi
2018/04/27 10:59:04
will do.
| |
77 } | |
78 | |
79 function booleanAttribute(name, value) | |
Thomas Greiner
2018/04/25 17:29:14
Detail: This function looks useful also for other
a.giammarchi
2018/04/26 10:44:51
Usually the way I go is: as soon as more than a co
Thomas Greiner
2018/04/26 17:00:52
That's a fair point. And it should be fine as long
a.giammarchi
2018/04/27 10:59:05
makes sense.
| |
80 { | |
81 if (asBoolean(value)) | |
82 { | |
83 this.setAttribute(name, "true"); | |
84 } | |
85 else | |
86 { | |
87 this.removeAttribute(name); | |
88 } | |
89 } | |
LEFT | RIGHT |