From cb74165073d5a1ec6c32b81c7755f2da18816513 Mon Sep 17 00:00:00 2001 From: Lucio Lelii Date: Sun, 26 Feb 2023 13:10:00 +0100 Subject: [PATCH] move operation added --- src/app/model/actions/actions.ts | 3 +- src/app/model/actions/copy-item.ts | 17 +++++--- src/app/model/actions/move-item.ts | 39 +++++++++++++++++ src/app/storagehub.service.ts | 16 +++++++ src/app/ws-viewer/ws-viewer.component.html | 38 ++++++++++------- src/app/ws-viewer/ws-viewer.component.ts | 49 ++++++++++++++++++---- 6 files changed, 133 insertions(+), 29 deletions(-) create mode 100644 src/app/model/actions/move-item.ts diff --git a/src/app/model/actions/actions.ts b/src/app/model/actions/actions.ts index 57c2d40..141d8ab 100644 --- a/src/app/model/actions/actions.ts +++ b/src/app/model/actions/actions.ts @@ -1,11 +1,12 @@ import { Action } from "./action"; import { CopyAction } from "./copy-item"; import { DeleteAction } from "./delete-item"; +import { MoveAction } from "./move-item"; import { RenameAction } from "./rename-item"; export class Actions { - private static actions: Action[] = [new CopyAction(),new RenameAction(), new DeleteAction()]; + private static actions: Action[] = [new MoveAction(), new CopyAction(),new RenameAction(), new DeleteAction()]; public static getActionsPerType(type: string): Action[] { /*switch (type) { case 'SharedFolder': diff --git a/src/app/model/actions/copy-item.ts b/src/app/model/actions/copy-item.ts index ee88419..58f9c02 100644 --- a/src/app/model/actions/copy-item.ts +++ b/src/app/model/actions/copy-item.ts @@ -1,4 +1,4 @@ -import { AlertOptions, ModalOptions } from "@ionic/angular"; +import { ModalOptions } from "@ionic/angular"; import { Observable } from "rxjs"; import { StoragehubService } from "src/app/storagehub.service"; import { WsViewerComponent } from "src/app/ws-viewer/ws-viewer.component"; @@ -8,19 +8,26 @@ import { Action } from "./action"; export class CopyAction extends Action { - override getModalOptions(item: WSItem, storagehub: StoragehubService, postOp?: Function): ModalOptions | undefined { + override getModalOptions(item: WSItem, storagehub: StoragehubService, reload: Function): ModalOptions | undefined { return { component: WsViewerComponent, componentProps: { finishLabel: "Copy here", - title: "Copy "+item.getTitle() + title: "Select destination folder for "+item.getTitle(), + notClickableIds: [item.item.id], + notSelectableIds: [item.item.parentId], + onSelected: (destinationItem:WSItem) =>{ + this.actionHandler({item: item, destinationItem: destinationItem}, storagehub).subscribe( + () => reload() + ) + } } }; } - actionHandler(data: {item: WSItem, newName : string}, storagehub: StoragehubService): Observable { - return storagehub.renameItem(data.item.item.id, data.newName); + actionHandler(data: {item: WSItem, destinationItem : WSItem}, storagehub: StoragehubService): Observable { + return storagehub.copyItem(data.destinationItem.item.id, data.item.item.id, data.item.item.name); } getName(): string { diff --git a/src/app/model/actions/move-item.ts b/src/app/model/actions/move-item.ts new file mode 100644 index 0000000..f4aaba1 --- /dev/null +++ b/src/app/model/actions/move-item.ts @@ -0,0 +1,39 @@ +import { ModalOptions } from "@ionic/angular"; +import { Observable } from "rxjs"; +import { StoragehubService } from "src/app/storagehub.service"; +import { WsViewerComponent } from "src/app/ws-viewer/ws-viewer.component"; +import { WSItem } from "../ws-item"; +import { Action } from "./action"; + +export class MoveAction extends Action { + + + override getModalOptions(item: WSItem, storagehub: StoragehubService, reload: Function): ModalOptions | undefined { + return { + component: WsViewerComponent, + componentProps: { + finishLabel: "Move here", + title: "Select destination folder for "+item.getTitle(), + notClickableIds: [item.item.id], + notSelectableIds: [item.item.parentId], + onSelected: (destinationItem:WSItem) =>{ + this.actionHandler({item: item, destinationItem: destinationItem}, storagehub).subscribe( + () => reload() + ) + } + } + }; + } + + + actionHandler(data: {item: WSItem, destinationItem : WSItem}, storagehub: StoragehubService): Observable { + return storagehub.moveItem(data.destinationItem.item.id, data.item.item.id); + } + + getName(): string { + return "Move"; + } + getActionType(): string | undefined { + return undefined; + } +} \ No newline at end of file diff --git a/src/app/storagehub.service.ts b/src/app/storagehub.service.ts index 99a6de4..4f50fee 100644 --- a/src/app/storagehub.service.ts +++ b/src/app/storagehub.service.ts @@ -105,6 +105,22 @@ export class StoragehubService { ); } + copyItem(destinationId: string, itemId: string, name:string) : Observable { + let copyItemUrl = `${shURL}/items/${itemId}/copy?gcube-token=${token}`; + let body = `destinationId=${destinationId}&fileName=${name}`; + return this.http.put(copyItemUrl,body,{observe: 'body', responseType: 'text' ,headers: { 'Content-Type' : 'application/x-www-form-urlencoded' }}).pipe( + catchError(this.error) + ); + } + + moveItem(destinationId: string, itemId: string) : Observable { + let copyItemUrl = `${shURL}/items/${itemId}/move?gcube-token=${token}`; + let body = `destinationId=${destinationId}`; + return this.http.put(copyItemUrl,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/ws-viewer/ws-viewer.component.html b/src/app/ws-viewer/ws-viewer.component.html index 6f0ff34..104e487 100644 --- a/src/app/ws-viewer/ws-viewer.component.html +++ b/src/app/ws-viewer/ws-viewer.component.html @@ -1,29 +1,39 @@ - Cancel + close {{ title }} - -

Empty folder

-
- - - {{ i.getIconInfo() }} - - {{ i.getTitle() }} - - keyboard_arrow_right - - + + +

+

+ + arrow_back_ios + {{ currentItem?.getTitle() }} + +

+ +

Empty folder

+
+ + + {{ i.getIconInfo() }} + + {{ i.getTitle() }} + + keyboard_arrow_right + + +
- {{ finishLabel }} + {{ finishLabel }} \ No newline at end of file diff --git a/src/app/ws-viewer/ws-viewer.component.ts b/src/app/ws-viewer/ws-viewer.component.ts index 3d8d98d..f89c4da 100644 --- a/src/app/ws-viewer/ws-viewer.component.ts +++ b/src/app/ws-viewer/ws-viewer.component.ts @@ -1,7 +1,7 @@ import { CommonModule } from '@angular/common'; import { Component, Input, OnInit } from '@angular/core'; import { MatIconModule } from '@angular/material/icon'; -import { IonicModule, ModalController } from '@ionic/angular'; +import { IonicModule, LoadingController, ModalController } from '@ionic/angular'; import { WSItem } from '../model/ws-item'; import { StoragehubService } from '../storagehub.service'; @@ -17,20 +17,27 @@ import { StoragehubService } from '../storagehub.service'; }) export class WsViewerComponent implements OnInit { - constructor(private storagehub: StoragehubService, private modalCtrl: ModalController) { } + constructor(private storagehub: StoragehubService, private modalCtrl: ModalController, + private loadingCtrl: LoadingController) { } - @Input() toExcludeOnOperation: string[] = []; + @Input() notClickableIds: string[] = []; + @Input() notSelectableIds: string[] = []; @Input() finishLabel: string = "Confirm"; @Input() title: string = "Operation"; + @Input() onSelected :Function = () => {}; + currentItem: WSItem | undefined; - items: WSItem[] = [] + anchestors: WSItem[] | undefined ; + items: WSItem[] | undefined = undefined; + + ngOnInit() { - this.loadDocuments(); + this.loadDocuments(); } - + loadDocuments() { if (this.currentItem) { this.storagehub.getChildren(this.currentItem.item.id).subscribe( @@ -43,7 +50,6 @@ export class WsViewerComponent implements OnInit { this.storagehub.getWsRoot().subscribe( (res) => this.addVresToWs(new WSItem(res)) ); - } addVresToWs(wsRoot: WSItem) { @@ -55,24 +61,49 @@ export class WsViewerComponent implements OnInit { }) } + isCurrentItemSelectable(){ + return this.currentItem && !this.notSelectableIds.includes(this.currentItem.item.id); + } + + isItemClickable(item: WSItem){ + return item.isFolder() && !this.notClickableIds.includes(item.item.id); + } + itemClicked(item: WSItem) { + this.items = undefined; + if (this.currentItem) + this.anchestors?.push(this.currentItem); + else this.anchestors =[] this.currentItem = item; this.ngOnInit(); } + backClicked() { + this.items = undefined; + if (this.anchestors?.length == 0){ + this.anchestors = undefined; + this.currentItem = undefined; + } else { + var tmpitem = this.anchestors?.pop(); + this.currentItem = tmpitem; + } + this.ngOnInit(); + } + cancel() { return this.modalCtrl.dismiss(null, 'cancel'); } confirm() { + this.onSelected(this.currentItem); return this.modalCtrl.dismiss(null, 'confirm'); } sort(item1: WSItem, item2: WSItem) { if (item1.isFolder() && !item2.isFolder()) return -1; if (!item1.isFolder() && item2.isFolder()) return 1; - if (item1.item.title > item2.item.title) return -1; - if (item1.item.title < item2.item.title) return 1; + if (item1.item.title > item2.item.title) return 1; + if (item1.item.title < item2.item.title) return -1; return 0; } }