-
Notifications
You must be signed in to change notification settings - Fork 0
/
eModal.min.js
1 lines (1 loc) · 8.84 KB
/
eModal.min.js
1
!function(define){define(["jquery"],function($,root){var $modal,BIN="recycle-bin",DIV="<div>",EMPTY="",EVENT_CLICK="click",EVENT_HIDE="hide",EVENT_SHOW="shown.bs.modal",EVENT_SUBMIT="submit",FOOTER_ID="eFooter",HIDE=EVENT_HIDE+".bs.modal",INPUT="input",KEY_DANGER="danger",LABEL={OK:"Cancel",True:"False",Yes:"No"},lastParams={},MODAL_BODY="modal-body",MODAL_DIALOG=".modal-dialog",options={},REC_MODAL_CONTENT="modal-rec",SIZE={sm:"sm",lg:"lg",xl:"xl"},TMP_MODAL_CONTENT="modal-tmp",defaultSettings={allowContentRecycle:!0,confirmLabel:Object.keys(LABEL)[0],labels:LABEL,loadingHtml:'<h5>Loading...</h5><div class=progress><div class="progress-bar progress-bar-striped active" style="width: 100%"></div></div>',size:EMPTY,title:"Attention",autofocus:!1};return(root=root||{}).addLabel=addLabel,root.ajax=ajax,root.alert=alert,root.close=close,root.confirm=confirm,root.emptyBin=emptyBin,root.iframe=iframe,root.prompt=prompt,root.setId=setId,root.setEModalOptions=setEModalOptions,root.setModalOptions=setModalOptions,root.size=SIZE,root.version="1.2.67",root;function _build(message){$modal.modal(options).find(".modal-content").append(message)}function _createDeferred(){var defer,q;try{q=require("Q")}catch(e){q=window.Q}return q?defer=q.defer():(defer=$.Deferred()).promise=defer.promise(),defer.promise.element=_getModalInstance(!0).find(MODAL_DIALOG),defer}function _getFooter(buttons){if(!1===buttons)return EMPTY;var messageFotter=$(DIV).addClass("modal-footer").prop("id",FOOTER_ID);if(buttons)for(var i=0,k=buttons.length;i<k;i++){var btnOp=buttons[i],btn=$("<button>").addClass("btn btn-"+(btnOp.style||"primary"));for(var index in btnOp)if(btnOp.hasOwnProperty(index))switch(index){case"close":btnOp[index]&&btn.attr("data-dismiss","modal").addClass("x");break;case EVENT_CLICK:var fn=btnOp.click.bind(_getModalInstance(!0).find(".modal-content"));btn.click(fn);break;case"text":btn.html(btnOp[index]);break;default:btn.attr(index,btnOp[index])}messageFotter.append(btn)}else messageFotter.append('<button class="x btn btn-primary" data-dismiss=modal type=button>Close</button>');return messageFotter}function _getMessage(data){var $message,content=data.loading?defaultSettings.loadingHtml:data.message||data;return content.on||content.onclick?($message=!0===data.clone?$(content).clone():$(content)).addClass(data.useBin&&!data.loading?REC_MODAL_CONTENT:TMP_MODAL_CONTENT):$message=$(DIV).attr("style","position:relative;word-wrap:break-word;").addClass(MODAL_BODY).html(content),data.css&&data.css!==$message.css&&$message.css(data.css),$message}function _getModalInstance(skipEventChageIfExists){return $modal||($("#"+BIN).length||$("body").append($(DIV).prop("id",BIN).hide()),$modal=createModalElement()),skipEventChageIfExists&&$modal?$modal:(defaultSettings.autofocus&&$modal.on(EVENT_SHOW,function(){$(this).find(INPUT).first().focus()}),$modal);function createModalElement(){return $('<div class="modal fade" tabindex="-1"><style>.modal-xl{width:96%;}.modal-body{max-height: calc(100vh - 145px);overflow-y: auto;}</style><div class=modal-dialog><div class=modal-content> <div class=modal-header><button type=button class="x close" data-dismiss=modal aria-label="Close"><span aria-hidden=true>×</span></button><h5 class=modal-title></h5></div></div></div></div>').on("hidden.bs.modal",_recycleModal).on(EVENT_CLICK,"button.x",function(ev){var btn=$(ev.currentTarget);if(btn.prop("type")!==EVENT_SUBMIT)return $modal.modal(EVENT_HIDE);try{if(btn.closest("form")[0].checkValidity())return close()}catch(e){return close()}return $modal})}}function _jQueryMinVersion(version){var $ver=$.fn.jquery.split("."),ver=version.split("."),$major=$ver[0],$minor=$ver[1],$patch=$ver[2],major=ver[0],minor=ver[1],patch=ver[2];return!(major>$major||major===$major&&minor>$minor||major===$major&&minor===$minor&&patch>$patch)}function _recycleModal(){if($modal){var $content=$modal.find("."+REC_MODAL_CONTENT).removeClass(REC_MODAL_CONTENT).appendTo("#"+BIN);$modal.off(HIDE).off(EVENT_SHOW).find(".modal-content > div:not(:first-child)").remove(),defaultSettings.allowContentRecycle&&!lastParams.clone||$content.remove()}}function _setup(params,title){if(!params)throw new Error("Invalid parameters!");_recycleModal(),lastParams=params;var $ref=_getModalInstance(),size="modal-"+(params.size||defaultSettings.size);$ref.find(MODAL_DIALOG).removeClass("modal-sm modal-lg modal-xl").addClass(params.size||defaultSettings.size?size:null),$ref.find(".modal-title").html((params.title||title||defaultSettings.title)+" ").append($("<small>").html(params.subtitle||EMPTY)),$ref.on(HIDE,params.onHide)}function addLabel(yes,no){LABEL[yes]=no}function ajax(data,title){var dfd=_createDeferred();"object"==typeof data&&setEModalOptions(data);var params={async:!0,deferred:dfd,xhrFields:{withCredentials:!1},loading:!0,title:title||defaultSettings.title,url:data.url||data,dataType:data.dataType||"text"};return data.url&&$.extend(params,data),$.ajax(params).done(ok).fail(error),alert(params,title);function ok(html){return $modal.find("."+MODAL_BODY).html(data.success?data.success(html):html),dfd.resolve($modal)}function error(responseText,textStatus){var msg=data.error?data.error(responseText,textStatus,params):'<div class="alert alert-danger"><strong>XHR Fail: </strong>URL [ '+params.url+"] load fail.</div>";return $modal.find("."+MODAL_BODY).html(msg),dfd.reject(responseText)}}function alert(data,title){_setup(data,title);var dfd=data.deferred||_createDeferred(),$message;return"object"==typeof data&&setEModalOptions(data),_build($(DIV).append(_getMessage(data),_getFooter(data.buttons))),data.async||$modal.on(EVENT_SHOW,dfd.resolve),dfd.promise}function confirm(data,title){var dfd=_createDeferred();return"object"==typeof data&&setEModalOptions(data),alert({async:!0,buttons:[{close:!0,click:click,text:LABEL[data.label]?LABEL[data.label]:LABEL[defaultSettings.confirmLabel],style:data.style&&data.style[0]||KEY_DANGER},{close:!0,click:click,text:LABEL[data.label]?data.label:defaultSettings.confirmLabel,style:data.style&&data.style[1]}],deferred:dfd,message:data.message||data,onHide:click,size:data.size,title:data.title||title});function click(ev){close();var key=$(ev.currentTarget).html();return LABEL[key]?dfd.resolve():dfd.reject()}}function iframe(params,title){var dfd=_createDeferred();"object"==typeof data&&setEModalOptions(data);var postData=params.data?Object.keys(params.data).map(mapData).join(" "):"",html='<div class=modal-body style="position: absolute;width: 100%;background-color: rgba(255,255,255,0.8);height: 100%;">%1%</div><iframe class="embed-responsive-item" sandbox="allow-same-origin allow-scripts allow-forms allow-popups allow-popups-to-escape-sandbox allow-modals allow-top-navigation" frameborder=0 src="%0%" %2% style="width:100%;height:75vh;display:block;"/>'.replace("%0%",params.message||params.url||params).replace("%1%",defaultSettings.loadingHtml).replace("%2%",postData),message=_jQueryMinVersion("3.0.0")?$(html).on("load",iframeReady):$(html).load(iframeReady);return alert({async:!0,buttons:params.buttons||!1,deferred:dfd,message:message,size:params.size||SIZE.xl,title:params.title||title});function mapData(item){return item+'="'+params.data[item]+'"'}function iframeReady(){return $(this).parent().find("div."+TMP_MODAL_CONTENT).fadeOut(function(){$(this).remove()}),dfd.resolve()}}function emptyBin(){return $("#"+BIN+" > *").remove()}function prompt(data,title){var dfd=_createDeferred();"object"==typeof data&&setEModalOptions(data);var params={deferred:dfd};if("object"==typeof data?$.extend(params,data):(params.message=data,params.title=title),params.async=!0,params.buttons)for(var btn,i=0,k=params.buttons.length;i<k;i++)(btn=params.buttons[i]).style=(btn.style||"default")+" pull-left",btn.type=btn.type||"button";var buttons=_getFooter([{close:!0,type:"reset",text:LABEL.OK,style:KEY_DANGER},{close:!1,type:EVENT_SUBMIT,text:defaultSettings.confirmLabel}].concat(params.buttons||[]));return params.buttons=!1,params.onHide=submit,params.message=$('<form role=form style="margin-bottom:0;"><div class=modal-body><label for=prompt-input class=control-label>'+(params.message||EMPTY)+'</label><input type=text class=form-control required autocomplete="on" value="'+(params.value||EMPTY)+(params.pattern?'" pattern="'+params.pattern:EMPTY)+'"></div></form>').append(buttons).on(EVENT_SUBMIT,submit),alert(params);function submit(ev){var value=$modal.find(INPUT).val();return close(),ev.type!==EVENT_SUBMIT?dfd.reject(value):dfd.resolve(value),!1}}function setId(id){_getModalInstance(!0).find(MODAL_DIALOG).prop("id",id)}function setEModalOptions(overrideOptions){return $.extend(defaultSettings,overrideOptions)}function setModalOptions(overrideOptions){return $modal&&$modal.remove(),$.extend(options,overrideOptions)}function close(){return $modal&&$modal.off(HIDE).modal(EVENT_HIDE),$modal}})}("function"==typeof define&&define.amd?define:function(args,mName){this.eModal="undefined"!=typeof module&&module.exports?mName(require(args[0],{}),module.exports):mName(window.$)});