Skip to content

Instantly share code, notes, and snippets.

@corbane
Last active January 21, 2020 16:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corbane/8a4d120269861a9cf6d90ecd420cb02a to your computer and use it in GitHub Desktop.
Save corbane/8a4d120269861a9cf6d90ecd420cb02a to your computer and use it in GitHub Desktop.
window.addEventListener("DOMContentLoaded", function() {
// this must match the rule `.partial-block {max-height: 245px; ...} `
let max_height = 245;
let btn_more_tpl = '<div class="show-block-btn">Show more ...</div>';
let btn_less_tpl = '<div class="show-block-btn">Show less ...</div>';
let btn_tpl = $("<tr><td></td><td>"+btn_more_tpl+"</td></tr>")[0];
let last_exp = null;
let last_btn = null;
let time_factor = 0.04;
init(".method td.field-body");
init(".function td.field-body");
function on_expand(event)
{
let btn = event.currentTarget;
let exp = btn._expandable_target;
if( last_exp == exp )
{
btn.childNodes[1].innerHTML = btn_more_tpl;
last_exp = null;
last_btn = null;
let top = $(get_parent_dl(exp)).offset().top;
if( top < window.pageYOffset )
SmoothScrollTo(top, function() { exp.classList.remove("expanded-block") });
}
else
{
if( last_exp )
{
last_height = $(last_exp).height();
last_exp.classList.remove("expanded-block")
last_btn.childNodes[1].innerHTML = btn_more_tpl;
if( last_height > $(window).height() )
{
// for ie
let top = $(get_parent_dl(exp)).offset().top;
if( top < window.pageYOffset && window.pageYOffset - top > $(window).height() )
SmoothScrollTo(top);
}
}
exp.classList.add("expanded-block")
btn.childNodes[1].innerHTML = btn_less_tpl;
last_exp = exp;
last_btn = btn;
}
}
function init(query)
{
let matches = document.querySelectorAll(query);
for( let i = 0 ; i < matches.length ; i++ )
{
let exp = matches.item(i);
if( exp.firstElementChild &&
parseInt(window.getComputedStyle(exp.firstElementChild).height) > max_height )
{
exp.classList.add("partial-block");
let clone = btn_tpl.cloneNode(true);
clone._expandable_target = exp;
clone.addEventListener("click", on_expand);
exp.parentElement.insertAdjacentElement('afterend', clone);
}
}
}
function get_parent_dl(elem)
{
var cur = elem.parentElement;
while(cur && cur.nodeName != "DL") cur = cur.parentElement;
return cur;
}
function SmoothScrollTo (pos, cb)
{
if ( pos < 0 )
return;
pos-=10
var cur_pos = window.scrollY || window.screenTop;
var start = null;
var delay;
if( cur_pos < pos )
{
delay = (pos-cur_pos/200)*time_factor;
window.requestAnimationFrame(function up (cur_time)
{
start = start===null ? cur_time : start;
var progress = cur_time - start;
window.scrollTo(0, ((pos - cur_pos) * progress / delay) + cur_pos);
if ( progress < delay )
window.requestAnimationFrame(up);
else
{
window.scrollTo(0, pos);
if(cb) cb();
}
});
}
else
{
delay = (cur_pos-pos/200)*time_factor;
window.requestAnimationFrame(function down (currentTime)
{
start = start===null ? currentTime : start;
var progress = currentTime - start;
window.scrollTo(0, cur_pos - ((cur_pos - pos) * progress / delay));
if ( progress < delay )
window.requestAnimationFrame(down);
else
{
window.scrollTo(0, pos);
if(cb) cb();
}
});
}
}
})
/* Prevent Long enum lists */
.field-body {
display: block;
width: 100%;
overflow-y: auto !important;
}
/* Hide home icon in search area */
.wy-side-nav-search > a:hover {background: none; opacity: 0.9}
.wy-side-nav-search > a.icon::before {content: none}
.partial-block { max-height: 245px; overflow-y: scroll !important; }
.expanded-block { max-height: none !important; }
.show-block-btn {
user-select: none;
cursor: pointer;
text-align: center;
}
.show-block-btn:hover {
text-decoration: underline;
background-color: #f0f0f0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment