-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
widget.ts
241 lines (213 loc) · 9.4 KB
/
widget.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/********************************************************************************
* Copyright (C) 2017 TypeFox and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the Eclipse
* Public License v. 2.0 are satisfied: GNU General Public License, version 2
* with the GNU Classpath Exception which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
********************************************************************************/
// tslint:disable:no-any
import { injectable, decorate, unmanaged } from 'inversify';
import { Widget } from '@phosphor/widgets';
import { Message } from '@phosphor/messaging';
import { Emitter, Event, Disposable, DisposableCollection, MaybePromise } from '../../common';
import { KeyCode, KeysOrKeyCodes } from '../keyboard/keys';
import PerfectScrollbar from 'perfect-scrollbar';
decorate(injectable(), Widget);
decorate(unmanaged(), Widget, 0);
export * from '@phosphor/widgets';
export * from '@phosphor/messaging';
export const DISABLED_CLASS = 'theia-mod-disabled';
export const EXPANSION_TOGGLE_CLASS = 'theia-ExpansionToggle';
export const COLLAPSED_CLASS = 'theia-mod-collapsed';
export const SELECTED_CLASS = 'theia-mod-selected';
export const FOCUS_CLASS = 'theia-mod-focus';
@injectable()
export class BaseWidget extends Widget {
protected readonly onScrollYReachEndEmitter = new Emitter<void>();
readonly onScrollYReachEnd: Event<void> = this.onScrollYReachEndEmitter.event;
protected readonly onScrollUpEmitter = new Emitter<void>();
readonly onScrollUp: Event<void> = this.onScrollUpEmitter.event;
protected readonly toDispose = new DisposableCollection(
this.onScrollYReachEndEmitter,
this.onScrollUpEmitter
);
protected readonly toDisposeOnDetach = new DisposableCollection();
protected scrollBar?: PerfectScrollbar;
protected scrollOptions?: PerfectScrollbar.Options;
dispose(): void {
if (this.isDisposed) {
return;
}
super.dispose();
this.toDispose.dispose();
}
protected onCloseRequest(msg: Message): void {
super.onCloseRequest(msg);
this.dispose();
}
protected onBeforeAttach(msg: Message): void {
if (this.title.iconClass === '') {
this.title.iconClass = 'no-icon';
}
super.onBeforeAttach(msg);
}
protected onAfterDetach(msg: Message): void {
if (this.title.iconClass === 'no-icon') {
this.title.iconClass = '';
}
super.onAfterDetach(msg);
}
protected onBeforeDetach(msg: Message): void {
this.toDisposeOnDetach.dispose();
super.onBeforeDetach(msg);
}
protected onAfterAttach(msg: Message): void {
super.onAfterAttach(msg);
if (this.scrollOptions) {
(async () => {
const container = await this.getScrollContainer();
container.style.overflow = 'hidden';
this.scrollBar = new PerfectScrollbar(container, this.scrollOptions);
this.disableScrollBarFocus(container);
this.toDisposeOnDetach.push(addEventListener(container, <any>'ps-y-reach-end', () => { this.onScrollYReachEndEmitter.fire(undefined); }));
this.toDisposeOnDetach.push(addEventListener(container, <any>'ps-scroll-up', () => { this.onScrollUpEmitter.fire(undefined); }));
this.toDisposeOnDetach.push(Disposable.create(() => {
if (this.scrollBar) {
this.scrollBar.destroy();
this.scrollBar = undefined;
}
// tslint:disable-next-line:no-null-keyword
container.style.overflow = null;
}));
})();
}
}
protected getScrollContainer(): MaybePromise<HTMLElement> {
return this.node;
}
protected disableScrollBarFocus(scrollContainer: HTMLElement): void {
for (const thumbs of [scrollContainer.getElementsByClassName('ps__thumb-x'), scrollContainer.getElementsByClassName('ps__thumb-y')]) {
for (let i = 0; i < thumbs.length; i++) {
const element = thumbs.item(i);
if (element) {
element.removeAttribute('tabIndex');
}
}
}
}
protected onUpdateRequest(msg: Message): void {
super.onUpdateRequest(msg);
if (this.scrollBar) {
this.scrollBar.update();
}
}
protected addUpdateListener<K extends keyof HTMLElementEventMap>(element: HTMLElement, type: K, useCapture?: boolean): void {
this.addEventListener(element, type, e => {
this.update();
e.preventDefault();
}, useCapture);
}
protected addEventListener<K extends keyof HTMLElementEventMap>(element: HTMLElement, type: K, listener: EventListenerOrEventListenerObject<K>, useCapture?: boolean): void {
this.toDisposeOnDetach.push(addEventListener(element, type, listener, useCapture));
}
protected addKeyListener<K extends keyof HTMLElementEventMap>(
element: HTMLElement,
keysOrKeyCodes: KeyCode.Predicate | KeysOrKeyCodes,
action: (event: KeyboardEvent) => boolean | void | Object, ...additionalEventTypes: K[]): void {
this.toDisposeOnDetach.push(addKeyListener(element, keysOrKeyCodes, action, ...additionalEventTypes));
}
protected addClipboardListener<K extends 'cut' | 'copy' | 'paste'>(element: HTMLElement, type: K, listener: EventListenerOrEventListenerObject<K>): void {
this.toDisposeOnDetach.push(addClipboardListener(element, type, listener));
}
}
export function setEnabled(element: HTMLElement, enabled: boolean): void {
element.classList.toggle(DISABLED_CLASS, !enabled);
element.tabIndex = enabled ? 0 : -1;
}
export function createIconButton(...classNames: string[]): HTMLSpanElement {
const icon = document.createElement('i');
icon.classList.add(...classNames);
const button = document.createElement('span');
button.tabIndex = 0;
button.appendChild(icon);
return button;
}
// tslint:disable-next-line:no-any
export type EventListener<K extends keyof HTMLElementEventMap> = (this: HTMLElement, event: HTMLElementEventMap[K]) => any;
export interface EventListenerObject<K extends keyof HTMLElementEventMap> {
handleEvent(evt: HTMLElementEventMap[K]): void;
}
export namespace EventListenerObject {
// tslint:disable-next-line:no-any
export function is<K extends keyof HTMLElementEventMap>(listener: any | undefined): listener is EventListenerObject<K> {
return !!listener && 'handleEvent' in listener;
}
}
export type EventListenerOrEventListenerObject<K extends keyof HTMLElementEventMap> = EventListener<K> | EventListenerObject<K>;
export function addEventListener<K extends keyof HTMLElementEventMap>(
element: HTMLElement, type: K, listener: EventListenerOrEventListenerObject<K>, useCapture?: boolean
): Disposable {
element.addEventListener(type, listener, useCapture);
return Disposable.create(() =>
element.removeEventListener(type, listener)
);
}
export function addKeyListener<K extends keyof HTMLElementEventMap>(
element: HTMLElement,
keysOrKeyCodes: KeyCode.Predicate | KeysOrKeyCodes,
action: (event: KeyboardEvent) => boolean | void | Object, ...additionalEventTypes: K[]): Disposable {
const toDispose = new DisposableCollection();
const keyCodePredicate = (() => {
if (typeof keysOrKeyCodes === 'function') {
return keysOrKeyCodes;
} else {
return (actual: KeyCode) => KeysOrKeyCodes.toKeyCodes(keysOrKeyCodes).some(k => k.equals(actual));
}
})();
toDispose.push(addEventListener(element, 'keydown', e => {
const kc = KeyCode.createKeyCode(e);
if (keyCodePredicate(kc)) {
const result = action(e);
if (typeof result !== 'boolean' || result) {
e.stopPropagation();
e.preventDefault();
}
}
}));
for (const type of additionalEventTypes) {
toDispose.push(addEventListener(element, type, e => {
// tslint:disable-next-line:no-any
const event = (type as any)['keydown'];
const result = action(event);
if (typeof result !== 'boolean' || result) {
e.stopPropagation();
e.preventDefault();
}
}));
}
return toDispose;
}
export function addClipboardListener<K extends 'cut' | 'copy' | 'paste'>(element: HTMLElement, type: K, listener: EventListenerOrEventListenerObject<K>): Disposable {
const documentListener = (e: ClipboardEvent) => {
const activeElement = document.activeElement;
if (activeElement && element.contains(activeElement)) {
if (EventListenerObject.is(listener)) {
listener.handleEvent(e);
} else {
listener.bind(element)(e);
}
}
};
document.addEventListener(type, documentListener);
return Disposable.create(() =>
document.removeEventListener(type, documentListener)
);
}