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

Unified Diff: js/io-toggle.js

Issue 29730644: Issue 6514 - IOToggle Custom Element (Closed)
Patch Set: Created March 23, 2018, 10:25 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « css/io-toggle.scss ('k') | package.json » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: js/io-toggle.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/js/io-toggle.js
@@ -0,0 +1,85 @@
+/* globals module, require */
+
+"use strict";
+
+const IOElement = require("./io-element");
+
+class IOToggle extends IOElement
+{
+ // action, checked, and disabled should be reflected down the button
+ static get observedAttributes()
+ {
+ return ["action", "checked", "disabled"];
+ }
+
+ created()
+ {
+ this.addEventListener("click", this);
+ this.render();
+ }
+
+ get checked()
+ {
+ return this.hasAttribute("checked");
+ }
+
+ set checked(value)
+ {
+ booleanAttribute.call(this, "checked", value);
+ this.render();
+ }
+
+ get disabled()
+ {
+ return this.hasAttribute("disabled");
+ }
+
+ set disabled(value)
+ {
+ booleanAttribute.call(this, "disabled", value);
+ this.firstElementChild.disabled = this._disabled;
+ }
+
+ onclick(event)
+ {
+ if (!this.disabled)
+ {
+ this.checked = !this.checked;
+ this.dispatchEvent(new CustomEvent("change", {
+ bubbles: true,
+ cancelable: true,
+ detail: this.checked
+ }));
+ }
+ }
+
+ render()
+ {
+ this.html`
+ <button
+ role="checkbox"
+ data-action="${this.action}"
+ aria-checked="${this.checked}"
+ aria-disabled="${this.disabled}"
+ />`;
+ }
+}
+
+IOToggle.define("io-toggle");
+
+function asBoolean(value)
+{
+ return typeof value === "string" ? JSON.parse(value) : !!value;
+}
+
+function booleanAttribute(name, value)
+{
+ if (asBoolean(value))
+ {
+ this.setAttribute(name, "true");
+ }
+ else
+ {
+ this.removeAttribute(name);
+ }
+}
« 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