Files
polymer-starter-kit/app/elements/routing.html

84 lines
2.2 KiB
HTML
Raw Normal View History

2015-05-25 22:10:27 -07:00
<!--
@license
Copyright (c) 2015 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
-->
<script src="../bower_components/page/page.js"></script>
<script>
window.addEventListener('WebComponentsReady', function() {
// We use Page.js for routing. This is a Micro
// client-side router inspired by the Express router
// More info: https://visionmedia.github.io/page.js/
2015-09-17 12:29:46 -07:00
// Removes end / from app.baseUrl which page.base requires for production
if (window.location.port === '') { // if production
page.base(app.baseUrl.replace(/\/$/, ''));
}
2015-09-17 12:29:46 -07:00
// Middleware
function scrollToTop(ctx, next) {
app.scrollPageToTop();
2015-09-17 12:29:46 -07:00
next();
}
2015-11-20 15:25:05 -08:00
function closeDrawer(ctx, next) {
app.closeDrawer();
next();
}
function setFocus(selected){
document.querySelector('section[data-route="' + selected + '"] .page-title').focus();
}
2015-11-20 15:25:05 -08:00
2015-09-17 12:29:46 -07:00
// Routes
2015-11-20 15:25:05 -08:00
page('*', scrollToTop, closeDrawer, function(ctx, next) {
next();
});
page('/', function() {
2015-09-17 12:29:46 -07:00
app.route = 'home';
setFocus(app.route);
});
2015-11-20 15:25:05 -08:00
page(app.baseUrl, function() {
app.route = 'home';
setFocus(app.route);
});
2015-11-20 15:25:05 -08:00
page('/users', function() {
app.route = 'users';
setFocus(app.route);
});
2015-11-20 15:25:05 -08:00
page('/users/:name', function(data) {
app.route = 'user-info';
app.params = data.params;
setFocus(app.route);
});
2015-11-20 15:25:05 -08:00
page('/contact', function() {
app.route = 'contact';
setFocus(app.route);
});
2015-11-20 15:25:05 -08:00
// 404
page('*', function() {
app.$.toast.text = 'Can\'t find: ' + window.location.href + '. Redirected you to Home Page';
app.$.toast.show();
page.redirect(app.baseUrl);
});
// add #! before urls
page({
hashbang: true
});
});
</script>