appRun.$inject = ['$rootScope', '$state', '$timeout', '$location', 'Credentials', 'appMsg', 'XVars', 'Vars', 'XServ', 'appOpts', 'UserSrv'];
function appRun( $rootScope, $state, $timeout, $location, creds, appMsg, xVars, Vars, xServ, Opts, srvUsr){
var rco = $rootScope, iState;
rco.creds = creds;
//----------------------------------------------
// Ui router
//----------------------------------------------
rco.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
//window.nxutil.clearConsole();
console.log ('------/State [ '+fromState.name+' ] -> [ '+toState.name + ' ] - logged::' + creds.logged());
if(!toState.data)
toState.data = {};
if( toState.data._denied){
event.preventDefault();
nxutil.miniPop(1, '禁止访问、半路跳出!');
return;
}
}
rco.$on('$stateChangeSuccess',function(event, toState, toParams, fromState, fromParams) {
rco.$emit("App.idle");
});
rco.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) {//Here we intercept any state change to check the credentials
log.warn(toState);
rco.$emit("App.exception");
log.error('------/State change Error!', error);
$('body').NxNotifs({
style: 'simple',
message: '切换state:: ' + toState.url + ' 失败!' + error,
position: 'bottom-right',
timeout: 0,
type:'danger'
}).show();
});
//----------------------------------------------
// Load states for current user
//----------------------------------------------
function _loadStates(){
srvUsr.states().then(function(result){
if( result && result.status == 'ok'){
// Only create the state reference here and detail settings will be loaded upon the module being displayed
angular.forEach(result.data, function (iter) {
iState = {};
iState.url = iter.url;
iState.resolve = Opts.ozLoad(null);
iState.data = {_inited:false};
xVars.$stateProvider.state(iter.key, iState);
});
}
}).finally(function(){
vm.uiObj.busy = false;
})
}
_loadStates();
//----------------------------------------------
// addUnloadEvent
//----------------------------------------------
rco.addUnloadEvent = function(){
if( window.addEventListener) {
window.addEventListener("beforeunload", handleUnloadEvent);
} else {
window.attachEvent("onbeforeunload", handleUnloadEvent);//For IE browsers
}
function handleUnloadEvent(event) {
event.returnValue = "您确定要离开吗?";
};
}
//Call this when you want to remove the event, example, if users fills necessary info
rco.removeUnloadEvent = function(){
if (window.removeEventListener) {
window.removeEventListener("beforeunload", handleUnloadEvent);
} else {
window.detachEvent("onbeforeunload", handleUnloadEvent);
}
}
rco.addUnloadEvent();
}