Skip to content

Instantly share code, notes, and snippets.

@quirkey
Created December 2, 2009 19:41
Show Gist options
  • Save quirkey/247480 to your computer and use it in GitHub Desktop.
Save quirkey/247480 to your computer and use it in GitHub Desktop.
;(function($) {
$.fn.extend({
getID: function() {
return parseInt($(this).attr("id").match(/\d+/)[0]);
},
setInputHint: function() {
if ($(this).length > 1) {
$(this).each(function() {
$(this).setInputHint();
});
} else {
var el = $(this);
el._default = el.val();
el.focus(function() {
if(el._default != el.val()) return;
el.removeClass('hint').val('');
})
.blur(function() {
if($.trim(el.val()) != '') return;
el.addClass('hint').val(el._default);
})
.addClass('hint');
}
},
addSelector: function(selector) {
var classes = selector.split('.'),
$this = $(this);
$.each(classes, function(i, c) {
if (c.match(/^#/)) {
$this.attr('id', c.replace('#',''));
} else {
$this.addClass(c);
}
});
return $this;
},
findOrAppend: function(selector, element) {
var $this = $(this),
$el = $this.find(selector);
if ($el.length > 0) return $el;
// el doesnt exist so lets make it
if (typeof element == 'undefined') {
// figure out how to build the element from the selector
var parts = selector.split(' ');
$el = $("<div>");
if (parts.length == 1) {
// single selector so just added
$el.addSelector(selector);
} else {
// selector with multiple depth so we just want the last selector
$el.addSelector(parts.pop());
// find this down the parts
$this = $this.findOrAppend(parts.join(' '));
}
} else {
// we provided a specific element
$el = $(element);
}
// append it
$this.append($el);
// return it
return $el;
},
soon: function(callback, when) {
var context = this, wrapped_callback = function() {
callback.apply(context);
};
setTimeout(wrapped_callback, when || 200);
return context;
},
slideTo: function(speed) {
var top = $(this).offset().top;
$('body,html').animate({'scrollTop': top + 'px'}, speed);
}
});
$.extend({
log: function() {
if ($.data(document, 'debug.log') == true) {
if (typeof window.console != 'undefined') {
window.console.log.apply(window.console, arguments);
} else if (typeof console != 'undefined') {
console.log.apply(this, arguments);
} else {
// do nothing
}
}
},
// like prototype.js's bind()
shove: function(fn, object) {
return function() {
return fn.apply(object, arguments);
}
},
// is val blank?
blank: function(val) {
return !val || val === '' || !val.match(/\w+/);
},
timestamp: function() {
return Math.floor(new Date().getTime()/1000);
},
togglePush: function(first, second) {
var index = $.inArray(second, first);
index != -1 ?
first.splice(index, 1) :
first.push(second)
return first;
},
// backport param from trunk
param: function( a ) {
var s = [],
param_traditional = jQuery.param.traditional;
function add( key, value ){
// If value is a function, invoke it and return its value
value = jQuery.isFunction(value) ? value() : value;
s[ s.length ] = encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
// If an array was passed in, assume that it is an array
// of form elements
if ( jQuery.isArray(a) || a.jquery )
// Serialize the form elements
jQuery.each( a, function() {
add( this.name, this.value );
});
else
// Encode parameters from object, recursively. If
// jQuery.param.traditional is set, encode the "old" way
// (the way 1.3.2 or older did it)
jQuery.each( a, function buildParams( prefix, obj ) {
if ( jQuery.isArray(obj) )
jQuery.each( obj, function(i,v){
// Due to rails' limited request param syntax, numeric array
// indices are not supported. To avoid serialization ambiguity
// issues, serialized arrays can only contain scalar values. php
// does not have this issue, but we should go with the lowest
// common denominator
add( prefix + ( param_traditional ? "" : "[]" ), v );
});
else if ( typeof obj == "object" )
if ( param_traditional )
add( prefix, obj );
else
jQuery.each( obj, function(k,v){
buildParams( prefix ? prefix + "[" + k + "]" : k, v );
});
else
add( prefix, obj );
});
// Return the resulting serialization
return s.join("&").replace('%20', "+");
}
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment