1 |
|
2 |
|
3 | import { Signal } from '@lumino/signaling';
|
4 | import { Widget } from '@lumino/widgets';
|
5 |
|
6 |
|
7 |
|
8 | export 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 |
|
15 |
|
16 |
|
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 |
|
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 |
|
44 |
|
45 | get valueChanged() {
|
46 | return this._valueChanged;
|
47 | }
|
48 | |
49 |
|
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 |
|
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 |
|
\ | No newline at end of file |