UNPKG

ngx-named-routes

Version:

Add named routes to your angular 7 projects

167 lines (162 loc) 5.76 kB
import { __values, __assign } from 'tslib'; import { Injectable, defineInjectable, inject } from '@angular/core'; import { Router } from '@angular/router'; import { cloneDeep } from 'lodash'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var NgxNamedRoutesService = /** @class */ (function () { function NgxNamedRoutesService(__router) { this.__router = __router; this.counter = 0; this.__named_routes = {}; } /** * @param {?=} child_route * @return {?} */ NgxNamedRoutesService.prototype.loadRoutes = /** * @param {?=} child_route * @return {?} */ function (child_route) { var e_1, _a; this.counter++; /** @type {?} */ var routes = []; routes = child_route ? child_route.routes : cloneDeep(this.__router.config); try { for (var routes_1 = __values(routes), routes_1_1 = routes_1.next(); !routes_1_1.done; routes_1_1 = routes_1.next()) { var route = routes_1_1.value; /** @type {?} */ var backslash_required = void 0; if (child_route && ['', '/'].indexOf(child_route.parent_path) === -1) { backslash_required = child_route.parent_path[0] === '/' ? '' : '/'; route.path = backslash_required + child_route.parent_path + (route.path !== '' ? '/' + route.path : ''); } else { backslash_required = route.path[0] === '/' && route.path !== '' ? '' : '/'; route.path = backslash_required + route.path; } if (route.name && !this.__named_routes[route.name]) { this.__named_routes[route.name] = __assign({}, route); } if (route.children) { this.loadRoutes({ routes: route.children, parent_path: route.path }); } if (route._loadedConfig) { this.loadRoutes({ routes: route._loadedConfig.routes, parent_path: route.path }); } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (routes_1_1 && !routes_1_1.done && (_a = routes_1.return)) _a.call(routes_1); } finally { if (e_1) throw e_1.error; } } return this.__named_routes; }; /** * @param {?} name * @param {?=} param_values * @return {?} */ NgxNamedRoutesService.prototype.getRoute = /** * @param {?} name * @param {?=} param_values * @return {?} */ function (name, param_values) { // TODO: si no existe el nombre, llamar nuevamente a loadRoutes, porque el módulo puede haber sido cargado perezosamente // need preloading strategy PreloadAllModules /** @type {?} */ var raw_route; if (!this.__named_routes[name]) { this.loadRoutes(); if (!this.__named_routes[name]) { throw new Error("La ruta " + name + " no existe o el m\u00F3dulo que la define a\u00FAn no ha sido cargado."); } raw_route = this.__named_routes[name].path; if (raw_route) { return this.fillRouteParams(raw_route, param_values); } } else { raw_route = this.__named_routes[name].path; return this.fillRouteParams(raw_route, param_values); } }; /** * @return {?} */ NgxNamedRoutesService.prototype.getNamedRoutes = /** * @return {?} */ function () { if (Object.keys(this.__named_routes).length === 0) { this.loadRoutes(); } return this.__named_routes; }; /** * @param {?} name * @return {?} */ NgxNamedRoutesService.prototype.getNamedRoute = /** * @param {?} name * @return {?} */ function (name) { if (Object.keys(this.__named_routes).length === 0) { this.loadRoutes(); } return this.__named_routes[name]; }; /** * @private * @param {?} raw_route * @param {?=} param_values * @return {?} */ NgxNamedRoutesService.prototype.fillRouteParams = /** * @private * @param {?} raw_route * @param {?=} param_values * @return {?} */ function (raw_route, param_values) { if (!param_values || param_values === {}) { return raw_route; } /** @type {?} */ var route = raw_route; for (var param_key in param_values) { route = route.replace(':' + param_key, param_values[param_key]); } return route; }; NgxNamedRoutesService.decorators = [ { type: Injectable, args: [{ providedIn: 'root' },] } ]; /** @nocollapse */ NgxNamedRoutesService.ctorParameters = function () { return [ { type: Router } ]; }; /** @nocollapse */ NgxNamedRoutesService.ngInjectableDef = defineInjectable({ factory: function NgxNamedRoutesService_Factory() { return new NgxNamedRoutesService(inject(Router)); }, token: NgxNamedRoutesService, providedIn: "root" }); return NgxNamedRoutesService; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ export { NgxNamedRoutesService }; //# sourceMappingURL=ngx-named-routes.js.map