jAlert.min.js 12.6 KB
!function($){Date.now||(Date.now=function(){return+new Date}),$.fn.jAlert=function(options){$("body").focus(),$("body").blur();var themes=["default","green","dark_green","red","dark_red","black","brown","gray","dark_gray","blue","dark_blue","yellow"],sizes=["xsm","sm","md","lg","xlg","full","auto"],sizeAliases={xsmall:"xsm",small:"sm",medium:"md",large:"lg",xlarge:"xlg"},backgroundColors=["white","black"],styles=[],classes=["animated"],backgroundClasses=[];if(this.length>1)return this.each(function(){$.fn.jAlert(options)}),this;if("undefined"!=typeof $(this)[0]&&"undefined"!=$(this)[0].jAlert)return $(this)[0].jAlert;if($.each($.fn.jAlert.defaults,function(key,val){var lowerKey=key.toLowerCase();"undefined"!=typeof options[lowerKey]&&(options[key]=options[lowerKey])}),options=$.extend({},$.fn.jAlert.defaults,options),options.id)var alert_id=options.id;else var unique=Date.now().toString()+Math.floor(1e5*Math.random()),alert_id="ja_"+unique;var alert={set:function(key,val){return alert[key]=val,alert},__set:function(key,val){return alert.set(key,val)},get:function(key){return alert[key]},__get:function(key){return alert.get(key)},centerAlert:function(){var viewportHeight=$(window).height(),alertHeight=alert.instance.height(),diff=viewportHeight-alertHeight,margin=diff/2;return margin=margin>200?margin-100:margin,margin=margin<=0?0:margin,margin-=1,alert.instance.css("margin-top",margin+"px"),alert.instance.css("margin-bottom","0px"),$("body").css("overflow","hidden"),diff>5?alert.instance.parents(".ja_wrap").css("position","fixed"):(alert.instance.parents(".ja_wrap").css("position","absolute"),$("html, body").animate({scrollTop:top-50},200)),alert},animateAlert:function(which){return"hide"==which?(alert.instance.jAlert().blurBackground&&$("body").removeClass("ja_blur"),alert.instance.removeClass(alert.showAnimation).addClass(alert.hideAnimation)):(alert.instance.jAlert().blurBackground&&$("body").addClass("ja_blur"),alert.centerAlert(),alert.instance.addClass(alert.showAnimation).removeClass(alert.hideAnimation).show()),alert},closeAlert:function(remove,onClose){return 0!=remove&&(remove=!0),alert.instance&&(alert.instance.unbind("DOMSubtreeModified"),alert.animateAlert("hide"),window.setTimeout(function(){var alertWrap=alert.instance.parents(".ja_wrap");remove?alertWrap.remove():alertWrap.hide(),"function"==typeof onClose?onClose(alert.instance):"function"==typeof alert.onClose&&alert.onClose(alert.instance),$(".jAlert:visible").length>0?$(".jAlert:visible:last").jAlert().centerAlert():$("body").css("overflow","auto")},alert.animationTimeout)),alert},showAlert:function(replaceOthers,removeOthers,onOpen,onClose){0!=replaceOthers&&(replaceOthers=!0),removeOthers!==!1&&(removeOthers=!0),replaceOthers&&$(".jAlert:visible").jAlert().closeAlert(removeOthers);var wrap=alert.instance.parents(".ja_wrap");return $("body").append(wrap),alert.animateAlert("show"),"function"==typeof onClose&&(alert.onClose=onClose),window.setTimeout(function(){"function"==typeof onOpen&&onOpen(alert.instance)},alert.animationTimeout),alert}};if($.each(options,function(key,val){alert.set(key,val)}),alert.set("id",alert_id),alert.content&&0===alert.content.indexOf("#")&&$(alert.content).length>0&&(alert.content=$(alert.content).html()),alert.video&&alert.video.indexOf("youtube.com/watch?v=")>-1&&alert.video.indexOf("embed")===-1&&(alert.video=alert.video.replace("watch?v=","embed/")),"confirm"==alert.type&&(alert.content||(alert.content=alert.confirmQuestion),alert.btns=[{text:alert.confirmBtnText,theme:"green",class:"confirmBtn",closeAlert:!0,onClick:alert.onConfirm},{text:alert.denyBtnText,theme:"red",class:"denyBtn",closeAlert:!0,onClick:alert.onDeny}],alert.autofocus=alert.confirmAutofocus),alert.color&&(alert.theme=alert.color),$.inArray(alert.theme,themes)==-1)return console.log("jAlert Config Error: Invalid theme selection."),!1;if(classes.push("ja_"+alert.theme),alert.class&&classes.push(alert.class),alert.classes&&classes.push(alert.classes),alert.fullscreen&&classes.push("ja_fullscreen"),alert.noPadContent&&classes.push("ja_no_pad"),alert.title||classes.push("ja_noTitle"),alert.width&&(alert.size=alert.width),alert.size&&"object"==typeof alert.size&&("undefined"==typeof alert.size.width||"undefined"==typeof alert.size.height))return console.log("jAlert Config Error: Invalid size selection (try a preset or make sure you're including height and width in your size object)."),!1;if(alert.size?"object"==typeof alert.size?(styles.push("width: "+alert.size.width+";"),styles.push("height: "+alert.size.height+";"),classes.push("ja_setheight")):("undefined"!=typeof sizeAliases[alert.size]&&(alert.size=sizeAliases[alert.size]),$.inArray(alert.size,sizes)>-1?classes.push("ja_"+alert.size):styles.push("width: "+alert.size+";")):classes.push("ja_sm"),$.inArray(alert.backgroundColor,backgroundColors)==-1)return console.log("jAlert Config Error: Invalid background color selection."),!1;backgroundClasses.push("ja_wrap_"+alert.backgroundColor),alert.onOpen=[alert.onOpen];var onload="onload='$.fn.jAlert.mediaLoaded($(this))'",loader="<div class='ja_loader'>Loading...</div>";alert.picture&&(alert.image=alert.picture),alert.image?(alert.content="<div class='ja_media_wrap'>"+loader+"<img src='"+alert.image+"' class='ja_img' "+onload+"'",alert.imageWidth&&(alert.content+=" style='width: "+alert.imageWidth+"'"),alert.content+="></div>"):alert.video?(alert.content="<div class='ja_media_wrap'>"+loader+"<div class='ja_video'></div></div>",alert.onOpen.unshift(function(elem){var iframe=document.createElement("iframe");iframe.src=elem.jAlert().video,iframe.addEventListener?iframe.addEventListener("load",function(){$.fn.jAlert.mediaLoaded($(this))},!0):iframe.attachEvent?iframe.attachEvent("onload",function(){$.fn.jAlert.mediaLoaded($(this))}):iframe.onload=function(){$.fn.jAlert.mediaLoaded($(this))},elem.find(".ja_video").append(iframe)})):alert.iframe?(alert.iframeHeight||(alert.iframeHeight=$(window).height()+"px"),alert.content="<div class='ja_media_wrap'>"+loader+"</div>",alert.onOpen.unshift(function(elem){var iframe=document.createElement("iframe");iframe.src=elem.jAlert().iframe,iframe.className="ja_iframe",iframe.addEventListener?iframe.addEventListener("load",function(){$.fn.jAlert.mediaLoaded($(this))},!0):iframe.attachEvent?iframe.attachEvent("onload",function(){$.fn.jAlert.mediaLoaded($(this))}):iframe.onload=function(){$.fn.jAlert.mediaLoaded($(this))},elem.find(".ja_media_wrap").append(iframe)})):alert.ajax&&(alert.content="<div class='ja_media_wrap'>"+loader+"</div>",onAjaxCallbacks=alert.onOpen,alert.onOpen=[function(elem){$.ajax(elem.jAlert().ajax,{async:!0,complete:function(jqXHR,textStatus){elem.find(".ja_media_wrap").replaceWith(jqXHR.responseText),$.each(onAjaxCallbacks,function(index,onAjax){onAjax(elem)})},error:function(jqXHR,textStatus,errorThrown){alert.onAjaxFail(elem,"Error getting content: Code: "+jqXHR.status+" : Msg: "+jqXHR.statusText)}})}]);var getBtnHTML=function(btn){if("undefined"==typeof btn.href&&(btn.href=""),"undefined"==typeof btn.class&&(btn.class=""),"undefined"==typeof btn.theme?btn.class+=" ja_btn_default":btn.class+=" ja_btn_"+btn.theme,"undefined"==typeof btn.text&&(btn.text=""),"undefined"==typeof btn.id){var unique=Date.now().toString()+Math.floor(1e5*Math.random());btn.id="ja_btn_"+unique}return"undefined"==typeof btn.target&&(btn.target="_self"),"undefined"==typeof btn.closeAlert&&(btn.closeAlert=!0),$("body").off("click","#"+btn.id),$("body").on("click","#"+btn.id,function(e){var button=$(this);btn.closeAlert&&button.parents(".jAlert").jAlert().closeAlert();var callbackResponse=!0;return"function"==typeof btn.onClick&&(callbackResponse=btn.onClick(e,button)),!callbackResponse||btn.closeAlert?(e.preventDefault(),!1):callbackResponse}),"<a href='"+btn.href+"' id='"+btn.id+"' target='"+btn.target+"' class='ja_btn "+btn.class+"'>"+btn.text+"</a> "},addAlert=function(content){var html="";html+='<div class="ja_wrap '+backgroundClasses.join(" ")+'"><div class="jAlert '+classes.join(" ")+'" style="'+styles.join(" ")+'" id="'+alert.id+'"><div>',alert.closeBtn&&(html+="<div class='closejAlert ja_close",alert.closeBtnAlt?html+=" ja_close_alt":alert.closeBtnRoundWhite?html+=" ja_close_round_white":alert.closeBtnRound&&(html+=" ja_close_round"),html+="'>&times;</div>"),alert.title&&(html+="<div class='ja_title'><div>"+alert.title+"</div></div>"),html+='<div class="ja_body">'+content,alert.btns&&(html+='<div class="ja_btn_wrap ',alert.btnBackground&&(html+="optBack"),html+='">'),"object"==typeof alert.btns[0]?$.each(alert.btns,function(index,btn){"object"==typeof btn&&(html+=getBtnHTML(btn))}):"object"==typeof alert.btns?html+=getBtnHTML(alert.btns):alert.btns&&console.log("jAlert Config Error: Incorrect value for btns (must be object or array of objects): "+alert.btns),alert.btns&&(html+="</div>"),html+="</div></div></div></div>";var alertHTML=$(html);if(alert.replaceOtherAlerts){var alerts=$(".jAlert:visible");alerts.each(function(){$(this).jAlert().closeAlert()})}return $("body").append(alertHTML),alert.instance=$("#"+alert.id),alert.instance[0].jAlert=alert,$("body").css("overflow","hidden"),alert.animateAlert("show"),alert.closeBtn&&alert.instance.on("click",".closejAlert",function(e){return e.preventDefault(),$(this).parents(".jAlert:first").jAlert().closeAlert(),!1}),alert.closeOnClick&&($(document).off("mouseup touchstart",$.fn.jAlert.onMouseUp),$(document).on("mouseup touchstart",$.fn.jAlert.onMouseUp)),alert.closeOnEsc&&($(document).off("keydown",$.fn.jAlert.onEscKeyDown),$(document).on("keydown",$.fn.jAlert.onEscKeyDown)),alert.onOpen&&$.each(alert.onOpen,function(index,onOpen){onOpen(alert.instance)}),alert.autofocus?alert.instance.find(alert.autofocus).focus():alert.instance.focus(),alert.instance.bind("DOMSubtreeModified",function(){alert.centerAlert()}),alert.instance};return this.initialize=function(){return alert.content||alert.image||alert.video||alert.iframe||alert.ajax?(alert.content||(alert.content=""),addAlert(alert.content)):(console.log("jAlert potential error: No content defined"),addAlert(""))},this.initialize(),alert},$.fn.jAlert.defaults={title:!1,content:!1,noPadContent:!1,fullscreen:!1,image:!1,imageWidth:"auto",video:!1,ajax:!1,onAjaxFail:function(alert,errorThrown){alert.jAlert().closeAlert(),errorAlert(errorThrown)},iframe:!1,iframeHeight:!1,class:"",classes:"",id:!1,showAnimation:"fadeInUp",hideAnimation:"fadeOutDown",animationTimeout:600,theme:"default",backgroundColor:"black",blurBackground:!1,size:!1,replaceOtherAlerts:!1,closeOnClick:!1,closeOnEsc:!0,closeBtn:!0,closeBtnAlt:!1,closeBtnRound:!0,closeBtnRoundWhite:!1,btns:!1,btnBackground:!0,autofocus:!1,onOpen:function(alert){return!1},onClose:function(alert){return!1},type:"modal",confirmQuestion:"Are you sure?",confirmBtnText:"Yes",denyBtnText:"No",confirmAutofocus:".confirmBtn",onConfirm:function(e,btn){return e.preventDefault(),console.log("confirmed"),!1},onDeny:function(e,btn){return e.preventDefault(),!1}},$.fn.jAlert.onMouseUp=function(e){var target=e.target?e.target:e.srcElement,lastVisibleAlert=$(".jAlert:visible:last");lastVisibleAlert.length>0&&lastVisibleAlert.jAlert().closeOnClick&&($(target).is(".jAlert *")||lastVisibleAlert.jAlert().closeAlert())},$.fn.jAlert.onEscKeyDown=function(e){if(27===e.keyCode){var lastVisibleAlert=$(".jAlert:visible:last");lastVisibleAlert.length>0&&lastVisibleAlert.jAlert().closeOnEsc&&lastVisibleAlert.jAlert().closeAlert()}},$.fn.attachjAlert=function(e){return e.preventDefault(),$.jAlert($(this).data()),!1},$.jAlert=function(options){if("current"==options){var latest=$(".jAlert:visible:last");return latest.length>0&&latest.jAlert()}return"attach"==options?($("[data-jAlert]").off("click",$.fn.attachjAlert),$("[data-jAlert]").on("click",$.fn.attachjAlert),$("[data-jalert]").off("click",$.fn.attachjAlert),$("[data-jalert]").on("click",$.fn.attachjAlert),!1):$.fn.jAlert(options)},$.fn.alertOnClick=function(options){$(this).on("click",function(e){return e.preventDefault(),$.jAlert(options),!1})},$.alertOnClick=function(selector,options){$("body").on("click",selector,function(e){return e.preventDefault(),$.jAlert(options),!1})},$.fn.closeAlert=function(remove,onClose){$(this).jAlert().closeAlert(remove,onClose)};var $jAlertResizeTimeout;$(window).resize(function(){window.clearTimeout($jAlertResizeTimeout),$jAlertResizeTimeout=window.setTimeout(function(){$(".jAlert:visible").each(function(){$(this).jAlert().centerAlert()})},200)}),$.fn.jAlert.mediaLoaded=function(elem){var wrap=elem.parents(".ja_media_wrap"),vid_wrap=wrap.find(".ja_video"),alert=elem.parents(".jAlert:first"),jalert=alert.jAlert();wrap.find(".ja_loader").remove(),vid_wrap.length>0?vid_wrap.fadeIn("fast"):elem.fadeIn("fast"),"undefined"!=typeof jalert.iframeHeight&&jalert.iframeHeight&&(elem.css("display","block"),elem.height(jalert.iframeHeight)),jalert.centerAlert()}}(jQuery);