Skip to content

Instantly share code, notes, and snippets.

View THEtheChad's full-sized avatar

Chad Elliott THEtheChad

View GitHub Profile
@THEtheChad
THEtheChad / gist:2341660
Created April 9, 2012 05:23
Add spacers to the Mac doc.
defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
killall Dock
@THEtheChad
THEtheChad / traceEventOffset.js
Created April 23, 2012 22:50
Return the offset relative to parent element
function traceEventOffset(target_el, event){
var current_el = event.target
, cur_left = 0
, cur_top = 0
;//var
while(current_el != target_el){
cur_left += current_el.offsetLeft;
cur_top += current_el.offsetTop;
@THEtheChad
THEtheChad / injectScript.js
Created May 3, 2012 15:53
Asynchronously load appended scripts and execute callback when finished.
function injectScript(src, callback){
var s = document.createElement('script')
;//var
s.type = 'text/javascript';
s.src = src;
s.async = true;
s.onreadystatechange = s.onload = function(){
@THEtheChad
THEtheChad / Defer.js
Created May 3, 2012 16:24
Defers execution of the callback until exec is run. At that point, all future calls are executed immediately.
function Defer(callback){
if(!this instanceof Defer)
return new Defer(callback);
var queue = []
, callback = callback
, slice = Array.prototype.slice
, changeling = function(){
queue.push({
'context': this,
@THEtheChad
THEtheChad / findPos.js
Created May 17, 2012 14:42
Gets the elements offset relative to the page
function findPos(obj){
var cur_left = 0
, cur_top = 0
;//var
if(obj.offsetParent){
do{
cur_left += obj.offsetLeft;
cur_top += obj.offsetTop;
@THEtheChad
THEtheChad / subclass.js
Created May 30, 2012 15:51
Function used to create subclasses in javascript
// Created using underscore.js source code
// === INHERIT HELPER ===
// used to create subclasses
var // empty function used for dummy constructor
ctor = function(){}
// method for extending objects
// recommended that you replace with something
//from a library such as underscore.js
@THEtheChad
THEtheChad / asyncCount.js
Created June 28, 2012 20:58
Function used to group async calls
function asyncCount(num, callback){
   if(!this instanceof asyncCount)
       return new asyncCount(num, callback);
   var count = num;
   return function(){
       --count || callback();
   }
}
@THEtheChad
THEtheChad / STATES.js
Created June 28, 2012 21:33
State abbreviation lookup list
var STATES = {
'Alabama' : 'AL',
'Alaska' : 'AK',
'Arizona' : 'AZ',
'Arkansas' : 'AR',
'California' : 'CA',
'Colorado' : 'CO',
'Connecticut' : 'CT',
'Delaware' : 'DE',
'DistrictofColumbia': 'DC',
@THEtheChad
THEtheChad / base64.js
Created June 29, 2012 01:07
Encode/Decode base64 URL params
/**
* app.util.base64 encode / decode
* http://www.webtoolkit.info/
**/
app.util.base64 = {
// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
// public method for encoding
@THEtheChad
THEtheChad / perform.js
Created July 12, 2012 01:30
Used with backbone. Generates a function that uses the model passed as its context. This function can be passed a conditional parameter as its first argument to check against and a callback to be performed if that/those conditions are met.
function performOnceOn(model){
// store our model in the closure
var model = model;
return function(conditions, callback){
// internally reference params for use in nested functions
var callback = callback
, conditions = conditions
, current = model.attributes