Skip to content

Instantly share code, notes, and snippets.

@nathanaelnsmith
nathanaelnsmith / DomBasedRouter.js
Last active November 23, 2023 09:52
ES6 module implementation of Paul Irish's DOM based routing
export default (routes) => {
return {
fire (func,funcname, args){
funcname = (funcname === undefined) ? 'init' : funcname;
if (func !== '' && routes[func] && typeof routes[func][funcname] == 'function'){
routes[func][funcname](args);
}
},
load() {
var bodyId = document.body.id;
@nathanaelnsmith
nathanaelnsmith / throttleRequests.js
Created July 28, 2017 18:07
Throttle calling a provided function on every element in the calling array.
function throttle(set, limit, threshold, callback) {
let timer = 0;
window.setInterval(() => {
timer++;
}, threshold);
let i = 0;
let requestChunk = timer;
let requestsSent = 0;
while (i < set.length) {
// requests within limit
@nathanaelnsmith
nathanaelnsmith / concatStrings.js
Last active November 3, 2016 23:29
Append or prepend a string to a predefined string
var prepend = concatStrings('prependWith-'),
append = concatStrings('-appendWith', true);
console.log(prepend('baseString')); // outputs prependWith-baseString
console.log(append('baseString')); // outputs baseString-appendWith
function concatStrings (classFragmentA, reverse) {
return function (classFragmentB) {
var fragments = [classFragmentA, classFragmentB];
return (reverse) ? fragments.reverse().join('') : fragments.join('');
/*
* Author: Nathanael Smith
* Description: This module stores functions in a run queue until they're needed to be executed.
*/
var ExeQue = (function(){
function store (location, func) {
var queue = getQueue(location).concat(func);
if (location instanceof jQuery) {
@nathanaelnsmith
nathanaelnsmith / listingorder.css
Last active August 26, 2015 18:22
css listing order
.module {
// extends
// includes
// element styles
// media queries
// element states
@nathanaelnsmith
nathanaelnsmith / autoTab.js
Created June 13, 2013 23:24
Auto Tab Form fields with maxlength set
(function(){
var autoTab = {
keysPressed : 0,
init : function () {
this.keyEvents();
},
keyEvents : function () {
var self = this; self.keyEvents.validKey = false;
$('input[maxlength]').on('keypress', function(e){
self.keyEvents.validKey = true;
// Normalize & Layout
article, aside, details, figcaption, figure, footer,
header, hgroup, nav, section, summary { display: block; }
audio, canvas, video {
display: inline-block;
*display: inline;
*zoom: 1;
}
audio:not([controls]) { display: none; }
[hidden] { display: none; }
@nathanaelnsmith
nathanaelnsmith / blocks-equalize.js
Last active December 16, 2015 17:18
Match all blocks in a set to the same height.
$(function(){
$('.block-grid').each(function(index){
var blocks = $.makeArray($(this).find('.block')).sort(sortByHeight)[0];
$(this).find('.block').height($(blocks).height());
});
});
function sortByHeight(a,b) {
return ($(b).height() - $(a).height());
}
@nathanaelnsmith
nathanaelnsmith / speedbump.js
Created January 11, 2013 00:35
External Link Speedbump
// external link speedbump
$.expr[":"].external = function (a) {
// DO NOT INCLUDE THE FOLLOWING MATCHES: EMAIL LINKS, TELEPHONE LINKS, HOSTNAME
return !a.href.match(/^mailto\:/) && !a.href.match(/^tel\:/) && a.hostname != location.hostname
};
$('a:external').click(function(e){
e.preventDefault();
href = $(this).attr('href');
$.colorbox({
href: '/_diffs/templates/portal_pop_up-speedbump.html',
@nathanaelnsmith
nathanaelnsmith / custom-upload.html
Last active December 10, 2015 21:58
Hide the file input using css, create a disabled text input to display file name once selected, use custom button to trigger file browse click. After file is selected, JS grabs value and inserts into text field.
<span class="custom-upload">
<i class="icomoon-upload"></i>
<input type="file" name="receipt" id="receipt" class="in-lrg">
<input type="text" name="file-name" id="file-name" class="in-lrg" disabled>
</span>