extract toggling code into toggleSidebar()

This commit is contained in:
Bulat Musin 2018-02-05 15:31:01 +03:00
parent 7a50f7910e
commit 2298dfc20e
1 changed files with 21 additions and 20 deletions

View File

@ -415,8 +415,7 @@ function playpen_text(playpen) {
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { } try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
} }
// Toggle sidebar function toggleSidebar() {
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
if (html.classList.contains("sidebar-hidden")) { if (html.classList.contains("sidebar-hidden")) {
showSidebar(); showSidebar();
} else if (html.classList.contains("sidebar-visible")) { } else if (html.classList.contains("sidebar-visible")) {
@ -428,7 +427,10 @@ function playpen_text(playpen) {
showSidebar(); showSidebar();
} }
} }
}); }
// Toggle sidebar
sidebarToggleButton.addEventListener('click', toggleSidebar);
document.addEventListener('touchstart', function (e) { document.addEventListener('touchstart', function (e) {
firstContact = { firstContact = {
@ -460,17 +462,8 @@ function playpen_text(playpen) {
switch (e.key) { switch (e.key) {
case 'F9': case 'F9':
if (html.classList.contains("sidebar-hidden")) { console.log(window.location.href);
showSidebar(); toggleSidebar();
} else if (html.classList.contains("sidebar-visible")) {
hideSidebar();
} else {
if (getComputedStyle(sidebar)['transform'] === 'none') {
hideSidebar();
} else {
showSidebar();
}
}
break; break;
} }
}); });
@ -490,19 +483,27 @@ function playpen_text(playpen) {
case 'ArrowRight': case 'ArrowRight':
case 'j': case 'j':
e.preventDefault(); e.preventDefault();
var nextButton = document.querySelector('.nav-chapters.next'); var previousButton = document.querySelector('.nav-chapters.previous');
if (nextButton) { if (previousButton) {
window.location.href = nextButton.href; window.location.href = previousButton.href;
} }
break; break;
case 'ArrowLeft': case 'ArrowLeft':
case 'k': case 'k':
e.preventDefault(); e.preventDefault();
var previousButton = document.querySelector('.nav-chapters.previous'); var nextButton = document.querySelector('.nav-chapters.next');
if (previousButton) { if (nextButton) {
window.location.href = previousButton.href; window.location.href = nextButton.href;
} }
break; break;
case 'h':
e.preventDefault();
// TODO: add code
break;
case 'l':
e.preventDefault();
// TODO: add code
break;
} }
}); });
})(); })();