243 lines
7.2 KiB
TypeScript
243 lines
7.2 KiB
TypeScript
import { Component, OnInit, Input, CUSTOM_ELEMENTS_SCHEMA, Output, EventEmitter, ElementRef, ViewChild } from '@angular/core';
|
|
|
|
import { AlertController, ToastController, IonicModule, ActionSheetController, ActionSheetButton, AlertOptions, ModalController, ModalOptions } from '@ionic/angular';
|
|
|
|
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
|
|
import { StoragehubService } from '../storagehub.service';
|
|
import { CommonModule } from '@angular/common';
|
|
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';
|
|
import { ItemsListComponent } from '../items-list/items-list.component';
|
|
import { Sorting, SortName, SortType } from '../model/sorting';
|
|
|
|
@Component({
|
|
standalone: true,
|
|
selector: 'show-folder',
|
|
providers: [FileOpener, StoragehubService],
|
|
templateUrl: './show-folder.component.html',
|
|
styleUrls: ['./show-folder.component.scss'],
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA],
|
|
imports: [
|
|
CommonModule, IonicModule, MatIconModule, ItemsListComponent
|
|
]
|
|
})
|
|
export class ShowFolderComponent implements OnInit {
|
|
|
|
@Output() folderClickedEvent = new EventEmitter<WSItem>();
|
|
|
|
filteredItems: WSItem[] | undefined;
|
|
_items: WSItem[] | undefined;
|
|
|
|
currentSortName = SortName.Name;
|
|
currentSortType = SortType.Asc;
|
|
|
|
@Input() set items(items: WSItem[] | undefined) {
|
|
this._items = items?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
|
|
this.filteredItems = this._items;
|
|
}
|
|
|
|
@Input() parentItem: WSItem | undefined = undefined;
|
|
|
|
@Input() title: string | undefined;
|
|
|
|
@Input() root: boolean = false;
|
|
|
|
underUploadItem: string[] = [];
|
|
|
|
selectedSegment = "all";
|
|
|
|
public get sortName(): typeof SortName {
|
|
return SortName;
|
|
}
|
|
|
|
@ViewChild('filepicker') uploader!: ElementRef;
|
|
|
|
customSortAlertOptions = {
|
|
header: 'Sort by',
|
|
translucent: true,
|
|
};
|
|
|
|
constructor(
|
|
private actionSheetCtrl: ActionSheetController,
|
|
private storagehub: StoragehubService,
|
|
private alertCtrl: AlertController,
|
|
private modalCtrl: ModalController,
|
|
private toastController: ToastController
|
|
) {
|
|
}
|
|
|
|
|
|
ngOnInit(): void {
|
|
}
|
|
|
|
loadDocuments() {
|
|
this.filteredItems = undefined;
|
|
if (this.parentItem) {
|
|
this.storagehub.getChildren(this.parentItem.item.id).subscribe(
|
|
(res) => {
|
|
const tmpItems$: WSItem[] = []
|
|
const tmpFiltered$: WSItem[] = []
|
|
var segmentFilterFunction = this.getSegmentFilterFunction();
|
|
res.forEach(i => {
|
|
var localItem = new WSItem(i);
|
|
tmpItems$.push(localItem);
|
|
if (segmentFilterFunction(localItem))
|
|
tmpFiltered$.push(localItem);
|
|
});
|
|
this._items = tmpItems$.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
|
|
this.filteredItems = tmpFiltered$.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
|
|
})
|
|
}
|
|
}
|
|
|
|
getSegmentFilterFunction(): (value: WSItem) => unknown {
|
|
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;
|
|
this.filteredItems = undefined;
|
|
this.selectedSegment = value;
|
|
if (this.selectedSegment == "all")
|
|
this.filteredItems = this._items;
|
|
else {
|
|
const tmpFiltered$: WSItem[] = []
|
|
this._items?.filter(this.getSegmentFilterFunction()).forEach((i) => tmpFiltered$.push(i));
|
|
this.filteredItems = tmpFiltered$;
|
|
}
|
|
}
|
|
|
|
changeSortType() {
|
|
if (this.currentSortType == SortType.Asc)
|
|
this.currentSortType = SortType.Desc;
|
|
else this.currentSortType = SortType.Asc;
|
|
this.updateSort();
|
|
}
|
|
|
|
changeSortName($event: any) {
|
|
var sort : SortName = $event.target.value;
|
|
if (this.currentSortName != sort){
|
|
this.currentSortName = sort;
|
|
this.updateSort();
|
|
}
|
|
}
|
|
|
|
updateSort() {
|
|
this._items = this._items?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
|
|
this.filteredItems = this.filteredItems?.sort(Sorting.getSortFunction(this.currentSortName, this.currentSortType));
|
|
}
|
|
|
|
itemClicked(item: WSItem) {
|
|
if (item.isFolder()) {
|
|
this.folderClickedEvent.emit(item);
|
|
}
|
|
}
|
|
|
|
addFile() {
|
|
this.uploader.nativeElement.click();
|
|
}
|
|
|
|
async fileSelected($event: any) {
|
|
const selected = $event.target.files[0];
|
|
|
|
if (selected && this.parentItem) {
|
|
this.underUploadItem = [selected.name];
|
|
const upload$ = this.storagehub.uploadFile(this.parentItem.item.id, selected.name, selected);
|
|
upload$.subscribe({
|
|
next: () => this.loadDocuments(),
|
|
error: () => this.underUploadItem = [],
|
|
complete: () => this.underUploadItem = []
|
|
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
async createFolder() {
|
|
var createFolderAction: CreateFolderAction = new CreateFolderAction();
|
|
if (this.parentItem)
|
|
this.actionHandler(createFolderAction, this.parentItem);
|
|
}
|
|
|
|
async presentActionSheet(item: WSItem) {
|
|
var buttons: ActionSheetButton<any>[] = [];
|
|
Actions.getActionsPerType(item.type).forEach(action => buttons.push({
|
|
text: action.getName(),
|
|
data: {
|
|
action: action.getName(),
|
|
},
|
|
role: action.getActionType(),
|
|
handler: () => this.actionHandler(action, item)
|
|
}));
|
|
|
|
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();
|
|
}
|
|
|
|
async presentAlertControl(options: AlertOptions) {
|
|
let alert = await this.alertCtrl.create(options);
|
|
await alert.present();
|
|
}
|
|
|
|
async presentModal(options: ModalOptions) {
|
|
let modal = await this.modalCtrl.create(options);
|
|
await modal.present();
|
|
}
|
|
|
|
async presentToast(text: string) {
|
|
const toast = await this.toastController.create({
|
|
message: text,
|
|
duration: 1500
|
|
});
|
|
await toast.present();
|
|
}
|
|
|
|
async actionHandler(action: Action, item: WSItem) {
|
|
var alertOptions: undefined | AlertOptions = action.getAlertOptions(item, this.storagehub, () => this.loadDocuments());
|
|
if (alertOptions) {
|
|
await this.presentAlertControl(alertOptions);
|
|
} else {
|
|
var modalOptions: undefined | ModalOptions = action.getModalOptions(item, this.storagehub,
|
|
() => this.loadDocuments(), (text: string) => this.presentToast(text));
|
|
if (modalOptions)
|
|
await this.presentModal(modalOptions);
|
|
else
|
|
action.actionHandler(item, this.storagehub);
|
|
}
|
|
}
|
|
|
|
}
|