|
1 | | -class Router { |
2 | | - constructor(el, defaultRoute = "") { |
| 1 | +/* |
| 2 | + * Copyright 2017-present Palantir Technologies, Inc. All rights reserved. |
| 3 | + * Licensed under the BSD-3 License as modified (the “License”); you may obtain |
| 4 | + * a copy of the license in the LICENSE and PATENTS files in the root of this |
| 5 | + * repository. |
| 6 | + */ |
| 7 | +var Router = /** @class */ (function () { |
| 8 | + function Router(el, defaultRoute) { |
| 9 | + if (defaultRoute === void 0) { defaultRoute = ""; } |
3 | 10 | this.el = el; |
4 | | - this.routes = {}; |
5 | 11 | this.defaultRoute = defaultRoute; |
| 12 | + this.routes = {}; |
| 13 | + this.currentRoute = null; |
6 | 14 | } |
7 | | - |
8 | | - start() { |
9 | | - const routeHandler = () => this.route(); |
| 15 | + Router.prototype.start = function () { |
| 16 | + var _this = this; |
| 17 | + var routeHandler = function () { return _this.route(); }; |
10 | 18 | window.addEventListener("hashchange", routeHandler); |
11 | 19 | window.addEventListener("load", routeHandler); |
12 | 20 | this.route(); |
13 | | - } |
14 | | - |
15 | | - register(route) { |
| 21 | + }; |
| 22 | + Router.prototype.register = function (route) { |
16 | 23 | this.routes[route.route] = route; |
17 | | - } |
18 | | - |
19 | | - route() { |
20 | | - const hashRoute = location.hash.slice(1) || this.defaultRoute; |
21 | | - const route = this.routes[hashRoute]; |
22 | | - |
| 24 | + }; |
| 25 | + Router.prototype.route = function () { |
| 26 | + var hashRoute = location.hash.slice(1) || this.defaultRoute; |
| 27 | + var route = this.routes[hashRoute]; |
23 | 28 | if (this.el && route && route !== this.currentRoute) { |
24 | 29 | this.currentRoute = route; |
25 | 30 | this.el.innerHTML = route.render(); |
26 | 31 | selectCurrent(route.route); |
27 | | - } else { |
| 32 | + } |
| 33 | + else { |
28 | 34 | this.currentRoute = null; |
29 | 35 | } |
30 | | - } |
| 36 | + }; |
| 37 | + return Router; |
| 38 | +}()); |
| 39 | +function queryAll(element, selector) { |
| 40 | + return Array.from(element.querySelectorAll(selector)); |
31 | 41 | } |
32 | | - |
33 | | -const nav = document.querySelector("#nav"); |
| 42 | +var nav = document.querySelector("#nav"); |
34 | 43 | function selectCurrent(route) { |
35 | 44 | try { |
36 | | - nav.querySelectorAll("a").forEach((a) => a.classList.toggle("selected", false)); |
37 | | - nav.querySelectorAll('a[href="#' + route + '"]').forEach((a) => a.classList.toggle("selected", true)); |
38 | | - } catch (err) { |
| 45 | + queryAll(nav, "a").forEach(function (a) { return a.classList.toggle("selected", false); }); |
| 46 | + queryAll(nav, 'a[href="#' + route + '"]').forEach(function (a) { return a.classList.toggle("selected", true); }); |
| 47 | + } |
| 48 | + catch (err) { |
39 | 49 | // just bail if this doesn't work (IE) |
40 | 50 | } |
41 | 51 | } |
42 | | - |
43 | | -const router = new Router(document.querySelector("#content"), "docs"); |
44 | | -const routables = document.querySelectorAll("[data-route]"); |
45 | | -routables.forEach((routable) => { |
46 | | - const route = routable.getAttribute("data-route"); |
| 52 | +var router = new Router(document.querySelector("#content"), "docs"); |
| 53 | +var routables = queryAll(document.body, "[data-route]"); |
| 54 | +routables.forEach(function (routable) { |
| 55 | + var route = routable.getAttribute("data-route"); |
47 | 56 | router.register({ |
48 | | - route, |
49 | | - render: () => routable.innerHTML, |
| 57 | + render: function () { return routable.innerHTML; }, |
| 58 | + route: route, |
50 | 59 | }); |
51 | 60 | }); |
52 | 61 | router.start(); |
0 commit comments