Skip to content

Commit

Permalink
rustdoc: Foldable impl blocks
Browse files Browse the repository at this point in the history
Addresses rust-lang#40363, rust-lang#45720, rust-lang#24483, rust-lang#23986 and so on

* Expands and refactors collapseDocs and toggleAllDocs
* Adds [-] toggle to all impls (including inherent impl)
* Makes it hiding though main css file, not though element style

May need to be addressed:

* "[-]" and anchor link copier are overlaid a bit
* Inherent methods are also hidden by the global [-] toggle.
* Auto-collapsing "Iterator" and so on by default is not implemented yet
* Tested only shallowly and only in Chromiuim
* No tests. Are there tests for css/js part here?
* The new implementation may be a bit slower.
  • Loading branch information
vi committed Jan 31, 2018
1 parent 21882aa commit 73f52d3
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 51 deletions.
156 changes: 105 additions & 51 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@
document.onkeydown = handleShortcut;
document.onclick = function(ev) {
if (hasClass(ev.target, 'collapse-toggle')) {
collapseDocs(ev.target);
collapseDocs(ev.target, "toggle");
} else if (hasClass(ev.target.parentNode, 'collapse-toggle')) {
collapseDocs(ev.target.parentNode);
collapseDocs(ev.target.parentNode, "toggle");
} else if (ev.target.tagName === 'SPAN' && hasClass(ev.target.parentNode, 'line-numbers')) {
var prev_id = 0;

Expand Down Expand Up @@ -1618,74 +1618,124 @@
e.innerHTML = labelForToggleButton(false);
});
toggle.title = "collapse all docs";
onEach(document.getElementsByClassName("docblock"), function(e) {
e.style.display = 'block';
});
onEach(document.getElementsByClassName("toggle-label"), function(e) {
e.style.display = 'none';
});
onEach(document.getElementsByClassName("toggle-wrapper"), function(e) {
removeClass(e, "collapsed");
});
onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
onEveryMatchingChild(e, "inner", function(i_e) {
i_e.innerHTML = labelForToggleButton(false);
});
collapseDocs(e, "show");
});
} else {
addClass(toggle, "will-expand");
onEveryMatchingChild(toggle, "inner", function(e) {
e.innerHTML = labelForToggleButton(true);
});
toggle.title = "expand all docs";
onEach(document.getElementsByClassName("docblock"), function(e) {
e.style.display = 'none';
});
onEach(document.getElementsByClassName("toggle-label"), function(e) {
e.style.display = 'inline-block';
});
onEach(document.getElementsByClassName("toggle-wrapper"), function(e) {
addClass(e, "collapsed");
});

onEach(document.getElementsByClassName("collapse-toggle"), function(e) {
onEveryMatchingChild(e, "inner", function(i_e) {
i_e.innerHTML = labelForToggleButton(true);
});
collapseDocs(e, "hide");
});
}
}

