forked from rigon/bootpopup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootpopup.min.js
1 lines (1 loc) · 6.14 KB
/
bootpopup.min.js
1
function bootpopup(t){if(!(this instanceof bootpopup))return new bootpopup(t);var o=this;this.formid="bootpopup-form"+String(Math.random()).substr(2),this.options={title:document.title,showclose:!0,size:"normal",size_labels:"col-sm-4",size_inputs:"col-sm-8",content:[],onsubmit:"close",buttons:["close"],before:function(){},dismiss:function(){},close:function(){},ok:function(){},cancel:function(){},yes:function(){},no:function(){},complete:function(){},submit:function(t){return o.callback(o.options.onsubmit,t),!1}},this.addOptions=function(t){var i=[];for(key in t)key in this.options&&(this.options[key]=t[key]),["close","ok","cancel","yes","no"].indexOf(key)>=0&&i.push(key);return i.length>0&&("buttons"in t||(this.options.buttons=[]),i.forEach(function(t){o.options.buttons.indexOf(t)<0&&o.options.buttons.push(t)})),"string"!=typeof t.onsubmit&&(this.options.buttons.indexOf("close")>0?this.options.onsubmit="close":this.options.buttons.indexOf("ok")>0?this.options.onsubmit="ok":this.options.buttons.indexOf("yes")>0&&(this.options.onsubmit="yes")),this.options},this.setOptions=function(t){return this.options=t,this.options},this.create=function(){var t="modal-dialog";"large"==this.options.size&&(t+=" modal-lg"),"small"==this.options.size&&(t+=" modal-sm"),this.modal=$('<div class="modal fade" tabindex="-1" role="dialog" aria-labelledby="bootpopup-title"></div>'),this.dialog=$("<div></div>",{class:t,role:"document"}),this.content=$('<div class="modal-content"></div>'),this.dialog.append(this.content),this.modal.append(this.dialog),this.header=$('<div class="modal-header"></div>'),this.options.showclose&&this.header.append('<button type="button" class="bootpopup-button close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>'),this.header.append('<h4 class="modal-title" id="bootpopup-title">'+this.options.title+"</h4>"),this.content.append(this.header),this.body=$('<div class="modal-body"></div>'),this.form=$("<form></form>",{id:this.formid,class:"form-horizontal",submit:function(t){return o.options.submit(t)}}),this.body.append(this.form),this.content.append(this.body);for(var i in this.options.content){var s=this.options.content[i];switch(typeof s){case"string":this.form.append(s);break;case"object":for(var n in s){var e=s[n];for(var a in e)"function"==typeof e[a]&&(e[a]="("+e[a]+")(this)");if(INPUT_SHORTCUT_TYPES.indexOf(n)>=0&&(e.type=n,n="input"),"input"==n){"checkbox"===e.type&&void 0===e.class&&(e.class=""),e.id=void 0===e.id?"bootpopup-input"+String(Math.random()).substr(2):e.id,e.class=void 0===e.class?"form-control":e.class,e.type=void 0===e.type?"text":e.type;var c=$("<input />",e);"checkbox"===e.type&&(c=$('<div class="checkbox"></div>').append($("<label></label>").append(c).append(e.label)),e.label="");var p=$('<div class="form-group"></div>').appendTo(this.form);$("<label></label>",{for:e.id,class:"control-label "+this.options.size_labels,text:e.label}).appendTo(p);var l=$("<div></div>",{class:this.options.size_inputs});l.append(c),p.append(l)}else this.form.append($("<"+n+"></"+n+">",e))}break;default:throw"Invalid entry type"}}this.footer=$('<div class="modal-footer"></div>'),this.content.append(this.footer);for(key in this.options.buttons){var r=this.options.buttons[key],d="",u="";switch(r){case"close":d="btn-primary",u="Close";break;case"ok":d="btn-primary",u="OK";break;case"cancel":d="btn-default",u="Cancel";break;case"yes":d="btn-primary",u="Yes";break;case"no":d="btn-default",u="No"}var h=$("<button></button>",{type:"button",text:u,class:"btn "+d,"data-dismiss":"modal","data-callback":r,"data-form":this.formid,click:function(t){var i=$(t.target).data("callback");o.callback(i,t)}});switch(this.footer.append(h),r){case"close":this.btnClose=h;break;case"ok":this.btnOk=h;break;case"cancel":this.btnCancel=h;break;case"yes":this.btnYes=h;break;case"no":this.btnNo=h}}this.modal.on("hide.bs.modal",this.options.dismiss),this.modal.on("hidden.bs.modal",function(t){o.options.complete(t),o.modal.remove()}),!1===this.options.showclose&&this.modal.attr("data-backdrop","static"),$(document.body).append(this.modal)},this.show=function(){this.options.before(this),this.modal.modal()},this.data=function(){var t={},o=this.form.serializeArray();for(var i in o)t[o[i].name]=o[i].value;return t},this.callback=function(t,o){var i=this.options[t];if("function"==typeof i){var s=this.form.serializeArray(),n=i(this.data(),s,o);return this.modal.modal("hide"),n}},this.dismiss=function(){this.callback("dismiss")},this.submit=function(){this.callback("submit")},this.close=function(){this.callback("close")},this.ok=function(){this.callback("ok")},this.cancel=function(){this.callback("cancel")},this.yes=function(){this.callback("yes")},this.no=function(){this.callback("no")},this.addOptions(t),this.create(),this.show()}var INPUT_SHORTCUT_TYPES=["button","text","submit","color","url","password","hidden","file","number","email","reset","date","checkbox"];bootpopup.alert=function(t,o,i){return"function"==typeof o&&(i=o),"string"!=typeof o&&(o=document.title),"function"!=typeof i&&(i=function(){}),bootpopup({title:o,content:[{p:{text:t}}],dismiss:function(){i()}})},bootpopup.confirm=function(t,o,i){"function"==typeof o&&(i=o),"string"!=typeof o&&(o=document.title),"function"!=typeof i&&(i=function(){});var s=!1;return bootpopup({title:o,showclose:!1,content:[{p:{text:t}}],buttons:["no","yes"],yes:function(){s=!0},dismiss:function(){i(s)}})},bootpopup.prompt=function(t,o,i,s,n){var e=function(){};"function"==typeof o&&(e=o),"function"==typeof i&&(e=i),"function"==typeof s&&(e=s),"function"==typeof n&&(e=n),"object"==typeof t&&(s=i,i=o,o=null),"string"!=typeof i&&(i="Please, provide values for:"),"string"!=typeof s&&(s=document.title);var a=[{p:{text:i}}];return"object"==typeof t?t.forEach(function(t){"string"!=typeof t?("string"!=typeof t.name&&(t.name=t.label.toLowerCase().replace(/\s+/g,"-")),"string"!=typeof t.type&&(t.type="text"),a.push({input:t})):a.push(t)}):("string"!=typeof o&&(o="text"),a.push({input:{type:o,name:"value",label:t}}),callback_tmp=e,e=function(t){callback_tmp(t.value)}),bootpopup({title:s,content:a,buttons:["cancel","ok"],ok:function(t){e(t)}})},"function"==typeof define&&define(["jquery","bootstrap"],function(){return bootpopup});