import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { Observable } from 'rxjs/Rx'; import {COOKIE} from "../../openaireLibrary/login/utils/helper.class" import { HtmlPageContent } from "../../domain/html-page-content"; @Injectable() export class HtmlPageContentService { constructor(private http:Http) { } static removeNulls(obj){ var isArray = obj instanceof Array; for (var k in obj){ if (obj[k]===null || obj[k]==='') isArray ? obj.splice(k,1) : delete obj[k]; else if (typeof obj[k]=="object") HtmlPageContentService.removeNulls(obj[k]); } } getHtmlPageContent(community : string, page: string, adminToolsAPIURL:string) { return this.http.get(adminToolsAPIURL + 'htmlpagecontent?community=' + community + "&page="+page) .map(res => res.json()) .catch(this.handleError); } updateHtmlPageContent(htmlPageContent: HtmlPageContent, adminToolsAPIURL:string) { let headers = new Headers({'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); HtmlPageContentService.removeNulls(htmlPageContent); return this.http.post(adminToolsAPIURL + 'htmlpagecontent/update', JSON.stringify(htmlPageContent), options) .map(res => 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'); } }