/
hover.tsx
138 lines (123 loc) · 3.37 KB
/
hover.tsx
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
import { Widget, PanelLayout } from '@phosphor/widgets';
import { HoverBox } from '@jupyterlab/apputils';
import { Message } from '@phosphor/messaging';
import { hoverItem } from '../style/lineForm';
import { clickedItem, interactiveItem } from '../style/statusBar';
export function showPopup(options: Popup.IOptions): Popup | null {
let dialog = new Popup(options);
dialog.launch();
return dialog;
}
export class Popup extends Widget {
constructor(options: Popup.IOptions) {
super();
this._body = options.body;
this._body.addClass(hoverItem);
this._anchor = options.anchor;
this._align = options.align;
let layout = (this.layout = new PanelLayout());
layout.addWidget(options.body);
this._body.node.addEventListener('resize', () => {
this.update();
});
}
launch() {
this.setGeometry();
Widget.attach(this, document.body);
this.update();
this._anchor.addClass(clickedItem);
this._anchor.removeClass(interactiveItem);
}
setGeometry() {
let aligned = 0;
const anchorRect = this._anchor.node.getBoundingClientRect();
const bodyRect = this._body.node.getBoundingClientRect();
if (this._align === 'right') {
aligned = -(bodyRect.width - anchorRect.width);
}
const style = window.getComputedStyle(this._body.node);
HoverBox.setGeometry({
anchor: anchorRect,
host: document.body,
maxHeight: 500,
minHeight: 20,
node: this._body.node,
offset: {
horizontal: aligned
},
privilege: 'forceAbove',
style
});
}
protected onUpdateRequest(msg: Message): void {
this.setGeometry();
this.setGeometry();
super.onUpdateRequest(msg);
}
protected onAfterAttach(msg: Message): void {
document.addEventListener('click', this, false);
this.node.addEventListener('keypress', this, false);
window.addEventListener('resize', this, false);
}
protected onAfterDetach(msg: Message): void {
document.removeEventListener('click', this, false);
this.node.removeEventListener('keypress', this, false);
window.removeEventListener('resize', this, false);
}
protected _evtClick(event: MouseEvent): void {
if (
!!event.target &&
!(
this._body.node.contains(event.target as HTMLElement) ||
this._anchor.node.contains(event.target as HTMLElement)
)
) {
this.dispose();
}
}
protected onResize(): void {
this.update();
}
dispose() {
super.dispose();
this._anchor.removeClass(clickedItem);
this._anchor.addClass(interactiveItem);
}
protected _evtKeydown(event: KeyboardEvent): void {
// Check for escape key
switch (event.keyCode) {
case 27: // Escape.
event.stopPropagation();
event.preventDefault();
this.dispose();
break;
default:
break;
}
}
handleEvent(event: Event): void {
switch (event.type) {
case 'keydown':
this._evtKeydown(event as KeyboardEvent);
break;
case 'click':
this._evtClick(event as MouseEvent);
break;
case 'resize':
this.onResize();
break;
default:
break;
}
}
private _body: Widget;
private _anchor: Widget;
private _align: 'left' | 'right' | undefined;
}
export namespace Popup {
export interface IOptions {
body: Widget;
anchor: Widget;
align?: 'left' | 'right';
}
}