Last active
April 21, 2017 19:54
-
-
Save salsalabs/91020a16b6cd99cbcc6a to your computer and use it in GitHub Desktop.
Script to capitalize all workds in a Salsa form before the form is submitted.
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
// Generated by LiveScript 1.5.0 | |
// https://gist.github.com/salsalabs/add8c513630be2c5f95abef383cd856a | |
var initCap, capitalize, blurHandler, getInputs, notUsed, unwantedTypes, skip, run, actor, interval; | |
initCap = function(t){ | |
switch (t) { | |
case null: | |
return t; | |
default: | |
return t.slice(0, 1).toUpperCase() + t.slice(1).toLowerCase(); | |
} | |
}; | |
capitalize = function(t){ | |
return t.split(' ').map(initCap).join(' '); | |
}; | |
blurHandler = function(event){ | |
return event.target.value = capitalize(event.target.value); | |
}; | |
getInputs = function(){ | |
// SalsaStaff 175757: This doesn't work in IE. Replaced with something that does. | |
// return Array.from(document.querySelectorAll('input')).filter(skip); | |
return [].map.call(document.querySelectorAll('input'), function(e) { return e; }).filter(skip); | |
}; | |
notUsed = ['cc', 'ccExpMonth', 'ccExpYear', 'checkingAccountNumber', 'checkingRoutingNumber', 'Email', 'CVV2', 'amountOther']; | |
unwantedTypes = ['hidden', 'radio', 'checkbox', 'submit']; | |
skip = function(e){ | |
return !in$(e.name, notUsed) && !in$(e.type, unwantedTypes); | |
}; | |
run = function(){ | |
// A blur handler to change values as they are typed. A submit handler to | |
// make sure that the browser didn't sneak a value in and not trigger the blur. | |
getInputs().forEach(function(e){ | |
return e.addEventListener('blur', blurHandler); | |
}); | |
return document.querySelector('.salsa form').addEventListener('submit', function(){ | |
return getInputs().forEach(function(e){ | |
return e.blur(); | |
}); | |
}); | |
}; | |
if (/action_KEY=\d+/.test(window.location.href)) { | |
if (document.getElementById('tempPostalCode') !== null) { | |
console.log("Waiting for the action to get to the letter page."); | |
actor = function(){ | |
if (document.getElementsByName('First_Name').length !== 0) { | |
clearInterval(interval); | |
return run(); | |
} | |
}; | |
interval = setInterval(actor, 200); | |
} | |
} else { | |
run(); | |
} | |
function in$(x, xs){ | |
var i = -1, l = xs.length >>> 0; | |
while (++i < l) if (x === xs[i]) return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment