Skip to content

Commit fa2f7bf

Browse files
authored
Optimize the documentation JS files (#20075)
This PR optimizes the three files in the `/docs/_assets/js` directory for performance. In `toolbar.js`, jQuery selectors have been cached as variables to prevent constant calls to the DOM. In `sidebar.js`, the multiple `toggleClass(...).toggleClass(...)` have been merged into one In `api-search.js`: - Defined functions have been switched to newer ES format (`const myFunc = () => {}`) - JSON has been switched to **JS**ON (`{"key":"value"}` -> `{key:"value"}`)
1 parent ece87c3 commit fa2f7bf

File tree

3 files changed

+52
-64
lines changed

3 files changed

+52
-64
lines changed

Diff for: docs/_assets/js/api-search.js

+26-44
Original file line numberDiff line numberDiff line change
@@ -28,66 +28,48 @@
2828
* }
2929
* ```
3030
*/
31-
onmessage = function(e) {
32-
var docs = e.data.docs;
33-
var searchTerm = e.data.search;
34-
35-
var regexForTerm = function(query) {
36-
var escaped = query.replace(/([\.\*\+\?\|\(\)\[\]\\])/g, '\\$1');
31+
onmessage = function({ data: { docs, search } }) {
32+
const regexForTerm = (query) => {
33+
const escaped = query.replace(/([\.\*\+\?\|\(\)\[\]\\])/g, '\\$1');
3734
if (query.toLowerCase() != query) {
3835
// Regexp that matches CamelCase subbits: "BiSe" is
3936
// "[a-z]*Bi[a-z]*Se" and matches "BitSet", "ABitSet", ...
4037
return new RegExp(escaped.replace(/([A-Z])/g,"[a-z]*$1"));
4138
}
42-
else { // if query is all lower case make a normal case insensitive search
43-
return new RegExp(escaped, "i");
44-
}
39+
// if query is all lower case make a normal case insensitive search
40+
return new RegExp(escaped, "i");
4541
};
4642

47-
var searchRegex = regexForTerm(searchTerm);
43+
const searchRegex = regexForTerm(search);
4844

49-
var filterPackages = function(entity) {
50-
switch(entity.kind) {
51-
case "val":
52-
case "def":
53-
case "type":
54-
case "package":
55-
return false;
56-
default:
57-
return true;
58-
}
59-
};
45+
const filterPackages = (entity) => !["val", "def", "type", "package"].includes(entity.kind);
6046

61-
// look at this higher order function, such syntax:
62-
var messageParentIfMatches = function(parent) {
63-
return function(entity) {
64-
var fullName = entity.path.join('.');
47+
const messageParentIfMatches = (parent) => (entity) => {
48+
const fullName = entity.path.join('.');
6549

66-
if (searchRegex.test(fullName)) {
50+
if (searchRegex.test(fullName)) {
51+
postMessage({
52+
type: "entityResult",
53+
package: parent,
54+
entity
55+
});
56+
}
57+
58+
entity.members.forEach((member) => {
59+
if (searchRegex.test(member.name)) {
6760
postMessage({
68-
"type": "entityResult",
69-
"package": parent,
70-
"entity": entity
61+
type: "memberResult",
62+
package: parent,
63+
parent: entity,
64+
member
7165
});
7266
}
73-
74-
var searchChild = function(member) {
75-
if (searchRegex.test(member.name)) {
76-
postMessage({
77-
"type": "memberResult",
78-
"package": parent,
79-
"parent": entity,
80-
"member": member,
81-
});
82-
}
83-
};
84-
entity.members.forEach(searchChild);
85-
};
67+
});
8668
};
8769

88-
docs.forEach(function(pack) {
70+
docs.forEach((pack) => {
8971
pack.members
9072
.filter(filterPackages)
9173
.forEach(messageParentIfMatches(pack));
9274
});
93-
}
75+
};

Diff for: docs/_assets/js/sidebar.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
function toggleSection(titleElement) {
33
const title = $(titleElement);
44
title.siblings("ul").toggleClass("toggled");
5-
title.children("i.fas").toggleClass("fa-angle-right").toggleClass("fa-angle-down");
5+
title.children("i.fas").toggleClass("fa-angle-right fa-angle-down");
66
}

Diff for: docs/_assets/js/toolbar.js

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
1-
$(document).ready(function() {
2-
$("#menu-icon").click(() => {
3-
$(".sidebar").toggleClass("toggled");
4-
})
5-
$("#search-icon").click(() => {
6-
$("#searchbar").toggleClass("shown");
7-
$("#search-api-input").focus();
8-
})
9-
const searchInput = $("#search-api-input");
10-
searchInput.keydown(evt => {
11-
if (evt.which == 13) {
12-
const baseUrl = $("#baseurl-input").val();
13-
window.location = (
14-
baseUrl + "/api/search.html?" +
15-
"searchTerm=" + searchInput.val() +
16-
"&previousUrl=" + encodeURI(window.location)
17-
);
1+
$(function() {
2+
const menuIcon = $("#menu-icon");
3+
const sidebar = $(".sidebar");
4+
menuIcon.on("click", () => {
5+
sidebar.toggleClass("toggled");
6+
});
7+
8+
const searchIcon = $("#search-icon");
9+
const searchbar = $("#searchbar");
10+
const searchApiInput = $("#search-api-input");
11+
searchIcon.on("click", () => {
12+
searchbar.toggleClass("shown");
13+
searchApiInput.focus();
14+
});
15+
16+
const baseurlInput = $("#baseurl-input");
17+
searchApiInput.keydown(evt => {
18+
if (evt.which === 13) { // Enter
19+
const baseUrl = baseurlInput.val();
20+
const searchTerm = searchApiInput.val();
21+
const previousUrl = encodeURI(window.location);
22+
const searchUrl = `${baseUrl}/api/search.html?searchTerm=${searchTerm}&previousUrl=${previousUrl}`;
23+
window.location = searchUrl;
1824
}
19-
})
20-
})
25+
});
26+
});

0 commit comments

Comments
 (0)