-
Notifications
You must be signed in to change notification settings - Fork 37
/
Switch.ts
214 lines (180 loc) · 5.85 KB
/
Switch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright (c) 2010, 2023 BSI Business Systems Integration AG
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
import {Device, EnumObject, HtmlComponent, InitModelOf, KeyStrokeContext, objects, scout, strings, SwitchEventMap, SwitchModel, SwitchNavigationKeyStroke, SwitchToggleKeyStroke, tooltips, Widget} from '../index';
export class Switch extends Widget implements SwitchModel {
declare model: SwitchModel;
declare eventMap: SwitchEventMap;
declare self: Switch;
static DisplayStyle = {
DEFAULT: 'default',
SLIDER: 'slider'
} as const;
activated: boolean;
label: string;
labelHtmlEnabled: boolean;
labelVisible: boolean;
tooltipText: string;
iconVisible: boolean;
displayStyle: SwitchDisplayStyle;
tabbable: boolean;
$label: JQuery;
$button: JQuery;
constructor() {
super();
this.activated = false;
this.label = null;
this.labelHtmlEnabled = false;
this.labelVisible = null;
this.tooltipText = null;
this.iconVisible = false;
this.displayStyle = Switch.DisplayStyle.DEFAULT;
this.tabbable = false;
this.$label = null;
this.$button = null;
}
protected override _init(model: InitModelOf<this>) {
super._init(model);
this.resolveTextKeys(['label', 'tooltipText']);
}
protected override _render() {
this.$container = this.$parent.appendDiv('switch')
.on('mousedown', this._onSwitchMouseDown.bind(this));
this.htmlComp = HtmlComponent.install(this.$container, this.session);
this.$label = this.$container.appendDiv('switch-label');
this.$button = this.$container.appendDiv('switch-button');
}
protected override _renderProperties() {
super._renderProperties();
this._renderActivated();
this._renderLabel();
this._renderTooltipText();
this._renderIconVisible();
this._renderDisplayStyle();
this._renderTabbable();
}
protected override _remove() {
this.$label = null;
this.$button = null;
super._remove();
}
protected override _renderEnabled() {
super._renderEnabled();
if (this.rendered) {
this._renderTabbable();
}
}
protected override _createKeyStrokeContext(): KeyStrokeContext {
return new KeyStrokeContext();
}
protected override _initKeyStrokeContext() {
super._initKeyStrokeContext();
this.registerKeyStrokes([
new SwitchToggleKeyStroke(this),
new SwitchNavigationKeyStroke(this)
]);
}
setActivated(activated: boolean) {
this.setProperty('activated', activated);
}
protected _renderActivated() {
this.$button.toggleClass('activated', this.activated);
}
setLabel(label: string) {
this.setProperty('label', label);
}
protected _renderLabel() {
if (this.labelHtmlEnabled) {
this.$label.html(this.label);
} else {
this.$label.text(this.label);
}
this._renderLabelVisible();
}
setLabelHtmlEnabled(labelHtmlEnabled: boolean) {
this.setProperty('labelHtmlEnabled', labelHtmlEnabled);
}
protected _renderLabelHtmlEnabled() {
this._renderLabel();
}
setLabelVisible(labelVisible: boolean) {
this.setProperty('labelVisible', labelVisible);
}
protected _renderLabelVisible() {
let labelVisible = objects.isNullOrUndefined(this.labelVisible) ? strings.hasText(this.label) : !!this.labelVisible;
this.$label.setVisible(labelVisible);
this.invalidateLayoutTree();
}
setTooltipText(tooltipText: string) {
this.setProperty('tooltipText', tooltipText);
}
protected _renderTooltipText() {
if (strings.hasText(this.tooltipText)) {
tooltips.install(this.$container, {
parent: this,
text: this.tooltipText,
$anchor: this.$button
});
} else {
tooltips.uninstall(this.$container);
}
}
setIconVisible(iconVisible: boolean) {
this.setProperty('iconVisible', iconVisible);
}
protected _renderIconVisible() {
this.$button.toggleClass('icon-visible', !!this.iconVisible);
}
setDisplayStyle(displayStyle: SwitchDisplayStyle) {
this.setProperty('displayStyle', displayStyle);
}
protected _renderDisplayStyle() {
this.$container.toggleClass('style-default', this.displayStyle === Switch.DisplayStyle.DEFAULT);
this.$container.toggleClass('style-slider', this.displayStyle === Switch.DisplayStyle.SLIDER);
this.invalidateLayoutTree();
}
setTabbable(tabbable: boolean) {
this.setProperty('tabbable', tabbable);
}
protected _renderTabbable() {
let tabbable = this.tabbable && this.enabledComputed && !Device.get().supportsOnlyTouch();
this.$container.setTabbable(tabbable);
this.$container.toggleClass('unfocusable', tabbable);
}
protected _onSwitchMouseDown(event: JQuery.MouseDownEvent) {
if (event.button !== 0) { // 0 = main button
return;
}
this.toggleSwitch(event);
}
/**
* @param originalEvent original event that caused the toggle
* @param newValue if set, the `activated` property is set to this value.
* Otherwise, it is set to opposite of the current value.
*/
toggleSwitch(originalEvent?: JQuery.Event, activated?: boolean) {
if (!this.enabledComputed) {
return;
}
let oldValue = this.activated;
let newValue = scout.nvl(activated, !this.activated);
let event = this.trigger('switch', {
originalEvent: originalEvent,
oldValue: oldValue,
newValue: newValue
});
if (!event.defaultPrevented) {
this.setActivated(newValue);
}
// When the action is clicked the user wants to execute the action and not see the tooltip -> cancel the task
// If it is already displayed it will stay
this.$container && tooltips.cancel(this.$container);
}
}
export type SwitchDisplayStyle = EnumObject<typeof Switch.DisplayStyle>;