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

152 lines
3.7 KiB
TypeScript
Raw Normal View History

2023-02-23 18:11:45 +01:00
import { Component, OnInit, Input, CUSTOM_ELEMENTS_SCHEMA, Output, EventEmitter, ElementRef, ViewChild } from '@angular/core';
2023-02-16 17:18:54 +01:00
2023-02-23 18:11:45 +01:00
import { Platform, AlertController, ToastController, IonicModule, ActionSheetController, ActionSheetButton } from '@ionic/angular';
2023-02-16 17:18:54 +01:00
import { FileOpener } from '@awesome-cordova-plugins/file-opener/ngx';
import { Router, ActivatedRoute } from '@angular/router';
import { StoragehubService } from '../storagehub.service';
import { CommonModule } from '@angular/common';
2023-02-20 18:06:39 +01:00
import { Item } from '../model/item.model';
2023-02-23 18:11:45 +01:00
import { MatIconModule } from '@angular/material/icon';
import { WSItem } from '../model/ws-item';
2023-02-16 17:18:54 +01:00
@Component({
standalone: true,
selector: 'show-folder',
providers: [FileOpener, StoragehubService],
templateUrl: './show-folder.component.html',
styleUrls: ['./show-folder.component.scss'],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [
2023-02-23 18:11:45 +01:00
CommonModule, IonicModule, MatIconModule
2023-02-16 17:18:54 +01:00
]
})
export class ShowFolderComponent implements OnInit {
2023-02-20 18:06:39 +01:00
@Output() folderClickedEvent = new EventEmitter<Item>();
2023-02-23 18:11:45 +01:00
@Input() items: Item[] = [];
2023-02-20 18:06:39 +01:00
2023-02-23 18:11:45 +01:00
wsItems: WSItem[] = [];
@Input() parentItem: WSItem | undefined;
2023-02-20 18:06:39 +01:00
@Input() tabName: string = "";
2023-02-16 17:18:54 +01:00
2023-02-23 18:11:45 +01:00
@ViewChild('filepicker') uploader!: ElementRef;
2023-02-16 17:18:54 +01:00
constructor(
2023-02-23 18:11:45 +01:00
private actionSheetCtrl: ActionSheetController,
private storagehub: StoragehubService,
private alertCtrl: AlertController
2023-02-20 18:06:39 +01:00
) { }
2023-02-23 18:11:45 +01:00
ngOnInit(): void {
2023-02-20 18:06:39 +01:00
}
2023-02-23 18:11:45 +01:00
ngOnChanges() {
this.items.forEach(i => this.wsItems.push(new WSItem(i, this.storagehub)) );
2023-02-20 18:06:39 +01:00
}
2023-02-16 17:18:54 +01:00
2023-02-23 18:11:45 +01:00
loadDocuments() {
if (this.parentItem)
this.storagehub.getChildren(this.parentItem.item.id, false).subscribe(
(res) => {
this.items = res;
this.items.forEach(i => this.wsItems.push(new WSItem(i, this.storagehub)) );
}
)
2023-02-16 17:18:54 +01:00
}
2023-02-23 18:11:45 +01:00
itemClicked(item: WSItem) {
if (item.isFolder()) {
this.folderClickedEvent.emit(item.item);
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() {
console.log("add fiel called");
this.uploader.nativeElement.click();
}
async fileSelected($event: any) {
const selected = $event.target.files[0];
if (selected && this.parentItem) {
const upload$ = this.storagehub.uploadFile(this.parentItem.item.id, selected.name, selected);
upload$.subscribe(
suc => this.loadDocuments()
);
2023-02-20 18:06:39 +01:00
}
2023-02-23 18:11:45 +01:00
}
async createFolder() {
let alert = await this.alertCtrl.create({
header: 'Create folder',
message: 'Please specify the name of the new folder',
inputs: [
{
name: 'name',
type: 'text',
placeholder: 'new folder'
}
],
buttons: [
{
text: 'Cancel',
role: 'cancel'
},
{
text: 'Create',
handler: (data) => {
if (this.parentItem) {
const createFolder$ = this.storagehub.createFolder(this.parentItem.item.id, data.name, "");
createFolder$.subscribe(
suc => this.loadDocuments()
);
}
}
}
]
});
await alert.present();
2023-02-20 18:06:39 +01:00
}
2023-02-23 18:11:45 +01:00
async presentActionSheet(item: WSItem) {
var buttons :ActionSheetButton<any>[] = [];
item.getActions().forEach( action => buttons.push({
text: action.name,
data: {
action: action.name,
},
handler: () => action.execute()
}));
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-16 17:18:54 +01:00
}