Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3
import {throwError} from 'rxjs';
4
import {CustomOptions} from './servicesUtils/customOptions.class';
5
import {catchError} from "rxjs/operators";
6

    
7
@Injectable()
8
export class LayoutService {
9
    constructor(private http: HttpClient) {
10
    }
11

    
12
    static removeNulls(obj) {
13
        const isArray = obj instanceof Array;
14
        for (let k in obj) {
15
            if (obj[k] === null || obj[k] === '') {
16
                isArray ? obj.splice(k, 1) : delete obj[k];
17
            } else if (typeof obj[k] === 'object') {
18
                LayoutService.removeNulls(obj[k]);
19
            }
20
        }
21
    }
22

    
23
    saveLayout(pid: string, url: string, layout: any) {
24
        LayoutService.removeNulls(layout);
25
        return this.http.post(url + 'community/' + pid + '/updateLayout', JSON.stringify(layout), CustomOptions.getAuthOptionsWithBody())
26
            //.map(res => res.json())
27
            .pipe(catchError(this.handleError));
28
    }
29

    
30
    getLayout(pid: string, url: string) {
31
        return this.http.get(url + 'community/' + pid + '/layout')
32
            //.map(res => res.json())
33
            .pipe(catchError(this.handleError));
34
    }
35

    
36
    loadDefaultLayout(pid: string, url: string) {
37
        return this.http.post(url + 'community/' + pid + '/resetLayout', null, CustomOptions.getAuthOptionsWithBody())
38
            //.map(res => res.json())
39
            .pipe(catchError(this.handleError));
40
    }
41
    mockLayout():any {
42

    
43

    
44
        return this.http.get("./assets/customizationOptions.json") ;
45

    
46
    }
47

    
48
    private handleError(error: HttpErrorResponse) {
49
        // in a real world app, we may send the error to some remote logging infrastructure
50
        // instead of just logging it to the console
51
        console.error(error);
52
        return throwError(error.error || 'Server error');
53
    }
54
}
(3-3/26)