-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstates.js
252 lines (248 loc) · 10.5 KB
/
states.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'use strict';
// Make sure to include the `ui.router` module as a dependency
angular.module('algorea')
.run(['$rootScope', '$state', '$stateParams', function ($rootScope, $state, $stateParams) {
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
$rootScope.templatesPrefix = (config.domains.current.compiledMode || !config.domains.current.assetsBaseUrl) ? '' : config.domains.current.assetsBaseUrl;
}]);
angular.module('algorea')
.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$sceDelegateProvider', function ($stateProvider, $urlRouterProvider, $locationProvider, $sceDelegateProvider) {
if (config.domains.current.assetsBaseUrl) {
$sceDelegateProvider.resourceUrlWhitelist([
'self',
config.domains.current.assetsBaseUrl+'**'
]);
}
var templatesPrefix = (config.domains.current.compiledMode || !config.domains.current.assetsBaseUrl) ? '' : config.domains.current.assetsBaseUrl;
$urlRouterProvider
.otherwise(config.domains.current.defaultPath);
$stateProvider
.state("contents", {
url: "/contents/*path/:section",
params: {
path: config.domains.current.ProgressRootItemId
},
views: {
'right': {
template: '<div display-item from="main"></div>',
controller: 'rightNavigationController',
},
'breadcrumbs': {
templateUrl: templatesPrefix+'navigation/views/super-bread-crumbs.html',
controller: 'superBreadCrumbsController',
},
},
})
.state("oldcontents", {
// Legacy URLs
url: "/contents/*path?sell&selr&viewl&viewr"
})
.state('profile', {
url: "/profile/:section",
views: {
'right': {
templateUrl: templatesPrefix+'profile/profile.html',
controller: 'profileController',
},
'breadcrumbs': {
templateUrl: templatesPrefix+'navigation/views/breadcrumbs-profile.html'
},
},
}).state("groupAdminGroup", {
url: "/groupAdmin/:idGroup/:section",
views: {
'right': {
templateUrl: templatesPrefix+'groupAdmin/group.html',
controller: 'groupAdminController',
},
'breadcrumbs': {
templateUrl: templatesPrefix+'groupAdmin/breadcrumbs.html',
controller: 'groupAdminBreadCrumbsController',
},
}
}).state("forum", {
url: "/forum/",
views: {
'right': {
templateUrl: templatesPrefix+'forum/index.html',
controller: 'forumIndexController',
},
'breadcrumbs': {
template: '',
},
},
}).state("newThread", {
url: "/forum/thread/new",
views: {
'right': {
templateUrl: templatesPrefix+'forum/thread.html',
controller: 'forumThreadController',
},
'breadcrumbs': {
template: '',
},
},
}).state("newThreadType", {
url: "/forum/thread/new/:sType",
views: {
'right': {
templateUrl: templatesPrefix+'forum/thread.html',
controller: 'forumThreadController',
},
'breadcrumbs': {
template: '',
},
},
}).state("thread", {
url: "/forum/thread/:idThread",
views: {
'right': {
templateUrl: templatesPrefix+'forum/thread.html',
controller: 'forumThreadController',
},
'breadcrumbs': {
template: '',
},
},
});
$locationProvider.html5Mode(true);
}]);
angular.module('algorea')
.service('pathService', ['$stateParams', '$state', '$rootScope', '$timeout', 'itemService','$view','$window','$location', function($stateParams, $state, $rootScope, $timeout, itemService,$view, $window,$location) {
/* Warning: tricks at work here!
*
* As of today (2014-03-18), none of the available routers handle the possibility
* to have query params reload the views and others not. But we have to
* do that here, and we do it with the following code.
*
* This should be handled differently when a router will handle this kind
* of features.
*
* Another possible solution is described here:
* https://github.com/angular-ui/ui-router/issues/322
* but I find it less elegant because it breaks ui-sref
*/
$rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
// Keep legacy URLs valid
if(toState.name == 'contents' || toState.name == 'oldcontents') {
var toPath = toParams.path;
var sanitizedPath = toPath.replace(/\//g, '-');
if(toState.name == 'oldcontents' || sanitizedPath != toPath) {
event.preventDefault();
toParams.path = sanitizedPath;
$state.go('contents', toParams);
return;
}
}
if (fromState.name == 'contents' && toState.name == 'contents' && fromParams.path == toParams.path) {
// here, only the parameters that shouldn't change the view are changed in the URL
event.preventDefault();
/* Ok, we prevent default, *but*, preventing default in this signal
* handling means to rewrite the url in its previous state, which
* we don't want!
*
* So the following code executes right after the end of the
* current $digest and re-replaces the url by its wanted value
* and sends a signal to the controller to change the task view.
*/
$timeout(function() {
$state.go(toState, toParams, {notify: false, location: 'replace'});
$rootScope.$broadcast('algorea.taskViewChange', toParams, fromParams);
},0);
return;
}
$timeout(function() {
$rootScope.$broadcast('algorea.reloadView', 'breadcrumbs');
// $rootScope.$broadcast('algorea.reloadView', 'right');
}, 0);
});
/*
* Simple service for path parsing and analysis and url factoring
*/
return {
// analyzes path parameters
getPathParams: function (pane) {
var pathParams = {};
pathParams.path = $stateParams.path.split('-');
pathParams.pathStr = $stateParams.path;
if (pane == 'menu') {
return pathParams;
}
pathParams.section = $stateParams.section;
if(pane == 'left') {
var basePath = pathParams.path.slice(0, pathParams.path.length-1);
pathParams.currentItemID = pathParams.path.length > 1 ? pathParams.path[pathParams.path.length-2] : -2;
pathParams.parentItemID = pathParams.path.length > 2 ? pathParams.path[pathParams.path.length-3] : -2;
} else {
var basePath = pathParams.path;
pathParams.currentItemID = pathParams.path[pathParams.path.length-1];
pathParams.parentItemID = pathParams.path.length > 1? pathParams.path[pathParams.path.length-2] : -2;
}
pathParams.basePathStr = basePath.join('-');
pathParams.baseDepth = basePath.length;
return pathParams;
},
getPathAtDepth: function(path, depth) {
if(typeof depth != 'undefined' && depth != null) {
return path.slice(0, depth+1);
}
return path;
},
getPathStrAtDepth: function(pathStr, depth) {
if(typeof depth != 'undefined' && depth != null) {
pathStr = pathStr.split('-').slice(0, depth+1).join('-');
}
return pathStr;
},
// returns string to pass to ui-sref for a link to current item
// view is optional and contains the view the task must show (in relevant case)
getSref: function(panel, depth, pathParams, relativePath, view) {
if (panel == 'menu') {
return this.getSrefString(pathParams.pathStr, depth);
}
var path = pathParams.basePathStr + relativePath;
var newDepth = pathParams.baseDepth + depth;
return this.getSrefString(path, newDepth, view ? view : null);
},
// returns function to go to relative path:
getStateGo: function(panel, depth, pathParams, relativePath, view) {
if (panel == 'menu') {
return this.getSrefFunction(pathParams.pathStr, depth);
}
var path = pathParams.basePathStr + relativePath;
var newDepth = pathParams.baseDepth + depth;
return this.getSrefFunction(path, newDepth, null, view ? view : null);
},
getSrefString: function(path, depth, section) {
// Get string for ui-sref, targetting depth in path
path = this.getPathStrAtDepth(path, depth);
return "contents("+JSON.stringify({path: path, section: section})+")";
},
getSrefFunction: function(path, depth, section) {
// Get function to go to depth in path
path = this.getPathStrAtDepth(path, depth);
return function() {
if(!section) { section = $stateParams.section; }
$state.go("contents", {path: path, section: section})
};
},
goToResolution: function(pathParams) {
$state.go('contents', {
path: pathParams.pathStr,
section: 'editor',
});
},
openItemFromLink: function(itemPath) {
// Currently only used by platform.openUrl
var params = {};
params.path = typeof itemPath == 'string' ? itemPath : itemPath.path;
params.path = params.path.replace(/\//g, '-');
if(itemPath && itemPath.newTab) {
window.open($state.href('contents', params));
} else {
$state.go('contents', params);
}
}
};
}]);