Skip to content

Commit 8225bba

Browse files
authored
Merge pull request #18 from martinRenou/clear_selection_when_clicking_empty_space
Clear selection when clicking the empty space
2 parents ebd89cf + ed06cab commit 8225bba

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

src/unfold.ts

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,39 @@ export class DirTreeListing extends DirListing {
331331
});
332332
}
333333

334+
/**
335+
* Handle 'mousedown' event
336+
*
337+
* Note: This allow to change the path to the root and clear selection when the user
338+
* is clicking on an empty space.
339+
*/
340+
private _eventMouseDown(event: MouseEvent): void {
341+
const entry = this.modelForClick(event);
342+
343+
if (entry) {
344+
if (entry.type === 'directory') {
345+
this.model.path = '/' + entry.path;
346+
347+
if (
348+
this._singleClickToUnfold &&
349+
(event.button === 0 || // State toggled on main button
350+
(event.button === 2 && !this.model.isOpen(entry.path)) || // State toggled on right click if folder is closed
351+
event.type === 'click') // State toggled on click and double click
352+
) {
353+
this.model.toggle(entry.path);
354+
}
355+
} else {
356+
this.model.path = '/' + PathExt.dirname(entry.path);
357+
}
358+
} else {
359+
// TODO Upstream this logic to JupyterLab (clearing selection when clicking the empty space)?
360+
this.clearSelectedItems();
361+
this.update();
362+
363+
this.model.path = this.model.rootPath;
364+
}
365+
}
366+
334367
private _hitTestNodes(nodes: HTMLElement[], event: MouseEvent): number {
335368
return ArrayExt.findFirstIndex(
336369
nodes,
@@ -356,43 +389,14 @@ export class DirTreeListing extends DirListing {
356389
break;
357390
case 'mousedown':
358391
super.handleEvent(event);
359-
this._changeModelPath(event as MouseEvent);
392+
this._eventMouseDown(event as MouseEvent);
360393
break;
361394
default:
362395
super.handleEvent(event);
363396
break;
364397
}
365398
}
366399

367-
/**
368-
* Change the model path on each 'mousedown' event
369-
*
370-
* Note: This allow to change the path to the root when the user
371-
* is clicking on an empty space.
372-
*/
373-
private _changeModelPath(event: MouseEvent): void {
374-
const entry = this.modelForClick(event);
375-
376-
if (entry) {
377-
if (entry.type === 'directory') {
378-
this.model.path = '/' + entry.path;
379-
380-
if (
381-
this._singleClickToUnfold &&
382-
(event.button === 0 || // State toggled on main button
383-
(event.button === 2 && !this.model.isOpen(entry.path)) || // State toggled on right click if folder is closed
384-
event.type === 'click') // State toggled on click and double click
385-
) {
386-
this.model.toggle(entry.path);
387-
}
388-
} else {
389-
this.model.path = '/' + PathExt.dirname(entry.path);
390-
}
391-
} else {
392-
this.model.path = this.model.rootPath;
393-
}
394-
}
395-
396400
private _singleClickToUnfold = true;
397401
}
398402

0 commit comments

Comments
 (0)