forked from SeleniumHQ/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.js
221 lines (200 loc) · 6.22 KB
/
docs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
"use strict";
function $(loc) {
loc = String(loc);
var els = document.querySelectorAll(loc);
return els.length > 0 ? els : els[0];
}
window.addEventListener("load", addStructure);
window.addEventListener("load", addAnchors);
window.addEventListener("load", addToc);
window.addEventListener("load", paginate);
window.addEventListener("load", insertHeader);
window.addEventListener("load", insertFooter);
window.addEventListener("load", populateHeaderYs);
window.addEventListener("load", populateTocEls);
window.addEventListener("load", highlightCode);
window.addEventListener("scroll", trackHeaders);
window.addEventListener("scroll", moveToc);
function addStructure() {}
/**
* Function that parses through the current page, and adds
* anchors on the h1-h6 tags for TOC navigation
*/
function addAnchors() {
var whitelist = "abcdefghijklmnopqrstuvwxyz0123456789 ";
function sanitise(s) {
if (!s)
return;
s = s.trim().toLowerCase();
s = Array.prototype.filter.call(s, function(c) { return whitelist.indexOf(c) >= 0 }).join("");
return s.replace(/\s/g, "_")
}
var hs = $("h1, h2, h3, h4, h5, h6");
for (var i=0; i< hs.length; i++) {
hs[i].id = sanitise(hs[i].textContent);
}
}
/**
* Add the table of contents to the DOM.
*/
function addToc() {
var toc = document.createElement("nav");
toc.id = "toc";
toc.innerHTML = "<h1><a href=index.html>Table of Contents</a></h1>";
var hs = $("h1, h2, h3, h4, h5, h6");
for (var i=0; i< hs.length; i++) {
var level = hs[i].tagName.substring(1);
if (level == 1)
continue;
toc.innerHTML += "<a href=#" + hs[i].id + " class=level" + level + ">" + hs[i].textContent + "</a>"
}
document.body.insertBefore(toc, document.body.firstChild);
}
/**
* Retrieve the current page, without an extension
* @returns string "index" | "intro" | etc.
*/
function getCurrentPage() {
var paths = location.href.split('/');
return paths[paths.length-1].split('.')[0];
}
/**
* Adds a pagination nav to the page
*/
function paginate() {
var prev = document.querySelector("link[rel=prev]");
var next = document.querySelector("link[rel=next]");
var nav = document.createElement("nav");
nav.id = "pagination";
if (prev)
nav.innerHTML = "<a class=prev href=" + prev.href + ">" + prev.title + "</a>";
if (next)
nav.innerHTML += "<a class=next href=" + next.href + ">" + next.title + "</a>";
if (prev || next)
document.body.appendChild(nav);
}
/**
* Listener function that will put the header into the DOM as the first child in <body>
*/
function insertHeader() {
var header = document.createElement("header");
header.innerHTML = "<h1>Selenium Documentation</h1>";
if (getCurrentPage() != 'index' || window.location.pathname === '/docs' || window.location.pathname === '/docs/')
header.innerHTML += "<a id='home_link' href='index.html'>back to index</a>";
document.body.insertBefore(header, document.body.firstChild);
}
/**
* Listener function that will put the footer into the DOM as the last child in <body>
*/
// TODO(ato): Warning, this is unsafe.
function insertFooter() {
var footer = document.createElement("footer");
var path = window.location.pathname;
if (path === "/docs" || path === "/docs/")
path = "/docs/index.html";
var file = path.substring(path.lastIndexOf("/") + 1);
footer.innerHTML = "<div class=links>" +
" </div>" +
"<div class=meta>" +
" <p>© 2013-" + new Date().getFullYear() + ", <a href=attr.html>Software Freedom Conservancy</a>." +
" <p>Last updated " + document.lastModified +
" <p>" +
" <ul>" +
" <li><a href=//github.com/seleniumhq/docs/issues>Report a bug</a>" +
" <li><a href=//github.com/seleniumhq/docs/edit/gh-pages/" + file + ">Edit</a>" +
" </ul>" +
"</div>";
document.body.appendChild(footer);
}
var hs = [];
var headerYs = {};
var curHeader = null;
var tocEls = {};
/**
* Function that stores the `y` coords of each html header
*/
function populateHeaderYs() {
var hdrs = $("h1[id], h2[id], h3[id], h4[id], h5[id], h6[id]");
for (var i=0; i< hdrs.length; i++) {
hs.push(hdrs[i]);
headerYs[hdrs[i].offsetTop] = hdrs[i].id;
};
}
/**
* Function that populates the elements within the Table of Contents floaty
*/
function populateTocEls() {
var els = $("nav#toc > a");
for (var i=0; i< els.length; i++) {
var anchor = els[i].href.substring(els[i].href.indexOf("#"));
tocEls[anchor] = els[i];
}
}
/**
* Function that highlights pre code blocks using https://highlightjs.org/usage/
* It dynamically loads required css and js without changing all html files.
*/
function highlightCode() {
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = "github.css";
link.onload = function() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "highlight.pack.js";
script.onload = function() {
var blocks = $("pre code");
for (var i = 0; i < blocks.length; i++) {
hljs.highlightBlock(blocks[i]);
}
}
document.head.appendChild(script);
}
document.head.appendChild(link);
}
/**
* Listener function that observes the html headers, and will determine whether they have disappeared
* @param ev the scroll event
*/
function trackHeaders(ev) {
var pageY = ev.pageY;
var cur = hs[0].id;
var gone = Object.keys(headerYs).filter(function(y, h) { return y <= pageY });
if (gone.length > 0) {
var curY = gone[gone.length - 1];
cur = headerYs[curY];
}
updateToc(cur);
}
/**
* Update the table of contents with where the user is on the page
* @param id the id of the header element
*/
function updateToc(id) {
if (curHeader == id) {
return;
}
Object.keys(tocEls).map(function(id) { tocEls[id].classList.remove("current") });
var sel = "#" + id;
if (!(sel in tocEls))
return;
curHeader = tocEls[sel];
curHeader.classList.add("current");
}
/**
* Listener event that moves the table of contents with the user's viewport
* @param ev the scroll event
*/
function moveToc(ev) {
var toc = $("nav#toc")[0];
var firstEl = $("nav + h1")[0];
var firstElSt = firstEl.getBoundingClientRect();
var offset = document.body.classList.contains("front") ? 300 : 200;
var firstElRealTop = firstElSt.bottom - (firstElSt.height - offset);
if (firstElRealTop - 50 < 0) {
toc.style.top = "50px";
} else {
toc.style.top = firstElSt.y + offset + "px";
}
}