From ee0032464b347aeb4f367878b8c0cdb74d3050eb Mon Sep 17 00:00:00 2001 From: Mathilde Date: Sun, 11 Nov 2018 17:54:35 +0900 Subject: [PATCH] add support for guillotina @duplicate --- src/services/resource.service.ts | 39 ++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/services/resource.service.ts b/src/services/resource.service.ts index e1ef314..a13ce73 100644 --- a/src/services/resource.service.ts +++ b/src/services/resource.service.ts @@ -14,6 +14,7 @@ import { Vocabulary } from '../vocabularies'; import { APIService } from './api.service'; import { CacheService } from './cache.service'; import { ConfigurationService } from './configuration.service'; +import { ReplaySubject } from 'rxjs'; interface NavigationItem { title: string; @@ -35,6 +36,8 @@ export class ResourceService { } | null> = new EventEmitter(); traversingUnauthorized: EventEmitter = new EventEmitter(); + copySource: ReplaySubject = new ReplaySubject(1); + public static getSearchQueryString( query: { [key: string]: any }, options: SearchOptions = {}, @@ -114,16 +117,48 @@ export class ResourceService { }); } + /** + * Copy a resource (Plone REST API) + * @param sourcePath + * @param targetPath + */ copy(sourcePath: string, targetPath: string) { - const path = targetPath + '/@copy'; return this.emittingModified( this.api.post(targetPath + '/@copy', { source: this.api.getFullPath(sourcePath), }), - path, + targetPath, ); } + /** + * Copy a resource (same as copy, but in Guillotina) + * @param sourcePath + * @param targetPath + */ + duplicate(sourcePath: string, targetPath: string): Observable { + const url = sourcePath + '/@duplicate'; + const containerInPath: string = this.getContainerInPath(targetPath); + const newId: string = sourcePath.split('/').pop() || ''; + return this.emittingModified( + this.api.post(url, { + destination: containerInPath, + new_id: newId, + }), + targetPath, + ); + } + + private getContainerInPath(targetPath: string): string { + if (targetPath.indexOf('http') === 0) { + const startPosition: number = targetPath.indexOf('https') === 0 ? 8 : 7; + const startIndex: number = targetPath.indexOf('/', startPosition); + targetPath = targetPath.substring(startIndex); + } + + return '/' + targetPath.split('/').slice(3).join('/'); + } + create(path: string, model: any) { return this.emittingModified(this.api.post(path, model), path); }