(function () {
'use strict';
/**
* Only module for the app, encompasses the Main MHS website and Collection Search.
* @name mohistory
* @ngdoc module
*/
angular.module('mohistory', ['ui.router', 'ngAria', 'countUpModule', 'infinite-scroll', 'duScroll', 'formio']);
// Reduce the number of times infinite-scroll listens to scroll events.
angular.module('infinite-scroll').value('THROTTLE_MILLISECONDS', 550);
// Startup hook for the mohistory app. Sets up listeners for UI-Router.
angular.module('mohistory').run(onStart);
onStart.$inject = ['$rootScope', '$state', '$transitions', '$window', '$anchorScroll', '$location', '$timeout'];
/**
* Startup script for the mohistory app. Sets up listeners for UI-Router.
* @memberof mohistory
* @name onStart
* @ngdoc config
* @param {object} $rootScope AngularJS service for storing app wide scope
* @param {object} $state UI-Router service which allows access to information regarding current router state
* @param {object} $stateParams UI-Router service which provides access to route parameters
* @param {object} $transitions UI-Router service manages transitions between states/routes
* @param {object} $window AngularJS wrapper for the window object
* @param {object} $anchorScroll AngularJS service controlling scroll behavior
* @param {object} $location AngularJS service which tracks changes to the URL
* @param {object} $timeout AngularJS service for async operations
*/
function onStart($rootScope, $state, $transitions, $window, $anchorScroll, $location, $timeout) {
$rootScope.$state = $state;
$rootScope.loadingContent = false;
var loaderCountDown = undefined;
var reloadCount = 0;
var maxRouteChanges = 8;
var currentPath = '';
// UI-Router hook triggered when a transition from one state to another is triggered
$transitions.onStart({}, function ($transition$) {
var stateName = $transition$.to().name;
// Show loading symbol if the transition takes more than 3s
if (stateName !== 'main.collections' && stateName !== 'main.blog' && stateName !== 'main.events' && stateName !== 'main.search') {
if (!angular.isDefined(loaderCountDown)) {
loaderCountDown = $timeout(function () {
$rootScope.loadingContent = true;
}, 3000);
}
// Scroll to the top of the page or top of the search
$anchorScroll('top-of-body');
}
});
// UI-Router hook triggered when a transition from one state to another is started
$transitions.onEnter({}, function ($transition$) {
// Safe guard against search routes being identified as firstID routes
if (!$transition$.params().secondID && $transition$.params().firstID) {
if ($transition$.params().firstID === 'collections') {
console.log($transition$.params().firstID + ' is not the route you are looking for!')
$location.search({});
$transition$.router.stateService.target('main.collections');
} else if ($transition$.params().firstID === 'search') {
console.log($transition$.params().firstID + ' is not the route you are looking for!')
$location.search({});
$transition$.router.stateService.target('main.search');
} else if ($transition$.params().firstID === 'blog') {
console.log($transition$.params().firstID + ' is not the route you are looking for!')
$location.search({});
$transition$.router.stateService.target('main.blog');
} else if ($transition$.params().firstID === 'events') {
console.log($transition$.params().firstID + ' is not the route you are looking for!')
$location.search({});
$transition$.router.stateService.target('main.events');
}
}
});
// UI-Router hook triggered when the transition from one state to another is completed
$transitions.onSuccess({}, function ($transition$) {
// Once transition has finished turn loading symbol off
$rootScope.loadingContent = false;
if (angular.isDefined(loaderCountDown)) {
$timeout.cancel(loaderCountDown);
loaderCountDown = undefined;
}
// If not first page load, set focus to the first H1 element on
// the page. This acts as an announcement to Screen Reader users.
$timeout(function () {
if (!jQuery.isEmptyObject($transition$.params('from'))) {
$('h1').first().attr('tabindex', -1).focus();
}
}, 200);
// Force page to reload after maxRouteChanges in order to prevent odd
// UI problems.
if(currentPath !== $location.path()) {
currentPath = $location.path();
reloadCount++;
}
if(currentPath !== '' && reloadCount >= maxRouteChanges) {
console.log('Reloading...');
location.reload(false);
}
});
// Redirect to /society when form is submitted
$rootScope.$on('formSubmission', function(e, submission) {
e.stopPropagation();
$state.go('main.firstID', {'firstID': 'society'});
});
}
})();