workspace-ionic-app/src/app/ws/ws.page.ts

100 lines
2.6 KiB
TypeScript
Raw Normal View History

2023-02-16 17:18:54 +01:00
import { Component, OnInit } from '@angular/core';
2023-03-14 15:56:50 +01:00
import { AlertController} from '@ionic/angular';
2023-02-16 17:18:54 +01:00
import { Router, ActivatedRoute } from '@angular/router';
2023-02-20 18:06:39 +01:00
import { StoragehubService } from '../storagehub.service';
2023-02-23 18:11:45 +01:00
import { WSItem } from '../model/ws-item';
2023-02-24 17:28:40 +01:00
import { Item } from '../model/item.model';
2023-03-14 15:56:50 +01:00
import { presentConnectionAlert } from '../_helper/utils';
2023-02-16 17:18:54 +01:00
@Component({
selector: 'app-ws',
2023-02-20 18:06:39 +01:00
providers: [StoragehubService],
2023-02-16 17:18:54 +01:00
templateUrl: 'ws.page.html',
2023-02-20 18:06:39 +01:00
styleUrls: ['ws.page.scss'],
2023-02-16 17:18:54 +01:00
})
2023-02-20 18:06:39 +01:00
export class WsPage implements OnInit {
2023-02-16 17:18:54 +01:00
2023-03-01 12:06:56 +01:00
values: WSItem[] | undefined = undefined;
2023-02-20 18:06:39 +01:00
2023-03-01 12:06:56 +01:00
item: WSItem | undefined;
2023-02-20 18:06:39 +01:00
2023-02-25 12:53:05 +01:00
root: boolean = false;
2023-03-01 12:06:56 +01:00
private static cashItems: WSItem[] = [];
2023-02-20 18:06:39 +01:00
constructor(private storagehub: StoragehubService,
2023-02-16 17:18:54 +01:00
private router: Router,
2023-03-14 15:56:50 +01:00
private route: ActivatedRoute,
private alertCtrl: AlertController) { }
2023-02-20 18:06:39 +01:00
ngOnInit() {
2023-03-01 12:06:56 +01:00
var folderId: string | undefined = this.route.snapshot.paramMap.get('folderId') || undefined;
var tmpItem: WSItem | undefined = undefined;
if (folderId)
tmpItem = WsPage.cashItems.find((value) => value.item.id === folderId);
this.root = !folderId;
if (!tmpItem) {
if (!folderId)
2023-03-14 15:56:50 +01:00
this.storagehub.getWsRoot().then( (obs) => obs.subscribe({
next: (res) => {
2023-03-01 12:06:56 +01:00
this.item = new WSItem(res);
this.onSuccess(res)
2023-03-14 15:56:50 +01:00
},
error: (err) => presentConnectionAlert(err, this.alertCtrl)
}
))
2023-03-01 12:06:56 +01:00
else //folder is not cached
2023-03-14 15:56:50 +01:00
this.storagehub.getItem(folderId).then( (obs) => obs.subscribe({
next: (res) => {
2023-03-01 12:06:56 +01:00
this.item = new WSItem(res);
this.onSuccess(res)
2023-03-14 15:56:50 +01:00
},
error: err => presentConnectionAlert(err, this.alertCtrl)
}
))
2023-03-01 12:06:56 +01:00
} else {
this.item = tmpItem;
this.onSuccess(tmpItem.item);
}
2023-02-20 18:06:39 +01:00
}
2023-02-16 17:18:54 +01:00
2023-02-20 18:06:39 +01:00
private onSuccess(item: Item) {
2023-03-01 12:06:56 +01:00
2023-03-14 15:56:50 +01:00
this.storagehub.getChildren(item.id, false).then( (obs) => obs.subscribe({
next: (res) => {
2023-03-01 12:06:56 +01:00
const tmpItems$: WSItem[] = []
2023-02-24 17:28:40 +01:00
res.forEach(i => tmpItems$.push(new WSItem(i)));
this.values = tmpItems$;
2023-03-14 15:56:50 +01:00
},
error: (err) => presentConnectionAlert(err, this.alertCtrl),
})
)
2023-02-16 17:18:54 +01:00
}
2023-03-01 12:06:56 +01:00
public getValues(): WSItem[] | undefined {
return this.values;
2023-02-16 17:18:54 +01:00
}
2023-02-24 17:28:40 +01:00
openFolder(item: WSItem) {
2023-03-01 12:06:56 +01:00
if (this.values)
WsPage.cashItems = this.values;
2023-02-24 17:28:40 +01:00
this.router.navigateByUrl(`tabs/ws/${item.item.id}`);
2023-02-16 17:18:54 +01:00
}
2023-03-01 12:06:56 +01:00
public getCurrentItem(): WSItem | undefined {
return this.item;
}
getTitle() {
return this.root ? "My Workspace" : this.item?.getTitle()
2023-02-20 18:06:39 +01:00
}
2023-03-14 15:56:50 +01:00
2023-03-21 16:21:39 +01:00
enableCreateOperations() : boolean {
return this.getCurrentItem() != undefined;
}
2023-02-16 17:18:54 +01:00
}