-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.js
440 lines (347 loc) · 10.5 KB
/
router.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/**
* Instance of Route.
* Initial config setup.
* Init route parameter
*/
var Route = function(options, cb) {
for(url in options) {
if(options[url].hasOwnProperty('url')) {
window.routes = this;
this.options = options;
}
}
this.clearDiv = false;
this.defaultRoute = null;
this.currentRoute = this.currentRoute ? this.currentRoute : null;
this.location = window.location ? window.location : null;
this.eventRef = $('*[data-route]');
this.container = $("#pageContainer");
this.activeClass = 'm-menu__item--expanded';
/**
* Init route parameters
*/
this.prepareRoutes();
}
/**
* Initialization of routes.
*/
Route.prototype.prepareRoutes = function(){
// Default Route Executation
this.defaultRoute = {};
for(var index in routes.options) {
if(routes.options[index].hasOwnProperty('default')) {
this.defaultRoute[index] = routes.options[index];
if(!this.location.hash.length)
this.executeRoute(Object.keys(this.defaultRoute));
}
}
};
/**
* Executation of route where XML request and response perform.
*/
var sourceR = null;
Route.prototype.executeRoute = function(route, options = null, cb = null) {
if(sourceR) {
sourceR.cancel('Operation canceled');
// sourceR = null;
}
var self = this,
CancelToken = axios.CancelToken;
sourceR = CancelToken.source();
if(routes.options.hasOwnProperty(route)) {
this.currentRoute = routes.options[route];
var u = this.currentRoute.hasOwnProperty('url') ? this.currentRoute.url :
options.hasOwnProperty('url') ? options.url : '404';
/**
* Start Router XHR Process
*/
axios({
url : u.trim(),
method : this.currentRoute.hasOwnProperty('method') ? this.currentRoute.method.toLowerCase() : 'get',
cancelToken: sourceR.token,
}).then( function(response) {
if(!response.data && (response.data && response.data != '')) {
return toastr.error("Something went Wrong");
}
// Remove Page Router Loader
$("#renderPartialPage").find('.m-loader.page').remove();
// Change Hash
self.changeUrl(u);
if(self.clearDiv) {
$('.__route_container_class').html('');
}
// Has Container
// Default : #renderPartialPage
if(routes.options[route].hasOwnProperty('container')) {
// Has Parent Route
// Prepare Parent route before child route
if(routes.options[route].hasOwnProperty('parentRoute')) {
var prev_response = response,
callback = routes.options[route].hasOwnProperty('callback') ? routes.options[route].callback : '',
pageUrl = routes.options[route].hasOwnProperty('url') ? routes.options[route].url : routes.options[route];
// Execute parent URL
self.executeRoute(routes.options[route].parentRoute, null, function(){
self.changeUrl(pageUrl.trim());
$(routes.options[route].container).addClass('__route_container_class').html(prev_response.data);
// Current URL callback
if(callback) {
window[callback](prev_response);
}
})
}
} else {
self.container.addClass('__route_container_class').html(response.data);
}
// Add Active Class
var pageUrl = options.hasOwnProperty('url') ? options.url : routes.options[route];
if(routes.options[route].hasOwnProperty('class')) {
routes.activeClass = routes.options[route].class;
$(document).find('.'+routes.activeClass).removeClass(routes.activeClass);
$('*[data-route="'+pageUrl+'"]').addClass(routes.activeClass);
} else {
$('*[data-route]').closest('li').siblings('li').removeClass(routes.activeClass+ " m-menu__item--open");
$('*[data-route="'+pageUrl+'"]').closest('li').addClass(routes.activeClass+ " m-menu__item--open");
}
self.currentRoute.hasOwnProperty('callback') ? window[self.currentRoute['callback']](response) : '';
(typeof cb === 'function') ? cb(response) : '';
// If Has Initial Load on Parent
if(routes.options[route].hasOwnProperty('initialLoad')) {
return self.executeRoute(routes.options[route].initialLoad);
}
}).catch( function(error){
showError(error);
self.currentRoute.hasOwnProperty('callback') ? window[self.currentRoute['callback']](error) : '';
$("#renderPartialPage").find('.m-loader.page').remove();
(typeof cb === 'function') ? cb(error) : '';
});
} else {
return this.notFound();
}
}
/**
* 404 Error for route path.
*/
Route.prototype.notFound = function() {
$("#renderPartialPage").find('.m-loader.page').remove();
toastr.error("View Not Found");
}
/**
* Get Current URL
* Current URL is defined on executeRoute method
*/
Route.prototype.getCurrentUrl = function() {
return (this.currentRoute !== null) ? this.currentRoute.url : false;
}
/**
* Reload Page using URL
*/
Route.prototype.redirect = function(url) {
if(!url && url === '' || url === ' ') {
toastr.error("Route Not Found");
}
routeIndex = url;
if(url.split('/').length > 1) {
routeIndex = routes.mapRoutes(url);
}
routes.executeRoute(routeIndex, {
url: url
});
}
/**
* Change Hash path on browser
*/
Route.prototype.changeUrl = function(url = null) {
var u = '';
if(url) {
u = url;
} else {
u = this.currentRoute.hasOwnProperty('url') ? this.currentRoute.url : url;
}
if(u.split('#').length > 1) {
u = u.split('#')[1];
}
this.location.hash = u;
};
/**
* Find dynamic placeholder of route
*/
Route.prototype.getPlaceholder = function(r) {
var placeHolders = [];
if(r.split("{").length >= 2){
for(var i =0; i < r.split("{").length; i++) {
if(r.split("{")[i].split('}').length >= 2) {
placeHolders.push(r.split("{")[i].split('}')[0]);
}
}
}
return placeHolders;
};
/**
* Get the defined dynamic route path using browser URL
*/
var matched = '';
var u = '';
Route.prototype.mapRoutes = function(url = null) {
var r = '';
if(url) {
var prev = ''
if(this.options.hasOwnProperty(url) && this.options[url].hasOwnProperty('url')) {
return url;
}
for(var index in this.options) {
if(index != prev) {
u = '';
}
prev = index;
if(index.split('/').length == url.split('/').length) {
for(var i = 0; i <= index.split('/').length; i++) {
if(index.split('/')[i] != undefined && index.split('/')[i].match(/\{(.*?)\}/g) == null) {
if(index.split('/')[i].trim() == url.split('/')[i].trim()) {
matched = true;
if(matched) {
u += index.split('/')[i]+'/';
}
} else {
matched = false;
}
} else {
if((index.split('/')[i] != undefined && url.split('/')[i] != undefined) && (index.split('/')[i].trim().match(/\{(.*?)\}/g) != null) && url.split('/')[i].trim().length) {
matched = true;
if(matched) {
u += index.split('/')[i]+'/';
}
} else {
matched = false;
}
}
}
if(url.split('/').length == u.replace(/.$/,"").split('/').length) {
r = u.replace(/.$/,"");
}
} else {
}
}
return r ? r : 'map not found';
}
};
function removePrevClass(self) {
if(routes.location.hash && routes.location.hash.split("#")[1].trim() != self.attr('data-route')) {
var prevRoute = routes.mapRoutes(routes.location.hash.split("#")[1].trim());
var prevRouteConfig = routes.options[prevRoute];
if(!prevRouteConfig)
return;
var removeClassList = prevRouteConfig.hasOwnProperty('class') ? prevRouteConfig.class : false;
if(removeClassList) {
$('*[data-route="'+routes.location.hash.split("#")[1].trim()+'"]').removeClass(removeClassList);
}
}
}
/**
* Event using *[data-route] attributes of DOM for route executation/navigation
*/
$(document).off('click','*[data-route]').on('click','*[data-route]', function(e) {
var self = $(this),
routeName = self.attr('data-route');
if(routeName === '' || routeName === ' ') {
toastr.error("Route Not Found");
}
if(routeName.split('/').length > 1) {
routeName = routes.mapRoutes(routeName);
}
removePrevClass(self);
$("#renderPartialPage").append('<div class="m-loader page" rel="pageLoader"><i class="mdi mdi-48px mdi-spin mdi-loading"></i></div>');
routes.executeRoute(routeName, {
url: self.attr('data-route')
});
});
/**
* On page reload get defined dynamic route using @mapRoutes method
* process route params for callback and get dynamic content
*/
window.onload = function() {
$("#renderPartialPage").append('<div class="m-loader page" rel="pageLoader"><i class="mdi mdi-48px mdi-spin mdi-loading"></i></div>');
var url = window.location.hash.length ? window.location.hash : '';
var routeIndex = url.split('#')[1];
if(routeIndex && routeIndex.split('/').length > 1) {
routeIndex = routes.mapRoutes(routeIndex);
}
if(url && url.split('#')[1] !== "undefined") {
if(routes.currentRoute && routes.currentRoute.hasOwnProperty('default')) {
// console.log("Default route detected");
} else {
if(Object.keys(window.routes).length && window.routes instanceof Route) {
routes.executeRoute(routeIndex, {
url: window.location.hash.split("#")[1]
});
}
}
}
}
/**
* Hash Change Event
*/
var oldURL = [];
window.onhashchange = function(e) {
oldURL.push(e.oldURL);
}
$(document).off('click','.redirect-back').on('click','.redirect-back', redirectBack);
function redirectBack(e) {
if(typeof e !== "undefined") {
e.preventDefault();
}
if(oldURL.length) {
var lastURLIndex = oldURL.length - 1,
url = oldURL[lastURLIndex],
routeIndex = url.split("#")[1];
if(routeIndex && routeIndex.split('/').length > 1) {
routeIndex = routes.mapRoutes(routeIndex);
}
if(url && typeof url !== "undefined") {
if(routes.currentRoute && routes.currentRoute.hasOwnProperty('default')) {
// console.log("Default route detected");
} else {
if(Object.keys(window.routes).length && window.routes instanceof Route) {
addFormLoader();
routes.executeRoute(routeIndex, {
url: url.split("#")[1]
}, function() {
removeFormLoader();
oldURL.pop();
});
}
}
}
} else {
// console.warn('Direct jumped path detected');
}
}
/**
* Show Error Message On Response status > 500
*/
function showError(error) {
if(error && error.response &&
error.response.status && error.response.status >= 500 &&
error.response.data) {
// Display Errors
for(var i = 0; i < error.response.data.length; i++) {
if(error.response.data[i].type == "error") {
toastr.error(error.response.data[i].data);
}
}
}
}
new Route({
'web/home': {
url: 'web/home',
default: true,
},
'web/about': {
url: 'web/about',
},
'web/application': {
url: 'web/application',
},
'web/partners': {
url: 'roles',
}
});