Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: js/io-toggle.js

Issue 29730644: Issue 6514 - IOToggle Custom Element (Closed)
Patch Set: Created March 26, 2018, 2:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /* globals module, require */
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
3 "use strict";
4
5 const IOElement = require("./io-element");
6
7 class IOToggle extends IOElement
8 {
9 // action, checked, and disabled should be reflected down the button
10 static get observedAttributes()
11 {
12 return ["action", "checked", "disabled"];
13 }
14
15 created()
16 {
17 this.addEventListener("click", this);
18 this.render();
19 }
20
21 get checked()
22 {
23 return this.hasAttribute("checked");
24 }
25
26 set checked(value)
27 {
28 booleanAttribute.call(this, "checked", value);
29 this.render();
30 }
31
32 get disabled()
33 {
34 return this.hasAttribute("disabled");
35 }
36
37 set disabled(value)
38 {
39 booleanAttribute.call(this, "disabled", value);
40 this.firstElementChild.disabled = this._disabled;
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 }
42
43 onclick(event)
44 {
45 if (!this.disabled)
46 {
47 this.checked = !this.checked;
48 if (this.ownerDocument.activeElement !== this.firstElementChild)
49 {
50 this.firstElementChild.focus();
51 }
52 this.dispatchEvent(new CustomEvent("change", {
53 bubbles: true,
54 cancelable: true,
55 detail: this.checked
56 }));
57 }
58 }
59
60 render()
61 {
62 this.html`
63 <button
64 role="checkbox"
65 data-action="${this.action}"
66 aria-checked="${this.checked}"
67 aria-disabled="${this.disabled}"
68 />`;
69 }
70 }
71
72 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 }
OLDNEW

Powered by Google App Engine
This is Rietveld