datahub/wherehows-web/app/routes/application.js

162 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-02-13 15:05:30 -08:00
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
2017-02-13 15:05:30 -08:00
2017-06-19 16:35:00 -07:00
const { Route, run, get, inject: { service }, testing } = Ember;
export default Route.extend(ApplicationRouteMixin, {
// Injected Ember#Service for the current user
sessionUser: service('current-user'),
/**
* Runtime application configuration options
* @type {Ember.Service}
*/
configurator: service(),
/**
* Metrics tracking service
* @type {Ember.Service}
*/
metrics: service(),
/**
* Attempt to load the current user and application configuration options
* @returns {Promise}
*/
beforeModel() {
this._super(...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
*/
2017-06-19 16:35:00 -07:00
async model() {
const isInternal = await get(this, 'configurator.getConfig')('isInternal');
/**
* properties for the navigation link to allow a user to provide feedback
* @type {{href: string, target: string, title: string}}
*/
const feedbackMail = {
href: 'mailto:wherehows-dev@linkedin.com?subject=WhereHows Feedback',
target: '_blank',
title: 'Provide Feedback'
};
2017-06-19 16:35:00 -07:00
const brand = {
logo: isInternal ? '/assets/assets/images/wherehows-logo.png' : ''
};
return { feedbackMail, brand };
},
/**
* Perform post model operations
* @return {Promise}
*/
afterModel() {
this._super(...arguments);
2017-06-19 16:35:00 -07:00
return Promise.all([this._setupMetricsTrackers(), this._trackCurrentUser()]);
},
/**
* Augments sessionAuthenticated.
* @override ApplicationRouteMixin.sessionAuthenticated
*/
sessionAuthenticated() {
this._super(...arguments);
this._loadCurrentUser();
},
/**
* __It reloads the Ember.js application__ by redirecting the browser
* to the application's root URL so that all in-memory data (such as Ember
* Data stores etc.) gets cleared.
* @override ApplicationRouteMixin.sessionInvalidated
* @link https://github.com/simplabs/ember-simple-auth/issues/1048
*/
sessionInvalidated() {
if (!testing) {
window.location.replace('/');
}
},
/**
* Internal method to invoke the currentUser service's load method
* If an exception occurs during the load for the current user,
* invalidate the session.
* @returns {Promise<T, V>|RSVP.Promise<any>|Ember.RSVP.Promise<any, any>|Promise.<T>}
* @private
*/
_loadCurrentUser() {
return get(this, 'sessionUser').load().catch(() => get(this, 'sessionUser').invalidateSession());
},
/**
* Loads the application configuration object
* @return {Promise.<any>|void}
* @private
*/
_loadConfig() {
return get(this, 'configurator.load')();
},
/**
* Requests app configuration props then if enabled, sets up metrics
* tracking using the supported trackers
* @return {Promise.<TResult>}
* @private
*/
2017-06-19 16:35:00 -07:00
async _setupMetricsTrackers() {
const { tracking = {} } = await get(this, 'configurator.getConfig')();
if (tracking.isEnabled) {
const metrics = get(this, 'metrics');
const { trackers = {} } = tracking;
const { piwikSiteId, piwikUrl = '//piwik.corp.linkedin.com/piwik/' } = trackers.piwik;
metrics.activateAdapters([
{
name: 'Piwik',
environments: ['all'],
config: {
piwikUrl,
siteId: piwikSiteId
}
2017-06-19 16:35:00 -07:00
}
]);
}
},
/**
* Tracks the currently logged in user
* @return {Promise.<isEnabled|((feature:string)=>boolean)|*>}
* @private
*/
async _trackCurrentUser() {
const { tracking = {} } = await get(this, 'configurator.getConfig')();
// Check if tracking is enabled prior to invoking
// Passes an anonymous function to track the currently logged in user using the singleton `current-user` service
return (
tracking.isEnabled &&
get(this, 'sessionUser').trackCurrentUser(userId => get(this, 'metrics').identify({ userId }))
);
},
2017-02-13 15:05:30 -08:00
init() {
this._super(...arguments);
run.scheduleOnce('afterRender', this, 'processLegacyDomOperations');
2017-02-13 15:05:30 -08:00
},
processLegacyDomOperations() {
// TODO: DSS-6122 Refactor Remove tree legacy operations & references
2017-02-13 15:05:30 -08:00
window.legacyMain();
}
});