forked from SeleniumHQ/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs.js
306 lines (278 loc) · 9.08 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use strict";
function $(loc) {
loc = String(loc);
var els = document.querySelectorAll(loc);
return els.length > 0 ? els : els[0];
}
var languages = [
{ pretty: "Java", safe: "java", comment: "//"},
{ pretty: "Python", safe: "python", comment: "#"},
{ pretty: "C#", safe: "cs", comment: "//"},
{ pretty: "Ruby", safe: "ruby", comment: "#"},
{ pretty: "JavaScript", safe: "javascript", comment: "//"}
];
function chosenLanguage() {
return localStorage.language ? localStorage.language : 'Java';
}
function showOnly(language, target) {
localStorage.language = language;
for (var i = 0; i < languages.length; i++) {
var codeSamples = $('pre>code:not(.shell).' + languages[i].safe);
for (var j = 0; j < codeSamples.length; j++) {
if(language === languages[i].pretty || codeSamples[j].hasAttribute("always-show")) {
codeSamples[j].style.display = 'block';
} else {
codeSamples[j].style.display = 'none';
}
}
}
var allLanguages = $('.languageSelect>span');
for (i = 0; i < allLanguages.length; i++) {
if(allLanguages[i].innerText === language) {
allLanguages[i].classList.add('chosen');
} else {
allLanguages[i].classList.remove('chosen');
}
}
if(target) {
target.scrollIntoView();
}
}
function collapseSnippets() {
addLanguageSelect();
showOnly(chosenLanguage(), null);
}
function containsLanguageBindings(pre) {
for (var i = 0; i < languages.length; i++) {
if(containsLanguage(pre, languages[i].safe)) {
return true;
}
}
return false;
}
function containsLanguage(pre, language) {
return pre.querySelector('code:not([always-show]).' + language);
}
function addLanguageSelect() {
var pre = $("pre");
for (var i = 0; i < pre.length; i++) {
var languagesDiv = document.createElement('div');
languagesDiv.setAttribute('class', 'languageSelect');
pre[i].insertBefore(languagesDiv, pre[i].childNodes[0]);
if (containsLanguageBindings(pre[i])) {
for (var j = 0; j < languages.length; j++) {
var span = document.createElement('span');
span.innerText = languages[j].pretty;
span.classList.add(languages[j].safe);
span.onclick = function() {
showOnly(this.innerText, this.parentNode.parentNode);
};
if (!containsLanguage(pre[i], languages[j].safe)) {
span.classList.add('missing');
var code = document.createElement('code');
code.classList.add(languages[j].safe);
code.innerText= languages[j].comment
+ 'We don\'t have a ' + languages[j].pretty
+ ' code sample yet - Help us out and raise a PR';
pre[i].appendChild(code);
}
languagesDiv.appendChild(span);
}
}
}
}
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("load", collapseSnippets);
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";
}
}