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 | 5x 55x 55x 55x 55x 55x 55x 55x 55x 55x 57x 57x 57x 57x 72x 1x 71x 71x 71x 71x 3x 3x 1x 6x 7x 2x 2x 1x 1x 1x 2x 2x 1x 1x 5x 5x 5x 2x 2x 3x 3x 2x 2x 1x 1x 1x 4x 4x 3x 3x 1x 4x 2x 10x 8x 8x 8x 8x 8x 8x 8x 8x 2x 8x 8x 8x 5x 4x 3x 1x 5x | import ns from '../namespace'; import Events from './Events'; import Router from './Router'; import RouteNames from './RouteNames'; import GenericError from '../error/GenericError'; ns.namespace('ima.router'); /** * The basic implementation of the {@codelink Router} interface, providing the * common or default functionality for parts of the API. * * @abstract */ export default class AbstractRouter extends Router { /** * Initializes the router. * * @param {PageManager} pageManager The page manager handling UI rendering, * and transitions between pages if at the client side. * @param {RouteFactory} factory Factory for routes. * @param {Dispatcher} dispatcher Dispatcher fires events to app. * @example * router.link('article', {articleId: 1}); * @example * router.redirect('http://www.example.com/web'); * @example * router.add( * 'home', * '/', * ns.app.page.home.Controller, * ns.app.page.home.View, * { * onlyUpdate: false, * autoScroll: true, * allowSPA: true, * documentView: null * } * ); */ constructor(pageManager, factory, dispatcher) { super(); /** * The page manager handling UI rendering, and transitions between * pages if at the client side. * * @type {PageManager} */ this._pageManager = pageManager; /** * Factory for routes. * * @type {RouteFactory} */ this._factory = factory; /** * Dispatcher fires events to app. * * @type {Dispatcher} */ this._dispatcher = dispatcher; /** * The current protocol used to access the application, terminated by a * colon (for example {@code https:}). * * @type {string} */ this._protocol = ''; /** * The application's host. * * @type {string} */ this._host = ''; /** * The URL path pointing to the application's root. * * @type {string} */ this._root = ''; /** * The URL path fragment used as a suffix to the {@code _root} field * that specifies the current language. * * @type {string} */ this._languagePartPath = ''; /** * Storage of all known routes. The key are the route names. * * @type {Map<string, Route>} */ this._routes = new Map(); } /** * @inheritdoc */ init(config) { this._protocol = config.$Protocol || ''; this._root = config.$Root || ''; this._languagePartPath = config.$LanguagePartPath || ''; this._host = config.$Host; } /** * @inheritdoc */ add(name, pathExpression, controller, view, options = undefined) { if (this._routes.has(name)) { throw new GenericError( `ima.router.AbstractRouter.add: The route with name ${name} ` + `is already defined` ); } let factory = this._factory; let route = factory.createRoute( name, pathExpression, controller, view, options ); this._routes.set(name, route); return this; } /** * @inheritdoc */ remove(name) { this._routes.delete(name); return this; } /** * @inheritdoc */ getPath() { throw new GenericError( 'The getPath() method is abstract and must be overridden.' ); } /** * @inheritdoc */ getUrl() { return this.getBaseUrl() + this.getPath(); } /** * @inheritdoc */ getBaseUrl() { return this.getDomain() + this._root + this._languagePartPath; } /** * @inheritdoc */ getDomain() { return this._protocol + '//' + this._host; } /** * @inheritdoc */ getHost() { return this._host; } /** * @inheritdoc */ getProtocol() { return this._protocol; } /** * @inheritdoc */ getCurrentRouteInfo() { let path = this.getPath(); let route = this._getRouteByPath(path); Iif (!route) { throw new GenericError( `ima.router.AbstractRouter.getCurrentRouteInfo: The route ` + `for path ${path} is not defined.` ); } let params = route.extractParameters(path); return { route, params, path }; } /** * @inheritdoc * @abstract */ listen() { throw new GenericError( 'The listen() method is abstract and must be overridden.' ); } /** * @inheritdoc * @abstract */ redirect(url, options) { throw new GenericError( 'The redirect() method is abstract and must be overridden.' ); } /** * @inheritdoc */ link(routeName, params) { let route = this._routes.get(routeName); if (!route) { throw new GenericError( `ima.router.AbstractRouter:link has undefined route with ` + `name ${routeName}. Add new route with that name.` ); } return this.getBaseUrl() + route.toPath(params); } /** * @inheritdoc */ route(path, options = {}) { let routeForPath = this._getRouteByPath(path); let params = {}; if (!routeForPath) { params.error = new GenericError( `Route for path '${path}' is not configured.`, { status: 404 } ); return this.handleNotFound(params); } params = routeForPath.extractParameters(path); return this._handle(routeForPath, params, options); } /** * @inheritdoc */ handleError(params, options = {}) { let routeError = this._routes.get(RouteNames.ERROR); if (!routeError) { let error = new GenericError( `ima.router.AbstractRouter:handleError cannot process the ` + `error because no error page route has been configured. Add ` + `a new route named '${RouteNames.ERROR}'.`, params ); return Promise.reject(error); } return this._handle(routeError, params, options); } /** * @inheritdoc */ handleNotFound(params, options = {}) { let routeNotFound = this._routes.get(RouteNames.NOT_FOUND); if (!routeNotFound) { let error = new GenericError( `ima.router.AbstractRouter:handleNotFound cannot processes ` + `a non-matching route because no not found page route has ` + `been configured. Add new route named ` + `'${RouteNames.NOT_FOUND}'.`, params ); return Promise.reject(error); } return this._handle(routeNotFound, params, options); } /** * @inheritdoc */ isClientError(reason) { return ( reason instanceof GenericError && reason.getHttpStatus() >= 400 && reason.getHttpStatus() < 500 ); } /** * @inheritdoc */ isRedirection(reason) { return ( reason instanceof GenericError && reason.getHttpStatus() >= 300 && reason.getHttpStatus() < 400 ); } /** * Strips the URL path part that points to the application's root (base * URL) from the provided path. * * @protected * @param {string} path Relative or absolute URL path. * @return {string} URL path relative to the application's base URL. */ _extractRoutePath(path) { return path.replace(this._root + this._languagePartPath, ''); } /** * Handles the provided route and parameters by initializing the route's * controller and rendering its state via the route's view. * * The result is then sent to the client if used at the server side, or * displayed if used as the client side. * * @param {Route} route The route that should have its * associated controller rendered via the associated view. * @param {Object<string, (Error|string)>} params Parameters extracted from * the URL path and query. * @param {{ * onlyUpdate: ( * boolean| * function( * (string|function(new: Controller, ...*)), * (string|function( * new: React.Component, * Object<string, *>, * ?Object<string, *> * )) * ): boolean * )=, * autoScroll: boolean=, * allowSPA: boolean=, * documentView: ?AbstractDocumentView= * }} options The options overrides route options defined in the * {@code routes.js} configuration file. * @return {Promise<Object<string, *>>} A promise that resolves when the * page is rendered and the result is sent to the client, or * displayed if used at the client side. */ _handle(route, params, options) { let controller = route.getController(); let view = route.getView(); options = Object.assign({}, route.getOptions(), options); let data = { route, params, path: this.getPath(), options }; this._dispatcher.fire(Events.BEFORE_HANDLE_ROUTE, data, true); return this._pageManager .manage(controller, view, options, params) .then(response => { response = response || {}; if (params.error && params.error instanceof Error) { response.error = params.error; } data.response = response; this._dispatcher.fire(Events.AFTER_HANDLE_ROUTE, data, true); return response; }); } /** * Returns the route matching the provided URL path part. The path may * contain a query. * * @param {string} path The URL path. * @return {?Route} The route matching the path, or {@code null} if no such * route exists. */ _getRouteByPath(path) { for (let route of this._routes.values()) { Eif (route.matches(path)) { return route; } } return null; } } ns.ima.router.AbstractRouter = AbstractRouter; |