-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathform-submitter.min.js
executable file
·6 lines (6 loc) · 2.6 KB
/
form-submitter.min.js
1
2
3
4
5
6
/*! Form Submitter v1.1 | ©2013-2016 GestiXi | Licensed under the MIT license */
!function(f){f.fn.formSubmitter=function(a){return this.each(function(){var c=f(this),b="FORM"===c[0].tagName?c.parent().find("form"):c.find("form"),e=b.data("formSubmitter");if(e)console.warn("formSubmitter is already handling the form");else{var d=f.extend({},f.fn.formSubmitter.defaults,"object"==typeof a&&a);b.data("formSubmitter",e=new g(this,b,d));b.find("input[type=submit]").on("click touchstart",function(){b.find("input[type=submit]").removeAttr("clicked");f(this).attr("clicked","true")});
b.on("submit",function(a){a.preventDefault();if(d.willSend.call(e,a)){var b=f("input[type=submit][clicked=true]");b.prop("disabled",!0);e.clickedButton=b;e.handler(a);d.didSend.call(e,a)}})}})};f.fn.formSubmitter.defaults={willSend:function(a){return!0},formSettings:function(a){a=this.$form;return{method:a.attr("method"),url:a.attr("action"),data:a.serialize()}},didSend:function(a){},willReceive:function(a){return JSON.parse(a)},didReceive:function(a,c){},notificationDelay:8E3,notificationLayout:"position:fixed;top:10px;left:10px;width:350px;z-index:1000;"};
var g=function(a,c,b){this.element=a;this.$form=c;this.options=b};f.fn.formSubmitter.Constructor=g;g.prototype={constructor:g,element:null,$form:null,options:null,handler:function(a){var c=this,b=this.options;a=b.formSettings.call(this,a);f.ajax(a).done(function(a){var d=c.clickedButton;d&&d.prop("disabled",!1);if(a=b.willReceive.call(c,a))d=c.handleResponse(a),b.didReceive.call(c,a,d)});return!1},handleResponse:function(a){var c=this.$form,b=a.notification,e=a.helpInline,d=a.submitForm,g=a.redirectUrl;
g&&(document.location.href=g);d&&this.submitForm(d);f(".help-inline").html("");f(".form-group").removeClass("has-error");if(e){var e=JSON.parse(e),h;for(h in e)d=c.find(".help-inline."+h),d.html(e[h]),d.closest(".form-group").addClass("has-error")}b&&this.notify(b);return a},notify:function(a){var c=this.options.notificationDelay;f("#notification-container").length||f('<div id="notification-container" style="'+this.options.notificationLayout+'"></div>').appendTo("body");var b=f(a);b.hide().appendTo("#notification-container").slideDown().delay(c).fadeOut(400,
function(){b.remove()})},submitForm:function(a){a=JSON.parse(a);var c=document.createElement("form");c.setAttribute("method",a.method||"post");c.setAttribute("action",a.action);for(var b=0;b<a.inputs.length;b++){var e=a.inputs[b],d=document.createElement("input");d.setAttribute("type",e.type||"hidden");d.setAttribute("name",e.name);d.setAttribute("value",e.value);c.appendChild(d)}document.body.appendChild(c);c.submit()}}}(window.jQuery);