@microsoft/fast-router 1.0.0-alpha.3 → 1.0.0-alpha.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,12 @@
1
1
  import { __awaiter } from "tslib";
2
2
  import { isNavigationPhaseContributor } from "./contributors.js";
3
3
  class NavigationPhaseImpl {
4
+ get route() {
5
+ return this.routes[this.routes.length - 1];
6
+ }
7
+ get router() {
8
+ return this.routers[this.routers.length - 1];
9
+ }
4
10
  constructor(name, route, router, commitActions, cancelActions) {
5
11
  this.name = name;
6
12
  this.commitActions = commitActions;
@@ -12,12 +18,6 @@ class NavigationPhaseImpl {
12
18
  this.routes.push(route);
13
19
  this.routers.push(router);
14
20
  }
15
- get route() {
16
- return this.routes[this.routes.length - 1];
17
- }
18
- get router() {
19
- return this.routers[this.routers.length - 1];
20
- }
21
21
  cancel(callback) {
22
22
  this.canceled = true;
23
23
  if (callback) {
@@ -1,5 +1,5 @@
1
1
  const encode = encodeURIComponent;
2
- const encodeKey = (key) => encode(key).replace("%24", "$");
2
+ const encodeKey = (key) => encode(key).replace(/%24/g, "$");
3
3
  function buildParam(key, value, traditional) {
4
4
  let result = [];
5
5
  if (value === null || value === undefined) {
@@ -51,14 +51,15 @@ function parseComplexParam(queryParams, keys, value) {
51
51
  if (j < keysLastIndex) {
52
52
  // The value has to be an array or a false value
53
53
  // It can happen that the value is no array if the key was repeated with traditional style like `list=1&list[]=2`
54
- const prevValue = !currentParams[key] || typeof currentParams[key] === "object"
55
- ? currentParams[key]
56
- : [currentParams[key]];
57
- currentParams = currentParams[key] =
58
- prevValue || (isNaN(keys[j + 1]) ? {} : []);
54
+ const prevValue = !currentParams.get(key) || typeof currentParams.get(key) === "object"
55
+ ? currentParams.get(key)
56
+ : [currentParams.get(key)];
57
+ currentParams.set(key, prevValue || (isNaN(keys[j + 1]) ? {} : []));
58
+ currentParams = currentParams.get(key);
59
59
  }
60
60
  else {
61
- currentParams = currentParams[key] = value;
61
+ currentParams.set(key, value);
62
+ currentParams = currentParams.get(key);
62
63
  }
63
64
  }
64
65
  }
@@ -1,6 +1,8 @@
1
1
  import { __awaiter } from "tslib";
2
2
  import { QueryString } from "./query-string.js";
3
- const defaultParameterConverter = (value) => value;
3
+ const defaultParameterConverter = (name, value, context) => {
4
+ return value;
5
+ };
4
6
  /**
5
7
  * @beta
6
8
  */
@@ -35,7 +37,7 @@ export class RecognizedRoute {
35
37
  this.typedParams = typedParams;
36
38
  this.queryParams = queryParams;
37
39
  this.allParams = Object.assign(Object.assign({}, params), queryParams);
38
- this.allTypedParams = Object.assign(Object.assign({}, typedParams), queryParams);
40
+ this.allTypedParams = Object.assign(Object.assign({}, queryParams), typedParams);
39
41
  }
40
42
  get settings() {
41
43
  return this.endpoint.settings;
@@ -247,13 +249,13 @@ function compareChains(a, b) {
247
249
  return a.compareTo(b);
248
250
  }
249
251
  class RecognizeResult {
252
+ get isEmpty() {
253
+ return this.candidates.length === 0;
254
+ }
250
255
  constructor(rootState) {
251
256
  this.candidates = [];
252
257
  this.candidates = [new Candidate([""], [rootState], [], this)];
253
258
  }
254
- get isEmpty() {
255
- return this.candidates.length === 0;
256
- }
257
259
  getSolution() {
258
260
  const candidates = this.candidates.filter(hasEndpoint);
259
261
  if (candidates.length === 0) {
@@ -386,12 +388,17 @@ export class DefaultRouteRecognizer {
386
388
  const paramTypes = endpoint.paramTypes;
387
389
  const params = candidate.getParams();
388
390
  const typedParams = {};
391
+ const converterContext = {
392
+ endpoint,
393
+ params,
394
+ typedParams,
395
+ queryParams,
396
+ };
389
397
  for (let i = 0, ii = paramNames.length; i < ii; ++i) {
390
- const name = paramNames[i];
398
+ const paramName = paramNames[i];
399
+ const paramValue = params[paramName];
391
400
  const convert = converters[paramTypes[i]] || defaultParameterConverter;
392
- const untypedValue = params[name];
393
- const typedValue = yield convert(untypedValue);
394
- typedParams[name] = typedValue;
401
+ typedParams[paramName] = yield convert(paramName, paramValue, converterContext);
395
402
  }
396
403
  return new RecognizedRoute(endpoint, params, typedParams, queryParams);
397
404
  });
@@ -532,13 +539,13 @@ function isNotEmpty(segment) {
532
539
  return segment.length > 0;
533
540
  }
534
541
  class StaticSegment {
542
+ get kind() {
543
+ return 3 /* SegmentKind.static */;
544
+ }
535
545
  constructor(value, caseSensitive) {
536
546
  this.value = value;
537
547
  this.caseSensitive = caseSensitive;
538
548
  }
539
- get kind() {
540
- return 3 /* SegmentKind.static */;
541
- }
542
549
  appendTo(state) {
543
550
  const { value, value: { length }, } = this;
544
551
  if (this.caseSensitive) {
@@ -566,13 +573,13 @@ class StaticSegment {
566
573
  }
567
574
  }
568
575
  class DynamicSegment {
576
+ get kind() {
577
+ return 2 /* SegmentKind.dynamic */;
578
+ }
569
579
  constructor(name, optional) {
570
580
  this.name = name;
571
581
  this.optional = optional;
572
582
  }
573
- get kind() {
574
- return 2 /* SegmentKind.dynamic */;
575
- }
576
583
  appendTo(state) {
577
584
  state = state.append(/* segment */ this, /* value */ "/");
578
585
  return state;
@@ -588,12 +595,12 @@ class DynamicSegment {
588
595
  }
589
596
  }
590
597
  class StarSegment {
591
- constructor(name) {
592
- this.name = name;
593
- }
594
598
  get kind() {
595
599
  return 1 /* SegmentKind.star */;
596
600
  }
601
+ constructor(name) {
602
+ this.name = name;
603
+ }
597
604
  appendTo(state) {
598
605
  state = state.append(/* segment */ this, /* value */ "");
599
606
  return state;
@@ -1,9 +1,8 @@
1
1
  import { __awaiter } from "tslib";
2
2
  import { FASTElement } from "@microsoft/fast-element";
3
- import { composedParent } from "@microsoft/fast-element/utilities";
3
+ import { composedParent } from "@microsoft/fast-element/utilities.js";
4
4
  import { NavigationMessage } from "./navigation.js";
5
5
  import { childRouteParameter } from "./routes.js";
6
- import { RouterExecutionContext } from "./view.js";
7
6
  const routerProperty = "$router";
8
7
  function findParentRouterForElement(element) {
9
8
  let parent = element;
@@ -32,13 +31,20 @@ export const Router = Object.freeze({
32
31
  class RouterBase extends BaseType {
33
32
  constructor() {
34
33
  super();
35
- Router.getOrCreateFor(this);
36
- }
37
- get config() {
38
- return this[routerProperty].config;
39
- }
40
- set config(value) {
41
- this[routerProperty].config = value;
34
+ const router = Router.getOrCreateFor(this);
35
+ const config = this.config || null;
36
+ delete this.config;
37
+ Reflect.defineProperty(this, "config", {
38
+ get() {
39
+ return router.config;
40
+ },
41
+ set(value) {
42
+ router.config = value;
43
+ },
44
+ });
45
+ if (config !== null) {
46
+ router.config = config;
47
+ }
42
48
  }
43
49
  }
44
50
  const proto = RouterBase.prototype;
@@ -114,7 +120,7 @@ export class DefaultRouter {
114
120
  }
115
121
  this.parentRouter = findParentRouterForElement(this.host);
116
122
  }
117
- return this.parentRouter || null;
123
+ return this.parentRouter;
118
124
  }
119
125
  get level() {
120
126
  if (this.parent === null) {
@@ -137,7 +143,8 @@ export class DefaultRouter {
137
143
  return __awaiter(this, void 0, void 0, function* () {
138
144
  this.newRoute = route;
139
145
  this.newView = yield command.createView();
140
- this.newView.bind(route.allTypedParams, RouterExecutionContext.create(this));
146
+ this.newView.context.router = this;
147
+ this.newView.bind(route.allTypedParams);
141
148
  this.newView.appendTo(this.host);
142
149
  yield command.transition.begin(this.host, this.view, this.newView);
143
150
  return {
@@ -17,7 +17,7 @@ function getFallbackCommand(config, definition) {
17
17
  return Render.fromDefinition(config, definition);
18
18
  }
19
19
  }
20
- const booleanConverter = value => {
20
+ const booleanConverter = (name, value) => {
21
21
  if (value === void 0 || value === null) {
22
22
  return false;
23
23
  }
@@ -31,11 +31,11 @@ const booleanConverter = value => {
31
31
  }
32
32
  };
33
33
  const defaultConverters = {
34
- number: value => (value === void 0 ? NaN : parseFloat(value)),
35
- float: value => (value === void 0 ? NaN : parseFloat(value)),
36
- int: value => (value === void 0 ? NaN : parseInt(value)),
37
- integer: value => (value === void 0 ? NaN : parseInt(value)),
38
- Date: value => (value === void 0 ? new Date(Date.now()) : new Date(value)),
34
+ number: (name, value) => (value === void 0 ? NaN : parseFloat(value)),
35
+ float: (name, value) => (value === void 0 ? NaN : parseFloat(value)),
36
+ int: (name, value) => (value === void 0 ? NaN : parseInt(value)),
37
+ integer: (name, value) => (value === void 0 ? NaN : parseInt(value)),
38
+ Date: (name, value) => value === void 0 ? new Date(Date.now()) : new Date(value),
39
39
  boolean: booleanConverter,
40
40
  bool: booleanConverter,
41
41
  };
@@ -129,9 +129,9 @@ export class RouteCollection {
129
129
  normalizedConverter = converter.convert.bind(converter);
130
130
  }
131
131
  else if (converter.prototype && "convert" in converter.prototype) {
132
- normalizedConverter = (value) => {
132
+ normalizedConverter = (name, value, context) => {
133
133
  const obj = this.owner.construct(converter);
134
- return obj.convert(value);
134
+ return obj.convert(name, value, context);
135
135
  };
136
136
  }
137
137
  else {
package/dist/esm/view.js CHANGED
@@ -1,18 +1,6 @@
1
1
  import { __awaiter } from "tslib";
2
- import { ElementStyles, ExecutionContext, html, } from "@microsoft/fast-element";
2
+ import { ElementStyles, html, } from "@microsoft/fast-element";
3
3
  import { isFASTElementHost } from "./router.js";
4
- /**
5
- * @beta
6
- */
7
- export const RouterExecutionContext = Object.freeze({
8
- create(router) {
9
- return Object.create(ExecutionContext.default, {
10
- router: {
11
- value: router,
12
- },
13
- });
14
- },
15
- });
16
4
  /**
17
5
  * @beta
18
6
  */
@@ -33,17 +21,11 @@ export const Transition = Object.freeze({
33
21
  * @beta
34
22
  */
35
23
  export class FASTElementLayout {
36
- constructor(template = null, styles = null, runBeforeCommit = true) {
24
+ constructor(template = null, styles = undefined, runBeforeCommit = true) {
25
+ var _a;
37
26
  this.template = template;
38
27
  this.runBeforeCommit = runBeforeCommit;
39
- this.styles =
40
- styles === void 0 || styles === null
41
- ? null
42
- : Array.isArray(styles)
43
- ? new ElementStyles(styles)
44
- : styles instanceof ElementStyles
45
- ? styles
46
- : new ElementStyles([styles]);
28
+ this.styles = (_a = ElementStyles.normalize(styles)) !== null && _a !== void 0 ? _a : null;
47
29
  }
48
30
  beforeCommit(routerElement) {
49
31
  return __awaiter(this, void 0, void 0, function* () {
@@ -64,8 +46,8 @@ export class FASTElementLayout {
64
46
  if (routerElement.$fastController.template !== this.template) {
65
47
  routerElement.$fastController.template = this.template;
66
48
  }
67
- if (routerElement.$fastController.styles !== this.styles) {
68
- routerElement.$fastController.styles = this.styles;
49
+ if (routerElement.$fastController.mainStyles !== this.styles) {
50
+ routerElement.$fastController.mainStyles = this.styles;
69
51
  }
70
52
  }
71
53
  }