UNPKG

5.46 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.UrlRouter = void 0;
4var common_1 = require("../common");
5var urlRule_1 = require("./urlRule");
6function appendBasePath(url, isHtml5, absolute, baseHref) {
7 if (baseHref === '/')
8 return url;
9 if (isHtml5)
10 return common_1.stripLastPathElement(baseHref) + url;
11 if (absolute)
12 return baseHref.slice(1) + url;
13 return url;
14}
15/**
16 * Updates URL and responds to URL changes
17 *
18 * ### Deprecation warning:
19 * This class is now considered to be an internal API
20 * Use the [[UrlService]] instead.
21 * For configuring URL rules, use the [[UrlRules]] which can be found as [[UrlService.rules]].
22 */
23var UrlRouter = /** @class */ (function () {
24 /** @internal */
25 function UrlRouter(/** @internal */ router) {
26 var _this = this;
27 this.router = router;
28 // Delegate these calls to [[UrlService]]
29 /** @deprecated use [[UrlService.sync]]*/
30 this.sync = function (evt) { return _this.router.urlService.sync(evt); };
31 /** @deprecated use [[UrlService.listen]]*/
32 this.listen = function (enabled) { return _this.router.urlService.listen(enabled); };
33 /** @deprecated use [[UrlService.deferIntercept]]*/
34 this.deferIntercept = function (defer) { return _this.router.urlService.deferIntercept(defer); };
35 /** @deprecated use [[UrlService.match]]*/
36 this.match = function (urlParts) { return _this.router.urlService.match(urlParts); };
37 // Delegate these calls to [[UrlRules]]
38 /** @deprecated use [[UrlRules.initial]]*/
39 this.initial = function (handler) {
40 return _this.router.urlService.rules.initial(handler);
41 };
42 /** @deprecated use [[UrlRules.otherwise]]*/
43 this.otherwise = function (handler) {
44 return _this.router.urlService.rules.otherwise(handler);
45 };
46 /** @deprecated use [[UrlRules.removeRule]]*/
47 this.removeRule = function (rule) { return _this.router.urlService.rules.removeRule(rule); };
48 /** @deprecated use [[UrlRules.rule]]*/
49 this.rule = function (rule) { return _this.router.urlService.rules.rule(rule); };
50 /** @deprecated use [[UrlRules.rules]]*/
51 this.rules = function () { return _this.router.urlService.rules.rules(); };
52 /** @deprecated use [[UrlRules.sort]]*/
53 this.sort = function (compareFn) { return _this.router.urlService.rules.sort(compareFn); };
54 /** @deprecated use [[UrlRules.when]]*/
55 this.when = function (matcher, handler, options) { return _this.router.urlService.rules.when(matcher, handler, options); };
56 this.urlRuleFactory = new urlRule_1.UrlRuleFactory(router);
57 }
58 /** Internal API. */
59 UrlRouter.prototype.update = function (read) {
60 var $url = this.router.locationService;
61 if (read) {
62 this.location = $url.url();
63 return;
64 }
65 if ($url.url() === this.location)
66 return;
67 $url.url(this.location, true);
68 };
69 /**
70 * Internal API.
71 *
72 * Pushes a new location to the browser history.
73 *
74 * @internal
75 * @param urlMatcher
76 * @param params
77 * @param options
78 */
79 UrlRouter.prototype.push = function (urlMatcher, params, options) {
80 var replace = options && !!options.replace;
81 this.router.urlService.url(urlMatcher.format(params || {}), replace);
82 };
83 /**
84 * Builds and returns a URL with interpolated parameters
85 *
86 * #### Example:
87 * ```js
88 * matcher = $umf.compile("/about/:person");
89 * params = { person: "bob" };
90 * $bob = $urlRouter.href(matcher, params);
91 * // $bob == "/about/bob";
92 * ```
93 *
94 * @param urlMatcher The [[UrlMatcher]] object which is used as the template of the URL to generate.
95 * @param params An object of parameter values to fill the matcher's required parameters.
96 * @param options Options object. The options are:
97 *
98 * - **`absolute`** - {boolean=false}, If true will generate an absolute url, e.g. "http://www.example.com/fullurl".
99 *
100 * @returns Returns the fully compiled URL, or `null` if `params` fail validation against `urlMatcher`
101 */
102 UrlRouter.prototype.href = function (urlMatcher, params, options) {
103 var url = urlMatcher.format(params);
104 if (url == null)
105 return null;
106 options = options || { absolute: false };
107 var cfg = this.router.urlService.config;
108 var isHtml5 = cfg.html5Mode();
109 if (!isHtml5 && url !== null) {
110 url = '#' + cfg.hashPrefix() + url;
111 }
112 url = appendBasePath(url, isHtml5, options.absolute, cfg.baseHref());
113 if (!options.absolute || !url) {
114 return url;
115 }
116 var slash = !isHtml5 && url ? '/' : '';
117 var cfgPort = cfg.port();
118 var port = (cfgPort === 80 || cfgPort === 443 ? '' : ':' + cfgPort);
119 return [cfg.protocol(), '://', cfg.host(), port, slash, url].join('');
120 };
121 Object.defineProperty(UrlRouter.prototype, "interceptDeferred", {
122 /** @deprecated use [[UrlService.interceptDeferred]]*/
123 get: function () {
124 return this.router.urlService.interceptDeferred;
125 },
126 enumerable: false,
127 configurable: true
128 });
129 return UrlRouter;
130}());
131exports.UrlRouter = UrlRouter;
132//# sourceMappingURL=urlRouter.js.map
\No newline at end of file