-
Notifications
You must be signed in to change notification settings - Fork 66
/
app-route-converter-behavior.js
105 lines (96 loc) · 2.8 KB
/
app-route-converter-behavior.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
@license
Copyright (c) 2016 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at
http://polymer.github.io/LICENSE.txt The complete set of authors may be found at
http://polymer.github.io/AUTHORS.txt The complete set of contributors may be
found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as
part of the polymer project is also subject to an additional IP rights grant
found at http://polymer.github.io/PATENTS.txt
*/
import '@polymer/polymer/polymer-legacy.js';
/**
* Provides bidirectional mapping between `path` and `queryParams` and a
* app-route compatible `route` object.
*
* For more information, see the docs for `app-route-converter`.
*
* @polymerBehavior
*/
export const AppRouteConverterBehavior = {
properties: {
/**
* A model representing the deserialized path through the route tree, as
* well as the current queryParams.
*
* A route object is the kernel of the routing system. It is intended to
* be fed into consuming elements such as `app-route`.
*
* @type {?Object|undefined}
*/
route: {
type: Object,
notify: true,
},
/**
* A set of key/value pairs that are universally accessible to branches of
* the route tree.
*
* @type {?Object}
*/
queryParams: {
type: Object,
notify: true,
},
/**
* The serialized path through the route tree. This corresponds to the
* `window.location.pathname` value, and will update to reflect changes
* to that value.
*/
path: {
type: String,
notify: true,
}
},
observers: [
'_locationChanged(path, queryParams)',
'_routeChanged(route.prefix, route.path)',
'_routeQueryParamsChanged(route.__queryParams)'
],
/** @override */
created: function() {
this.linkPaths('route.__queryParams', 'queryParams');
this.linkPaths('queryParams', 'route.__queryParams');
},
/**
* Handler called when the path or queryParams change.
*/
_locationChanged: function() {
if (this.route && this.route.path === this.path &&
this.queryParams === this.route.__queryParams) {
return;
}
this.route = {prefix: '', path: this.path, __queryParams: this.queryParams};
},
/**
* Handler called when the route prefix and route path change.
*/
_routeChanged: function() {
if (!this.route) {
return;
}
this.path = this.route.prefix + this.route.path;
},
/**
* Handler called when the route queryParams change.
*
* @param {Object} queryParams A set of key/value pairs that are
* universally accessible to branches of the route tree.
*/
_routeQueryParamsChanged: function(queryParams) {
if (!this.route) {
return;
}
this.queryParams = queryParams;
}
};