Skip to content

Commit

Permalink
add support for guillotina @Duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathilde committed Nov 11, 2018
1 parent 375efc8 commit ee00324
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/services/resource.service.ts
Expand Up @@ -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;
Expand All @@ -35,6 +36,8 @@ export class ResourceService {
} | null> = new EventEmitter();
traversingUnauthorized: EventEmitter<string> = new EventEmitter();

copySource: ReplaySubject<string> = new ReplaySubject<string>(1);

public static getSearchQueryString(
query: { [key: string]: any },
options: SearchOptions = {},
Expand Down Expand Up @@ -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<any> {
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);
}
Expand Down

0 comments on commit ee00324

Please sign in to comment.