Skip to content

Js improvements #50214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 24 additions & 32 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"union",
"foreigntype"];

var search_input = document.getElementsByClassName('search-input')[0];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think i'm a little fuzzy on JS's scoping/object rules. Does this automatically get updated if someone types in a search, deletes it, then searches for something else? Having everything reference this variable here seems a little off, but my intuition is based around Rust's rules, not JavaScript's. >_>

Copy link
Member Author

@GuillaumeGomez GuillaumeGomez Apr 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider it the same as a reference (or a view). You point to an element but you get the accurate information when you access its fields.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks! C# has a similar system for what they call "reference types".


// On the search screen, so you remain on the last tab you opened.
//
// 0 for "In Names"
Expand Down Expand Up @@ -411,8 +413,8 @@
// but only if the input bar is empty. This avoid the obnoxious issue
// where you start trying to do a search, and the index loads, and
// suddenly your search is gone!
if (document.getElementsByClassName("search-input")[0].value === "") {
document.getElementsByClassName("search-input")[0].value = params.search || '';
if (search_input.value === "") {
search_input.value = params.search || '';
}

/**
Expand Down Expand Up @@ -1118,7 +1120,6 @@
});
});

var search_input = document.getElementsByClassName('search-input')[0];
search_input.onkeydown = function(e) {
// "actives" references the currently highlighted item in each search tab.
// Each array in "actives" represents a tab.
Expand Down Expand Up @@ -1169,7 +1170,7 @@
// Does nothing, it's just to avoid losing "focus" on the highlighted element.
} else if (e.which === 27) { // escape
removeClass(actives[currentTab][0], 'highlighted');
document.getElementsByClassName('search-input')[0].value = '';
search_input.value = '';
defocusSearchBar();
} else if (actives[currentTab].length > 0) {
removeClass(actives[currentTab][0], 'highlighted');
Expand Down Expand Up @@ -1206,7 +1207,6 @@
return;
}

console.log(item);
shown.push(id_ty);
name = item.name;
type = itemTypes[item.ty];
Expand Down Expand Up @@ -1268,7 +1268,7 @@
}

function showResults(results) {
var output, query = getQuery(document.getElementsByClassName('search-input')[0].value);
var output, query = getQuery(search_input.value);

currentResults = query.id;
output = '<h1>Results for ' + escape(query.query) +
Expand Down Expand Up @@ -1375,14 +1375,13 @@

function search(e) {
var params = getQueryStringParams();
var search_input = document.getElementsByClassName('search-input')[0];
var query = getQuery(search_input.value.trim());

if (e) {
e.preventDefault();
}

if (!query.query || query.id === currentResults) {
if (query.query.length === 0 || query.id === currentResults) {
if (query.query.length > 0) {
putBackSearch(search_input);
}
Expand Down Expand Up @@ -1468,9 +1467,6 @@
function startSearch() {
var searchTimeout;
var callback = function() {
var search_input = document.getElementsByClassName('search-input');
if (search_input.length < 1) { return; }
search_input = search_input[0];
clearTimeout(searchTimeout);
if (search_input.value.length === 0) {
if (browserSupportsHistoryApi()) {
Expand All @@ -1488,7 +1484,6 @@
searchTimeout = setTimeout(search, 500);
}
};
var search_input = document.getElementsByClassName("search-input")[0];
search_input.onkeyup = callback;
search_input.oninput = callback;
document.getElementsByClassName("search-form")[0].onsubmit = function(e) {
Expand Down Expand Up @@ -1538,9 +1533,9 @@
// nothing there, which lets you really go back to a
// previous state with nothing in the bar.
if (params.search) {
document.getElementsByClassName('search-input')[0].value = params.search;
search_input.value = params.search;
} else {
document.getElementsByClassName('search-input')[0].value = '';
search_input.value = '';
}
// Some browsers fire 'onpopstate' for every page load
// (Chrome), while others fire the event only when actually
Expand Down Expand Up @@ -1950,28 +1945,14 @@
return wrapper;
}

onEach(document.getElementById('main').getElementsByClassName('docblock'), function(e) {
if (e.parentNode.id === "main") {
var otherMessage;
if (hasClass(e, "type-decl")) {
otherMessage = '&nbsp;Show&nbsp;type&nbsp;declaration';
}
e.parentNode.insertBefore(createToggle(otherMessage), e);
if (otherMessage && getCurrentValue('rustdoc-item-declarations') !== "false") {
collapseDocs(e.previousSibling.childNodes[0], "toggle");
}
}
});

onEach(document.getElementsByClassName('docblock'), function(e) {
if (hasClass(e, 'autohide')) {
var wrap = e.previousElementSibling;
if (wrap && hasClass(wrap, 'toggle-wrapper')) {
var toggle = wrap.childNodes[0];
var extra = false;
if (e.childNodes[0].tagName === 'H3') {
onEach(toggle.getElementsByClassName('toggle-label'), function(i_e) {
i_e.innerHTML = " Show " + e.childNodes[0].innerHTML;
});
extra = true;
}
e.style.display = 'none';
addClass(wrap, 'collapsed');
Expand All @@ -1980,9 +1961,22 @@
});
onEach(toggle.getElementsByClassName('toggle-label'), function(e) {
e.style.display = 'inline-block';
if (extra === true) {
i_e.innerHTML = " Show " + e.childNodes[0].innerHTML;
}
});
}
}
if (e.parentNode.id === "main") {
var otherMessage;
if (hasClass(e, "type-decl")) {
otherMessage = '&nbsp;Show&nbsp;type&nbsp;declaration';
}
e.parentNode.insertBefore(createToggle(otherMessage), e);
if (otherMessage && getCurrentValue('rustdoc-item-declarations') !== "false") {
collapseDocs(e.previousSibling.childNodes[0], "toggle");
}
}
})

autoCollapseAllImpls(getPageId());
Expand Down Expand Up @@ -2088,8 +2082,6 @@
}
}

var search_input = document.getElementsByClassName("search-input")[0];

if (search_input) {
search_input.onfocus = function() {
putBackSearch(this);
Expand Down