Stone, Avi (as206k) | 9b2ceb3 | 2018-04-12 16:36:39 +0300 | [diff] [blame] | 1 | import { Injectable } from '@angular/core'; |
| 2 | |
| 3 | interface HostParams { |
| 4 | readonly userId: string; |
| 5 | readonly contextType: string; |
| 6 | readonly vfcmtUuid: string; |
| 7 | readonly lifecycleState: string; |
| 8 | readonly isOwner: string; |
| 9 | } |
| 10 | |
| 11 | @Injectable() |
| 12 | export class HostService { |
| 13 | /* Public Members */ |
| 14 | public static getParams(): HostParams { |
| 15 | return this.getQueryParamsObj(window.location.hash) as HostParams; |
| 16 | } |
| 17 | |
| 18 | public static disableLoader(): void { |
| 19 | this.postMessage('READY', null); |
| 20 | } |
| 21 | |
| 22 | /* Private Methods */ |
| 23 | private static postMessage(eventName: string, data: string): void { |
| 24 | window.parent.postMessage( |
| 25 | { |
| 26 | type: eventName, |
| 27 | data: data |
| 28 | }, |
| 29 | '*' |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | private static getQueryParamsObj(query: string): object { |
| 34 | return query |
| 35 | .substring(7) // removes '?' that always appears as prefix to the query-string |
| 36 | .split('&') // splits query-string to "key=value" strings |
| 37 | .map(p => p.split('=')) // splits each "key=value" string to [key,value] array |
| 38 | .reduce((res, p) => { |
| 39 | // converts to a dictionary (object) of params |
| 40 | res[p[0]] = p[1]; |
| 41 | return res; |
| 42 | }, {}); |
| 43 | } |
| 44 | } |