/**
* AngularJS Service for parsing/storing parameters from the URL & query string
*
* @module URLParamsService
* @see https://docs.angularjs.org/api/ng/type/angular.Module#service
*/
angular.module('tgaApp').service('URLParamsService', ['$location', 'envService',
function URLParamsService($location, envService) {
/**
* Get URL Param
* @param {string} key
* @returns {string}
*/
this.getParam = function (key) {
const p = $location.search()[key];
if (angular.isArray(p)) {
return p[0];
}
return p;
};
/**
* Object containing relevant parameters parsed from URL by key/value.
*
* @private
* @member {Object} userParams
*/
const userParams = {
bypass_reg: this.getParam('bypass_reg'),
country: this.getParam('country'),
course_id: this.getParam('course_id'),
challenge_id: this.getParam('challenge_id'),
display_name: this.getParam('display_name'),
email: this.getParam('email'),
employee_id: this.getParam('employee_id'),
event_mode: this.getParam('event_mode'),
first_name: this.getParam('first_name'),
hub_id: this.getParam('hub_id'),
initials: this.getParam('initials'),
instructor: this.getParam('instructor'),
language: this.getParam('language'),
last_name: this.getParam('last_name'),
organization: this.getParam('organization'),
phone: this.getParam('phone'),
segment: this.getParam('segment'),
session_group_id: this.getParam('session_group_id'),
team_id: this.getParam('team_id'),
token: this.getParam('token'),
username: this.getParam('username'),
};
/**
* Object containing site parameters (such as token and slug) parsed from URL by key/value.
*
* @private
* @member {Object} siteParams
*/
const siteParams = {
token: this.getParam('token'),
slug: $location.path() ? $location.path().substring(1) : null,
};
/**
* Get user parameters.
*
* @return {Object}
*/
this.getUserParams = () => userParams;
/**
* Get site parameters, overriding the slug if `forceSlug` is set.
*
* @return {Object}
*/
this.getSiteParams = () => {
const newSiteParams = {
...siteParams,
};
const forceSlug = envService.read('forceSlug');
if (forceSlug) {
newSiteParams.slug = forceSlug;
}
return newSiteParams;
};
/**
* Set default values in internal objects where necessary.
* This is called when the service is initialized.
*/
this.setDefaults = () => {
if (!userParams.display_name) {
if (!userParams.initials && !userParams.first_name) {
userParams.display_name = null;
} else {
userParams.display_name = userParams.first_name
? userParams.first_name
: userParams.initials;
}
}
if (!userParams.username) {
if (!userParams.email) {
userParams.username = null;
} else {
userParams.username = userParams.email;
}
}
if (!userParams.bypass_reg) {
userParams.bypass_reg = false;
}
};
this.setDefaults();
},
]);