Skip to content
Snippets Groups Projects
Commit 008809e0 authored by Silvio Giebl's avatar Silvio Giebl
Browse files

Include custom js, added js namespace

parent 96653f6d
No related branches found
No related tags found
No related merge requests found
--- ---
--- ---
(function (jtd, undefined) {
// Event handling // Event handling
function addEvent(el, type, handler) { jtd.addEvent = function(el, type, handler) {
if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler); if (el.attachEvent) el.attachEvent('on'+type, handler); else el.addEventListener(type, handler);
} }
function removeEvent(el, type, handler) { jtd.removeEvent = function(el, type, handler) {
if (el.detachEvent) el.detachEvent('on'+type, handler); else el.removeEventListener(type, handler); if (el.detachEvent) el.detachEvent('on'+type, handler); else el.removeEventListener(type, handler);
}
jtd.onReady = function(ready) {
// 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();
});
} }
// Show/hide mobile menu // Show/hide mobile menu
function toggleNav(){ function initNav() {
const mainNav = document.querySelector('.js-main-nav'); const mainNav = document.querySelector('.js-main-nav');
const pageHeader = document.querySelector('.js-page-header'); const pageHeader = document.querySelector('.js-page-header');
const navTrigger = document.querySelector('.js-main-nav-trigger'); const navTrigger = document.querySelector('.js-main-nav-trigger');
addEvent(navTrigger, 'click', function(e){ jtd.addEvent(navTrigger, 'click', function(e){
e.preventDefault(); e.preventDefault();
var text = navTrigger.innerText; var text = navTrigger.innerText;
var textToggle = navTrigger.getAttribute('data-text-toggle'); var textToggle = navTrigger.getAttribute('data-text-toggle');
...@@ -37,7 +48,7 @@ function initSearch() { ...@@ -37,7 +48,7 @@ function initSearch() {
var request = new XMLHttpRequest(); var request = new XMLHttpRequest();
request.open('GET', '{{ "assets/js/search-data.json" | absolute_url }}', true); request.open('GET', '{{ "assets/js/search-data.json" | absolute_url }}', true);
request.onload = function() { request.onload = function(){
if (request.status >= 200 && request.status < 400) { if (request.status >= 200 && request.status < 400) {
// Success! // Success!
var data = JSON.parse(request.responseText); var data = JSON.parse(request.responseText);
...@@ -67,7 +78,7 @@ function initSearch() { ...@@ -67,7 +78,7 @@ function initSearch() {
} }
}; };
request.onerror = function() { request.onerror = function(){
// There was a connection error of some sort // There was a connection error of some sort
console.log('There was a connection error'); console.log('There was a connection error');
}; };
...@@ -85,7 +96,7 @@ function initSearch() { ...@@ -85,7 +96,7 @@ function initSearch() {
searchResults.classList.remove('active'); searchResults.classList.remove('active');
} }
addEvent(searchInput, 'keydown', function(e){ jtd.addEvent(searchInput, 'keydown', function(e){
switch (e.keyCode) { switch (e.keyCode) {
case 38: // arrow up case 38: // arrow up
e.preventDefault(); e.preventDefault();
...@@ -129,7 +140,7 @@ function initSearch() { ...@@ -129,7 +140,7 @@ function initSearch() {
} }
}); });
addEvent(searchInput, 'keyup', function(e){ jtd.addEvent(searchInput, 'keyup', function(e){
switch (e.keyCode) { switch (e.keyCode) {
case 27: // When esc key is pressed, hide the results and clear the field case 27: // When esc key is pressed, hide the results and clear the field
hideResults(); hideResults();
...@@ -252,7 +263,7 @@ function initSearch() { ...@@ -252,7 +263,7 @@ function initSearch() {
} }
}); });
addEvent(searchInput, 'blur', function(){ jtd.addEvent(searchInput, 'blur', function(){
setTimeout(function(){ hideResults() }, 300); setTimeout(function(){ hideResults() }, 300);
}); });
} }
...@@ -263,22 +274,16 @@ function pageFocus() { ...@@ -263,22 +274,16 @@ function pageFocus() {
mainContent.focus(); mainContent.focus();
} }
// Document ready // Document ready
function ready(){ jtd.onReady(function(){
toggleNav(); initNav();
pageFocus(); pageFocus();
if (typeof lunr !== 'undefined') { if (typeof lunr !== 'undefined') {
initSearch(); initSearch();
} }
}
// 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();
}); });
})(window.jtd = window.jtd || {});
{% include_relative _custom.js %}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment