ngx-named-routes
Version:
Add named routes to your angular 7 projects
219 lines (209 loc) • 8.95 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/router'), require('lodash')) :
typeof define === 'function' && define.amd ? define('ngx-named-routes', ['exports', '@angular/core', '@angular/router', 'lodash'], factory) :
(factory((global['ngx-named-routes'] = {}),global.ng.core,global.ng.router,global.lodash));
}(this, (function (exports,i0,i1,lodash) { 'use strict';
/*! *****************************************************************************
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at http://www.apache.org/licenses/LICENSE-2.0
THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
MERCHANTABLITY OR NON-INFRINGEMENT.
See the Apache Version 2.0 License for specific language governing permissions
and limitations under the License.
***************************************************************************** */
var __assign = function () {
__assign = Object.assign || function __assign(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s)
if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
function __values(o) {
var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0;
if (m)
return m.call(o);
return {
next: function () {
if (o && i >= o.length)
o = void 0;
return { value: o && o[i++], done: !o };
}
};
}
/**
* @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 : lodash.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: i0.Injectable, args: [{
providedIn: 'root'
},] }
];
/** @nocollapse */
NgxNamedRoutesService.ctorParameters = function () {
return [
{ type: i1.Router }
];
};
/** @nocollapse */ NgxNamedRoutesService.ngInjectableDef = i0.defineInjectable({ factory: function NgxNamedRoutesService_Factory() { return new NgxNamedRoutesService(i0.inject(i1.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
*/
exports.NgxNamedRoutesService = NgxNamedRoutesService;
Object.defineProperty(exports, '__esModule', { value: true });
})));
//# sourceMappingURL=ngx-named-routes.umd.js.map