Skip to content

Commit ee24336

Browse files
authored
IE11 support (#64)
* write docs script in TS to compile to ES5 * target ES5 for source code * update docs site * add core-js shim to docs site
1 parent 5e180ff commit ee24336

File tree

8 files changed

+168
-81
lines changed

8 files changed

+168
-81
lines changed

docs/assets/index.js

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,61 @@
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 = ""; }
310
this.el = el;
4-
this.routes = {};
511
this.defaultRoute = defaultRoute;
12+
this.routes = {};
13+
this.currentRoute = null;
614
}
7-
8-
start() {
9-
const routeHandler = () => this.route();
15+
Router.prototype.start = function () {
16+
var _this = this;
17+
var routeHandler = function () { return _this.route(); };
1018
window.addEventListener("hashchange", routeHandler);
1119
window.addEventListener("load", routeHandler);
1220
this.route();
13-
}
14-
15-
register(route) {
21+
};
22+
Router.prototype.register = function (route) {
1623
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];
2328
if (this.el && route && route !== this.currentRoute) {
2429
this.currentRoute = route;
2530
this.el.innerHTML = route.render();
2631
selectCurrent(route.route);
27-
} else {
32+
}
33+
else {
2834
this.currentRoute = null;
2935
}
30-
}
36+
};
37+
return Router;
38+
}());
39+
function queryAll(element, selector) {
40+
return Array.from(element.querySelectorAll(selector));
3141
}
32-
33-
const nav = document.querySelector("#nav");
42+
var nav = document.querySelector("#nav");
3443
function selectCurrent(route) {
3544
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) {
3949
// just bail if this doesn't work (IE)
4050
}
4151
}
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");
4756
router.register({
48-
route,
49-
render: () => routable.innerHTML,
57+
render: function () { return routable.innerHTML; },
58+
route: route,
5059
});
5160
});
5261
router.start();

docs/assets/index.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
8+
interface IRoute {
9+
route: string;
10+
render: () => string;
11+
}
12+
13+
class Router {
14+
private routes: Record<string, IRoute> = {};
15+
private currentRoute: IRoute | null = null;
16+
17+
constructor(public el: HTMLElement, private defaultRoute = "") {}
18+
19+
public start() {
20+
const routeHandler = () => this.route();
21+
window.addEventListener("hashchange", routeHandler);
22+
window.addEventListener("load", routeHandler);
23+
this.route();
24+
}
25+
26+
public register(route: IRoute) {
27+
this.routes[route.route] = route;
28+
}
29+
30+
public route() {
31+
const hashRoute = location.hash.slice(1) || this.defaultRoute;
32+
const route = this.routes[hashRoute];
33+
34+
if (this.el && route && route !== this.currentRoute) {
35+
this.currentRoute = route;
36+
this.el.innerHTML = route.render();
37+
selectCurrent(route.route);
38+
} else {
39+
this.currentRoute = null;
40+
}
41+
}
42+
}
43+
44+
function queryAll(element: Element, selector: string) {
45+
return Array.from(element.querySelectorAll<HTMLElement>(selector));
46+
}
47+
48+
const nav = document.querySelector("#nav")!;
49+
function selectCurrent(route: string) {
50+
try {
51+
queryAll(nav, "a").forEach(a => a.classList.toggle("selected", false));
52+
queryAll(nav, 'a[href="#' + route + '"]').forEach(a => a.classList.toggle("selected", true));
53+
} catch (err) {
54+
// just bail if this doesn't work (IE)
55+
}
56+
}
57+
58+
const router = new Router(document.querySelector<HTMLElement>("#content")!, "docs");
59+
const routables = queryAll(document.body, "[data-route]");
60+
routables.forEach(routable => {
61+
const route = routable.getAttribute("data-route")!;
62+
router.register({
63+
render: () => routable.innerHTML,
64+
route,
65+
});
66+
});
67+
router.start();

0 commit comments

Comments
 (0)