Skip to content

Commit

Permalink
Cleaned up js modules
Browse files Browse the repository at this point in the history
  • Loading branch information
rdwatters committed Mar 3, 2016
1 parent 2b3b85d commit 27852ae
Show file tree
Hide file tree
Showing 68 changed files with 21,208 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
assets/node_modules/
static/css/fonts/proxnova/
static/css/fonts/pnova/
static/css/fonts/akkurat/
build-and-deploy-hugo.sh
my-notes.md
45 changes: 0 additions & 45 deletions assets/js/modules/fluidvids.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
//es2015 extra string methods *not* transpiled with Babel
if (!String.prototype.startsWith) {
String.prototype.startsWith = function(searchString, position) {
position = position || 0;
return this.indexOf(searchString, position) === position;
};
}
if (!String.prototype.includes) {
String.prototype.includes = function() {'use strict';
return String.prototype.indexOf.apply(this, arguments) !== -1;
};
}
if (!String.prototype.endsWith) {
String.prototype.endsWith = function(searchString, position) {
var subjectString = this.toString();
if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
position = subjectString.length;
}
position -= searchString.length;
var lastIndex = subjectString.indexOf(searchString, position);
return lastIndex !== -1 && lastIndex === position;
};
}
//http://purl.eligrey.com/github/classList.js/blob/master/classList.js
/*
ClassList.js Polyfill
Allows for .classList methods in browsers that lack native support (ie, <= IE9)
https://github.com/eligrey/classList.js/
*/

if ("document" in self && !("classList" in document.createElement("_"))) {
(function(j) {
Expand All @@ -35,7 +16,8 @@ if ("document" in self && !("classList" in document.createElement("_"))) {
b = Object,
k = String[f].trim || function() {
return this.replace(/^\s+|\s+$/g, "")
}, c = Array[f].indexOf || function(q) {
},
c = Array[f].indexOf || function(q) {
var p = 0,
o = this.length;
for (; p < o; p++) {
Expand All @@ -44,19 +26,22 @@ if ("document" in self && !("classList" in document.createElement("_"))) {
}
}
return -1
}, n = function(o, p) {
},
n = function(o, p) {
this.name = o;
this.code = DOMException[o];
this.message = p
}, g = function(p, o) {
},
g = function(p, o) {
if (o === "") {
throw new n("SYNTAX_ERR", "An invalid or illegal string was specified")
}
if (/\s/.test(o)) {
throw new n("INVALID_CHARACTER_ERR", "String contains an invalid character")
}
return c.call(p, o)
}, d = function(s) {
},
d = function(s) {
var r = k.call(s.getAttribute("class") || ""),
q = r ? r.split(/\s+/) : [],
p = 0,
Expand All @@ -67,7 +52,8 @@ if ("document" in self && !("classList" in document.createElement("_"))) {
this._updateClassName = function() {
s.setAttribute("class", this.toString())
}
}, e = d[f] = [],
},
e = d[f] = [],
i = function() {
return new d(this)
};
Expand Down Expand Up @@ -144,4 +130,4 @@ if ("document" in self && !("classList" in document.createElement("_"))) {
}
}
}(self))
};
};
File renamed without changes.
11 changes: 11 additions & 0 deletions assets/js/modules/polyfill-remove-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*Taken from https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove
Allows for ChildNode.remove() in browsers that do not support it.
*/
if (!('remove' in Element.prototype)) {
Element.prototype.remove = function() {
if (this.parentNode) {
this.parentNode.removeChild(this);
}
};
}

