aboutsummaryrefslogtreecommitdiff
path: root/catapult/third_party/polymer/components/app-route/demo/index.html
blob: 4a80ad08accdd714242f49ccc4a8bd3bc3d2ea1d (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<!DOCTYPE html>
<!--
@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
-->
<html>
<head>
<script src="../../webcomponentsjs/webcomponents-lite.js"></script>

<title>app-route Demo</title>
<link rel="import" href="../../polymer/polymer.html">

<link rel="import" href="../../iron-demo-helpers/url-bar.html">
<link rel="import" href="../../iron-pages/iron-pages.html">

<link rel="import" href="../app-location.html">
<link rel="import" href="../app-route.html">

<link rel="import" href="youtube-demo/youtube-search.html">
<link rel="import" href="youtube-demo/youtube-toolbar.html">
<link rel="import" href="youtube-demo/video-viewer.html">
<link rel="import" href="youtube-demo/search-results.html">

<style>
  body {
    margin: 0;
    padding: 0;
  }
  url-bar {
    background-color: white;
  }
</style>
</head>

<body>

<url-bar></url-bar>

<app-router-demo></app-router-demo>

<dom-module id="app-router-demo">
  <template>
    <style>
      :host {
        display: block;
        position: relative;
        height: 100vh;
        @apply(--paper-font-common-base);
      }

      :host([video-page-active]) {
        overflow: hidden;
      }

      :host([video-page-active]) iron-pages {
        transform: translateY(-170px);
      }

      iron-pages {
        transition: transform 0.3s;
      }
    </style>


    <!-- app-location binds with the URL and produces a route for
    app-route elements to consume. Since this demo needs to run without
    server cooperation (e.g. with polyserve, in the elements catalog, etc) we'll
    use the hash portion of the URL for our route paths. -->
    <app-location route="{{route}}" use-hash-as-path></app-location>

    <!-- app-routes parse route paths based on the their `pattern`.
    Parameters are extracted into the `data` object. The rest of the path that
    comes after the `pattern` is put into the `tail` object, which can be
    passed to the `route` property of downstream app-routes. -->
    <app-route route="{{route}}" pattern="/:page" data="{{data}}"></app-route>
    <app-route route="{{route}}" pattern="/search" tail="{{searchTail}}"></app-route>
    <app-route route="{{route}}" pattern="/video" tail="{{videoTail}}" active="{{videoPageActive}}"></app-route>

    <youtube-toolbar collapsed$="{{videoPageActive}}">
      <!-- The youtube-search has a app-route that consumes the tail of
      another route (`searchTail`) -->
      <youtube-search
          route="{{searchTail}}"
          video-data="{{videoData}}">
      </youtube-search>
    </youtube-toolbar>

    <iron-pages attr-for-selected="id" selected="{{data.page}}">
      <search-results id="search" items="{{videos}}"></search-results>

      <!-- The video-viewer has a app-route that consumes the tail of
      another route -->
      <video-viewer id="video" route="{{videoTail}}"></video-viewer>
    </iron-pages>

  </template>

  <script>
    window.addEventListener('WebComponentsReady', function() {
      Polymer({
        is: 'app-router-demo',

        properties: {
          route: {
            type: Object
          },

          videoData: {
            type: Object,
            observer: '_videoDataChanged'
          },

          videoPageActive: {
            type: Boolean,
            reflectToAttribute: true,
            observer: '_videoPageActiveChanged'
          },

          searchTail: {
            type: Object,
            notify: true
          },

          videoTail: {
            type: Object,
            notify: true
          },

          newCategory: {
            type: String
          },

          videos: {
            type: Array,
            value: function() {
              return [];
            }
          },

          data: {
            type: Object,
            value: function() {
              return {
                page: '/search/'
              };
            }
          }
        },

        observers: [
          '_onRoutePathChanged(route.path)'
        ],

        _onRoutePathChanged: function(path) {
          // If we do not have an initial URL, we redirect to /search
          if (!path) {
            this.set('route.path', '/search/');
          }
        },

        _videoDataChanged: function(data) {
          var allVideos = [];

          var that = this;

          data.items.forEach(function (videoItem) {
            var youtubeVideo = {
              id: videoItem.id.videoId,
              title: videoItem.snippet.title,
              thumbnail: videoItem.snippet.thumbnails.high.url
            };

            allVideos.push(youtubeVideo);
          });

          this.set('videos', allVideos);
        },

        _videoPageActiveChanged: function(videoPageActive, previousValue) {
          // change color of page on page change
          var newColor;

          if (videoPageActive) {
            // black
            newColor = 0;
          } else {
            // white
            newColor = 255;
          }

          document.body.style.backgroundColor = 'rgb(' + newColor + ',' + newColor
              + ','  + newColor + ')';

          // on first load, set the color then allow color transition animations
          if (previousValue === undefined) {
            document.body.style.transition = 'background-color .2s linear';
            return;
          }
        }
      });
    });
  </script>
</dom-module>
</body>
</html>