UNPKG

tfabrica

Version:

library for TFabrica - TechSol

118 lines (88 loc) 3.95 kB
import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions } from "@angular/http"; import 'rxjs/Rx'; import { Observable } from "rxjs"; import { TfabricaUserData } from '../models/tfabrica.userdata.model'; import { TfabricaUserMenu } from '../models/tfabrica.usermenu.model'; import { TfabricaUserMenuProperty } from '../models/tfabrica.usermenu.property.model'; import { TfabricaSharedService } from '../main/tfabrica.shared.service'; @Injectable() export class TfabricaUserMenuService { constructor(private http: Http, public tfabricaSharedService: TfabricaSharedService ) { } userData: TfabricaUserData; menuUrl = "/api/Menu/UserMenu"; menu(): Observable<TfabricaUserMenu[]> { //console.log("Start call service: " + this.menuUrl); let actThis = this; this.menuUrl = this.tfabricaSharedService.appSettings.userMenuApiUrl; this.menuUrl = this.tfabricaSharedService.appSettings.getuserMenuApiUrl(); let userData = this.tfabricaSharedService.getUser(); let bodyString = { "Username": userData.username, "App": this.tfabricaSharedService.appSettings.appName, "Token": userData.token }; let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON let options = new RequestOptions({ headers: headers }); // Create a request option return this.http.post(this.menuUrl, bodyString, options) // ...using post request .map((res: Response) => { let response = res.json(); console.log(response); let menus = actThis.convertResponseToUserMenu(response.menu); //console.log(menus); return menus; }) // ...and calling .json() on the response to return data .catch((error: any) => Observable.throw(error.json().error || 'Server error')); //...errors if any } convertResponseToUserMenu(res) { //console.log("Start convertResponseToUserMenu"); //console.log(res); let actThis = this; let menus = Array<TfabricaUserMenu>(); res.forEach(function (entry) { menus.push(actThis.convertSingleVoice(entry, undefined)); }); //console.log("End convertResponseToUserMenu -> menu:"); console.log(menus); return menus; } convertSingleVoice(entry, father: TfabricaUserMenu) { console.log("Start convertSingleVoice with entry:"); console.log(entry); let actThis = this; //console.log("Create new Voice:"); let newVoice = new TfabricaUserMenu(); newVoice.level = entry.Level; newVoice.title = entry.Title; newVoice.subTitle = entry.SubTitle; newVoice.levelClass = "sidenav-level-" + (8 - entry.Level); newVoice.isOpen = false; newVoice.subMenu = Array<TfabricaUserMenu>(); newVoice.path = entry.Path; newVoice.properties = Array<TfabricaUserMenuProperty>(); try { entry.Properties.forEach(function (property) { let newProp = new TfabricaUserMenuProperty(); newProp.key = property.Key; newProp.value = property.Value; newVoice.properties.push(newProp); }); } catch (Err) { } //console.log(newVoice); try { entry.SubMenu.forEach(function (subVoice) { actThis.convertSingleVoice(subVoice, newVoice); }); } catch (Err) { } if (father != undefined) { father.subMenu.push(newVoice); return father; } else { return newVoice; } } }