-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
jConfirm.min.js
executable file
·11 lines (11 loc) · 7.64 KB
/
jConfirm.min.js
1
2
3
4
5
6
7
8
9
10
11
/*
jConfirm
https://github.com/HTMLGuyLLC/jConfirm
Made with love by HTMLGuy, LLC
https://htmlguy.com
MIT Licensed
*/
(function(a){a.fn.jConfirm=function(b){var c=a(this);if(1<this.length)return this.each(function(){a(this).jConfirm(b)}),this;if("undefined"==typeof this||1!==this.length)return this;b=a.extend({},a.jConfirm.defaults,b,c.data()),b.class+=" jc-"+b.theme+"-theme",b.class+=" jc-"+b.size;let d={dom:this,dom_wrapped:c,position_debug:b.position_debug,follow_href:b.follow_href,open_new_tab:b.open_new_tab,hide_on_click:b.hide_on_click,question:b.question,theme:b.theme,class:b.class,backdrop:b.backdrop,btns:b.btns,confirm_text:b.confirm_text,deny_text:b.deny_text,show_deny_btn:b.show_deny_btn,position:b.position,show_now:b.show_now,dataAttr:"jConfirm",createTooltipHTML:function(){d.btns||(d.btns=[{text:d.confirm_text,class:-1<d.theme.indexOf("bootstrap")?"btn btn-success":"jc-button-highlight",event:"confirm"}],d.show_deny_btn&&d.btns.push({text:d.deny_text,class:-1<d.theme.indexOf("bootstrap")?"btn btn-secondary":"",event:"deny"}));let b=`<div class='jc-tooltip ${d.class}' role='tooltip'>
<div class='jc-arrow'></div>`;return d.question&&0<d.question.length&&(b+="<div class='jc-question'>"+d.question+"</div>"),b+="<div class='jc-buttons-wrap'>",a.each(d.btns,function(c,d){b+=`<div class='jc-button-wrap'>
<a href='#' data-event='${d.event}' class='jc-button ${d.class}'`,"object"==typeof d.data&&a.each(d.data,function(a,c){b+=` data-${a}="${c}"`}),b+=`>${d.text}</a>
</div>`}),b+="</div></div>",b},createBackdropHTML:function(){return!!d.backdrop&&`<div class='jc-backdrop jc-${d.backdrop}-backdrop'></div>`},destroy:function(){const b=d.dom_wrapped.data(d.dataAttr);"undefined"!=typeof b&&null!==b&&(b.dom_wrapped.off("touchstart mousedown",b.toggleTooltipHandler),b.dom_wrapped.off("click",b.preventDefaultHandler),a(window).off("resize",b.onResize),b.isVisible()&&b.hide(),b.dom_wrapped.data(b.dataAttr,null))},initialize:function(){return d.dom_wrapped.on("touchstart mousedown",d.toggleTooltipHandler),d.dom_wrapped.on("click",d.preventDefaultHandler),d.dom_wrapped.data(d.dataAttr,d),d.dom},preventDefaultHandler:function(a){return a.preventDefault(),!1},toggleTooltipHandler:function(a){return a.preventDefault(),d.isVisible()?d.hide():d.show(),!1},show:function(b){if(d.isVisible())return!1;null!==a.jConfirm.current&&a.jConfirm.current.hide();const c=a("body");"blurred"===d.backdrop?c.addClass("jc-blurred-body"):d.backdrop&&c.append(d.createBackdropHTML()),c.append(d.createTooltipHTML()),d.tooltip=a(".jc-tooltip"),d.attachTooltipHandlers(),d.positionTooltip(),a(window).on("resize",d.onResize),d.hide_on_click&&a(document).on("touchstart mousedown",d.onClickOutside);const e="jconfirm"+Date.now();d.tooltip.attr("id",e),d.dom.attr("aria-describedby",e),a.jConfirm.current={dom:d.dom,hide:d.hide},("undefined"==typeof b||b)&&d.dom.trigger("jc-show",{tooltip:d.tooltip})},isVisible:function(){return null!==a.jConfirm.current&&d.dom===a.jConfirm.current.dom},hide:function(b){return a(window).off("resize",d.onResize),d.hide_on_click&&a(document).off("touchstart mousedown",d.onClickOutside),d.dom.attr("aria-describedby",null),d.tooltip.remove(),"blurred"===d.backdrop?a("body").removeClass("jc-blurred-body"):d.backdrop&&a(".jc-backdrop").remove(),a.jConfirm.current=null,("undefined"==typeof b||b)&&d.dom.trigger("jc-hide"),d.dom},onResize:function(){d.hide(!1),d.show(!1)},onClickOutside:function(b){const c=a(b.target);c.hasClass("jc-tooltip")||c.parents(".jc-tooltip:first").length||d.hide()},attachTooltipHandlers:function(){d.tooltip.find(".jc-button").on("click",function(b){b.preventDefault();const c=a(this);return d.dom_wrapped.trigger(c.data("event"),c.data()),d.hide(),"confirm"===c.data("event")&&d.follow_href&&(d.open_new_tab?window.open(d.dom_wrapped.prop("href"),"_blank"):window.location.href=d.dom_wrapped.prop("href")),!1})},positionTooltip:function(){d.positionDebug("-- Start positioning --");let a=d.tooltip.find(".jc-arrow"),[b,c,e,f,g,h]=d.calculateSafePosition(d.position);return("undefined"==typeof g&&(d.positionDebug("Couldn't fit preferred position, downsizing and trying again"),d.tooltip.addClass("jc-tight-fit"),[b,c,e,f,g,h]=d.calculateSafePosition(d.position)),"undefined"==typeof g&&"auto"!==d.position&&(d.positionDebug("Couldn't fit preferred position"),[b,c,e,f,g,h]=d.calculateSafePosition("auto")),"undefined"==typeof g)?(d.positionDebug("Doesn't appear to fit. Displaying centered"),d.tooltip.addClass("jc-centered").css({top:"50%",left:"50%","margin-left":-(e/2),"margin-top":-(f/2)}),a.remove(),void d.positionDebug("-- Done positioning --")):(d.positionDebug({"Setting Position":{Left:g,Top:h}}),d.tooltip.css("left",g),d.tooltip.css("top",h),60>c&&(d.positionDebug("Element is less than "+c+"px. Setting arrow to hug the side tighter"),b+=" jc-arrow-super-hug"),a.addClass("jc-arrow-"+b),d.positionDebug("-- Done positioning --"),d)},calculateSafePosition:function(a){let b=d.tooltip.find(".jc-arrow"),c=d.tooltip.outerWidth(),e=d.tooltip.outerHeight();if(0===d.dom_wrapped.length||!d.dom_wrapped.is(":visible"))return d.position="auto",["none",0,c,e];let f=d.dom_wrapped.offset(),g=d.dom_wrapped.outerHeight(),h=d.dom_wrapped.outerWidth(),i=parseInt(d.dom_wrapped.css("marginLeft").replace("px","")),j=parseInt(d.dom_wrapped.css("marginTop").replace("px",""));f.left+=i,f.top+=j;let k=document.querySelector("body").offsetWidth,l=document.querySelector("body").offsetHeight,m=b.is(":visible")?b.outerHeight():0,n=b.is(":visible")?b.outerWidth():0,o={};o.below=5<l-(e+g+f.top),o.above=5<f.top-e,o.vertical_half=5<f.top+h/2-e/2,o.right=5<k-(c+h+f.left),o.right_half=5<k-f.left-h/2-c/2,o.right_full=5<k-f.left-c,o.left=5<f.left-c,o.left_half=5<f.left+h/2-c/2,o.left_full=5<f.left-c,d.positionDebug({"Clicked Element":{Left:f.left,Top:f.top}}),d.positionDebug({"Element Dimensions":{Height:g,Width:h},"Tooltip Dimensions":{Height:e,Width:c},"Window Dimensions":{Height:l,Width:k},"Arrow Dimensions":{Height:m,Width:n}}),d.positionDebug(o);let p,q,r;return("auto"===a||"bottom"===a)&&o.below&&o.left_half&&o.right_half?(d.positionDebug("Displaying below, centered"),p="top",q=f.left-c/2+h/2,r=f.top+g+m/2):("auto"===a||"top"===a)&&o.above&&o.left_half&&o.right_half?(d.positionDebug("Displaying above, centered"),p="bottom",q=f.left-c/2+h/2,r=f.top-e-m/2):("auto"===a||"left"===a)&&o.left&&o.vertical_half?(d.positionDebug("Displaying left, centered"),p="right",q=f.left-c-n/2,r=f.top+g/2-e/2):("auto"===a||"right"===a)&&o.right&&o.vertical_half?(d.positionDebug("Displaying right, centered"),p="left",q=f.left+h+n/2,r=f.top+g/2-e/2):("auto"===a||"bottom"===a)&&o.below&&o.right_full?(d.positionDebug("Displaying below, to the right"),p="top jt-arrow-hug-left",q=f.left,r=f.top+g+m/2):("auto"===a||"bottom"===a)&&o.below&&o.left_full?(d.positionDebug("Displaying below, to the left"),p="top jt-arrow-hug-right",q=f.left+h-c,r=f.top+g+m/2):("auto"===a||"top"===a)&&o.above&&o.right_full?(d.positionDebug("Displaying above, to the right"),p="bottom jt-arrow-hug-left",q=f.left,r=f.top-e-m/2):("auto"===a||"top"===a)&&o.above&&o.left_full&&(d.positionDebug("Displaying above, to the left"),p="bottom jt-arrow-hug-right",q=f.left+h-c,r=f.top-e-m/2),[p,h,c,e,q,r]},positionDebug:function(a){return!!d.position_debug&&("object"==typeof a?console.table(a):console.log(`Position: ${a}`))}};d.destroy();var e=d.initialize();return d.show_now&&d.show(),e},a.jConfirm={},a.jConfirm.current=null,a.jConfirm.defaults={btns:!1,position_debug:!1,question:"Are you sure?",confirm_text:"Yes",deny_text:"No",follow_href:!1,open_new_tab:!1,hide_on_click:!0,position:"auto",class:"",show_deny_btn:!0,theme:"black",size:"small",backdrop:!1,show_now:!1}})(jQuery);