From 3d40c43cc5f4a5266c759d8033254730dc895206 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Sat, 25 Feb 2023 12:53:05 +0100 Subject: [PATCH] rename action added --- src/app/model/actions.ts | 51 ++++++++++++++++--- .../show-folder/show-folder.component.html | 6 +-- src/app/show-folder/show-folder.component.ts | 49 +++++++++--------- src/app/storagehub.service.ts | 9 +++- src/app/trash/trash.page.html | 2 +- src/app/vres/vres.page.html | 2 +- src/app/vres/vres.page.ts | 3 ++ src/app/ws/ws.page.html | 2 +- src/app/ws/ws.page.ts | 8 ++- 9 files changed, 95 insertions(+), 37 deletions(-) diff --git a/src/app/model/actions.ts b/src/app/model/actions.ts index b3c8c4c..cfef9a7 100644 --- a/src/app/model/actions.ts +++ b/src/app/model/actions.ts @@ -14,8 +14,6 @@ export interface Action { getName(): string; getActionType(): string | undefined; - - mustReload(): boolean; } export class DeleteAction implements Action { @@ -32,7 +30,7 @@ export class DeleteAction implements Action { text: 'Yes', handler: () => { this.actionHandler(item, storagehub).subscribe( - reload() + () => reload() ); } }] @@ -51,14 +49,55 @@ export class DeleteAction implements Action { return "destructive"; } - mustReload(): boolean { - return true; +} + +export class RenameAction implements Action { + + getAlertOptions(item: WSItem, storagehub: StoragehubService, reload: Function): AlertOptions { + var title = item.getTitle(); + var options: AlertOptions = { + header: 'Rename Item', + message: 'Please specify the new name', + inputs: [ + { + name: 'name', + type: 'text', + placeholder: item.getTitle() + } + ], + buttons: [ + { + text: 'Cancel', + role: 'cancel' + }, + { + text: 'Rename', + handler: (data) => { + this.actionHandler({item: item, newName: data.name}, storagehub).subscribe( + () => reload() + ); + } + } + ] + }; + return options; + + } + actionHandler(data: {item: WSItem, newName : string}, storagehub: StoragehubService): Observable { + return storagehub.renameItem(data.item.item.id, data.newName); + } + + getName(): string { + return "Rename"; + } + getActionType(): string | undefined { + return undefined; } } export class Actions { - private static actions: Action[] = [new DeleteAction()]; + private static actions: Action[] = [new DeleteAction(), new RenameAction()]; public static getActionsPerType(type: string): Action[] { /*switch (type) { case 'SharedFolder': diff --git a/src/app/show-folder/show-folder.component.html b/src/app/show-folder/show-folder.component.html index 3f93e8b..72b4236 100644 --- a/src/app/show-folder/show-folder.component.html +++ b/src/app/show-folder/show-folder.component.html @@ -1,11 +1,11 @@ - - + + arrow_back_ios_new - {{ parentItem ? parentItem.getTitle() : tabName }} + {{ !root && parentItem ? parentItem.getTitle() : tabName }} diff --git a/src/app/show-folder/show-folder.component.ts b/src/app/show-folder/show-folder.component.ts index ddeccb7..fcbb3ab 100644 --- a/src/app/show-folder/show-folder.component.ts +++ b/src/app/show-folder/show-folder.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, Input, CUSTOM_ELEMENTS_SCHEMA, Output, EventEmitter, ElementRef, ViewChild } from '@angular/core'; +import { Component, OnInit, Input, CUSTOM_ELEMENTS_SCHEMA, Output, EventEmitter, ElementRef, ViewChild, ChangeDetectorRef } from '@angular/core'; import { Platform, AlertController, ToastController, IonicModule, ActionSheetController, ActionSheetButton, AlertOptions } from '@ionic/angular'; @@ -28,35 +28,38 @@ export class ShowFolderComponent implements OnInit { @Output() folderClickedEvent = new EventEmitter(); @Input() items: WSItem[] = []; - + @Input() parentItem: WSItem | undefined; @Input() tabName: string = ""; + @Input() root: boolean = false; + @ViewChild('filepicker') uploader!: ElementRef; constructor( private actionSheetCtrl: ActionSheetController, private storagehub: StoragehubService, - private alertCtrl: AlertController + private alertCtrl: AlertController, + private ref: ChangeDetectorRef ) { } ngOnInit(): void { } - + loadDocuments() { - console.log("loadDoc called"); - if (this.parentItem) - this.storagehub.getChildren(this.parentItem.item.id, false).subscribe( + if (this.parentItem) { + console.log("loadDoc called"); + this.storagehub.getChildren(this.parentItem.item.id).subscribe( (res) => { - const tmpItems$ : WSItem[] = [] + const tmpItems$ : WSItem[] = [] res.forEach(i => tmpItems$.push(new WSItem(i))); this.items = tmpItems$; - } - ) + }) + } } - + itemClicked(item: WSItem) { if (item.isFolder()) { this.folderClickedEvent.emit(item); @@ -112,8 +115,8 @@ export class ShowFolderComponent implements OnInit { } async presentActionSheet(item: WSItem) { - var buttons :ActionSheetButton[] = []; - Actions.getActionsPerType(item.type).forEach( action => buttons.push({ + var buttons: ActionSheetButton[] = []; + Actions.getActionsPerType(item.type).forEach(action => buttons.push({ text: action.getName(), data: { action: action.getName(), @@ -121,7 +124,7 @@ export class ShowFolderComponent implements OnInit { role: action.getActionType(), handler: () => this.actionHandler(action, item) })); - + buttons.push({ text: 'Cancel', role: 'cancel', @@ -131,7 +134,7 @@ export class ShowFolderComponent implements OnInit { }); const actionSheet = await this.actionSheetCtrl.create({ - + header: `${item.item.title}`, mode: 'ios', translucent: true, @@ -143,18 +146,18 @@ export class ShowFolderComponent implements OnInit { const result = await actionSheet.onDidDismiss(); } - async presentAlertControl(options: AlertOptions){ + async presentAlertControl(options: AlertOptions) { let alert = await this.alertCtrl.create(options); await alert.present(); } - async actionHandler(action: Action, item: WSItem){ - var options: AlertOptions = action.getAlertOptions(item, this.storagehub, () => {this.loadDocuments()}); - if (options){ - await this.presentAlertControl(options); - }else - action.actionHandler(item, this.storagehub); - + async actionHandler(action: Action, item: WSItem) { + var options: AlertOptions = action.getAlertOptions(item, this.storagehub, () => { this.loadDocuments() }); + if (options) { + await this.presentAlertControl(options); + } else + action.actionHandler(item, this.storagehub); + } } diff --git a/src/app/storagehub.service.ts b/src/app/storagehub.service.ts index d873655..99a6de4 100644 --- a/src/app/storagehub.service.ts +++ b/src/app/storagehub.service.ts @@ -92,12 +92,19 @@ export class StoragehubService { deleteItem(itemId: string) : Observable { let deleteItemUrl = `${shURL}/items/${itemId}?gcube-token=${token}`; - console.log("delete item done with url "+deleteItemUrl); return this.http.delete( deleteItemUrl).pipe( catchError(this.error) ); } + renameItem(itemId: string, newName: string) : Observable { + let renameItemUrl = `${shURL}/items/${itemId}/rename?gcube-token=${token}`; + let body = `newName=${newName}`; + return this.http.put(renameItemUrl,body,{observe: 'body', responseType: 'text' ,headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }}).pipe( + catchError(this.error) + ); + } + error(error: HttpErrorResponse) { let errorMessage = ''; if (error.error instanceof ErrorEvent) { diff --git a/src/app/trash/trash.page.html b/src/app/trash/trash.page.html index 705a378..0bd6c2f 100644 --- a/src/app/trash/trash.page.html +++ b/src/app/trash/trash.page.html @@ -1,4 +1,4 @@ - + diff --git a/src/app/vres/vres.page.html b/src/app/vres/vres.page.html index d8902ad..9aaad82 100644 --- a/src/app/vres/vres.page.html +++ b/src/app/vres/vres.page.html @@ -1,3 +1,3 @@ - + diff --git a/src/app/vres/vres.page.ts b/src/app/vres/vres.page.ts index 8151609..09a03a9 100644 --- a/src/app/vres/vres.page.ts +++ b/src/app/vres/vres.page.ts @@ -18,6 +18,8 @@ export class VresPage implements OnInit { folderid: string | undefined; + root: boolean = false; + constructor(private storagehub: StoragehubService, private router: Router, private route: ActivatedRoute) { } @@ -25,6 +27,7 @@ export class VresPage implements OnInit { ngOnInit() { this.folderid = this.route.snapshot.paramMap.get('folderid') || undefined; + this.root = !this.folderid; if (!this.folderid) this.storagehub.getVres().subscribe( (res) => { diff --git a/src/app/ws/ws.page.html b/src/app/ws/ws.page.html index dea960b..ec5fa5a 100644 --- a/src/app/ws/ws.page.html +++ b/src/app/ws/ws.page.html @@ -1,5 +1,5 @@ - + diff --git a/src/app/ws/ws.page.ts b/src/app/ws/ws.page.ts index 382aaee..ae3b1d0 100644 --- a/src/app/ws/ws.page.ts +++ b/src/app/ws/ws.page.ts @@ -20,15 +20,21 @@ export class WsPage implements OnInit { item : WSItem | undefined ; + root: boolean = false; + constructor(private storagehub: StoragehubService, private router: Router, private route: ActivatedRoute) { } ngOnInit() { this.folderid = this.route.snapshot.paramMap.get('folderid') || undefined; + this.root = !this.folderid; if (!this.folderid) this.storagehub.getWsRoot().subscribe( - (res) => this.onSuccess(res) + (res) => { + this.item = new WSItem(res); + this.onSuccess(res) + } ); else this.storagehub.getItem(this.folderid).subscribe(