function collapseDocs(toggle) {
function collapseDocs(toggle, mode) {
if (!toggle || !toggle.parentNode) {
return;
}
var relatedDoc = toggle.parentNode.nextElementSibling;
if (hasClass(relatedDoc, "stability")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if (hasClass(relatedDoc, "docblock")) {
if (!isHidden(relatedDoc)) {
relatedDoc.style.display = 'none';
onEach(toggle.childNodes, function(e) {
if (hasClass(e, 'toggle-label')) {

function adjustToggle(arg) {
return function(e) {
if (hasClass(e, 'toggle-label')) {
if (arg) {
e.style.display = 'inline-block';
}
if (hasClass(e, 'inner')) {
e.innerHTML = labelForToggleButton(true);
}
});
addClass(toggle.parentNode, 'collapsed');
} else {
relatedDoc.style.display = 'block';
removeClass(toggle.parentNode, 'collapsed');
onEach(toggle.childNodes, function(e) {
if (hasClass(e, 'toggle-label')) {
} else {
e.style.display = 'none';
}
if (hasClass(e, 'inner')) {
e.innerHTML = labelForToggleButton(false);
}
if (hasClass(e, 'inner')) {
e.innerHTML = labelForToggleButton(arg);
}
};
};

if (!hasClass(toggle.parentNode, "impl")) {
var relatedDoc = toggle.parentNode.nextElementSibling;
if (hasClass(relatedDoc, "stability")) {
relatedDoc = relatedDoc.nextElementSibling;
}
if (hasClass(relatedDoc, "docblock")) {
var action = mode;
if (action == "toggle") {
if(hasClass(relatedDoc, "hidden-by-usual-hider")) {
action="show";
} else {
action="hide";
}
}
if (action == "hide") {
addClass(relatedDoc, "hidden-by-usual-hider");
onEach(toggle.childNodes, adjustToggle(true));
addClass(toggle.parentNode, 'collapsed');
} else if (action == "show") {
removeClass(relatedDoc, "hidden-by-usual-hider");
removeClass(toggle.parentNode, 'collapsed');
onEach(toggle.childNodes, adjustToggle(false));
}
}
} else {
// we are collapsing the impl block
function implHider(addOrRemove) {
return function(n) {
if(hasClass(n, "method")) {
if (addOrRemove) {
addClass(n, "hidden-by-impl-hider");
} else {
removeClass(n, "hidden-by-impl-hider");
}
var ns = n.nextElementSibling;
while(true) {
if (ns && (
hasClass(ns, "docblock") ||
hasClass(ns, "stability") ||
false
)) {
if (addOrRemove) {
addClass(ns, "hidden-by-impl-hider");
} else {
removeClass(ns, "hidden-by-impl-hider");
}
ns = ns.nextElementSibling;
continue;
}
break;
}
}
});
}
}

var relatedDoc = toggle.parentNode;

while (!hasClass(relatedDoc, "impl-items")) {
relatedDoc = relatedDoc.nextElementSibling;
}

if (!relatedDoc) return;

// Hide all functions, but not associated types/consts

var action = mode;
if (action == "toggle") {
if(hasClass(relatedDoc, "fns-now-collapsed")) {
action="show";
} else {
action="hide";
}
}

if(action == "show") {
removeClass(relatedDoc, "fns-now-collapsed");
onEach(toggle.childNodes, adjustToggle(false));
onEach(relatedDoc.childNodes, implHider(false));
} else if (action == "hide") {
addClass(relatedDoc, "fns-now-collapsed");
onEach(toggle.childNodes, adjustToggle(true));
onEach(relatedDoc.childNodes, implHider(true));
}
}
}
Expand Down Expand Up @@ -1714,8 +1764,12 @@
hasClass(next.nextElementSibling, 'docblock'))) {
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
}
if (hasClass(e, 'impl')) {
insertAfter(toggle.cloneNode(true), e.childNodes[e.childNodes.length - 1]);
}
}
onEach(document.getElementsByClassName('method'), func);
onEach(document.getElementsByClassName('impl'), func);
onEach(document.getElementsByClassName('impl-items'), function(e) {
onEach(e.getElementsByClassName('associatedconstant'), func);
});
Expand Down Expand Up @@ -1803,7 +1857,7 @@
onEach(document.getElementById('main').getElementsByTagName('pre'), function(e) {
onEach(e.getElementsByClassName('attributes'), function(i_e) {
i_e.parentNode.insertBefore(createToggleWrapper(), i_e);
collapseDocs(i_e.previousSibling.childNodes[0]);
collapseDocs(i_e.previousSibling.childNodes[0], "toggle");
});
});

Expand Down
5 changes: 5 additions & 0 deletions src/librustdoc/html/static/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,8 @@ kbd {
background: #353535;
}
}

.hidden-by-impl-hider,
.hidden-by-usual-hider {
display: none;
}
5 changes: 5 additions & 0 deletions src/librustdoc/html/static/themes/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,8 @@ kbd {
background: #fff;
}
}

.hidden-by-impl-hider,
.hidden-by-usual-hider {
display: none;
}

0 comments on commit 73f52d3

Please sign in to comment.