Fix grid to keep available selection when searching for files

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent e32f37df87
commit 24a8b1ce39

@ -40,13 +40,18 @@ export class FileGridComponent implements OnChanges, OnInit {
} }
public ngOnInit(): void { public ngOnInit(): void {
this.gridEntries = this.files.map(file => {return {file, selected: false}}); this.gridEntries = this.files.map(file => {
return {file, selected: false}
});
this.setPartitionedGridEntries(); this.setPartitionedGridEntries();
} }
ngOnChanges(changes: SimpleChanges): void { ngOnChanges(changes: SimpleChanges): void {
if (changes["files"]) { if (changes["files"]) {
this.gridEntries = this.files.map(file => {return {file, selected: false}}); this.gridEntries = this.files.map(file => {
return {file, selected: false}
});
this.refreshFileSelections();
this.setPartitionedGridEntries(); this.setPartitionedGridEntries();
} }
} }
@ -59,7 +64,9 @@ export class FileGridComponent implements OnChanges, OnInit {
const previousSelectionSize = this.selectedEntries.length; const previousSelectionSize = this.selectedEntries.length;
if (!(this.shiftClicked || this.ctrlClicked) && this.selectedEntries.length > 0) { if (!(this.shiftClicked || this.ctrlClicked) && this.selectedEntries.length > 0) {
this.selectedEntries.forEach(entry => {if (entry !== clickedEntry) entry.selected = false}); this.selectedEntries.forEach(entry => {
if (entry !== clickedEntry) entry.selected = false
});
this.selectedEntries = []; this.selectedEntries = [];
} }
if (this.shiftClicked && this.selectedEntries.length > 0) { if (this.shiftClicked && this.selectedEntries.length > 0) {
@ -76,24 +83,28 @@ export class FileGridComponent implements OnChanges, OnInit {
} }
} }
if (this.selectedEntries.length == 1) { if (this.selectedEntries.length == 1) {
this.fileSelectEvent.emit(this.selectedEntries.map(entry => entry.file)[0]); this.fileSelectEvent.emit(
} else if (this.selectedEntries.length == 0 && previousSelectionSize == 1){ this.selectedEntries.map(entry => entry.file)[0]);
} else if (this.selectedEntries.length == 0 && previousSelectionSize == 1) {
this.fileSelectEvent.emit(undefined); this.fileSelectEvent.emit(undefined);
} else { } else {
this.fileMultiselectEvent.emit(this.selectedEntries.map(entry => entry.file)); this.fileMultiselectEvent.emit(
this.selectedEntries.map(entry => entry.file));
} }
} }
private setPartitionedGridEntries() { private setPartitionedGridEntries() {
this.partitionedGridEntries = []; this.partitionedGridEntries = [];
this.selectedEntries = [];
let scrollToIndex = -1; let scrollToIndex = -1;
let selectedEntry: GridEntry | undefined = undefined; let selectedEntry: GridEntry | undefined = undefined;
for (let i = 0; i < (Math.ceil(this.gridEntries.length / this.columns)); i++) { for (let i = 0; i < (Math.ceil(
const entries = this.gridEntries.slice(i * this.columns, Math.min(this.gridEntries.length, (i + 1) * this.columns)); this.gridEntries.length / this.columns)); i++) {
const entries = this.gridEntries.slice(i * this.columns,
Math.min(this.gridEntries.length, (i + 1) * this.columns));
this.partitionedGridEntries.push(entries); this.partitionedGridEntries.push(entries);
const preselectedEntry = entries.find(e => e.file.hash == this.preselectedFile?.hash); const preselectedEntry = entries.find(
e => e.file.hash == this.preselectedFile?.hash);
if (preselectedEntry) { if (preselectedEntry) {
scrollToIndex = i; scrollToIndex = i;
@ -106,13 +117,21 @@ export class FileGridComponent implements OnChanges, OnInit {
this.virtualScroll?.scrollToIndex(scrollToIndex); this.virtualScroll?.scrollToIndex(scrollToIndex);
if (selectedEntry) { if (selectedEntry) {
selectedEntry.selected = true; selectedEntry.selected = true;
this.selectedEntries = [selectedEntry]; this.selectedEntries.push(selectedEntry);
} }
} }
}, 0); }, 0);
} }
} }
private refreshFileSelections() {
const newSelection: GridEntry[] = this.gridEntries.filter(
entry => this.selectedEntries.findIndex(
e => e.file.id == entry.file.id) >= 0);
newSelection.forEach(entry => entry.selected = true);
this.selectedEntries = newSelection;
}
private handleShiftSelect(clickedEntry: GridEntry): void { private handleShiftSelect(clickedEntry: GridEntry): void {
const lastEntry = this.selectedEntries[this.selectedEntries.length - 1]; const lastEntry = this.selectedEntries[this.selectedEntries.length - 1];
let found = false; let found = false;
@ -140,16 +159,24 @@ export class FileGridComponent implements OnChanges, OnInit {
@HostListener("window:keydown", ["$event"]) @HostListener("window:keydown", ["$event"])
private handleKeydownEvent(event: KeyboardEvent) { private handleKeydownEvent(event: KeyboardEvent) {
switch (event.key) { switch (event.key) {
case "Shift": this.shiftClicked = true; break; case "Shift":
case "Control": this.ctrlClicked = true; break; this.shiftClicked = true;
break;
case "Control":
this.ctrlClicked = true;
break;
} }
} }
@HostListener("window:keyup", ["$event"]) @HostListener("window:keyup", ["$event"])
private handleKeyupEvent(event: KeyboardEvent) { private handleKeyupEvent(event: KeyboardEvent) {
switch (event.key) { switch (event.key) {
case "Shift": this.shiftClicked = false; break; case "Shift":
case "Control": this.ctrlClicked = false; break; this.shiftClicked = false;
break;
case "Control":
this.ctrlClicked = false;
break;
} }
} }
} }

@ -1,5 +1,6 @@
export class File { export class File {
constructor( constructor(
public id: number,
public name: string | undefined, public name: string | undefined,
public comment: string | undefined, public comment: string | undefined,
public hash: string, public hash: string,

@ -35,6 +35,7 @@ export class FilesTabSidebarComponent implements OnInit, OnChanges {
this.fileService.displayedFiles.subscribe(async files => { this.fileService.displayedFiles.subscribe(async files => {
this.files = files; this.files = files;
await this.loadTagsForDisplayedFiles(); await this.loadTagsForDisplayedFiles();
await this.refreshFileSelection();
}); });
this.repoService.selectedRepository.subscribe( this.repoService.selectedRepository.subscribe(
async (repo) => repo && this.fileSearch && await this.fileSearch.searchForFiles()); async (repo) => repo && this.fileSearch && await this.fileSearch.searchForFiles());
@ -75,10 +76,24 @@ export class FilesTabSidebarComponent implements OnInit, OnChanges {
(a, b) => a.getNormalizedOutput().localeCompare(b.getNormalizedOutput())); (a, b) => a.getNormalizedOutput().localeCompare(b.getNormalizedOutput()));
} }
showAllTagsFallback() { private async refreshFileSelection() {
const filteredSelection = this.selectedFiles.filter(
file => this.files.findIndex(f => f.hash === file.hash) >= 0);
if (filteredSelection.length === 0) {
this.tags = [];
this.showAllTagsFallback();
} else if (filteredSelection.length < this.selectedFiles.length) {
this.selectedFiles = filteredSelection;
await this.showFileDetails(this.selectedFiles);
}
}
private showAllTagsFallback() {
if (this.tags.length === 0) { if (this.tags.length === 0) {
this.tags = this.tagsOfFiles.sort( this.tags = this.tagsOfFiles.sort(
(a, b) => a.getNormalizedOutput().localeCompare(b.getNormalizedOutput()));; (a, b) => a.getNormalizedOutput()
.localeCompare(b.getNormalizedOutput()));
;
} }
} }
} }

Loading…
Cancel
Save