1
0
mirror of https://github.com/thangisme/notes.git synced 2024-11-09 07:37:34 -05:00
notes/assets/js/just-the-docs.js

171 lines
4.9 KiB
JavaScript
Raw Normal View History

2017-03-24 09:47:37 -04:00
// Event handling
function addEvent(el, type, handler) {
if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler);
}
function removeEvent(el, type, handler) {
if (el.detachEvent) el.detachEvent('on'+type, handler); else el.removeEventListener(type, handler);
}
// Show/hide mobile menu
function toggleNav(){
const nav = document.querySelector('.js-main-nav');
2017-11-08 11:23:05 -05:00
const auxNav = document.querySelector('.js-aux-nav');
2017-03-24 09:47:37 -04:00
const navTrigger = document.querySelector('.js-main-nav-trigger');
2018-10-23 11:41:25 -04:00
const search = document.querySelector('.js-search');
2017-03-24 09:47:37 -04:00
addEvent(navTrigger, 'click', function(){
var text = navTrigger.innerText;
var textToggle = navTrigger.getAttribute('data-text-toggle');
nav.classList.toggle('nav-open');
2017-11-08 11:23:05 -05:00
auxNav.classList.toggle('nav-open');
2017-03-24 09:47:37 -04:00
navTrigger.classList.toggle('nav-open');
2018-10-23 11:41:25 -04:00
search.classList.toggle('nav-open');
2017-03-24 09:47:37 -04:00
navTrigger.innerText = textToggle;
navTrigger.setAttribute('data-text-toggle', text);
textToggle = text;
})
}
// Site search
function initSearch() {
var index = lunr(function () {
this.ref('id');
2017-11-08 11:23:05 -05:00
this.field('title', { boost: 20 });
2018-10-23 12:51:11 -04:00
this.field('content', { boost: 10 });
2017-03-24 09:47:37 -04:00
this.field('url');
});
// Get the generated search_data.json file so lunr.js can search it locally.
2018-10-23 12:51:11 -04:00
sc = document.getElementsByTagName("script");
source = '';
for(idx = 0; idx < sc.length; idx++)
{
s = sc.item(idx);
if(s.src && s.src.match(/just-the-docs\.js$/))
{ source = s.src; }
}
jsPath = source.replace('just-the-docs.js', '');
jsonPath = jsPath + '/search-data.json';
2017-03-24 09:47:37 -04:00
var request = new XMLHttpRequest();
2018-10-23 12:51:11 -04:00
request.open('GET', jsonPath, true);
2017-03-24 09:47:37 -04:00
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// Success!
var data = JSON.parse(request.responseText);
var keys = Object.keys(data);
for(var i in data) {
index.add({
id: data[i].id,
title: data[i].title,
content: data[i].content,
url: data[i].url
});
}
searchResults(data);
} else {
// We reached our target server, but it returned an error
console.log('Error loading ajax request. Request status:' + request.status);
}
};
request.onerror = function() {
// There was a connection error of some sort
console.log('There was a connection error');
};
request.send();
function searchResults(dataStore) {
var searchInput = document.querySelector('.js-search-input');
var searchResults = document.querySelector('.js-search-results');
var store = dataStore;
2017-03-26 21:09:19 -04:00
function hideResults() {
searchResults.innerHTML = '';
searchResults.classList.remove('active');
}
addEvent(searchInput, 'keyup', function(e){
var query = this.value;
searchResults.innerHTML = '';
searchResults.classList.remove('active');
2017-03-24 09:47:37 -04:00
2017-03-26 21:09:19 -04:00
if (query === '') {
hideResults();
} else {
var results = index.search(query);
if (results.length > 1) {
searchResults.classList.add('active');
var resultsList = document.createElement('ul');
searchResults.appendChild(resultsList);
2017-03-24 09:47:37 -04:00
for (var i in results) {
2017-03-26 21:09:19 -04:00
var resultsListItem = document.createElement('li');
var resultsLink = document.createElement('a');
2017-04-08 18:51:14 -04:00
var resultsUrlDesc = document.createElement('span');
2017-03-24 09:47:37 -04:00
var resultsUrl = store[results[i].ref].url;
2018-10-23 12:51:11 -04:00
var resultsRelUrl = store[results[i].ref].relUrl;
2017-03-24 09:47:37 -04:00
var resultsTitle = store[results[i].ref].title;
2018-10-23 12:51:11 -04:00
resultsLink.setAttribute('href', resultsUrl);
2017-03-24 09:47:37 -04:00
resultsLink.innerText = resultsTitle;
2018-10-23 12:51:11 -04:00
resultsUrlDesc.innerText = resultsRelUrl;
2017-03-24 09:47:37 -04:00
2017-03-26 21:09:19 -04:00
resultsList.classList.add('search-results-list');
resultsListItem.classList.add('search-results-list-item');
resultsLink.classList.add('search-results-link');
2017-04-08 18:51:14 -04:00
resultsUrlDesc.classList.add('fs-2','text-grey-dk-000','d-block');
2017-03-24 09:47:37 -04:00
resultsList.appendChild(resultsListItem);
2017-03-26 21:09:19 -04:00
resultsListItem.appendChild(resultsLink);
2017-04-08 18:51:14 -04:00
resultsLink.appendChild(resultsUrlDesc);
2017-03-24 09:47:37 -04:00
}
}
2017-03-26 21:09:19 -04:00
// When esc key is pressed, hide the results and clear the field
if (e.keyCode == 27) {
hideResults();
searchInput.value = '';
}
}
2017-03-24 09:47:37 -04:00
});
addEvent(searchInput, 'blur', function(){
2017-04-08 18:51:14 -04:00
setTimeout(function(){ hideResults() }, 300);
2017-03-24 09:47:37 -04:00
});
}
}
// Document ready
function ready(){
toggleNav();
if (typeof lunr !== 'undefined') {
initSearch();
}
2017-03-24 09:47:37 -04:00
}
// in case the document is already rendered
if (document.readyState!='loading') ready();
// modern browsers
else if (document.addEventListener) document.addEventListener('DOMContentLoaded', ready);
// IE <= 8
else document.attachEvent('onreadystatechange', function(){
if (document.readyState=='complete') ready();
});