(function() {
'use strict';
/**
*** Before Unload Service
**/
angular
.module([appModule])
.factory('beforeUnload', [
'$rootScope', '$window', '$timeout', 'DataService',
BeforeUnload
]);
function BeforeUnload($rootScope, $window, $timeout, DataService) {
$window.onbeforeunload = function(e) {
var confirmation = {};
var event = $rootScope.$broadcast('onBeforeUnload', confirmation);
//TODO trigger a popup to give the use to save or not ...
if (event.defaultPrevented && DataService.isDirty()) {
return confirmation.message;
}
};
$window.onunload = function() {
//discard all local changes and removes from local storage
DataService.removeLocalState();
$rootScope.$broadcast('onUnload');
};
return {};
}
})();
(function() {
'use strict';
angular
.module('highlights-admin')
.run(['$rootScope', '$location', '$state', 'authService', 'DataService', 'beforeUnload', Run]);
function Run($rootScope, $location, $state, authService, DataService, beforeUnload) {
$rootScope.$on('$stateChangeStart', function(e, toState, toParams, fromState, fromParams) {
var isLogin = toState.name === 'public.login';
var authed = authService.isAuthed();
if (isLogin) {
//if going to login and authenticated then redirect to admin
if (authed === false) return; // no need to redirect
e.preventDefault();
$state.transitionTo('private.admin');
}
// now, redirect only not authenticated
if (authed === false) {
e.preventDefault(); // stop current execution
$state.transitionTo('public.login'); // go to login
}
});
$rootScope.$on('onBeforeUnload', function(e, confirmation) {
confirmation.message = 'You will lose all your changes.';
e.preventDefault();
});
}