2019-03-18 16:02:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
2019-07-22 16:15:23 +02:00
|
|
|
import {throwError} from 'rxjs';
|
2019-03-18 16:02:46 +01:00
|
|
|
import {CustomOptions} from './servicesUtils/customOptions.class';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {catchError} from "rxjs/operators";
|
2019-03-18 16:02:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class LayoutService {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient) {
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => res.json())
|
|
|
|
.pipe(catchError(this.handleError));
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
getLayout(pid: string, url: string) {
|
|
|
|
return this.http.get(url + 'community/' + pid + '/layout')
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => res.json())
|
|
|
|
.pipe(catchError(this.handleError));
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loadDefaultLayout(pid: string, url: string) {
|
|
|
|
return this.http.post(url + 'community/' + pid + '/resetLayout', null, CustomOptions.getAuthOptionsWithBody())
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => res.json())
|
|
|
|
.pipe(catchError(this.handleError));
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
2019-07-22 16:15:23 +02:00
|
|
|
mockLayout():any {
|
|
|
|
|
|
|
|
|
|
|
|
return this.http.get("./assets/customizationOptions.json") ;
|
|
|
|
|
|
|
|
}
|
2019-03-18 16:02:46 +01:00
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
private handleError(error: HttpErrorResponse) {
|
2019-03-18 16:02:46 +01:00
|
|
|
// 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);
|
2019-06-03 15:20:36 +02:00
|
|
|
return throwError(error.error || 'Server error');
|
2019-03-18 16:02:46 +01:00
|
|
|
}
|
|
|
|
}
|