forked from France-ioi/AlgoreaPlatform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstates.js
330 lines (327 loc) · 14.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
'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;
}]);
// Make sure to include the `ui.router` module as a dependency.
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?sell&selr&viewl&viewr",
//reloadOnSearch: false,
params: {
path: config.domains.current.ProgressRootItemId
},
views: {
'left': {
template: '<div class="sidebar-left-content" ng-include="\''+templatesPrefix+'navigation/views/navbaritem.html\'" ng-repeat="item in itemsList track by $index"></div>',
controller: 'leftNavigationController',
},
'right': {
template: '<div display-item></div>',
controller: 'rightNavigationController',
},
'breadcrumbs': {
templateUrl: templatesPrefix+'navigation/views/super-bread-crumbs.html',
controller: 'superBreadCrumbsController',
},
},
})
.state('profile', {
url: "/profile/:section",
views: {
'left': {
template: '',
},
'right': {
templateUrl: templatesPrefix+'profile/profile.html',
controller: 'profileController',
},
'breadcrumbs': {
templateUrl: templatesPrefix+'navigation/views/breadcrumbs-profile.html'
},
},
}).state("groupAdminGroup", {
url: "/groupAdmin/:idGroup/:section",
views: {
'left': {
template: '',
},
'right': {
templateUrl: templatesPrefix+'groupAdmin/group.html',
controller: 'groupAdminController',
},
'breadcrumbs': {
templateUrl: templatesPrefix+'groupAdmin/breadcrumbs.html',
controller: 'groupAdminBreadCrumbsController',
},
}
}).state('userInfos', {
url: "/userInfos",
views: {
'left': {
template: '',
},
'right': {
templateUrl: templatesPrefix+'userInfos/index.html',
controller: 'userInfosController',
},
'breadcrumbs': {
template: '<div class="breadcrumbs-item"><span class="breadcrumbs-item-active breadcrumbs-item-active-last">Profil</span></div>',
},
},
}).state("forum", {
url: "/forum/",
views: {
'left': {
template: '',
controller: 'leftNavigationController',
},
'right': {
templateUrl: templatesPrefix+'forum/index.html',
controller: 'forumIndexController',
},
'breadcrumbs': {
template: '',
},
},
}).state("concourir", {
url: "/concourir",
views: {
'left': {
template: '',
},
'right': {
templateUrl: templatesPrefix+'static/concourir.html',
},
'breadcrumbs': {
template: '',
},
},
}).state("newThread", {
url: "/forum/thread/new",
views: {
'left': {
template: '',
},
'right': {
templateUrl: templatesPrefix+'forum/thread.html',
controller: 'forumThreadController',
},
'breadcrumbs': {
template: '',
},
},
}).state("newThreadType", {
url: "/forum/thread/new/:sType",
views: {
'left': {
template: '',
},
'right': {
templateUrl: templatesPrefix+'forum/thread.html',
controller: 'forumThreadController',
},
'breadcrumbs': {
template: '',
},
},
}).state("thread", {
url: "/forum/thread/:idThread",
views: {
'left': {
template: '',
},
'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) {
if (fromState.name == 'contents' && toState.name == 'contents' && fromParams.path == toParams.path && fromParams.sell == toParams.sell && typeof toParams.selr == "undefined" && fromParams.selr == toParams.path.split('/').length) {
// 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;
}
/* This part is also a hack due to the limited capacities of routers.
* It's been done after reporting https://github.com/angular-ui/ui-router/issues/1744
* (which has a description of the addressed problem)
* The idea is not to reload left view when unnecessary. This was
* introduced in order not to reload left iframe in case of static content.
* The code computes left item before and after, and if it's the same,
* it just sends a signal instead of reloading the views. Then the scopes
* need to handle their reloading themselves by watching the signal.
*/
if (fromState.name == 'contents' && toState.name == 'contents') {
var toPath = toParams.path.split('/');
var toSelr = toParams.selr ? parseInt(toParams.selr) : toPath.length;
var toSell = toParams.sell ? parseInt(toParams.sell) : toSelr -1;
var toLeftItem = toPath[toSell-1];
var fromPath = fromParams.path.split('/');
var fromSelr = fromParams.selr ? parseInt(fromParams.selr) : fromPath.length;
var fromSell = fromParams.sell ? parseInt(fromParams.sell) : fromSelr -1;
var fromLeftItem = fromPath[fromSell-1];
if(fromLeftItem == toLeftItem) {
event.preventDefault();
$timeout(function() {
$state.go(toState, toParams, {notify: false, location: 'replace'});
$timeout(function() {
$rootScope.$broadcast('algorea.reloadView', 'breadcrumbs');
$rootScope.$broadcast('algorea.reloadView', 'right');
},0);
},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;
pathParams.selr = $stateParams.selr ? $stateParams.selr : pathParams.path.length;
pathParams.sell = ($stateParams.sell && $stateParams.sell <= pathParams.selr) ? $stateParams.sell : pathParams.selr -1;
if (pane == 'menu') {
return pathParams;
}
/*
* for /11/12/13/14/15/16?sell=2&selr=4, we have
* for left pane: base_path = /11/12
* for right pane: base_path = /11/12/13/14
*
* sell = -1 means not to print anything on the left, we return currentItemID == -2
* selr = r means to print resolution on the right, we return resolution = true
*/
var basePath = pathParams.path.slice(0,(pane=='left' ? pathParams.sell : pathParams.selr));
pathParams.basePathStr = basePath.join('/');
pathParams.baseDepth = basePath.length;
pathParams.currentItemID = 0;
pathParams.viewl = $stateParams.viewl;
pathParams.viewr = $stateParams.viewr;
pathParams.itemsOnBothSides = pathParams.selr == pathParams.sell;
if ($stateParams.sell === 0 && pane == 'left') {
pathParams.currentItemID = -2;
} else {
pathParams.currentItemID = pathParams.path[(pane=='left' ? pathParams.sell-1 : pathParams.selr-1)];
}
pathParams.parentItemID = -2;
if ((pane == 'left' && pathParams.sell > 1) || (pane == 'right' && pathParams.selr > 1)) {
pathParams.parentItemID = pathParams.path[(pane=='left' ? pathParams.sell-2 : pathParams.selr-2)];
}
return pathParams;
},
// 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, depth+1);
}
var sell = panel=='left' ? pathParams.sell : pathParams.selr;
var path = pathParams.basePathStr + relativePath;
sell = '' + (parseInt(sell) + Math.max(relativePath.split('/').length - 2, 0)); // If we're going much deeper, jump
var selr = null;
if (pathParams.pathStr.substring(0, path.length) == path && (pathParams.path[path.length+1] || pathParams.pathStr[path.length+1]=='/')) {
selr = pathParams.baseDepth + depth;
path = pathParams.pathStr;
if (pathParams.path.length == selr) {
selr=null;
}
}
return this.getSrefString(path, sell, selr, null, 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, depth+1);
}
var sell = panel=='left' ? pathParams.sell : pathParams.selr;
var path = pathParams.basePathStr + relativePath;
var selr = null;
if (pathParams.pathStr.substring(0, path.length) == path && (pathParams.path[path.length+1] || pathParams.pathStr[path.length+1]=='/')) {
selr = pathParams.baseDepth + depth;
path = pathParams.pathStr;
if (pathParams.path.length == selr) {
selr=null;
}
}
return this.getSrefFunction(path, sell, selr, null, view ? view : null);
},
getSrefString: function(path, sell, selr, viewl, viewr) {
return "contents("+JSON.stringify({path:path, sell: sell, selr: selr, viewl: viewl, viewr: viewr})+")";
},
getSrefFunction: function(path, sell, selr, viewl, viewr) {
return function() {
if(!viewl) { viewl = $stateParams.viewl; }
if(!viewr) { viewr = $stateParams.viewr; }
$state.go("contents", {path:path, sell: sell, selr: selr, viewl: viewl, viewr: viewr})
};
},
goToResolution: function(pathParams) {
$state.go('contents', {
path: pathParams.pathStr,
sell: pathParams.selr,
selr: pathParams.selr,
viewl: pathParams.viewl,
viewr: 'editor',
});
},
openItemFromLink: function(itemPath) {
// Currently only used by platform.openUrl
var selr = itemPath.split('/').length;
$state.go('contents', {
path: itemPath,
sell: Math.max(selr-1, 0),
selr: selr
});
}
};
}]);