| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import Route from '@ember/routing/route'; | 
					
						
							|  |  |  | import { run } from '@ember/runloop'; | 
					
						
							|  |  |  | import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin'; | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  | import Configurator, { getConfig } from 'wherehows-web/services/configurator'; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | import { makeAvatar } from 'wherehows-web/constants/avatars/avatars'; | 
					
						
							|  |  |  | import { inject as service } from '@ember/service'; | 
					
						
							|  |  |  | import { UnWrapPromise } from 'wherehows-web/typings/generic'; | 
					
						
							|  |  |  | import CurrentUser from '@datahub/shared/services/current-user'; | 
					
						
							|  |  |  | import BannerService from 'wherehows-web/services/banners'; | 
					
						
							|  |  |  | import Controller from '@ember/controller'; | 
					
						
							|  |  |  | import { IAvatar } from 'wherehows-web/typings/app/avatars'; | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  | import { IAppConfig } from '@datahub/shared/types/configurator/configurator'; | 
					
						
							|  |  |  | import UnifiedTracking from '@datahub/tracking/services/unified-tracking'; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Quick alias of the type of the return of the model hook | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | type Model = UnWrapPromise<ReturnType<ApplicationRoute['model']>>; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { scheduleOnce } = run; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Will initialize Marked which is a global (and probably from legacy code) library. | 
					
						
							|  |  |  |  * @param marked Marked Library | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | const initMarked = (marked: Window['marked']): void => { | 
					
						
							|  |  |  |   const markedRendererOverride = new marked.Renderer(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   markedRendererOverride.link = (href: string, title: string, text: string): string => | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     `<a href='${href}' title='${title || text}' target='_blank'>${text}</a>`; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   marked.setOptions({ | 
					
						
							|  |  |  |     gfm: true, | 
					
						
							|  |  |  |     tables: true, | 
					
						
							|  |  |  |     renderer: markedRendererOverride | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface IApplicationRouteModel { | 
					
						
							|  |  |  |   helpResources: Array<{ label: string; link: string }>; | 
					
						
							|  |  |  |   showStagingBanner: boolean; | 
					
						
							|  |  |  |   showLiveDataWarning: boolean; | 
					
						
							|  |  |  |   showChangeManagement: boolean; | 
					
						
							|  |  |  |   avatar: IAvatar; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class ApplicationRoute extends Route.extend(ApplicationRouteMixin) { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Injected service to manage the operations related to currently logged in user | 
					
						
							|  |  |  |    * On app load, avatar creation and loading user attributes are handled via this service | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   @service('current-user') | 
					
						
							|  |  |  |   sessionUser: CurrentUser; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |    * References the application tracking service which is used for analytics activation, setup, and management | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   @service('unified-tracking') | 
					
						
							|  |  |  |   trackingService: UnifiedTracking; | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Banner alert service | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   @service | 
					
						
							|  |  |  |   banners: BannerService; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   @service | 
					
						
							|  |  |  |   configurator: Configurator; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Attempt to load the current user and application configuration options | 
					
						
							|  |  |  |    * @returns {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   beforeModel(): Promise<[void, IAppConfig]> { | 
					
						
							|  |  |  |     super.beforeModel.apply(this, arguments); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Promise.all([this._loadCurrentUser(), this._loadConfig()]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Returns an object containing properties for the application route | 
					
						
							|  |  |  |    * @return {{feedbackMail: {href: string, target: string, title: string}}} | 
					
						
							|  |  |  |    * @override | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   model(): IApplicationRouteModel { | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |     const [showStagingBanner, showLiveDataWarning, showChangeManagement, avatarEntityProps, wikiLinks] = [ | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |       getConfig('isStagingBanner', { useDefault: true, default: false }), | 
					
						
							|  |  |  |       getConfig('isLiveDataWarning', { useDefault: true, default: false }), | 
					
						
							|  |  |  |       getConfig('showChangeManagement', { useDefault: true, default: false }), | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |       getConfig('userEntityProps'), | 
					
						
							|  |  |  |       getConfig('wikiLinks') | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     ]; | 
					
						
							|  |  |  |     const sessionUser = this.sessionUser || {}; | 
					
						
							| 
									
										
										
										
											2019-09-04 22:02:08 -07:00
										 |  |  |     const { userName = '', email = '', name = '', pictureLink = '' } = sessionUser.currentUser || {}; | 
					
						
							|  |  |  |     const avatar = makeAvatar(avatarEntityProps)({ userName, email, name, imageUrl: pictureLink }); | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       showStagingBanner, | 
					
						
							|  |  |  |       showLiveDataWarning, | 
					
						
							|  |  |  |       showChangeManagement, | 
					
						
							|  |  |  |       avatar, | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |       helpResources: [{ link: wikiLinks['appHelp'], label: 'DataHub Wiki' }] | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     }; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Perform post model operations | 
					
						
							|  |  |  |    * @return {Promise} | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   afterModel(): Promise<[void, false | void]> { | 
					
						
							|  |  |  |     super.afterModel.apply(this, arguments); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return Promise.all([this._setupMetricsTrackers(), this._trackCurrentUser()]); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Augments sessionAuthenticated. | 
					
						
							|  |  |  |    * @override ApplicationRouteMixin.sessionAuthenticated | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async sessionAuthenticated(): Promise<void> { | 
					
						
							|  |  |  |     // @ts-ignore waiting for this be solved: https://github.com/simplabs/ember-simple-auth/issues/1619
 | 
					
						
							|  |  |  |     super.sessionAuthenticated.apply(this, arguments); | 
					
						
							|  |  |  |     await this._loadCurrentUser(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Internal method to invoke the currentUser service's load method | 
					
						
							|  |  |  |    *   If an exception occurs during the load for the current user, | 
					
						
							|  |  |  |    *   invalidate the session. | 
					
						
							|  |  |  |    * @private | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   private async _loadCurrentUser(): Promise<void> { | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |     const { sessionUser } = this; | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await sessionUser.load(); | 
					
						
							|  |  |  |     } catch { | 
					
						
							|  |  |  |       sessionUser.invalidateSession(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Loads the application configuration object | 
					
						
							|  |  |  |    * @return {Promise.<any>|void} | 
					
						
							|  |  |  |    * @private | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   private _loadConfig(): Promise<IAppConfig> { | 
					
						
							|  |  |  |     return this.configurator.load(); | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |    * Delegates to the tracking service methods to activate tracking adapters | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |    * @private | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   private async _setupMetricsTrackers(): Promise<void> { | 
					
						
							|  |  |  |     const tracking = await getConfig('tracking'); | 
					
						
							|  |  |  |     this.trackingService.setupTrackers(tracking); | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * Tracks the currently logged in user | 
					
						
							|  |  |  |    * @return {Promise.<isEnabled|((feature:string)=>boolean)|*>} | 
					
						
							|  |  |  |    * @private | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |   private async _trackCurrentUser(): Promise<void> { | 
					
						
							|  |  |  |     const tracking = await getConfig('tracking'); | 
					
						
							|  |  |  |     this.trackingService.setCurrentUser(tracking); | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   init(): void { | 
					
						
							|  |  |  |     super.init.apply(this, arguments); | 
					
						
							|  |  |  |     scheduleOnce('afterRender', this, 'processLegacyDomOperations'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * At a more granular level, initializing the banner before the render loop of the entire page ends will results in the | 
					
						
							|  |  |  |    * render loop of the application breaking the css transition animation for our initial banners. This hook is being used | 
					
						
							|  |  |  |    * to schedule banners only after initial render has taken place in order to allow users see the banner animation | 
					
						
							|  |  |  |    * on entry | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   renderTemplate(_: Controller, model: Model): void { | 
					
						
							|  |  |  |     super.renderTemplate.apply(this, arguments); | 
					
						
							|  |  |  |     const { showStagingBanner, showLiveDataWarning, showChangeManagement } = model; | 
					
						
							|  |  |  |     const { banners } = this; | 
					
						
							| 
									
										
										
										
											2019-09-04 21:46:02 -07:00
										 |  |  |     scheduleOnce('afterRender', this, (): void => | 
					
						
							| 
									
										
										
										
											2019-08-31 20:51:14 -07:00
										 |  |  |       banners.appInitialBanners([showStagingBanner, showLiveDataWarning, showChangeManagement]) | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   processLegacyDomOperations(): void { | 
					
						
							|  |  |  |     // As marked is a legacy global variable, lets ignore the type
 | 
					
						
							|  |  |  |     if (window.marked) { | 
					
						
							|  |  |  |       initMarked(window.marked); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |