-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting.js
215 lines (191 loc) · 5.1 KB
/
routing.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
import config from './config';
/**
* This is main module.
* By Default its only the thing you should use working with fe-routing-js
* @module routing
*/
export default {
/**
* Returns current routing instance. If it does not exist instance will be created.
* @private
* @returns {Router} routing instance
*/
_ensureRouter() {
if (!this.instance) {
this.instance = this.createRouter();
}
return this.instance;
},
/**
* Creates instance of Router with config.routingOptions.
* @returns {Router} Router instance
* @param {routingOptions} [options]
*/
createRouter(options) {
return new config.Router(options || config.routingOptions);
},
/**
* Registers routeHandler.
* Alias for `routing.instance.get`.
* @example
* routing.get('some/page', (req, res) => { ... })
* @see {@link Router.get}
* @returns {Router}
*/
get(...args) {
return this._ensureRouter().get(...args);
},
/**
* Adds global middleware, router or route middleware.
* Alias for `routing.instance.use`.
* @example
* // adding global middleware to the end of array
* routing.use((req, res, next) => { next(); });
*
* // adding nested router
* routing.use('acc', accountRouter);
*
* // adding route middleware to the begining of route's middlewares array
* routing.use('some/page', (req, res, next) => { next(); });
* @see {@link Router.use}
* @returns {Router}
*/
use(...args) {
if (arguments.length == 1 && arguments[0] instanceof config.Router) {
if (this.isStarted()) {
throw new Error(
'Main router already initialized and started. You have to stop it first'
);
} else {
this.instance = arguments[0];
return this.instance;
}
}
return this._ensureRouter().use(...args);
},
// /**
// * Returns true if routing started
// * @returns {boolean}
// */
// isStarted() {
// if (this.instance) {
// return this.instance.isStarted();
// }
// return false;
// },
/**
* Starts routing
* @param {startOptions} [options]
* @returns {Router}
*/
start(options) {
if (!options) {
options = {};
}
this._ensureRouter();
if (this.isStarted()) {
throw new Error('Routing already started');
}
if (options.errorHandlers) {
//applying errorHandlers if any
this.instance.setErrorHandlers(
options.replaceErrorHandlers,
options.errorHandlers
);
}
if (typeof options.onRequestStart === 'function') {
this.instance.onRequestStart = options.onRequestStart;
}
if (typeof options.onRequestEnd === 'function') {
this.instance.onRequestEnd = options.onRequestEnd;
}
if (options.useHashes != null) {
//update routing useHashes flag
config.useHashes = options.useHashes === true;
}
config.isStarted = true;
let navigateOptions = Object.assign({}, options, { pushState: false });
if (options.trigger !== false) {
//triggering middlewares only if trigger is not disallowed.
this.navigate(navigateOptions);
}
this._setOnPopstate(navigateOptions);
},
/**
* Stops routing
* @see {@link Router.stop}
* @returns {Router}
*/
stop() {
if (!this.isStarted()) {
return;
}
config.isStarted = false;
this.instance.setCurrentUrl(null);
this._removeOnPopstate();
},
/**
* Removes middleware or routeHandler.
* Alias for `routing.instance.remove`.
* @see {@link Router.remove}
* @returns {(RouteHandler|void)}
*/
remove(...args) {
if (!this.instance) {
return;
}
return this.instance.remove(...args);
},
/**
* Initiates the request.
* Proxy method for Router instance's `navigate`.
* @see {@link Router.navigate}
*/
navigate(...args) {
if (!this.instance) {
return;
}
return this.instance.navigate(...args);
},
/**
* routing Configuration
* @see {@link configuration}
*/
config,
/**
* Returns routing state. True if started
* @return {boolean}
*/
isStarted() {
return config.isStarted === true;
},
/**
* Sets onpopstate handler to reflect on history go forward/back.
* @private
* @memberof Router
*/
_setOnPopstate() {
this._onPopstate = event => {
if (event == null || typeof event != 'object') {
event = {};
}
let state = event.state != null ? event.state : {};
let options = state.navigateOptions || { trigger: true };
options.pushState = false;
options.state = state;
return this.navigate(options);
};
window.addEventListener('popstate', this._onPopstate);
},
/**
* removes onpopstate handler
* @private
* @memberof Router
*/
_removeOnPopstate() {
if (typeof this._onPopstate == 'function') {
window.removeEventListener('popstate', this._onPopstate);
delete this._onPopstate;
}
}
};