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 22, 2018, 6:18 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
« no previous file with comments | « css/io-toggle.scss ('k') | package.json » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* globals module, require */
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;
41 }
42
43 onclick(event)
44 {
45 if (!this.disabled)
46 {
47 this.checked = !this.checked;
48 this.dispatchEvent(new CustomEvent("change", {
49 bubbles: true,
50 cancelable: true,
51 detail: this.checked
52 }));
53 }
54 }
55
56 render()
57 {
58 this.html`
59 <button
60 role="checkbox"
61 data-action="${this.action}"
62 aria-checked="${this.checked}"
63 aria-disabled="${this.disabled}"
64 />`;
65 }
66 }
67
68 IOToggle.define("io-toggle");
69
70 function asBoolean(value)
71 {
72 return typeof value === "string" ? JSON.parse(value) : !!value;
73 }
74
75 function booleanAttribute(name, value)
76 {
77 if (asBoolean(value))
78 {
79 this.setAttribute(name, "true");
80 }
81 else
82 {
83 this.removeAttribute(name);
84 }
85 }
OLDNEW
« no previous file with comments | « css/io-toggle.scss ('k') | package.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld