-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
1,165 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
"use strict"; | ||
|
||
// Fix back button cache problem | ||
window.onunload = function () { }; | ||
|
||
(function sidebar() { | ||
var html = document.querySelector("html"); | ||
var sidebar = document.getElementById("sidebar"); | ||
var sidebarScrollBox = document.getElementById("sidebar-scrollbox"); | ||
var sidebarLinks = document.querySelectorAll('#sidebar a'); | ||
var sidebarToggleButton = document.getElementById("sidebar-toggle"); | ||
var sidebarResizeHandle = document.getElementById("sidebar-resize-handle"); | ||
var firstContact = null; | ||
|
||
function showSidebar() { | ||
html.classList.remove('sidebar-hidden') | ||
html.classList.add('sidebar-visible'); | ||
Array.from(sidebarLinks).forEach(function (link) { | ||
link.setAttribute('tabIndex', 0); | ||
}); | ||
sidebarToggleButton.setAttribute('aria-expanded', true); | ||
sidebar.setAttribute('aria-hidden', false); | ||
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { } | ||
} | ||
|
||
|
||
var sidebarAnchorToggles = document.querySelectorAll('#sidebar a.toggle'); | ||
|
||
function toggleSection(ev) { | ||
ev.currentTarget.parentElement.classList.toggle('expanded'); | ||
} | ||
|
||
Array.from(sidebarAnchorToggles).forEach(function (el) { | ||
el.addEventListener('click', toggleSection); | ||
}); | ||
|
||
function hideSidebar() { | ||
html.classList.remove('sidebar-visible') | ||
html.classList.add('sidebar-hidden'); | ||
Array.from(sidebarLinks).forEach(function (link) { | ||
link.setAttribute('tabIndex', -1); | ||
}); | ||
sidebarToggleButton.setAttribute('aria-expanded', false); | ||
sidebar.setAttribute('aria-hidden', true); | ||
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { } | ||
} | ||
|
||
// Toggle sidebar | ||
sidebarToggleButton.addEventListener('click', function sidebarToggle() { | ||
if (html.classList.contains("sidebar-hidden")) { | ||
showSidebar(); | ||
} else if (html.classList.contains("sidebar-visible")) { | ||
hideSidebar(); | ||
} else { | ||
if (getComputedStyle(sidebar)['transform'] === 'none') { | ||
hideSidebar(); | ||
} else { | ||
showSidebar(); | ||
} | ||
} | ||
}); | ||
|
||
sidebarResizeHandle.addEventListener('mousedown', initResize, false); | ||
|
||
function initResize(e) { | ||
window.addEventListener('mousemove', resize, false); | ||
window.addEventListener('mouseup', stopResize, false); | ||
html.classList.add('sidebar-resizing'); | ||
} | ||
function resize(e) { | ||
document.documentElement.style.setProperty('--sidebar-width', (e.clientX - sidebar.offsetLeft) + 'px'); | ||
} | ||
//on mouseup remove windows functions mousemove & mouseup | ||
function stopResize(e) { | ||
html.classList.remove('sidebar-resizing'); | ||
window.removeEventListener('mousemove', resize, false); | ||
window.removeEventListener('mouseup', stopResize, false); | ||
} | ||
|
||
document.addEventListener('touchstart', function (e) { | ||
firstContact = { | ||
x: e.touches[0].clientX, | ||
time: Date.now() | ||
}; | ||
}, { passive: true }); | ||
|
||
document.addEventListener('touchmove', function (e) { | ||
if (!firstContact) | ||
return; | ||
|
||
var curX = e.touches[0].clientX; | ||
var xDiff = curX - firstContact.x, | ||
tDiff = Date.now() - firstContact.time; | ||
|
||
if (tDiff < 250 && Math.abs(xDiff) >= 150) { | ||
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300)) | ||
showSidebar(); | ||
else if (xDiff < 0 && curX < 300) | ||
hideSidebar(); | ||
|
||
firstContact = null; | ||
} | ||
}, { passive: true }); | ||
|
||
// Scroll sidebar to current active section | ||
var activeSection = sidebar.querySelector(".active"); | ||
if (activeSection) { | ||
sidebarScrollBox.scrollTop = activeSection.offsetTop; | ||
} | ||
})(); | ||
|
||
(function chapterNavigation() { | ||
document.addEventListener('keydown', function (e) { | ||
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; } | ||
if (window.search && window.search.hasFocus()) { return; } | ||
|
||
switch (e.key) { | ||
case 'ArrowRight': | ||
e.preventDefault(); | ||
var nextButton = document.querySelector('.nav-chapters.next'); | ||
if (nextButton) { | ||
window.location.href = nextButton.href; | ||
} | ||
break; | ||
case 'ArrowLeft': | ||
e.preventDefault(); | ||
var previousButton = document.querySelector('.nav-chapters.previous'); | ||
if (previousButton) { | ||
window.location.href = previousButton.href; | ||
} | ||
break; | ||
} | ||
}); | ||
})(); | ||
|
||
(function clipboard() { | ||
var clipButtons = document.querySelectorAll('.clip-button'); | ||
|
||
function hideTooltip(elem) { | ||
elem.firstChild.innerText = ""; | ||
elem.className = 'fa fa-copy clip-button'; | ||
} | ||
|
||
function showTooltip(elem, msg) { | ||
elem.firstChild.innerText = msg; | ||
elem.className = 'fa fa-copy tooltipped'; | ||
} | ||
|
||
var clipboardSnippets = new ClipboardJS('.clip-button', { | ||
text: function (trigger) { | ||
hideTooltip(trigger); | ||
let playpen = trigger.closest("pre"); | ||
return playpen_text(playpen); | ||
} | ||
}); | ||
|
||
Array.from(clipButtons).forEach(function (clipButton) { | ||
clipButton.addEventListener('mouseout', function (e) { | ||
hideTooltip(e.currentTarget); | ||
}); | ||
}); | ||
|
||
clipboardSnippets.on('success', function (e) { | ||
e.clearSelection(); | ||
showTooltip(e.trigger, "Copied!"); | ||
}); | ||
|
||
clipboardSnippets.on('error', function (e) { | ||
showTooltip(e.trigger, "Clipboard error!"); | ||
}); | ||
})(); | ||
|
||
(function scrollToTop () { | ||
var menuTitle = document.querySelector('.menu-title'); | ||
|
||
menuTitle.addEventListener('click', function () { | ||
document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}); | ||
})(); | ||
|
||
(function autoHideMenu() { | ||
var menu = document.getElementById('menu-bar'); | ||
|
||
var previousScrollTop = document.scrollingElement.scrollTop; | ||
|
||
document.addEventListener('scroll', function () { | ||
if (menu.classList.contains('folded') && document.scrollingElement.scrollTop < previousScrollTop) { | ||
menu.classList.remove('folded'); | ||
} else if (!menu.classList.contains('folded') && document.scrollingElement.scrollTop > previousScrollTop) { | ||
menu.classList.add('folded'); | ||
} | ||
|
||
if (!menu.classList.contains('bordered') && document.scrollingElement.scrollTop > 0) { | ||
menu.classList.add('bordered'); | ||
} | ||
|
||
if (menu.classList.contains('bordered') && document.scrollingElement.scrollTop === 0) { | ||
menu.classList.remove('bordered'); | ||
} | ||
|
||
previousScrollTop = Math.max(document.scrollingElement.scrollTop, 0); | ||
}, { passive: true }); | ||
})(); |
Oops, something went wrong.