-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.js
122 lines (97 loc) · 3.63 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
import DefineMap from 'can-define/map/';
import resources from '~/resources';
import localisation from '~/localisation';
import access from 'shuttle-access';
import stache from 'can-stache';
import route from 'can-route';
import state from '~/state';
import guard from 'shuttle-guard';
import each from 'can-util/js/each/';
import navbar from '~/navbar';
var Router = DefineMap.extend({
data: {
get() {
return state.routeData;
}
},
previousHash: 'string',
init: function () {
const self = this;
this.data.on('full', function () {
self.process.call(self);
});
},
start: function () {
route.register('{resource}');
route.register('{resource}/{action}');
route.register('{resource}/{id}/{action}');
route.register('{resource}/{id}/{item}/{action}');
route.data = this.data;
route.start();
},
process: function () {
var resource;
if ($('#application-content').length === 0) {
return;
}
if (!this.data.resource) {
return;
}
resource = resources.find(this.data.resource, this.data);
if (!resource) {
state.alerts.show({
message: localisation.value('exceptions.resource-not-found', {
resource: this.data.resource,
item: this.data.item|| '(-value)',
action: this.data.action || '(-action)',
id: this.data.id || '(-id)',
interpolation: {escapeValue: false}
}), type: 'warning', name: 'route-error'
});
return;
}
if (resource.permission && !access.hasPermission(resource.permission)) {
state.alerts.show({
message: localisation.value('security.access-denied', {
resource: this.data.resource,
item: this.data.item|| '(-value)',
action: this.data.action || '(-action)',
id: this.data.id || '(-id)',
permission: resource.permission,
interpolation: {escapeValue: false}
}), type: 'danger', name: 'route-error'
});
return;
}
state.alerts.clear();
navbar.controls.clear();
state.title = '';
var componentName = resource.componentName || 'abacus-' + resource.resource + (!!this.data.item ? `-${this.data.item}` : '') + (!!this.data.action ? `-${this.data.action}` : '');
$('#application-content').html(stache('<' + componentName + '></' + componentName + '>')());
},
goto: function (data) {
guard.againstUndefined(data, 'data');
if (typeof(data) !== 'object') {
throw new Error('Call \'router.goto\' with route data: e.g. router.goto({resource: \'the-resource\', action: \'the-action\'});');
}
if (!data.resource) {
throw new Error('The \'data\' argument does not contain a \'resource\' value.')
}
each(Object.getOwnPropertyNames(data), function (propertyName) {
if (
propertyName !== 'resource'
&&
propertyName !== 'item'
&&
propertyName !== 'action'
&&
propertyName !== 'id'
) {
throw new Error('The route data contains an unknown attribute \'' + propertyName + '\'.');
}
}
);
route.data.update(data, true);
}
});
export default new Router();