61 changes: 61 additions & 0 deletions assets/js/modules/search-lunr-ajax-call-in-jquery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//If using jQuery, uncomment the following to use jQuery methods for searching the site index with lunr.js
jQuery(function() {
// Initialize lunr with the fields to be searched, plus the boost.
window.idx = lunr(function() {
this.field('id');
this.field('href');
this.field('title');
this.field('description', { boost: 50 });
this.field('tags');
this.field('categories');
this.field('content', { boost: 20 })
});

// Get the generated search_data.json file so lunr.js can search it locally.
window.data = $.getJSON('/site-index.json');

// Wait for the data to load and add it to lunr
window.data.then(function(loaded_data) {
$.each(loaded_data, function(index, value) {
window.idx.add(
$.extend({ "id": index }, value)
);
});
});

// Run search query on keyup with focus in site search
$("#site-search").keyup(function(event) {
var query = $("#search-input").val(); // Get the value for the text field
if ((query.length > 2) && (event.keyCode !== 9)) {
var results = window.idx.search(query); // Get lunr to perform a search
display_search_results(results); // Hand the results off to be displayed
}
});

function display_search_results(results) {
var $search_results = $("#search-results");

// Wait for data to load
window.data.then(function(loaded_data) {

// Are there any results?
if (results.length) {
$search_results.empty(); // Clear any old results

// Iterate over the results
results.forEach(function(result) {
var item = loaded_data[result.ref];

// Build a snippet of HTML for this result
var appendString = '<li><a href="' + item.href + '">' + item.title + '</a><p>' + item.description + '</li>';

// Add the snippet to the collection of results.
$search_results.append(appendString);
});
} else {
// If there are no results, let the user know.
$search_results.html('<li>No results found.<br/>Please check spelling, spacing, yada...</li>');
}
});
}
});
66 changes: 66 additions & 0 deletions assets/js/modules/search-lunr-ajax-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
var searchData;
var searchInput = document.getElementById('search-input');
searchInput.addEventListener('keyup', lunrSearch, true);
window.index = lunr(function() {
this.field('id');
this.field('url');
this.field('title', { boost: 50 });
this.field('description');
this.field('tags');
this.field('categories');
this.field('content', { boost: 20 });
});

var searchReq = new XMLHttpRequest();
searchReq.open('GET', '/site-index.json', true);
searchReq.onload = function() {
if (this.status >= 200 && this.status < 400) {
// console.log("Got the site index!");
searchData = JSON.parse(this.response);
searchData.forEach(function(obj, index) {
obj['id'] = index;
window.index.add(obj);
});
} else {
// console.log("Did not get the site index.");
}
}
searchReq.onerror = function() {
console.log("Error when attempting to load site-index.json");
}
searchReq.send();

function lunrSearch(event) {
var query = document.querySelector("#search-input").value;
var searchResults = document.querySelector('#search-results');
if (query.length === 0) {
searchResults.innerHTML = '';
}
if ((event.keyCode !== 9) && (query.length > 2)) {
var matches = window.index.search(query);
displayResults(matches);
}
}

