UNPKG

5.19 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || function (d, b) {
3 for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
4 function __() { this.constructor = d; }
5 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
6};
7var collection_1 = require('../facade/collection');
8var exceptions_1 = require('../facade/exceptions');
9var lang_1 = require('../facade/lang');
10var promise_1 = require('../facade/promise');
11var instruction_1 = require('../instruction');
12var url_parser_1 = require('../url_parser');
13// RouteMatch objects hold information about a match between a rule and a URL
14var RouteMatch = (function () {
15 function RouteMatch() {
16 }
17 return RouteMatch;
18}());
19exports.RouteMatch = RouteMatch;
20var PathMatch = (function (_super) {
21 __extends(PathMatch, _super);
22 function PathMatch(instruction, remaining, remainingAux) {
23 _super.call(this);
24 this.instruction = instruction;
25 this.remaining = remaining;
26 this.remainingAux = remainingAux;
27 }
28 return PathMatch;
29}(RouteMatch));
30exports.PathMatch = PathMatch;
31var RedirectMatch = (function (_super) {
32 __extends(RedirectMatch, _super);
33 function RedirectMatch(redirectTo, specificity /** TODO #9100 */) {
34 _super.call(this);
35 this.redirectTo = redirectTo;
36 this.specificity = specificity;
37 }
38 return RedirectMatch;
39}(RouteMatch));
40exports.RedirectMatch = RedirectMatch;
41var RedirectRule = (function () {
42 function RedirectRule(_pathRecognizer, redirectTo) {
43 this._pathRecognizer = _pathRecognizer;
44 this.redirectTo = redirectTo;
45 this.hash = this._pathRecognizer.hash;
46 }
47 Object.defineProperty(RedirectRule.prototype, "path", {
48 get: function () { return this._pathRecognizer.toString(); },
49 set: function (val) { throw new exceptions_1.BaseException('you cannot set the path of a RedirectRule directly'); },
50 enumerable: true,
51 configurable: true
52 });
53 /**
54 * Returns `null` or a `ParsedUrl` representing the new path to match
55 */
56 RedirectRule.prototype.recognize = function (beginningSegment) {
57 var match = null;
58 if (lang_1.isPresent(this._pathRecognizer.matchUrl(beginningSegment))) {
59 match = new RedirectMatch(this.redirectTo, this._pathRecognizer.specificity);
60 }
61 return promise_1.PromiseWrapper.resolve(match);
62 };
63 RedirectRule.prototype.generate = function (params) {
64 throw new exceptions_1.BaseException("Tried to generate a redirect.");
65 };
66 return RedirectRule;
67}());
68exports.RedirectRule = RedirectRule;
69// represents something like '/foo/:bar'
70var RouteRule = (function () {
71 // TODO: cache component instruction instances by params and by ParsedUrl instance
72 function RouteRule(_routePath, handler, _routeName) {
73 this._routePath = _routePath;
74 this.handler = handler;
75 this._routeName = _routeName;
76 this._cache = new collection_1.Map();
77 this.specificity = this._routePath.specificity;
78 this.hash = this._routePath.hash;
79 this.terminal = this._routePath.terminal;
80 }
81 Object.defineProperty(RouteRule.prototype, "path", {
82 get: function () { return this._routePath.toString(); },
83 set: function (val) { throw new exceptions_1.BaseException('you cannot set the path of a RouteRule directly'); },
84 enumerable: true,
85 configurable: true
86 });
87 RouteRule.prototype.recognize = function (beginningSegment) {
88 var _this = this;
89 var res = this._routePath.matchUrl(beginningSegment);
90 if (lang_1.isBlank(res)) {
91 return null;
92 }
93 return this.handler.resolveComponentType().then(function (_) {
94 var componentInstruction = _this._getInstruction(res.urlPath, res.urlParams, res.allParams);
95 return new PathMatch(componentInstruction, res.rest, res.auxiliary);
96 });
97 };
98 RouteRule.prototype.generate = function (params) {
99 var generated = this._routePath.generateUrl(params);
100 var urlPath = generated.urlPath;
101 var urlParams = generated.urlParams;
102 return this._getInstruction(urlPath, url_parser_1.convertUrlParamsToArray(urlParams), params);
103 };
104 RouteRule.prototype.generateComponentPathValues = function (params) {
105 return this._routePath.generateUrl(params);
106 };
107 RouteRule.prototype._getInstruction = function (urlPath, urlParams, params) {
108 if (lang_1.isBlank(this.handler.componentType)) {
109 throw new exceptions_1.BaseException("Tried to get instruction before the type was loaded.");
110 }
111 var hashKey = urlPath + '?' + urlParams.join('&');
112 if (this._cache.has(hashKey)) {
113 return this._cache.get(hashKey);
114 }
115 var instruction = new instruction_1.ComponentInstruction(urlPath, urlParams, this.handler.data, this.handler.componentType, this.terminal, this.specificity, params, this._routeName);
116 this._cache.set(hashKey, instruction);
117 return instruction;
118 };
119 return RouteRule;
120}());
121exports.RouteRule = RouteRule;
122//# sourceMappingURL=rules.js.map
\No newline at end of file