255 lines
8.8 KiB
JavaScript
255 lines
8.8 KiB
JavaScript
/*try {
|
|
window.$ = window.jQuery = require('jquery');
|
|
} catch (e) {}
|
|
|
|
*/
|
|
|
|
//-----------------------------------------------
|
|
(function($){
|
|
|
|
var opts;
|
|
|
|
/**
|
|
* ajaxdataform Jquery object
|
|
* @param {Object} options
|
|
*
|
|
* json response
|
|
* {
|
|
* errors : { 'email' : '', 'username' : '' },
|
|
* data : { 'data1' : 'xy' },
|
|
* msg : 'message text',
|
|
* success : 1
|
|
* }
|
|
*
|
|
*/
|
|
$.fn.ajaxdataform = function(options)
|
|
{
|
|
// build main options
|
|
opts = $.extend({}, $.fn.ajaxdataform.defaults, options);
|
|
|
|
return this.each(function()
|
|
{
|
|
var mainform=this;
|
|
|
|
|
|
var target=opts.target;
|
|
var inserttype=opts.inserttype;
|
|
var data=opts.data;
|
|
var ajaxtype=opts.ajaxtype;
|
|
var datatype=opts.datatype;
|
|
var buttonselector=opts.buttonselector;
|
|
var buttoncontainer=opts.buttoncontainer;
|
|
var updateinputselector=opts.updateinputselector;
|
|
var formmsgselector=opts.formmsgselector;
|
|
var beforepost=opts.beforepost;
|
|
var afterpost=opts.afterpost;
|
|
var scrolltoerror=opts.scrolltoerror;
|
|
|
|
|
|
var button=$(this).find(buttonselector);
|
|
/*var button=$(this).find(buttoncontainer+' '+buttonselector);
|
|
var buttoncontainer=$(this).find(buttoncontainer);*/
|
|
|
|
var action=$(mainform).attr('action');
|
|
|
|
|
|
$(this).unbind('submit.ajaxdataform');
|
|
$(this).bind('submit.ajaxdataform', function(event)
|
|
{
|
|
$(button).trigger('click');
|
|
return false;
|
|
});
|
|
|
|
if( opts.submitonenter )
|
|
{
|
|
$(this).find('input[type=text],input[type=password]').unbind('keyup.ajaxdataform');
|
|
$(this).find('input[type=text],input[type=password]').bind('keyup.ajaxdataform', function(event)
|
|
{
|
|
if(event.keyCode==13) $(button).trigger('click');
|
|
});
|
|
}
|
|
|
|
//Bind------
|
|
$(button).unbind('click.ajaxdataform');
|
|
|
|
$(button).bind('click.ajaxdataform', function(event)
|
|
{
|
|
event.preventDefault();
|
|
|
|
var cnt=beforepost(mainform, this);
|
|
|
|
if(cnt==false) return;
|
|
|
|
//$(mainform).find('.rght').removeClass('error');
|
|
$(mainform).find('.field').removeClass('error');
|
|
|
|
|
|
$(mainform).addClass('ondatasend');
|
|
//$(buttoncontainer).find(formmsgselector).html( '' );
|
|
|
|
$(mainform).find(formmsgselector).removeClass('red');
|
|
$(mainform).find(formmsgselector).html( '' );
|
|
|
|
var adddata=data();
|
|
|
|
var buttonrel=$(this).attr('rel');
|
|
|
|
if(buttonrel)
|
|
adddata+='&option='+buttonrel;
|
|
|
|
$.ajax({
|
|
async: true,
|
|
type: ajaxtype,
|
|
url: action,
|
|
data: $(mainform).serialize()+adddata,
|
|
dataType: datatype,
|
|
success: function(result)
|
|
{
|
|
if( inserttype!='' && datatype=='html' )
|
|
{
|
|
switch(inserttype)
|
|
{
|
|
case 'innerhtml' : $(target).html(result); break;
|
|
case 'append' : $(target).append(result); break;
|
|
case 'replace' : $(target).replaceWith(result); break;
|
|
case 'after' : $(target).after(result); break;
|
|
case 'before' : $(target).before(result); break;
|
|
}
|
|
}
|
|
|
|
if(datatype=='json')
|
|
{
|
|
$(mainform).find('.field .status').html('<span class="ico ok">Ok</span>');
|
|
|
|
if( !result || typeof(result)!='object' )
|
|
{
|
|
alert('Server response error');
|
|
}
|
|
else
|
|
{
|
|
if(result.success==0)
|
|
{
|
|
for (idx in result.errors)
|
|
{
|
|
var value=result.errors[idx];
|
|
|
|
$(mainform).find('.field_'+idx+' .msg').html(value);
|
|
$(mainform).find('.field_'+idx).addClass('error');
|
|
|
|
$(mainform).find('.field_'+idx+' .status').html('<span class="ico error">Error</span>');
|
|
}
|
|
|
|
if( scrolltoerror && $(mainform).find('.field.error').length>0)
|
|
{
|
|
$('body,html').animate({scrollTop: $(mainform).find('.field.error:first').offset().top }, 500);
|
|
//$(document).scrollTo( $(mainform).find('.field.error:first'), 500, {axis:'y'} );
|
|
}
|
|
|
|
$(mainform).find(formmsgselector).addClass('red');
|
|
}
|
|
else
|
|
{
|
|
if( updateinputselector && result.data.update )
|
|
{
|
|
$(mainform).find(updateinputselector).val(result.data.update);
|
|
}
|
|
|
|
if( result.data.cre_nick ) $(mainform).find('.cre_nick').html(result.data.cre_nick);
|
|
if( result.data.cre_datum ) $(mainform).find('.cre_datum').html(result.data.cre_datum);
|
|
if( result.data.mod_nick ) $(mainform).find('.mod_nick').html(result.data.mod_nick);
|
|
if( result.data.mod_datum ) $(mainform).find('.mod_datum').html(result.data.mod_datum);
|
|
}
|
|
|
|
//$(buttoncontainer).find(formmsgselector).html( result.msg );
|
|
$(mainform).find(formmsgselector).html( result.msg );
|
|
}
|
|
}
|
|
|
|
afterpost(result, mainform);
|
|
|
|
$(mainform).removeClass('ondatasend');
|
|
},
|
|
error: function(xhr, ajaxOptions, thrownError) {
|
|
if (typeof window.ajaxErrorHandler === 'function') {
|
|
window.ajaxErrorHandler(xhr, ajaxOptions, thrownError);
|
|
} else {
|
|
alert(thrownError);
|
|
}
|
|
}
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
//-----------------------------------------------
|
|
$.fn.ajaxdataform.defaults =
|
|
{
|
|
target : '',
|
|
inserttype : '', // [innerhtml, append, replace, after, before]
|
|
ajaxtype : 'POST', //[GET, POST]
|
|
datatype : 'html', // [html, json]
|
|
data : function() { return ''; },
|
|
buttonselector : '.submit',
|
|
buttoncontainer : '.cnt_button',
|
|
formmsgselector : '.formmsg',
|
|
submitonenter : false,
|
|
scrolltoerror : true,
|
|
updateinputselector : 'input[name=update]',
|
|
beforepost : function () {},
|
|
afterpost : function () {}
|
|
};
|
|
|
|
|
|
})(jQuery);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------
|
|
/** @projectDescription jQuery Serialize Anything - Serialize anything (and not just forms!)
|
|
* @author Bramus! (Bram Van Damme)
|
|
* @version 1.0
|
|
* @website: http://www.bram.us/
|
|
* @license : BSD
|
|
*/
|
|
|
|
(function($) {
|
|
|
|
$.fn.serializeAnything = function(type) {
|
|
|
|
var toReturn = [];
|
|
var els = $(this).find(':input').get();
|
|
|
|
if(type==null) type='array';
|
|
|
|
$.each(els, function() {
|
|
if (this.name && !this.disabled && (this.checked || /select|textarea/i.test(this.nodeName) || /text|hidden|password/i.test(this.type)))
|
|
{
|
|
var val = $(this).val();
|
|
|
|
if( type=='string' )
|
|
toReturn.push( encodeURIComponent(this.name) + "=" + encodeURIComponent( val ) );
|
|
|
|
else if( type=='array' )
|
|
{
|
|
var iobj={ name : this.name, value : val }
|
|
toReturn.push( iobj );
|
|
}
|
|
}
|
|
});
|
|
|
|
if( type=='string' )
|
|
return toReturn.join("&").replace(/%20/g, "+");
|
|
if( type=='array' )
|
|
return toReturn;
|
|
|
|
|
|
}
|
|
|
|
})(jQuery);
|
|
|