function displayResults(results) {
var searchResults = document.querySelector('#search-results');
var inputVal = document.querySelector('#search-input').value;
if (results.length) {
searchResults.innerHTML = '';
results.forEach(function(result) {
var item = window.searchData[result.ref];
var section = item.section.split('-').map(s => s.charAt(0).toUpperCase() + s.slice(1)).join(' ');
var appendString = '<li class=\"search-result\"><a href="' + item.url + '"><h5>' + item.title + '</h5></a><span class=\"site-section\">In: ' + section + '</span><p>' + item.description + '</p><div class=\"search-result-tags\"><div class=\"tags\"></div>';
var tags = '';
for (var i = 0; i < item.tags.length; i++) {
appendString += '<a class=\"tag\" href=\"/tags/' + item.tags[i] + '\">' + item.tags[i] + '</a> ';
}
appendString += '</div></li>';
// appendString += tags + '</li>';
searchResults.innerHTML += appendString;

})
} else {
searchResults.innerHTML = '<li class=\"search-result none\">No results found for <span class=\"input-value\">' + inputVal + '</span>.<br/>Please check spelling and spacing.</li>';
}
}
27 changes: 27 additions & 0 deletions assets/js/modules/search-overlay-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
var searchOverlay = document.querySelector('.search-form');
var searchButton = document.getElementById('search-button');
var searchInput = document.getElementById('search-input');
var closeSearch = document.getElementById('close-search');
closeSearch.onclick = function() {
if (searchOverlay.classList.contains('open')) {
searchOverlay.classList.remove('open');
}
}
window.addEventListener('keyup', function(event) {
var keyPressed = event.keyCode;
if (keyPressed === 83 && searchOverlay.classList.contains('open')) {
return;
} else if (keyPressed === 83) {
searchOverlay.classList.add('open');
if (searchInput.value.length > 0) {
searchInput.value = '';
}
searchInput.focus();
} else if (keyPressed === 27 && searchOverlay.classList.contains('open')) {
searchOverlay.classList.remove('open');
}
}, true);
searchButton.addEventListener('click', function(event) {
searchOverlay.classList.toggle('open');
searchInput.focus();
}, true);
25 changes: 25 additions & 0 deletions assets/js/modules/site-header-toggle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var clickOrTouchStart;
if ('ontouchstart' in document.documentElement) {
clickOrTouchStart = "touchstart";
} else {
clickOrTouchStart = "click";
}
var headerToggle = document.getElementById('site-header-toggle');
(function() {
var siteSections = document.querySelectorAll('.site-section');
var address = location.pathname.split('/')[1];
if (address.length > 0) {
var addressTest = new RegExp(address);
for (var i = 0; i < siteSections.length; i++) {
if (addressTest.test(siteSections[i].dataset.section)) {
siteSections[i].className += " active";
}
}
}
})();
headerToggle.addEventListener(clickOrTouchStart, toggleHeader, false);

function toggleHeader() {
document.querySelector('.site-header').classList.toggle('open');
document.querySelector('main').classList.toggle('open');
}
48 changes: 48 additions & 0 deletions assets/js/modules/style-responsive-videos.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//Wrapvids.js
//2016, Ryan Watters, https://www.github.com/rdwatters
//MIT, Copyleft, Seriously-do-whatever-you-want-with-this license
//Included as part of Hugo and Jekyll starter kits
// Hugo: https://github.com/rdwatters/hugo-starter
// Jekyll: https://github.com/rdwatters/jekyll-gulp-starter
// In aforementioned starter kits starter kits, see also: js/modules/util-wrap-and-wrap-all.js for additional use of HTMLElement.prototype.wrap/wrapAll

(function wrapVids() {
var iframes = document.getElementsByTagName('iframe'),
videoWrapper = document.createElement('div'),
youTubeTest = new RegExp('youtube');
videoWrapper.className = "videoWrapper";
//Wrap function as substitute for jQuery .wrap(). Taken from http://stackoverflow.com/questions/3337587/wrapping-a-set-of-dom-elements-using-javascript
HTMLElement.prototype.wrap = function(elms) {
// Convert `elms` to an array, if necessary.
if (!elms.length) elms = [elms];

// Loops backwards to prevent having to clone the wrapper on the
// first element (see `child` below).
for (var i = elms.length - 1; i >= 0; i--) {
var child = (i > 0) ? this.cloneNode(true) : this;
var el = elms[i];

// Cache the current parent and sibling.
var parent = el.parentNode;
var sibling = el.nextSibling;

// Wrap the element (is automatically removed from its current
// parent).
child.appendChild(el);

// If the element had a sibling, insert the wrapper before
// the sibling to maintain the HTML structure; otherwise, just
// append it to the parent.
if (sibling) {
parent.insertBefore(child, sibling);
} else {
parent.appendChild(child);
}
}
};
for (var i = 0; i < iframes.length; i++) {
if (youTubeTest.test(iframes[i].getAttribute('src'))) {
videoWrapper.wrap(iframes[i]);
}
}
})();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*This IIFE takes all anchors on the page and add target="_blank" to any anchors to print documents (pdf, docx, etc) or external links.
/*This IIFE takes all anchors on the page and adds a target="_blank" attribute to all anchors to print documents (pdf, docx, etc) or external sites.
*/
(function targetBlank() {
// remove subdomain of current site's url and setup regex
Expand Down
Loading

0 comments on commit 27852ae

Please sign in to comment.