1 | import { IStaticResourceConfig } from 'aurelia-templating';
|
2 | import { Router } from 'aurelia-router';
|
3 | import { DOM } from 'aurelia-pal';
|
4 | import * as LogManager from 'aurelia-logging';
|
5 |
|
6 | const logger = LogManager.getLogger('route-href');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | export class RouteHref {
|
12 |
|
13 |
|
14 | static inject() {
|
15 | return [Router, DOM.Element];
|
16 | }
|
17 |
|
18 | |
19 |
|
20 |
|
21 | static $resource: IStaticResourceConfig = {
|
22 | type: 'attribute',
|
23 | name: 'route-href',
|
24 | bindables: [
|
25 | { name: 'route', changeHandler: 'processChange', primaryProperty: true },
|
26 | { name: 'params', changeHandler: 'processChange' },
|
27 | 'attribute'
|
28 | ] as any
|
29 | };
|
30 |
|
31 | |
32 |
|
33 |
|
34 | readonly router: Router;
|
35 |
|
36 | |
37 |
|
38 |
|
39 | readonly element: Element;
|
40 |
|
41 |
|
42 | isActive: boolean;
|
43 |
|
44 | |
45 |
|
46 |
|
47 | route: string;
|
48 |
|
49 | |
50 |
|
51 |
|
52 | params: Record<string, any>;
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 | attribute: string;
|
59 |
|
60 | constructor(
|
61 | router: Router,
|
62 | element: Element
|
63 | ) {
|
64 | this.router = router;
|
65 | this.element = element;
|
66 | this.attribute = 'href';
|
67 | }
|
68 |
|
69 | bind() {
|
70 | this.isActive = true;
|
71 | this.processChange();
|
72 | }
|
73 |
|
74 | unbind() {
|
75 | this.isActive = false;
|
76 | }
|
77 |
|
78 | attributeChanged(value: any, previous: any) {
|
79 | if (previous) {
|
80 | this.element.removeAttribute(previous);
|
81 | }
|
82 |
|
83 | return this.processChange();
|
84 | }
|
85 |
|
86 | processChange() {
|
87 | return this.router
|
88 | .ensureConfigured()
|
89 | .then((): null => {
|
90 | if (!this.isActive) {
|
91 |
|
92 | return null;
|
93 | }
|
94 | const element = this.element as Element & { au: any };
|
95 |
|
96 | const href = this.router.generate(this.route, this.params);
|
97 |
|
98 | if (element.au.controller) {
|
99 | element.au.controller.viewModel[this.attribute] = href;
|
100 | } else {
|
101 | element.setAttribute(this.attribute, href);
|
102 | }
|
103 |
|
104 |
|
105 | return null;
|
106 | })
|
107 | .catch((reason: any) => {
|
108 | logger.error(reason);
|
109 | });
|
110 | }
|
111 | }
|
112 |
|