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