1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.Param = exports.DefType = void 0;
|
4 | var common_1 = require("../common/common");
|
5 | var hof_1 = require("../common/hof");
|
6 | var predicates_1 = require("../common/predicates");
|
7 | var coreservices_1 = require("../common/coreservices");
|
8 | var paramType_1 = require("./paramType");
|
9 | var hasOwn = Object.prototype.hasOwnProperty;
|
10 | var isShorthand = function (cfg) {
|
11 | return ['value', 'type', 'squash', 'array', 'dynamic'].filter(hasOwn.bind(cfg || {})).length === 0;
|
12 | };
|
13 | var DefType;
|
14 | (function (DefType) {
|
15 | DefType[DefType["PATH"] = 0] = "PATH";
|
16 | DefType[DefType["SEARCH"] = 1] = "SEARCH";
|
17 | DefType[DefType["CONFIG"] = 2] = "CONFIG";
|
18 | })(DefType || (DefType = {}));
|
19 | exports.DefType = DefType;
|
20 | function getParamDeclaration(paramName, location, state) {
|
21 | var noReloadOnSearch = (state.reloadOnSearch === false && location === DefType.SEARCH) || undefined;
|
22 | var dynamic = common_1.find([state.dynamic, noReloadOnSearch], predicates_1.isDefined);
|
23 | var defaultConfig = predicates_1.isDefined(dynamic) ? { dynamic: dynamic } : {};
|
24 | var paramConfig = unwrapShorthand(state && state.params && state.params[paramName]);
|
25 | return common_1.extend(defaultConfig, paramConfig);
|
26 | }
|
27 | function unwrapShorthand(cfg) {
|
28 | cfg = isShorthand(cfg) ? { value: cfg } : cfg;
|
29 | getStaticDefaultValue['__cacheable'] = true;
|
30 | function getStaticDefaultValue() {
|
31 | return cfg.value;
|
32 | }
|
33 | var $$fn = predicates_1.isInjectable(cfg.value) ? cfg.value : getStaticDefaultValue;
|
34 | return common_1.extend(cfg, { $$fn: $$fn });
|
35 | }
|
36 | function getType(cfg, urlType, location, id, paramTypes) {
|
37 | if (cfg.type && urlType && urlType.name !== 'string')
|
38 | throw new Error("Param '" + id + "' has two type configurations.");
|
39 | if (cfg.type && urlType && urlType.name === 'string' && paramTypes.type(cfg.type))
|
40 | return paramTypes.type(cfg.type);
|
41 | if (urlType)
|
42 | return urlType;
|
43 | if (!cfg.type) {
|
44 | var type = location === DefType.CONFIG
|
45 | ? 'any'
|
46 | : location === DefType.PATH
|
47 | ? 'path'
|
48 | : location === DefType.SEARCH
|
49 | ? 'query'
|
50 | : 'string';
|
51 | return paramTypes.type(type);
|
52 | }
|
53 | return cfg.type instanceof paramType_1.ParamType ? cfg.type : paramTypes.type(cfg.type);
|
54 | }
|
55 |
|
56 | function getSquashPolicy(config, isOptional, defaultPolicy) {
|
57 | var squash = config.squash;
|
58 | if (!isOptional || squash === false)
|
59 | return false;
|
60 | if (!predicates_1.isDefined(squash) || squash == null)
|
61 | return defaultPolicy;
|
62 | if (squash === true || predicates_1.isString(squash))
|
63 | return squash;
|
64 | throw new Error("Invalid squash policy: '" + squash + "'. Valid policies: false, true, or arbitrary string");
|
65 | }
|
66 | function getReplace(config, arrayMode, isOptional, squash) {
|
67 | var defaultPolicy = [
|
68 | { from: '', to: isOptional || arrayMode ? undefined : '' },
|
69 | { from: null, to: isOptional || arrayMode ? undefined : '' },
|
70 | ];
|
71 | var replace = predicates_1.isArray(config.replace) ? config.replace : [];
|
72 | if (predicates_1.isString(squash))
|
73 | replace.push({ from: squash, to: undefined });
|
74 | var configuredKeys = common_1.map(replace, hof_1.prop('from'));
|
75 | return common_1.filter(defaultPolicy, function (item) { return configuredKeys.indexOf(item.from) === -1; }).concat(replace);
|
76 | }
|
77 | var Param = (function () {
|
78 | function Param(id, type, location, urlConfig, state) {
|
79 | var config = getParamDeclaration(id, location, state);
|
80 | type = getType(config, type, location, id, urlConfig.paramTypes);
|
81 | var arrayMode = getArrayMode();
|
82 | type = arrayMode ? type.$asArray(arrayMode, location === DefType.SEARCH) : type;
|
83 | var isOptional = config.value !== undefined || location === DefType.SEARCH;
|
84 | var dynamic = predicates_1.isDefined(config.dynamic) ? !!config.dynamic : !!type.dynamic;
|
85 | var raw = predicates_1.isDefined(config.raw) ? !!config.raw : !!type.raw;
|
86 | var squash = getSquashPolicy(config, isOptional, urlConfig.defaultSquashPolicy());
|
87 | var replace = getReplace(config, arrayMode, isOptional, squash);
|
88 | var inherit = predicates_1.isDefined(config.inherit) ? !!config.inherit : !!type.inherit;
|
89 |
|
90 | function getArrayMode() {
|
91 | var arrayDefaults = { array: location === DefType.SEARCH ? 'auto' : false };
|
92 | var arrayParamNomenclature = id.match(/\[\]$/) ? { array: true } : {};
|
93 | return common_1.extend(arrayDefaults, arrayParamNomenclature, config).array;
|
94 | }
|
95 | common_1.extend(this, { id: id, type: type, location: location, isOptional: isOptional, dynamic: dynamic, raw: raw, squash: squash, replace: replace, inherit: inherit, array: arrayMode, config: config });
|
96 | }
|
97 | Param.values = function (params, values) {
|
98 | if (values === void 0) { values = {}; }
|
99 | var paramValues = {};
|
100 | for (var _i = 0, params_1 = params; _i < params_1.length; _i++) {
|
101 | var param = params_1[_i];
|
102 | paramValues[param.id] = param.value(values[param.id]);
|
103 | }
|
104 | return paramValues;
|
105 | };
|
106 | |
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | Param.changed = function (params, values1, values2) {
|
118 | if (values1 === void 0) { values1 = {}; }
|
119 | if (values2 === void 0) { values2 = {}; }
|
120 | return params.filter(function (param) { return !param.type.equals(values1[param.id], values2[param.id]); });
|
121 | };
|
122 | |
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | Param.equals = function (params, values1, values2) {
|
132 | if (values1 === void 0) { values1 = {}; }
|
133 | if (values2 === void 0) { values2 = {}; }
|
134 | return Param.changed(params, values1, values2).length === 0;
|
135 | };
|
136 |
|
137 | Param.validates = function (params, values) {
|
138 | if (values === void 0) { values = {}; }
|
139 | return params.map(function (param) { return param.validates(values[param.id]); }).reduce(common_1.allTrueR, true);
|
140 | };
|
141 | Param.prototype.isDefaultValue = function (value) {
|
142 | return this.isOptional && this.type.equals(this.value(), value);
|
143 | };
|
144 | |
145 |
|
146 |
|
147 |
|
148 | Param.prototype.value = function (value) {
|
149 | var _this = this;
|
150 | |
151 |
|
152 |
|
153 | var getDefaultValue = function () {
|
154 | if (_this._defaultValueCache)
|
155 | return _this._defaultValueCache.defaultValue;
|
156 | if (!coreservices_1.services.$injector)
|
157 | throw new Error('Injectable functions cannot be called at configuration time');
|
158 | var defaultValue = coreservices_1.services.$injector.invoke(_this.config.$$fn);
|
159 | if (defaultValue !== null && defaultValue !== undefined && !_this.type.is(defaultValue))
|
160 | throw new Error("Default value (" + defaultValue + ") for parameter '" + _this.id + "' is not an instance of ParamType (" + _this.type.name + ")");
|
161 | if (_this.config.$$fn['__cacheable']) {
|
162 | _this._defaultValueCache = { defaultValue: defaultValue };
|
163 | }
|
164 | return defaultValue;
|
165 | };
|
166 | var replaceSpecialValues = function (val) {
|
167 | for (var _i = 0, _a = _this.replace; _i < _a.length; _i++) {
|
168 | var tuple = _a[_i];
|
169 | if (tuple.from === val)
|
170 | return tuple.to;
|
171 | }
|
172 | return val;
|
173 | };
|
174 | value = replaceSpecialValues(value);
|
175 | return predicates_1.isUndefined(value) ? getDefaultValue() : this.type.$normalize(value);
|
176 | };
|
177 | Param.prototype.isSearch = function () {
|
178 | return this.location === DefType.SEARCH;
|
179 | };
|
180 | Param.prototype.validates = function (value) {
|
181 |
|
182 | if ((predicates_1.isUndefined(value) || value === null) && this.isOptional)
|
183 | return true;
|
184 |
|
185 | var normalized = this.type.$normalize(value);
|
186 | if (!this.type.is(normalized))
|
187 | return false;
|
188 |
|
189 | var encoded = this.type.encode(normalized);
|
190 | return !(predicates_1.isString(encoded) && !this.type.pattern.exec(encoded));
|
191 | };
|
192 | Param.prototype.toString = function () {
|
193 | return "{Param:" + this.id + " " + this.type + " squash: '" + this.squash + "' optional: " + this.isOptional + "}";
|
194 | };
|
195 | return Param;
|
196 | }());
|
197 | exports.Param = Param;
|
198 |
|
\ | No newline at end of file |