[Trunk|Library]: Add LayoutService
git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@55008 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
parent
ea267d003b
commit
55693142a7
|
@ -0,0 +1,48 @@
|
|||
import {Injectable} from '@angular/core';
|
||||
import {Http, Response} from '@angular/http';
|
||||
import {DivId} from '../../domain/divId';
|
||||
import {Observable} from 'rxjs/Observable';
|
||||
import {CustomOptions} from './servicesUtils/customOptions.class';
|
||||
|
||||
@Injectable()
|
||||
export class LayoutService {
|
||||
constructor(private http: Http) {
|
||||
}
|
||||
|
||||
static removeNulls(obj) {
|
||||
const isArray = obj instanceof Array;
|
||||
for (let k in obj) {
|
||||
if (obj[k] === null || obj[k] === '') {
|
||||
isArray ? obj.splice(k, 1) : delete obj[k];
|
||||
} else if (typeof obj[k] === 'object') {
|
||||
LayoutService.removeNulls(obj[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
saveLayout(pid: string, url: string, layout: any) {
|
||||
LayoutService.removeNulls(layout);
|
||||
return this.http.post(url + 'community/' + pid + '/updateLayout', JSON.stringify(layout), CustomOptions.getAuthOptionsWithBody())
|
||||
.map(res => <DivId> res.json())
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
getLayout(pid: string, url: string) {
|
||||
return this.http.get(url + 'community/' + pid + '/layout')
|
||||
.map(res => <DivId> res.json())
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
loadDefaultLayout(pid: string, url: string) {
|
||||
return this.http.post(url + 'community/' + pid + '/resetLayout', null, CustomOptions.getAuthOptionsWithBody())
|
||||
.map(res => <DivId> res.json())
|
||||
.catch(this.handleError);
|
||||
}
|
||||
|
||||
private handleError(error: Response) {
|
||||
// in a real world app, we may send the error to some remote logging infrastructure
|
||||
// instead of just logging it to the console
|
||||
console.error(error);
|
||||
return Observable.throw(error.json().error || 'Server error');
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue