| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright 2016 The LUCI Authors. All rights reserved. | 2 Copyright 2016 The LUCI Authors. All rights reserved. |
| 3 Use of this source code is governed under the Apache License, Version 2.0 | 3 Use of this source code is governed under the Apache License, Version 2.0 |
| 4 that can be found in the LICENSE file. | 4 that can be found in the LICENSE file. |
| 5 --> | 5 --> |
| 6 | 6 |
| 7 <link rel="import" href="../inc/logdog-app-base/logdog-app-base.html"> | 7 <link rel="import" href="../inc/logdog-app-base/logdog-app-base.html"> |
| 8 | 8 |
| 9 <script src="../inc/bower_components/page/page.js"></script> | 9 <script src="../inc/bower_components/page/page.js"></script> |
| 10 | 10 |
| 11 <script> | 11 <script> |
| 12 window.addEventListener('WebComponentsReady', function() { | 12 window.addEventListener('WebComponentsReady', function() { |
| 13 // We use Page.js for routing. This is a Micro | 13 // We use Page.js for routing. This is a Micro |
| 14 // client-side router inspired by the Express router | 14 // client-side router inspired by the Express router |
| 15 // More info: https://visionmedia.github.io/page.js/ | 15 // More info: https://visionmedia.github.io/page.js/ |
| 16 // Removes end / from app.baseUrl which page.base requires for production | 16 // Removes end / from app.baseUrl which page.base requires for production |
| 17 var baseUrl = app.baseUrl; | 17 page.base(app.baseUrl); |
| 18 page.base(baseUrl); | |
| 19 | 18 |
| 20 // Middleware | 19 // Middleware |
| 21 function scrollToTop(ctx, next) { | 20 function scrollToTop(ctx, next) { |
| 22 app.scrollPageToTop(); | 21 app.scrollPageToTop(); |
| 23 next(); | 22 next(); |
| 24 } | 23 } |
| 25 function closeDrawer(ctx, next) { | 24 function closeDrawer(ctx, next) { |
| 26 app.closeDrawer(); | 25 app.closeDrawer(); |
| 27 next(); | 26 next(); |
| 28 } | 27 } |
| 29 | 28 |
| 30 // Hack to make this work on root-URL apps (e.g., localhost testing). | |
| 31 if ( baseUrl === "/" ) { | |
| 32 var oldReplace = page.replace; | |
| 33 page.replace = function(path, state, init, dispatch) { | |
| 34 if ( path.substring(0, 1) !== "/" ) { | |
| 35 path = "/" + path; | |
| 36 } | |
| 37 if ( path.substring(0, 3) !== "/#!" ) { | |
| 38 path = "/#!" + path; | |
| 39 } | |
| 40 return oldReplace(path, state, init, dispatch); | |
| 41 }; | |
| 42 } | |
| 43 | |
| 44 // Routes | 29 // Routes |
| 45 page("*", scrollToTop, closeDrawer, function(ctx, next) { | 30 page('*', scrollToTop, closeDrawer, function(ctx, next) { |
| 46 next(); | 31 next(); |
| 47 }); | 32 }); |
| 48 | 33 page('/', function() { |
| 49 page("/", function() { | 34 app.route = 'root'; |
| 50 app.route = "root"; | |
| 51 }); | 35 }); |
| 52 | 36 page('/list/*', function(data) { |
| 53 page("/list/*", function(data) { | 37 app.route = 'list'; |
| 54 app.route = "list"; | |
| 55 app.$.list.base = logdog.correctStreamPath(data.params[0]); | 38 app.$.list.base = logdog.correctStreamPath(data.params[0]); |
| 56 }); | 39 }); |
| 57 | 40 page('/stream/*', function(data) { |
| 58 page("/stream/*", function(data) { | 41 app.route = 'stream'; |
| 59 app.route = "stream"; | |
| 60 app.$.stream.streams = logdog.getQueryValues(data.querystring, "s"). | 42 app.$.stream.streams = logdog.getQueryValues(data.querystring, "s"). |
| 61 map(logdog.correctStreamPath); | 43 map(logdog.correctStreamPath); |
| 62 }); | 44 }); |
| 63 | 45 |
| 64 // 404 | 46 // 404 |
| 65 page(function() { | 47 page(function() { |
| 66 app.$.toast.text = "Can't find: " + window.location.href + | 48 app.$.toast.text = "Can't find: " + window.location.href + |
| 67 ". Redirected you to Home Page"; | 49 ". Redirected you to Home Page"; |
| 68 app.$.toast.show(); | 50 app.$.toast.show(); |
| 69 page.redirect(app.baseUrl); | 51 page.redirect(app.baseUrl); |
| 70 }); | 52 }); |
| 71 | 53 // add #! before urls |
| 72 page({ | 54 page({ |
| 73 hashbang: true, | 55 hashbang: true, |
| 74 }); | 56 }); |
| 75 }); | 57 }); |
| 76 </script> | 58 </script> |
| OLD | NEW |