fix: restore focus on reload

This commit is contained in:
Synox 2018-01-12 23:38:56 +01:00
parent 23bff41601
commit f1a64b260b

View file

@ -36,9 +36,10 @@ $purifier = new HTMLPurifier($purifier_config);
<meta name="turbolinks-cache-control" content="no-preview">
<script>
// https://stackoverflow.com/a/44353026
// https://stackoverflow.com/a/48234990/79461
var reloadWithTurbolinks = (function () {
var scrollPosition;
var focusId;
function reload() {
Turbolinks.visit(window.location.toString(), {action: 'replace'})
@ -46,12 +47,17 @@ $purifier = new HTMLPurifier($purifier_config);
document.addEventListener('turbolinks:before-render', function () {
scrollPosition = [window.scrollX, window.scrollY];
focusId = document.activeElement.id;
});
document.addEventListener('turbolinks:load', function () {
if (scrollPosition) {
window.scrollTo.apply(window, scrollPosition);
scrollPosition = null
}
if (focusId) {
document.getElementById(focusId).focus();
focusId = null;
}
});
return reload;
})();