-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.modal.min.js
29 lines (29 loc) · 12.1 KB
/
jquery.modal.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*
* jQuery Chaos Modal
* By Matthew Sigley
* Version 1.16.0
*/
(function(a){function v(c){cancelAnimationFrame(a.chaosModalResizeFrame);a.chaosModalCurrent&&(a.chaosModalResizeFrame=requestAnimationFrame(u))}function u(){a.chaosModalCurrent.resizeModal()}function g(c){if(c&&500>Date.now()-a.openTime)return!1;cancelAnimationFrame(a.chaosModalResizeFrame);a(document).off(".chaosmodal");null!=a.chaosModalCurrent?a.chaosModalCurrent.closeModal():(a(window).trigger("chaos-modal-preprocess-interrupt"),a("#chaos-modal-mask").hide().remove(),a.fn.closeLoading());a.modalMask=
null}function l(c){c.preventDefault();a.chaosModalCurrent.printModal()}function y(c){27==c.which&&g(c)}a.chaosModalExitFullscreen=function(){if(document.fullscreenElement||document.webkitFullscreenElement||document.mozFullScreenElement||document.msFullscreenElement){var c;"fullscreenEnabled"in document?c=document.exitFullscreen():"webkitFullscreenEnabled"in document?c=document.webkitExitFullscreen():"mozFullScreenEnabled"in document?c=document.mozCancelFullScreen():"msExitFullscreen"in document&&
(c=document.msExitFullscreen());c&&c instanceof Promise&&c.then({})}};a.chaosModalIsVisible=function(c){return null===c.offsetParent?!1:!0};a.chaosModalLoading=null;a.modalMask=null;a.chaosModalCurrent=null;a.chaosModalAlwaysAtTop=!1;a.chaosModalMaxWidth=0;a.chaosModalZIndex=1E5;a.chaosModalIndex=0;a.chaosModalImagesToLoad=!1;a.chaosModalIframesToLoad=!1;a.chaosModalVideosToLoad=!1;a.chaosModalClickTimer=!1;a.chaosModalResizeFrame=null;a.chaosModalPrintCSSElements=null;a.fn.openModal=function(c){a.chaosModalExitFullscreen();
if(null==a.chaosModalCurrent){var b=this.clone(!0),e=a("body");a("html");a(document);var f=a(window),r=c.cssClass,k=c.closeLink,m=c.printLink,t=c.galleryPrevLink,n=c.galleryNextLink;galleryLinkAreas=c.galleryLinkAreas;a.chaosModalAlwaysAtTop=c.alwaysAtTop;a.chaosModalMaxWidth=c.maxWidth;a.chaosModalCurrent=b;var p=b.html();b.attr({id:"chaos-current-modal","class":r});b.prepend('<div style="clear: both;"></div>');m&&b.prepend('<a class="print-link" style="float: left; margin: 5px;">Print</a>');k&&
b.prepend('<a class="close-link" style="display: block; box-sizing: border-box; position: absolute; z-index: 10; top: -1em; right: -1em; width: 2em; height: 2em; border: 0.2em solid #fff; border-radius: 100%; color: #fff; background: #000; text-align: center; font-size: 15px; line-height: 1.6em; text-decoration: none;">✕︎</a>');b.append('<div style="clear: both;"></div>');t&&(b.append('<a class="prev-link" style="display: block; box-sizing: border-box; position: absolute; z-index: 10; top: calc(50% - 1em); left: -1em; width: 2em; height: 2em; border: 0.2em solid #fff; border-radius: 100%; color: #fff; background: #000; text-align: center; font-size: 15px; line-height: 1.6em; text-decoration: none;">❮︎</a>'),
galleryLinkAreas&&b.append('<a class="prev-link area" style="display: block; position: absolute; top: 0; left: 0; width: 20%; height: 100%;"></a>'));n&&(b.append('<a class="next-link" style="display: block; box-sizing: border-box; position: absolute; z-index: 10; top: calc(50% - 1em); right: -1em; width: 2em; height: 2em; border: 0.2em solid #fff; border-radius: 100%; color: #fff; background: #000; text-align: center; font-size: 15px; line-height: 1.6em; text-decoration: none;">❯︎</a>'),
galleryLinkAreas&&b.append('<a class="next-link area" style="display: block; position: absolute; top: 0; right: 0; width: 80%; height: 100%;"></a>'));b.css({display:"block",position:"absolute",background:"#fff","z-index":a.chaosModalZIndex+2,left:"-1000000px",margin:"0",padding:"0"});b.find('img[loading="lazy"]').each(function(){this.loading="eager";this.src&&(this.src=this.src);this.srcset&&(this.srcset=this.srcset)});r=b.find('iframe[src=""]');k=b.find("video");1<r.length+k.length&&(c.iframeAddAutoplay=
!1);r.each(function(){var h=a(this),d=h.data("src");if(d){if(c.iframeAddAutoplay){var x=d.indexOf("?");-1==x?d+="?":x!=d.length-1&&(d+="&");d+="autoplay=1";h.attr("allow","autoplay")}h.attr("src",d).attr("data-src","")}});k.each(function(){var h=a(this),d=h.data("src");d&&h.attr("src",d).attr("data-src","")});k.css({position:"relative","z-index":"9"});b.prependTo(e);if(m){a.chaosModalPrintCSSElements||(a.chaosModalPrintCSSElements=a(document.head).find('link[rel="stylesheet"]'));e=a('<iframe class="print-iframe" width="100" height="100" style="position: absolute; left: -20000px; top: 0;"></iframe>');
b.append(e);var q=e.get(0).contentDocument;q.open();q.write("<!DOCTYPE html>");q.write("<html><head>");a.chaosModalPrintCSSElements.each(function(){q.write(this.outerHTML)});q.write("</head><body>");q.write(p);q.write("</body></html>");q.close()}var w=function(){b.width()>a.chaosModalMaxWidth?b.width(a.chaosModalMaxWidth):b.width(b.width());b.css("max-width","80%");b.find("img").css("max-width","100%");b.hide();b.resizeModal();if(0<b.find(".print-link").length)b.find(".print-link").on("click",l);
if(0<b.find(".close-link").length)b.find(".close-link").on("click",g);if(0<b.find(".prev-link").length)b.find(".prev-link").on("click",function(){t.click()});if(0<b.find(".next-link").length)b.find(".next-link").on("click",function(){n.click()});if(c.clickPassthrough)b.find(".fallback-link").on("click",function(){clearTimeout(a.chaosModalClickTimer)});a.modalMask.stop();b.show(0,function(){b.resizeModal();var h=["resize","load","transitionend","animationend"];for(i=0;i<h.length;i++)f.on(h[i],{},v);
a.fn.closeLoading();c.clickPassthrough&&(a.chaosModalClickTimer=setTimeout(function(){clearTimeout(a.chaosModalClickTimer);!window.open(c.clickPassthrough,c.clickTarget)&&confirm("Displaying New Document\n\nUse Save As... to download, then click back to return to this page.")&&(location.href=c.clickPassthrough)},3E3))})};p=b.find("img").filter(function(h,d){return!a.chaosModalIsVisible(d)||d.complete&&""!=d.getAttribute("src")&&("undefined"==typeof d.naturalWidth||0!=d.naturalWidth)?!1:!0});e=b.find("iframe").filter(function(h,
d){return a.chaosModalIsVisible(d)?!0:!1});m=b.find("video").filter(function(h,d){if(!a.chaosModalIsVisible(d)||2<d.readyState&&""!=d.getAttribute("src")&&("undefined"==typeof d.videoWidth||0!=d.videoWidth))return!1;d.load();return!0});a.chaosModalImagesToLoad=p.length;a.chaosModalIframesToLoad=e.length;a.chaosModalVideosToLoad=m.length;a.chaosModalImagesToLoad||a.chaosModalIframesToLoad||a.chaosModalVideosToLoad?(p.on("load",function(){--a.chaosModalImagesToLoad;a.chaosModalImagesToLoad||a.chaosModalIframesToLoad||
a.chaosModalVideosToLoad||w()}),e.each(function(h,d){d.dataset.src=d.src;d.src="about:blank"}),e.on("load",function(h){--a.chaosModalIframesToLoad;a.chaosModalImagesToLoad||a.chaosModalIframesToLoad||a.chaosModalVideosToLoad||w()}),e.each(function(h,d){d.src=d.dataset.src;delete d.dataset.src}),m.on("canplay",function(){--a.chaosModalVideosToLoad;a.chaosModalImagesToLoad||a.chaosModalIframesToLoad||a.chaosModalVideosToLoad||w()})):w();b.show();return this}};a.fn.closeModal=function(){cancelAnimationFrame(a.chaosModalResizeFrame);
var c=["resize","load","transitionend","animationend"];for(i=0;i<c.length;i++)a(window).on(c[i],v);a.chaosModalCurrent=null;0<this.find(".close-link").length&&this.find(".close-link").off("click");this.hide().remove();a("#chaos-modal-mask").hide().remove();a.fn.closeLoading();return this};a.fn.resizeModal=function(c){c=void 0===c?a.chaosModalAlwaysAtTop:c;var b=a(document),e=a(window),f=e.height(),r=e.width(),k=f/2-this.outerHeight()/2;f=Math.min(.1*r,.1*f,50);0>k&&(k=f);c&&e.scrollTop(0);k+=e.scrollTop();
c=b.height()-f-(k+this.outerHeight());0>c&&(k+=c);k<f&&(k=f);e.scrollLeft(0);e.scrollTop()>k-f&&e.scrollTop(k-f);this.css({top:k,left:"50%",transform:"translate(-50%,0)"});return this};a.fn.printModal=function(){var c=this.find(".print-iframe").get(0).contentWindow;c.focus();c.print()};a.fn.showLoading=function(){a.openTime=Date.now();if(null==a.modalMask){var c=a("body"),b=a(window),e=b.height();b=b.width();maskClose=!0;a.modalMask=a('<div id="chaos-modal-mask" class="chaos-modal-mask"></div>');
a.modalMask.css({position:"fixed","z-index":a.chaosModalZIndex,"background-color":"rgba(0, 0, 0, 0.5)",display:"none",top:"0",left:"0",width:"100%",height:"100%"});a.modalMask.prependTo(c);a.modalMask.show(0);c=a(document);a(window);a("html");maskClose&&(a.modalMask.on("click",g),c.on("keyup.chaosmodal",y));a("body").append('<div id="modal-loading" style="position: fixed; z-index: '+(a.chaosModalZIndex+1)+'; color: #fff;">Loading</div>');posx=b/2-parseInt(a("#modal-loading").css("width"))/2;posy=
e/2-parseInt(a("#modal-loading").css("height"))/2;a.chaosModalLoading=a("#modal-loading");a.chaosModalLoading.css("top",posy);a.chaosModalLoading.css("left",posx)}};a.fn.closeLoading=function(){a.chaosModalLoading&&a.chaosModalLoading.remove()};a.processModalLink=function(c){var b=a(this);c.preventDefault();b.off("click",a.processModalLink);var e=b.data("chaos-modal-box-id"),f=c=!1,r=/(^data:image\/[a-z0-9+\/=]*,)|(\.(jp(e|g|eg)|gif|png|bmp|wbmp|webp|svg|ico)((\?|#).*)?$)/i,k=/(\.(mp4)((\?|#).*)?$)/i,
m=!1,t=!1;c=e?a("#"+e).first():b.parent().find(".chaos-modal-box").first();c.length?(f=a("<div></div>").css({padding:"20px"}),c.clone(!0).css({display:"block"}).appendTo(f),f=a("<div></div>").css({background:"#fff"}).append(f)):(r.test(b.attr("href"))?(m=a('<img loading="lazy" src="'+b.attr("href")+'" />'),t=b.attr("title")):k.test(b.attr("href"))&&(m=a('<video src="'+b.attr("href")+'" controls />'),t=b.attr("title")),m||(e=b.find("img"),1==e.length&&r.test(e.attr("src"))&&(m=a('<img loading="lazy" src="'+
e.attr("src")+'" />'),t=e.attr("title"))),m||(e=b.find("video"),1==e.length&&r.test(e.attr("src"))&&(m=a('<video src="'+e.attr("src")+'" controls />'),t=e.attr("title"))),m&&(f=a("<div></div>").css({padding:"20px"}),m.css({display:"block","max-height":"calc(80vh - 40px)"}).appendTo(f),f=a("<div></div>").css({background:"#fff"}).append(f),b.data("chaos-modal-gallery-link-areas",!0)),t&&!b.data("chaos-modal-caption")&&(t=(t+"").replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g,"$1<br />$2"),b.data("chaos-modal-caption",
imageCaption)));f&&(c.remove(),b.data("chaos-modal-href",b.attr("href")),b.data("chaos-modal-target",b.attr("target")),b.attr({href:"#",target:null,id:"chaos-modal-link-"+a.chaosModalIndex}),f.attr({id:"chaos-modal-box-"+a.chaosModalIndex}),f.hide(),f.appendTo("body"),b.click(function(){g();a.fn.showLoading();var n={maxWidth:960,cssClass:!1,closeLink:!0,printLink:!1,alwaysAtTop:!1,preprocessing:!1,clickPassthrough:!1,iframeAddAutoplay:!0,caption:!1,galleryPrevLink:!1,galleryNextLink:!1,galleryLinkAreas:!1};
a.each(n,function(q,w){var h=b.data("chaos-modal-"+q);"undefined"!==typeof h&&(n[q]="maxWidth"==q?parseInt(h):h)});if(n.caption){var p=a("<div>"+n.caption+"</div>").css({position:"relative","z-index":9,display:"table-caption","caption-side":"bottom","padding-top":0,"padding-right":"20px","padding-bottom":"20px","padding-left":"20px",background:"#fff"});f.children().first().css("display","table").append(p);b.data("chaos-modal-caption","")}n.clickPassthrough&&(n.clickPassthrough=b.data("chaos-modal-href"),
n.clickTarget=b.data("chaos-modal-target"),n.clickTarget||(n.clickTarget="_self"));preprocess=b.attr("data-preprocess");p=a(this).attr("id").match(/chaos-modal-link-(\d+)/i);p[1]&&(p=a("#chaos-modal-box-"+p[1]),1==p.length&&"true"!=preprocess?p.openModal(n):1==p.length&&"true"==preprocess&&b.trigger("chaos-modal-preprocess",[p,n]));return!1}),b.click(),a.chaosModalIndex++);return!1}})(jQuery);
jQuery(document).ready(function(a){function v(){var g=a(this),l=g.attr("src");l&&g.attr("data-src",l).attr("src","")}a(".chaos-modal-link").not("a").each(function(){var g=a(this),l=g.children();g.removeClass("chaos-modal-link");l.not("a").length||1==l.length&&l.addClass("chaos-modal-link")});var u={};a("[data-chaos-modal-gallery]").each(function(){var g=a(this),l=g.data("chaos-modal-gallery");l in u||(u[l]=[]);u[l].push(g)});a.each(u,function(){var g=!1;a.each(this,function(){this.find("a.chaos-modal-link").each(function(){var l=
a(this);l.data("chaos-modal-gallery-prev-link",g);g&&g.data("chaos-modal-gallery-next-link",l);g=l})})});a("a.chaos-modal-link").on("click",a.processModalLink);a(".chaos-modal-box:hidden iframe").each(v);a(".chaos-modal-link").each(function(){var g=a(this).data("chaos-modal-box-id");g&&a("#"+g+":hidden iframe").each(v)})});