1 |
|
2 |
|
3 |
|
4 | import { IChangedArgs } from '@jupyterlab/coreutils';
|
5 | import { ISignal, Signal } from '@lumino/signaling';
|
6 | import { Widget } from '@lumino/widgets';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export class Switch extends Widget {
|
12 | constructor() {
|
13 | super();
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | this._button.className = 'jp-switch';
|
20 | this._button.setAttribute('role', 'switch');
|
21 |
|
22 | this._label.className = 'jp-switch-label';
|
23 |
|
24 | const track = document.createElement('div');
|
25 | track.className = 'jp-switch-track';
|
26 | track.setAttribute('aria-hidden', 'true');
|
27 |
|
28 | this._button.appendChild(this._label);
|
29 | this._button.appendChild(track);
|
30 | this.node.appendChild(this._button);
|
31 | }
|
32 |
|
33 | |
34 |
|
35 |
|
36 | get value(): boolean {
|
37 | return this._value;
|
38 | }
|
39 | set value(newValue: boolean) {
|
40 | const oldValue = this._value;
|
41 | if (oldValue === newValue) {
|
42 | return;
|
43 | }
|
44 | this._button.setAttribute('aria-checked', newValue.toString());
|
45 | this._value = newValue;
|
46 | this._valueChanged.emit({ name: 'value', oldValue, newValue });
|
47 | }
|
48 |
|
49 | |
50 |
|
51 |
|
52 | get valueChanged(): ISignal<this, IChangedArgs<boolean, boolean, 'value'>> {
|
53 | return this._valueChanged;
|
54 | }
|
55 |
|
56 | |
57 |
|
58 |
|
59 | get label(): string {
|
60 | return this._label.textContent ?? '';
|
61 | }
|
62 | set label(x: string) {
|
63 | this._label.textContent = x;
|
64 | }
|
65 |
|
66 | |
67 |
|
68 |
|
69 | get caption(): string {
|
70 | return this._button.title;
|
71 | }
|
72 | set caption(x: string) {
|
73 | this._button.title = x;
|
74 | this._label.title = x;
|
75 | }
|
76 |
|
77 | handleEvent(event: Event): void {
|
78 | switch (event.type) {
|
79 | case 'click':
|
80 | this.value = !this.value;
|
81 | break;
|
82 | default:
|
83 | break;
|
84 | }
|
85 | }
|
86 |
|
87 | protected onAfterAttach(): void {
|
88 | this._button.addEventListener('click', this);
|
89 | }
|
90 |
|
91 | protected onBeforeDetach(): void {
|
92 | this._button.removeEventListener('click', this);
|
93 | }
|
94 |
|
95 | private _button = document.createElement('button');
|
96 | private _label = document.createElement('label');
|
97 |
|
98 | private _value: boolean;
|
99 | private _valueChanged = new Signal<
|
100 | this,
|
101 | IChangedArgs<boolean, boolean, 'value'>
|
102 | >(this);
|
103 | }
|