Skip to content

Instantly share code, notes, and snippets.

@zachariahtimothy
Created December 11, 2012 16:52
Show Gist options
  • Save zachariahtimothy/4260229 to your computer and use it in GitHub Desktop.
Save zachariahtimothy/4260229 to your computer and use it in GitHub Desktop.
(function( $ ){
var rinput = /^(?:color|date|datetime|datetime-local|email|hidden|month|number|password|range|search|tel|text|time|url|week)$/i,
rCRLF = /\r?\n/g,
rselectTextarea = /^(?:select|textarea)/i;
$.prototype.serialize = function(args){
var options = args || {};
return this.map(function(){
return this.elements ? jQuery.makeArray( this.elements ) : this;
})
.filter(function(){
return this.name && !this.disabled &&
( this.checked || rselectTextarea.test( this.nodeName ) ||
rinput.test( this.type ) );
})
.map(function( i, elem ){
var $elem = $(this);
var val = $elem.val();
if (options.ignoreEmpty && !val){
return false;
}
var hasChanged = (function(nodeName){
switch (nodeName){
case 'INPUT':
return $elem.wayinInputBox('hasChanged');
break;
case 'TEXTAREA':
return $elem.wayinTextarea('hasChanged');
break;
}
})(elem.nodeName);
return val == null ?
null :
$.isArray( val ) ?
$.map( val, function( val, i ){
return { name: elem.name, value: val.replace( rCRLF, "\r\n" ), hasChanged: hasChanged };
}) :
{ name: elem.name, value: val.replace( rCRLF, "\r\n" ), hasChanged: hasChanged };
}).get();
};
var WayinInputBox = {
defaults: {
labelPosition:'outside',
showMaxVal: true
},
init: function(options){
return this.each(function(){
var $input = $(this),
data = $input.data('wayinInputBox'),
settings = $.extend(WayinInputBox.defaults, options);
var label = $input.prev('label');
if (settings.labelPosition === 'inside'){
$input.attr('placeholder', label.text());
label.remove();
}
if (!data){
}
$input.data('initialValue', $input.val());
});
},
hasChanged: function(){
return $(this).data('initialValue') !== $(this).val();
}
};
var WayinRadio = {
defaults: {
},
init: function(options){
return this.each(function(){
var $radio = $(this);
$radio.data('initialValue', $radio.val());
});
}
};
var WayinCheckBox = {
defaults: {
},
init: function(options){
return this.each(function(){
var $checkbox = $(this);
$checkbox.data('initialValue', $checkbox.val());
});
}
};
var WayinTextarea = {
defaults: {
},
init: function(options){
return this.each(function(){
var $textarea = $(this);
$textarea.data('initialValue', $textarea.val());
});
}
};
var WayinSelectList = {
defaults: {
},
init: function(options){
return this.each(function(){
var $select = $(this);
$select.data('initialValue', $select.val());
});
}
};
var WayinForm = {
defaults: {
version: '1.0'
},
inputs: function(){
},
init : function( options ) {
return this.each(function(){
var $form = $(this),
data = $form.data('wayinForm'),
settings = $.extend(WayinForm.defaults, options);
$form.addClass('wayinForm');
$('input:not(:checkbox,:radio)', $form).wayinInputBox();
$('input:checkbox', $form).wayinCheckBox();
$('input:radio', $form).wayinRadio();
$('textarea', $form).wayinTextarea();
$('select', $form).wayinSelectList();
if (!data) {
$form.data('wayinForm', {
target: $form,
settings: settings
});
}
});
},
destroy : function( ) {
return this.each(function(){
var $form = $(this),
data = $form.data('wayinForm');
// Namespacing FTW
$(window).unbind('.wayinForm');
data.wayinForm.remove();
$form.removeData('wayinForm');
});
}
};
$.fn.wayinInputBox = function( method ) {
if ( WayinInputBox[method] ) {
return WayinInputBox[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return WayinInputBox.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.wayinInputBox' );
}
};
$.fn.wayinCheckBox = function( method ) {
if ( WayinCheckBox[method] ) {
return WayinCheckBox[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return WayinCheckBox.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.wayinCheckBox' );
}
};
$.fn.wayinRadio = function( method ) {
if ( WayinRadio[method] ) {
return WayinRadio[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return WayinRadio.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.wayinRadio' );
}
};
$.fn.wayinTextarea = function( method ) {
if ( WayinTextarea[method] ) {
return WayinTextarea[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return WayinTextarea.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.wayinTextarea' );
}
};
$.fn.wayinSelectList = function( method ) {
if ( WayinSelectList[method] ) {
return WayinSelectList[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return WayinSelectList.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.wayinSelectList' );
}
};
$.fn.wayinForm = function( method ) {
if ( WayinForm[method] ) {
return WayinForm[method].apply(this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return WayinForm.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.wayinForm' );
}
};
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment