UNPKG

2.52 kBJavaScriptView Raw
1// Copyright (c) Jupyter Development Team.
2// Distributed under the terms of the Modified BSD License.
3import { Signal } from '@lumino/signaling';
4import { Widget } from '@lumino/widgets';
5/**
6 * A Switch widget
7 */
8export class Switch extends Widget {
9 constructor() {
10 super();
11 this._button = document.createElement('button');
12 this._label = document.createElement('label');
13 this._valueChanged = new Signal(this);
14 // switch accessibility refs:
15 // https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles/Switch_role
16 // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Accessibility_concerns
17 this._button.className = 'jp-switch';
18 this._button.setAttribute('role', 'switch');
19 this._label.className = 'jp-switch-label';
20 const track = document.createElement('div');
21 track.className = 'jp-switch-track';
22 track.setAttribute('aria-hidden', 'true');
23 this._button.appendChild(this._label);
24 this._button.appendChild(track);
25 this.node.appendChild(this._button);
26 }
27 /**
28 * The value of the switch.
29 */
30 get value() {
31 return this._value;
32 }
33 set value(newValue) {
34 const oldValue = this._value;
35 if (oldValue === newValue) {
36 return;
37 }
38 this._button.setAttribute('aria-checked', newValue.toString());
39 this._value = newValue;
40 this._valueChanged.emit({ name: 'value', oldValue, newValue });
41 }
42 /**
43 * A signal emitted when the value changes.
44 */
45 get valueChanged() {
46 return this._valueChanged;
47 }
48 /**
49 * The visible label of the switch.
50 */
51 get label() {
52 var _a;
53 return (_a = this._label.textContent) !== null && _a !== void 0 ? _a : '';
54 }
55 set label(x) {
56 this._label.textContent = x;
57 }
58 /**
59 * The caption (title) of the switch.
60 */
61 get caption() {
62 return this._button.title;
63 }
64 set caption(x) {
65 this._button.title = x;
66 this._label.title = x;
67 }
68 handleEvent(event) {
69 switch (event.type) {
70 case 'click':
71 this.value = !this.value;
72 break;
73 default:
74 break;
75 }
76 }
77 onAfterAttach() {
78 this._button.addEventListener('click', this);
79 }
80 onBeforeDetach() {
81 this._button.removeEventListener('click', this);
82 }
83}
84//# sourceMappingURL=switch.js.map
\No newline at end of file