-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFssTreeItem.ts
370 lines (325 loc) · 11.9 KB
/
FssTreeItem.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Element for displaying a single fsspec tree entry
import {
provideJupyterDesignSystem,
jpTreeItem
} from '@jupyter/web-components';
import { Signal } from '@lumino/signaling';
import { INotebookTracker } from '@jupyterlab/notebook';
import { fileIcon, folderIcon } from '@jupyterlab/ui-components';
import { FssTreeItemContext } from './FssTreeItemContext';
import { Logger } from './logger';
export class FssTreeItem {
root: any;
model: any;
// icon: HTMLElement;
nameLbl: HTMLElement;
sizeLbl: HTMLElement;
dirSymbol: HTMLElement;
container: HTMLElement;
isDir = false;
treeItemObserver: MutationObserver;
pendingExpandAction = false;
lazyLoadAutoExpand = true;
clickAnywhereDoesAutoExpand = true;
notebookTracker: INotebookTracker;
private readonly logger: Logger;
treeItemClicked: Signal<any, string>;
getBytesRequested: Signal<any, string>;
uploadRequested: Signal<any, any>; // All upload requests go here (kernel user_data, browser picker etc.)
constructor(
model: any,
autoExpand: boolean,
expandOnClickAnywhere: boolean,
notebookTracker: INotebookTracker
) {
this.logger = Logger.getLogger('FssTreeItem');
// The TreeItem component is the root and handles
// tree structure functionality in the UI
// We use the tagName `jp-tree-item` for Notebook 7 compatibility
if (!customElements.get('jp-tree-item')) {
provideJupyterDesignSystem().register(jpTreeItem());
this.logger.info('jpTreeItem web component registered');
}
const root = document.createElement('jp-tree-item');
root.setAttribute('name', 'jfss-treeitem-root');
this.root = root;
this.model = model;
this.lazyLoadAutoExpand = autoExpand;
this.clickAnywhereDoesAutoExpand = expandOnClickAnywhere;
this.notebookTracker = notebookTracker;
this.treeItemClicked = new Signal<this, string>(this);
this.getBytesRequested = new Signal<this, string>(this);
this.uploadRequested = new Signal<this, any>(this);
// Use a MutationObserver on the root TreeItem's shadow DOM,
// where the TreeItem's expand/collapse control will live once
// the item has children to show
const observeOptions = {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['class'],
attributeOldValue: true
};
this.treeItemObserver = new MutationObserver(
this.handleDomMutation.bind(this)
);
// The main container holds custom fsspec UI/functionality
const container = document.createElement('div');
container.classList.add('jfss-tree-item-container');
root.appendChild(container);
this.container = container;
// Reserve space in the layout for the file/folder icon
const dirSymbol = document.createElement('div');
dirSymbol.classList.add('jfss-dir-symbol');
container.appendChild(dirSymbol);
dirSymbol.style.visibility = 'hidden';
this.dirSymbol = dirSymbol;
// Show the name of this file/folder (a single path segment)
const nameLbl = document.createElement('div');
container.appendChild(nameLbl);
this.nameLbl = nameLbl;
// Show the name of this file/folder (a single path segment)
const sizeLbl = document.createElement('div');
sizeLbl.classList.add('jfss-filesize-lbl');
container.appendChild(sizeLbl);
this.sizeLbl = sizeLbl;
// Add click and right click handlers to the tree component
root.addEventListener('contextmenu', this.handleContext.bind(this));
root.addEventListener('click', this.handleClick.bind(this), true);
// Start observing for changes to the TreeItem's shadow root
if (this.root.shadowRoot) {
this.treeItemObserver.observe(this.root.shadowRoot, observeOptions);
this.logger.debug('MutationObserver attached to tree item shadow root');
} else {
this.logger.warn(
'Shadow root not available for tree item, observer not attached'
);
}
this.logger.debug('Tree item constructed', {
autoExpand,
expandOnClickAnywhere
});
}
appendChild(elem: any) {
this.logger.debug('Appending child element to tree item', {
childTagName: elem.tagName,
childId: elem.id || 'none'
});
this.root.appendChild(elem);
}
handleRequestBytes() {
this.logger.debug('Processing request for bytes', {
path: this.root.dataset.fss
});
this.getBytesRequested.emit(this.root.dataset.fss);
}
async handleUploadRequest(options: any) {
// Uploads can come from different places, gather info and emit it here
// (so that connected slots can route the request to the right place)
let is_browser_file_picker = false;
let is_jup_browser_file = false;
if (options) {
is_browser_file_picker = options.is_browser_file_picker;
is_jup_browser_file = options.is_jup_browser_file;
this.model.queuedPickerUploadInfo = {}; // Context click always resets this data
}
this.logger.debug('Handling upload request', {
path: this.root.dataset.fss,
isDirectory: this.isDir,
options
});
this.uploadRequested.emit({
user_path: this.root.dataset.fss,
is_dir: this.isDir,
is_browser_file_picker: is_browser_file_picker,
is_jup_browser_file: is_jup_browser_file
});
}
setMetadata(user_path: string, size: string) {
this.logger.debug('Setting item metadata', {
path: user_path,
size
});
this.root.dataset.fss = user_path;
this.root.dataset.fsize = size;
const sizeDisplay = `(${size.toLocaleString()})`;
this.sizeLbl.innerText = sizeDisplay;
}
setText(value: string) {
this.logger.debug('Setting item text', { value });
this.nameLbl.innerText = value;
}
setType(symbol: 'dir' | 'file') {
this.logger.debug('Setting item type', { type: symbol });
this.dirSymbol.replaceChildren();
this.dirSymbol.style.visibility = 'visible';
if (symbol === 'dir') {
folderIcon.element({ container: this.dirSymbol });
this.isDir = true;
this.sizeLbl.style.display = 'none';
}
if (symbol === 'file') {
fileIcon.element({ container: this.dirSymbol });
this.isDir = false;
}
}
handleDomMutation(records: any, observer: any) {
// This is used to auto-expand directory-type TreeItem's to show children after
// a lazy-load. It checks the TreeItem's shadow dom for the addition of an
// "expand-collapse-button" child control which is used to expand and show
// children (in the tree) of this class's root TreeItem node. By auto expanding here,
// we save the user from having to click twice on a folder (once to lazy-load
// and another time to expand) when they want to expand it
if (this.lazyLoadAutoExpand && this.pendingExpandAction) {
this.logger.debug('Processing DOM mutation', {
pendingExpandAction: this.pendingExpandAction,
recordCount: records.length
});
for (const rec of records) {
const addedNodes = rec?.addedNodes;
if (addedNodes) {
for (const node of addedNodes) {
if (
node?.classList &&
node.classList.contains('expand-collapse-button')
) {
this.logger.debug(
'Expand-collapse button detected, auto-expanding'
);
node.click();
this.root.scrollTo();
this.pendingExpandAction = false;
}
}
}
}
}
}
handleClick(event: any) {
// Filter click events to handle this item's root+shadow and container
if (
event.target === this.root ||
this.container.contains(event.target) ||
this.root.shadowRoot.contains(event.target)
) {
this.logger.debug('Tree item clicked', {
path: this.root.dataset.fss,
isDirectory: this.isDir,
clientX: event.clientX,
clientY: event.clientY
});
// Handles normal click events on the TreeItem (unlike the MutationObserver system
// which is for handling folder auto-expand after lazy load)
if (this.clickAnywhereDoesAutoExpand) {
const expander = this.root.shadowRoot.querySelector(
'.expand-collapse-button'
);
if (expander) {
const expRect = expander.getBoundingClientRect();
if (
event.clientX < expRect.left ||
event.clientX > expRect.right ||
event.clientY < expRect.top ||
event.clientY > expRect.bottom
) {
this.logger.debug('Click outside expander detected', {
clickPosition: {
x: event.clientX,
y: event.clientY
},
expanderBounds: {
left: expRect.left,
right: expRect.right,
top: expRect.top,
bottom: expRect.bottom
}
});
expander.click();
this.root.scrollTo();
}
}
}
// Fire connected slots that were supplied to this item on init
if (this.isDir) {
this.logger.debug('Invoking directory click slots');
this.treeItemClicked.emit(this.root.dataset.fss);
} else {
this.logger.debug('Invoking file click handler');
this.root.click();
}
}
}
expandItem() {
// This method's purpose is to expand folder items to show children
// after a lazy load, but when this is called, the expand controls aren't
// ready...a flag is set here to indicate that an expand action is desired,
// which is used by the MutationObserver member var's handler to find the
// expand/collapse Element when it is added so that it can be click()'d
this.logger.debug('Setting pending expand action', {
path: this.root.dataset.fss
});
this.pendingExpandAction = true;
}
handleContext(event: any) {
// Prevent ancestors from adding extra context boxes
event.stopPropagation();
// Prevent default browser context menu (unless shift pressed
// as per usual JupyterLab conventions)
if (!event.shiftKey) {
event.preventDefault();
} else {
this.logger.debug('Default context menu shown (shift+click)');
return;
}
this.logger.debug('Opening context menu', {
path: this.root.dataset.fss,
isDirectory: this.isDir,
clientX: event.clientX,
clientY: event.clientY
});
// Make/add the context menu
const context = new FssTreeItemContext(
this.model,
this.notebookTracker,
this
);
context.root.dataset.fss = this.root.dataset.fss;
const body = document.getElementsByTagName('body')[0];
body.appendChild(context.root);
// Position it under the mouse (top left corner normally,
// or bottom right if that corner is out-of-viewport)
const parentRect = body.getBoundingClientRect();
const contextRect = context.root.getBoundingClientRect();
let xCoord = event.clientX - parentRect.x;
let yCoord = event.clientY - parentRect.y;
const spacing = 12;
let positionMode = 'topLeft';
if (
xCoord + contextRect.width > window.innerWidth ||
yCoord + contextRect.height > window.innerHeight
) {
// Context menu is cut off when positioned under mouse at top left corner,
// use the bottom right corner instead
xCoord -= contextRect.width;
yCoord -= contextRect.height;
// Shift the menu so the mouse is inside it, not at the corner/edge
xCoord += spacing;
yCoord += spacing;
positionMode = 'bottomRight';
} else {
// Shift the menu so the mouse is inside it, not at the corner/edge
xCoord -= spacing;
yCoord -= spacing;
}
this.logger.debug('Positioned context menu', {
mode: positionMode,
position: { x: xCoord, y: yCoord },
viewportConstraints: {
width: window.innerWidth,
height: window.innerHeight
}
});
context.root.style.left = `${xCoord}` + 'px';
context.root.style.top = `${yCoord}` + 'px';
}
}