Skip to content

Instantly share code, notes, and snippets.

View toddmotto's full-sized avatar

Todd Motto toddmotto

View GitHub Profile
@toddmotto
toddmotto / gist:9346064
Created March 4, 2014 13:00
Fixed heights/content JavaScript applications
(function (root, document, undefined) {
var $$ = function () {
return document.querySelectorAll(arguments[0]);
};
var header = $$('.header')[0];
var headerHeight = header.offsetHeight;
var content = $$('.main')[0];
var sidebar = $$('.sidebar')[0];
@toddmotto
toddmotto / gist:9370782
Created March 5, 2014 16:34
Recursive Object traversal (loops through Object keys and Arrays)
var updateKeys = function (form) {
var objString = Object.prototype.toString;
var traverse = function (obj) {
if (objString.call(obj) === '[object Array]') {
for (var i = 0; i < obj.length; i++) {
console.log(obj[i].type, obj[i].title);
traverse(obj[i]);
}
var isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
};
@toddmotto
toddmotto / consoleLoge.js
Last active August 29, 2015 13:57
console.loge(), many log, much console
window.console.loge = function (msg) {
var gifs = ['wink','shake-space','peepers','prizza','hat','gradient','fat','rainbow','sunglasses','derp','shake'],
wow = ['', 'wow! ', 'amaze! ', 'dazzle! '],
adjs = ['so', 'such', 'many', 'much', 'very'],
randomizr = function (a) { return a[Math.floor(Math.random() * a.length)];},
message = '%c ' + randomizr(wow) + randomizr(adjs) + ' ' + typeof msg + ': ',
css = 'background: url(http://d1e3ezyatlol8u.cloudfront.net/img/212/doge-' + randomizr(gifs) + '-212.gif) no-repeat 0 0; background-size: 80px 80px; font-family: \'Comic Sans MS\', cursive; text-shadow: 0 1px 1px rgba(0,0,0,1); font-size: 14px; padding: 25px; line-height: 70px; color: #fff; font-weight: 100;';
console.log.apply(console, typeof msg === 'object' ? [message, css, msg] : [message += msg, css]);
};
@toddmotto
toddmotto / gist:9890835
Created March 31, 2014 12:04
AngularJS ng-options RegExp
/^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/
@toddmotto
toddmotto / gist:10960869
Created April 17, 2014 07:28
ECMAScript 5 .bind() Polyfill
Function.prototype.bind = Function.prototype.bind || function (target) {
var self = this;
return function (args) {
if (!(args instanceof Array)) {
args = [args];
}
self.apply(target, args);
};
};
<div class="module">
<div class="module__thing">
<h1 class="module__thing-title">Hi</h1>
</div>
<div class="module__content">
<p class="module__content-text">Oh, hello.</p>
</div>
</div>
@toddmotto
toddmotto / gist:ce1023cb755b31210563
Created May 6, 2014 14:53
Debunking the British Gas paste event
var input = document.querySelector('input[type=password]');
var britishGasPasteHandler = function () {
if (window.certificate) {
certificate.revoke();
}
};
input.addEventListener('paste', britishGasPasteHandler, false);
@toddmotto
toddmotto / qsa.js
Last active August 29, 2015 14:02
104 bytes jQuery-like wrapper, $('.class') returns NodeList, $('.class:first') returns first Node only, optional scoping $('.class', '.parent')
function $(a,b){return(b||document)['querySelector'+(b=/\:first$/,b.test(a)?'':'All')](a.replace(b,''))}
@toddmotto
toddmotto / xhr.js
Created June 10, 2014 13:56
XHR wrapper for AngularJS $http
/**
* @name xhr
* @desc Dynamic $http/$q
* @param {String} [type] HTTP method
* @param {Array} config Array of config to be called with .apply()
* @private
* @returns {Object} deferred.promise
*/
var xhr = function (type, config) {
if (!config && angular.isArray(type)) {