workspace-ionic-app/src/app/show-folder/show-folder.component.ts

340 lines
11 KiB
TypeScript
Raw Normal View History

2023-03-15 17:16:06 +01:00
import { Component, OnInit, Input, CUSTOM_ELEMENTS_SCHEMA, Output, EventEmitter, ElementRef, ViewChild, OnDestroy, AfterViewInit } from '@angular/core';
2023-02-16 17:18:54 +01:00
2023-03-15 17:16:06 +01:00
import { AlertController, ToastController, IonicModule, ActionSheetController, ActionSheetButton, AlertOptions, ModalController, ModalOptions, LoadingController, Platform, IonBackButtonDelegate, NavController } from '@ionic/angular';
2023-02-16 17:18:54 +01:00
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
import { StoragehubService } from '../storagehub.service';
import { CommonModule } from '@angular/common';
2023-02-23 18:11:45 +01:00
import { MatIconModule } from '@angular/material/icon';
import { WSItem } from '../model/ws-item';
import { Action } from '../model/actions/action';
import { CreateFolderAction } from '../model/actions/create-folder';
import { Actions } from '../model/actions/actions';
2023-03-01 12:06:56 +01:00
import { ItemsListComponent } from '../items-list/items-list.component';
import { Sorting, SortName, SortType } from '../model/sorting';
2023-03-13 17:39:14 +01:00
import { UploaderInfoService } from '../uploader-info.service';
import { EventService } from '../event.service';
import { OpenFile } from '../model/actions/open-file';
2023-03-14 15:56:50 +01:00
import { presentConnectionAlert } from '../_helper/utils';
2023-03-15 17:16:06 +01:00
import { Subscription } from 'rxjs';
2023-02-16 17:18:54 +01:00
@Component({
standalone: true,
selector: 'show-folder',
2023-03-13 17:39:14 +01:00
providers: [FileOpener, StoragehubService, UploaderInfoService],
2023-02-16 17:18:54 +01:00
templateUrl: './show-folder.component.html',
styleUrls: ['./show-folder.component.scss'],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [
2023-03-01 12:06:56 +01:00
CommonModule, IonicModule, MatIconModule, ItemsListComponent
2023-02-16 17:18:54 +01:00
]
})
2023-03-15 17:16:06 +01:00
export class ShowFolderComponent implements OnInit, OnDestroy, AfterViewInit {
2023-02-16 17:18:54 +01:00
2023-02-24 17:28:40 +01:00
@Output() folderClickedEvent = new EventEmitter<WSItem>();
2023-03-14 15:56:50 +01:00
2023-03-16 16:15:10 +01:00
filteredBySegmentItems: WSItem[] | undefined;
filtereBySearchItems: WSItem[] | undefined;
2023-03-01 12:06:56 +01:00
_items: WSItem[] | undefined;
2023-02-25 12:53:05 +01:00
2023-03-01 12:06:56 +01:00
currentSortName = SortName.Name;
currentSortType = SortType.Asc;
2023-02-20 18:06:39 +01:00
2023-03-01 12:06:56 +01:00
@Input() set items(items: WSItem[] | undefined) {
this._items = items?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
2023-03-16 16:15:10 +01:00
this.filteredBySegmentItems = this._items;
this.filtereBySearchItems = this._items
2023-03-01 12:06:56 +01:00
}
@Input() parentItem: WSItem | undefined = undefined;
@Input() title: string | undefined;
2023-02-16 17:18:54 +01:00
2023-02-25 12:53:05 +01:00
@Input() root: boolean = false;
2023-03-21 16:21:39 +01:00
@Input() enableCreateOperations = false;
2023-03-15 17:16:06 +01:00
2023-03-21 16:21:39 +01:00
searchenabled = false;
2023-03-15 17:16:06 +01:00
2023-03-01 12:06:56 +01:00
selectedSegment = "all";
public get sortName(): typeof SortName {
2023-03-13 17:39:14 +01:00
return SortName;
2023-03-01 12:06:56 +01:00
}
2023-03-13 17:39:14 +01:00
underUpload: string[];
2023-03-16 16:15:10 +01:00
currentSearch: string = "";
2023-03-13 17:39:14 +01:00
2023-02-23 18:11:45 +01:00
@ViewChild('filepicker') uploader!: ElementRef;
2023-03-21 16:21:39 +01:00
2023-03-01 12:06:56 +01:00
customSortAlertOptions = {
header: 'Sort by',
translucent: true,
};
2023-02-26 16:34:34 +01:00
2023-02-16 17:18:54 +01:00
constructor(
2023-02-23 18:11:45 +01:00
private actionSheetCtrl: ActionSheetController,
private storagehub: StoragehubService,
2023-02-25 12:53:05 +01:00
private alertCtrl: AlertController,
2023-02-26 16:34:34 +01:00
private modalCtrl: ModalController,
2023-03-13 17:39:14 +01:00
private toastController: ToastController,
private uploaderInfo: UploaderInfoService,
private event: EventService,
2023-03-14 15:56:50 +01:00
private fileOpener: FileOpener,
2023-03-15 17:16:06 +01:00
private loadingCtrl: LoadingController,
private navController: NavController
2023-03-01 12:06:56 +01:00
) {
2023-03-13 17:39:14 +01:00
this.underUpload = this.getUnderUploadItems();
2023-03-01 12:06:56 +01:00
}
2023-03-15 17:16:06 +01:00
ngAfterViewInit(): void {
}
backButtonSubscription: Subscription | undefined;
reloadSubscription: Subscription | undefined;
2023-03-01 12:06:56 +01:00
2023-02-23 18:11:45 +01:00
ngOnInit(): void {
2023-03-15 17:16:06 +01:00
this.reloadSubscription = this.event.ReloadEvent.subscribe((val) => {
2023-03-13 17:39:14 +01:00
console.log(`event received with value ${val} in item ${this.parentItem?.item.id}`);
if (val === this.parentItem?.item.id)
this.loadDocuments();
});
2023-03-15 17:16:06 +01:00
this.backButtonSubscription = this.event.BackButtonEvent.subscribe(() => this.onbackButtonPressed());
}
2023-03-21 16:21:39 +01:00
2023-03-15 17:16:06 +01:00
ngOnDestroy(): void {
var p: IonBackButtonDelegate
this.reloadSubscription?.unsubscribe();
this.backButtonSubscription?.unsubscribe();
2023-03-13 17:39:14 +01:00
}
getUnderUploadItems(): string[] {
const parentId = this.parentItem?.item.id;
return parentId ? this.uploaderInfo.getUnderUpload(parentId) : [];
2023-02-20 18:06:39 +01:00
}
2023-02-23 18:11:45 +01:00
loadDocuments() {
2023-03-16 16:15:10 +01:00
this.filtereBySearchItems = undefined;
2023-02-25 12:53:05 +01:00
if (this.parentItem) {
2023-03-14 15:56:50 +01:00
this.storagehub.getChildren(this.parentItem.item.id).then((obs) => obs.subscribe({
next: (res) => {
const tmpItems$: WSItem[] = []
2023-03-16 16:15:10 +01:00
const tmpFilteredBySegment$: WSItem[] = []
2023-03-01 12:06:56 +01:00
var segmentFilterFunction = this.getSegmentFilterFunction();
res.forEach(i => {
var localItem = new WSItem(i);
tmpItems$.push(localItem);
2023-03-21 16:21:39 +01:00
if (segmentFilterFunction(localItem)) {
2023-03-16 16:15:10 +01:00
tmpFilteredBySegment$.push(localItem);
}
2023-03-01 12:06:56 +01:00
});
this._items = tmpItems$.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
2023-03-16 16:15:10 +01:00
this.filteredBySegmentItems = tmpFilteredBySegment$.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
this.filtereBySearchItems = tmpFilteredBySegment$.filter(d => d.getTitle().toLowerCase().indexOf(this.currentSearch) > -1)
2023-03-13 17:39:14 +01:00
this.underUpload = this.getUnderUploadItems();
2023-03-14 15:56:50 +01:00
},
error: err => presentConnectionAlert(err, this.alertCtrl)
})
2023-03-13 17:39:14 +01:00
)
2023-02-25 12:53:05 +01:00
}
2023-02-16 17:18:54 +01:00
}
2023-02-25 12:53:05 +01:00
2023-03-21 16:21:39 +01:00
changeSearchEnabled() {
this.searchenabled = !this.searchenabled;
if (!this.searchenabled)
this.searchCleared()
}
2023-03-15 17:16:06 +01:00
onbackButtonPressed() {
if (!this.root) {
console.log("back clicked (not root)");
this.navController.pop();
} else console.log("back clicked (root)");
}
2023-03-16 16:15:10 +01:00
getSegmentFilterFunction(): (value: WSItem) => boolean {
2023-03-01 12:06:56 +01:00
var filterItemFunction;
switch (this.selectedSegment) {
case "shared":
filterItemFunction = (value: WSItem) => value.type == "SharedFolder";
break;
case "folders":
filterItemFunction = (value: WSItem) => value.isFolder();
break;
case "files":
filterItemFunction = (value: WSItem) => value.isFile();
break;
default:
filterItemFunction = (value: WSItem) => true;
}
return filterItemFunction;
}
filterBy(value: string) {
if (value == this.selectedSegment) return;
2023-03-21 16:21:39 +01:00
2023-03-16 16:15:10 +01:00
this.filtereBySearchItems = undefined;
2023-03-01 12:06:56 +01:00
this.selectedSegment = value;
2023-03-21 16:21:39 +01:00
2023-03-01 12:06:56 +01:00
if (this.selectedSegment == "all")
2023-03-21 16:21:39 +01:00
this.filteredBySegmentItems = this._items;
else
2023-03-16 16:15:10 +01:00
this.filteredBySegmentItems = this._items?.filter(this.getSegmentFilterFunction());
2023-03-21 16:21:39 +01:00
2023-03-16 16:15:10 +01:00
if (this.currentSearch.length != 0)
this.filtereBySearchItems = this.filteredBySegmentItems?.filter(d => d.getTitle().toLowerCase().indexOf(this.currentSearch) > -1);
else this.filtereBySearchItems = this.filteredBySegmentItems;
2023-03-01 12:06:56 +01:00
}
changeSortType() {
if (this.currentSortType == SortType.Asc)
this.currentSortType = SortType.Desc;
else this.currentSortType = SortType.Asc;
this.updateSort();
}
changeSortName($event: any) {
2023-03-13 17:39:14 +01:00
var sort: SortName = $event.target.value;
if (this.currentSortName != sort) {
2023-03-01 12:06:56 +01:00
this.currentSortName = sort;
this.updateSort();
}
}
updateSort() {
this._items = this._items?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
2023-03-21 16:21:39 +01:00
2023-03-16 16:15:10 +01:00
if (this.selectedSegment == "all")
this.filteredBySegmentItems = this._items;
else
this.filteredBySegmentItems = this.filteredBySegmentItems?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
2023-03-21 16:21:39 +01:00
if (this.currentSearch.length != 0)
2023-03-16 16:15:10 +01:00
this.filtereBySearchItems = this.filtereBySearchItems?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
else this.filtereBySearchItems = this.filteredBySegmentItems;
}
searchItems(event: any) {
const query: string = event.target.value.toLowerCase();
this.currentSearch = query;
this.filtereBySearchItems = this.filteredBySegmentItems?.filter(d => d.getTitle().toLowerCase().indexOf(query) > -1);
}
2023-03-21 16:21:39 +01:00
searchCleared() {
2023-03-16 16:15:10 +01:00
this.currentSearch = "";
2023-03-21 16:21:39 +01:00
this.filtereBySearchItems = this.filteredBySegmentItems
2023-03-01 12:06:56 +01:00
}
2023-02-23 18:11:45 +01:00
itemClicked(item: WSItem) {
2023-03-13 17:39:14 +01:00
if (item.isFolder())
2023-02-24 17:28:40 +01:00
this.folderClickedEvent.emit(item);
2023-03-14 15:56:50 +01:00
else if (item.isFile()) {
new OpenFile(item).open(this.storagehub, this.fileOpener, this.loadingCtrl, this.alertCtrl);
2023-02-20 18:06:39 +01:00
}
2023-02-16 17:18:54 +01:00
}
2023-02-23 18:11:45 +01:00
addFile() {
this.uploader.nativeElement.click();
}
async fileSelected($event: any) {
const selected = $event.target.files[0];
if (selected && this.parentItem) {
2023-03-13 17:39:14 +01:00
const parentId: string = this.parentItem?.item?.id;
this.uploaderInfo.uploadStarted(parentId, selected.name);
const upload$ = this.storagehub.uploadFile(parentId, selected.name, selected);
var first = true;
upload$.then((obs) => obs.subscribe({
next: () => {
if (first) {
this.loadDocuments();
first = false;
}
},
error: () => this.uploaderInfo.uploadFinished(parentId, selected.name),
complete: () => {
console.log
this.uploaderInfo.uploadFinished(parentId, selected.name);
this.loadDocuments();
}
2023-03-01 12:06:56 +01:00
2023-03-08 17:13:03 +01:00
})
);
2023-02-20 18:06:39 +01:00
}
2023-02-23 18:11:45 +01:00
}
async createFolder() {
var createFolderAction: CreateFolderAction = new CreateFolderAction();
if (this.parentItem)
this.actionHandler(createFolderAction, this.parentItem);
2023-02-20 18:06:39 +01:00
}
2023-02-23 18:11:45 +01:00
async presentActionSheet(item: WSItem) {
2023-02-25 12:53:05 +01:00
var buttons: ActionSheetButton<any>[] = [];
Actions.getActionsPerType(item.type).forEach(action => buttons.push({
2023-02-24 17:28:40 +01:00
text: action.getName(),
2023-02-23 18:11:45 +01:00
data: {
2023-02-24 17:28:40 +01:00
action: action.getName(),
2023-02-23 18:11:45 +01:00
},
2023-02-24 17:28:40 +01:00
role: action.getActionType(),
handler: () => this.actionHandler(action, item)
2023-02-23 18:11:45 +01:00
}));
2023-02-25 12:53:05 +01:00
2023-02-23 18:11:45 +01:00
buttons.push({
text: 'Cancel',
role: 'cancel',
data: {
action: 'cancel',
},
});
const actionSheet = await this.actionSheetCtrl.create({
header: `${item.item.title}`,
mode: 'ios',
translucent: true,
buttons: buttons,
cssClass: 'my-custom-class'
});
await actionSheet.present();
const result = await actionSheet.onDidDismiss();
2023-02-24 17:28:40 +01:00
}
2023-02-25 12:53:05 +01:00
async presentAlertControl(options: AlertOptions) {
2023-02-24 17:28:40 +01:00
let alert = await this.alertCtrl.create(options);
await alert.present();
}
2023-02-23 18:11:45 +01:00
async presentModal(options: ModalOptions) {
let modal = await this.modalCtrl.create(options);
await modal.present();
}
2023-02-25 12:53:05 +01:00
2023-02-26 16:34:34 +01:00
async presentToast(text: string) {
const toast = await this.toastController.create({
message: text,
duration: 1500
});
await toast.present();
}
async actionHandler(action: Action, item: WSItem) {
2023-02-26 16:34:34 +01:00
var alertOptions: undefined | AlertOptions = action.getAlertOptions(item, this.storagehub, () => this.loadDocuments());
if (alertOptions) {
await this.presentAlertControl(alertOptions);
} else {
2023-02-26 16:34:34 +01:00
var modalOptions: undefined | ModalOptions = action.getModalOptions(item, this.storagehub,
2023-03-13 17:39:14 +01:00
(itemId: string) => this.event.ReloadEvent.emit(itemId), (text: string) => this.presentToast(text));
if (modalOptions)
await this.presentModal(modalOptions);
else
action.actionHandler(item, this.storagehub);
}
2023-02-23 18:11:45 +01:00
}
2023-02-16 17:18:54 +01:00
}