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

90 lines
2.1 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Item } from '../model/item.model';
import { WSItem } from '../model/ws-item';
import { StoragehubService } from '../storagehub.service';
@Component({
selector: 'app-vres',
templateUrl: 'vres.page.html',
styleUrls: ['vres.page.scss'],
providers: [StoragehubService]
})
export class VresPage implements OnInit {
values: WSItem[] | undefined;
item: WSItem | undefined;
root: boolean = false;
private static cashItems: WSItem[] = [];
constructor(private storagehub: StoragehubService,
private router: Router,
private route: ActivatedRoute) { }
ngOnInit() {
var folderId: string | undefined = this.route.snapshot.paramMap.get('folderId') || undefined;
var tmpItem: WSItem | undefined = undefined;
if (folderId)
tmpItem = VresPage.cashItems.find((value) => value.item.id === folderId);
this.root = !folderId;
if (!tmpItem) {
if (!folderId)
this.storagehub.getVres().subscribe(
(res) => {
const tmpItems$: WSItem[] = []
res.forEach(i => tmpItems$.push(new WSItem(i)));
this.values = tmpItems$;
}
);
else //folder is not cached
this.storagehub.getItem(folderId).subscribe(
(res) => {
this.item = new WSItem(res);
this.onSuccess(res)
}
);
} else {
this.item = tmpItem;
this.onSuccess(tmpItem.item);
}
}
private onSuccess(item: Item) {
this.storagehub.getChildren(item.id, false).subscribe(
(res) => {
const tmpItems$: WSItem[] = []
res.forEach(i => tmpItems$.push(new WSItem(i)));
this.values = tmpItems$;
}
)
}
public getValues(): WSItem[] | undefined {
return this.values;
}
openFolder(item: WSItem) {
if (this.values)
VresPage.cashItems = this.values;
this.router.navigateByUrl(`tabs/vres/${item.item.id}`);
}
public getCurrentItem(): WSItem | undefined {
return this.item;
}
getTitle() {
return this.root ? "My VREs" : this.item?.getTitle()
}
}