|
|
@@ -1,6 +1,15 @@
|
|
|
-import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnChanges, Output } from '@angular/core';
|
|
|
+import {
|
|
|
+ ChangeDetectionStrategy,
|
|
|
+ Component,
|
|
|
+ EventEmitter,
|
|
|
+ Input,
|
|
|
+ OnChanges,
|
|
|
+ Output,
|
|
|
+ SimpleChanges,
|
|
|
+} from '@angular/core';
|
|
|
|
|
|
-import { Asset, GetAssetList } from '../../../common/generated-types';
|
|
|
+import { GetAssetList } from '../../../common/generated-types';
|
|
|
+import { SelectionManager } from '../../../common/utilities/selection-manager';
|
|
|
import { ModalService } from '../../../providers/modal/modal.service';
|
|
|
import { AssetPreviewDialogComponent } from '../asset-preview-dialog/asset-preview-dialog.component';
|
|
|
|
|
|
@@ -22,13 +31,16 @@ export class AssetGalleryComponent implements OnChanges {
|
|
|
@Output() selectionChange = new EventEmitter<AssetLike[]>();
|
|
|
@Output() deleteAssets = new EventEmitter<AssetLike[]>();
|
|
|
|
|
|
- selection: AssetLike[] = [];
|
|
|
+ selectionManager = new SelectionManager<AssetLike>({
|
|
|
+ multiSelect: this.multiSelect,
|
|
|
+ itemsAreEqual: (a, b) => a.id === b.id,
|
|
|
+ });
|
|
|
|
|
|
constructor(private modalService: ModalService) {}
|
|
|
|
|
|
- ngOnChanges() {
|
|
|
+ ngOnChanges(changes: SimpleChanges) {
|
|
|
if (this.assets) {
|
|
|
- for (const asset of this.selection) {
|
|
|
+ for (const asset of this.selectionManager.selection) {
|
|
|
// Update and selected assets with any changes
|
|
|
const match = this.assets.find(a => a.id === asset.id);
|
|
|
if (match) {
|
|
|
@@ -36,50 +48,30 @@ export class AssetGalleryComponent implements OnChanges {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ if (changes['assets']) {
|
|
|
+ this.selectionManager.setCurrentItems(this.assets);
|
|
|
+ }
|
|
|
+ if (changes['multiSelect']) {
|
|
|
+ this.selectionManager.setMultiSelect(this.multiSelect);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
toggleSelection(asset: AssetLike, event?: MouseEvent) {
|
|
|
- const index = this.selection.findIndex(a => a.id === asset.id);
|
|
|
- if (this.multiSelect && event?.shiftKey && 1 <= this.selection.length) {
|
|
|
- const lastSelection = this.selection[this.selection.length - 1];
|
|
|
- const lastSelectionIndex = this.assets.findIndex(a => a.id === lastSelection.id);
|
|
|
- const currentIndex = this.assets.findIndex(a => a.id === asset.id);
|
|
|
- const start = currentIndex < lastSelectionIndex ? currentIndex : lastSelectionIndex;
|
|
|
- const end = currentIndex > lastSelectionIndex ? currentIndex + 1 : lastSelectionIndex;
|
|
|
- this.selection.push(
|
|
|
- ...this.assets.slice(start, end).filter(a => !this.selection.find(s => s.id === a.id)),
|
|
|
- );
|
|
|
- } else if (index === -1) {
|
|
|
- if (this.multiSelect && (event?.ctrlKey || event?.shiftKey)) {
|
|
|
- this.selection.push(asset);
|
|
|
- } else {
|
|
|
- this.selection = [asset];
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (this.multiSelect && event?.ctrlKey) {
|
|
|
- this.selection.splice(index, 1);
|
|
|
- } else if (1 < this.selection.length) {
|
|
|
- this.selection = [asset];
|
|
|
- } else {
|
|
|
- this.selection.splice(index, 1);
|
|
|
- }
|
|
|
- }
|
|
|
- // Make the selection mutable
|
|
|
- this.selection = this.selection.map(x => ({ ...x }));
|
|
|
- this.selectionChange.emit(this.selection);
|
|
|
+ this.selectionManager.toggleSelection(asset, event);
|
|
|
+ this.selectionChange.emit(this.selectionManager.selection);
|
|
|
}
|
|
|
|
|
|
selectMultiple(assets: AssetLike[]) {
|
|
|
- this.selection = assets;
|
|
|
- this.selectionChange.emit(this.selection);
|
|
|
+ this.selectionManager.selectMultiple(assets);
|
|
|
+ this.selectionChange.emit(this.selectionManager.selection);
|
|
|
}
|
|
|
|
|
|
isSelected(asset: AssetLike): boolean {
|
|
|
- return !!this.selection.find(a => a.id === asset.id);
|
|
|
+ return this.selectionManager.isSelected(asset);
|
|
|
}
|
|
|
|
|
|
lastSelected(): AssetLike {
|
|
|
- return this.selection[this.selection.length - 1];
|
|
|
+ return this.selectionManager.lastSelected();
|
|
|
}
|
|
|
|
|
|
previewAsset(asset: AssetLike) {
|