1<!DOCTYPE html> 2<!-- 3@license 4Copyright (c) 2016 The Polymer Project Authors. All rights reserved. 5This code may only be used under the BSD style license found at 6http://polymer.github.io/LICENSE.txt 7The complete set of authors may be found at 8http://polymer.github.io/AUTHORS.txt 9The complete set of contributors may be found at 10http://polymer.github.io/CONTRIBUTORS.txt 11Code distributed by Google as part of the polymer project is also 12subject to an additional IP rights grant found at 13http://polymer.github.io/PATENTS.txt 14--> 15<html> 16<head> 17<script src="../../webcomponentsjs/webcomponents-lite.js"></script> 18 19<title>app-route Demo</title> 20<link rel="import" href="../../polymer/polymer.html"> 21 22<link rel="import" href="../../iron-demo-helpers/url-bar.html"> 23<link rel="import" href="../../iron-pages/iron-pages.html"> 24 25<link rel="import" href="../app-location.html"> 26<link rel="import" href="../app-route.html"> 27 28<link rel="import" href="youtube-demo/youtube-search.html"> 29<link rel="import" href="youtube-demo/youtube-toolbar.html"> 30<link rel="import" href="youtube-demo/video-viewer.html"> 31<link rel="import" href="youtube-demo/search-results.html"> 32 33<style> 34 body { 35 margin: 0; 36 padding: 0; 37 } 38 url-bar { 39 background-color: white; 40 } 41</style> 42</head> 43 44<body> 45 46<url-bar></url-bar> 47 48<app-router-demo></app-router-demo> 49 50<dom-module id="app-router-demo"> 51 <template> 52 <style> 53 :host { 54 display: block; 55 position: relative; 56 height: 100vh; 57 @apply(--paper-font-common-base); 58 } 59 60 :host([video-page-active]) { 61 overflow: hidden; 62 } 63 64 :host([video-page-active]) iron-pages { 65 transform: translateY(-170px); 66 } 67 68 iron-pages { 69 transition: transform 0.3s; 70 } 71 </style> 72 73 74 <!-- app-location binds with the URL and produces a route for 75 app-route elements to consume. Since this demo needs to run without 76 server cooperation (e.g. with polyserve, in the elements catalog, etc) we'll 77 use the hash portion of the URL for our route paths. --> 78 <app-location route="{{route}}" use-hash-as-path></app-location> 79 80 <!-- app-routes parse route paths based on the their `pattern`. 81 Parameters are extracted into the `data` object. The rest of the path that 82 comes after the `pattern` is put into the `tail` object, which can be 83 passed to the `route` property of downstream app-routes. --> 84 <app-route route="{{route}}" pattern="/:page" data="{{data}}"></app-route> 85 <app-route route="{{route}}" pattern="/search" tail="{{searchTail}}"></app-route> 86 <app-route route="{{route}}" pattern="/video" tail="{{videoTail}}" active="{{videoPageActive}}"></app-route> 87 88 <youtube-toolbar collapsed$="{{videoPageActive}}"> 89 <!-- The youtube-search has a app-route that consumes the tail of 90 another route (`searchTail`) --> 91 <youtube-search 92 route="{{searchTail}}" 93 video-data="{{videoData}}"> 94 </youtube-search> 95 </youtube-toolbar> 96 97 <iron-pages attr-for-selected="id" selected="{{data.page}}"> 98 <search-results id="search" items="{{videos}}"></search-results> 99 100 <!-- The video-viewer has a app-route that consumes the tail of 101 another route --> 102 <video-viewer id="video" route="{{videoTail}}"></video-viewer> 103 </iron-pages> 104 105 </template> 106 107 <script> 108 window.addEventListener('WebComponentsReady', function() { 109 Polymer({ 110 is: 'app-router-demo', 111 112 properties: { 113 route: { 114 type: Object 115 }, 116 117 videoData: { 118 type: Object, 119 observer: '_videoDataChanged' 120 }, 121 122 videoPageActive: { 123 type: Boolean, 124 reflectToAttribute: true, 125 observer: '_videoPageActiveChanged' 126 }, 127 128 searchTail: { 129 type: Object, 130 notify: true 131 }, 132 133 videoTail: { 134 type: Object, 135 notify: true 136 }, 137 138 newCategory: { 139 type: String 140 }, 141 142 videos: { 143 type: Array, 144 value: function() { 145 return []; 146 } 147 }, 148 149 data: { 150 type: Object, 151 value: function() { 152 return { 153 page: '/search/' 154 }; 155 } 156 } 157 }, 158 159 observers: [ 160 '_onRoutePathChanged(route.path)' 161 ], 162 163 _onRoutePathChanged: function(path) { 164 // If we do not have an initial URL, we redirect to /search 165 if (!path) { 166 this.set('route.path', '/search/'); 167 } 168 }, 169 170 _videoDataChanged: function(data) { 171 var allVideos = []; 172 173 var that = this; 174 175 data.items.forEach(function (videoItem) { 176 var youtubeVideo = { 177 id: videoItem.id.videoId, 178 title: videoItem.snippet.title, 179 thumbnail: videoItem.snippet.thumbnails.high.url 180 }; 181 182 allVideos.push(youtubeVideo); 183 }); 184 185 this.set('videos', allVideos); 186 }, 187 188 _videoPageActiveChanged: function(videoPageActive, previousValue) { 189 // change color of page on page change 190 var newColor; 191 192 if (videoPageActive) { 193 // black 194 newColor = 0; 195 } else { 196 // white 197 newColor = 255; 198 } 199 200 document.body.style.backgroundColor = 'rgb(' + newColor + ',' + newColor 201 + ',' + newColor + ')'; 202 203 // on first load, set the color then allow color transition animations 204 if (previousValue === undefined) { 205 document.body.style.transition = 'background-color .2s linear'; 206 return; 207 } 208 } 209 }); 210 }); 211 </script> 212</dom-module> 213</body> 214</html> 215