/
router-helper.provider.js
121 lines (107 loc) · 4.52 KB
/
router-helper.provider.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Help configure the state-base ui.router
(function () {
'use strict';
angular
.module('app.helper')
.provider('routerHelper', routerHelperProvider);
routerHelperProvider.$inject = ['$locationProvider', '$stateProvider', '$urlRouterProvider'];
/* @ngInject */
function routerHelperProvider ($locationProvider, $stateProvider, $urlRouterProvider) {
/* jshint validthis:true */
var config = {
mainTitle: undefined,
resolveAlways: {}
};
$locationProvider.html5Mode(true);
this.configure = function (cfg) {
angular.extend(config, cfg);
};
this.$get = RouterHelper;
RouterHelper.$inject = ['$location', '$rootScope', '$state', 'logger', 'resolve'];
/* @ngInject */
function RouterHelper ($location, $rootScope, $state, logger, resolve) {
var handlingStateChangeError = false;
var hasOtherwise = false;
var stateCounts = {
errors: 0,
changes: 0
};
var service = {
configureStates: configureStates,
getStates: getStates,
stateCounts: stateCounts
};
init();
return service;
///////////////
function configureStates (states, otherwisePath) {
states.forEach(function (state) {
// add login check if requireLogin is true
var data = state.config.data;
if (data && data.requireLogin === true) {
state.config.resolve = angular.extend(
state.config.resolve || {},
{'loginResolve': resolve.login}
);
}
state.config.resolve =
angular.extend(state.config.resolve || {}, config.resolveAlways);
$stateProvider.state(state.state, state.config);
});
if (otherwisePath && !hasOtherwise) {
hasOtherwise = true;
$urlRouterProvider.otherwise(otherwisePath);
}
}
function handleRoutingErrors () {
// Route cancellation:
// On routing error, go to the dashboard.
// Provide an exit clause if it tries to do it twice.
$rootScope.$on('$stateChangeError',
function (event, toState, toParams, fromState, fromParams, error) {
if (handlingStateChangeError) {
return;
}
stateCounts.errors++;
handlingStateChangeError = true;
var destination = (toState &&
(toState.title || toState.name || toState.loadedTemplateUrl)) ||
'unknown target';
var msg = 'Error routing to ' + destination + '. ' +
(error.data || '') + '. <br/>' + (error.statusText || '') +
': ' + (error.status || '');
logger.warning(msg, [toState]);
// handle requireLogin issue
if (error === 'requireLogin') {
$state.prev = {
state: toState.name,
params: toParams
};
$state.go('root.login');
} else {
$state.go('root.home');
}
}
);
}
function init () {
handleRoutingErrors();
updateDocTitle();
}
function getStates () {
return $state.get();
}
function updateDocTitle () {
$rootScope.$on('$stateChangeSuccess',
function (event, toState, toParams, fromState, fromParams) {
stateCounts.changes++;
handlingStateChangeError = false;
var title = (toState.data.title + ' - ' || '') + config.mainTitle;
$rootScope.title = title; // data bind to <title>
$rootScope._class = toState.data._class; // data bind to <body>
}
);
}
}
}
})();