docsify/lib/plugins/zoom-image.min.js
2018-02-11 22:25:14 +08:00

2 lines
7.4 KiB
JavaScript

!function(){var e=Object.assign||function(e){for(var t,o=1;o<arguments.length;o++)for(var n in t=arguments[o],t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n]);return e},t=function(e){if(Array.isArray(e)){for(var t=0,o=Array(e.length);t<e.length;t++)o[t]=e[t];return o}return Array.from(e)},o=["IMG"],n=[27,81],i=function(e){return o.includes(e.tagName)},r=function(e){return e.naturalWidth!==e.width},d=function(e){return e&&1===e.nodeType},a=function(a){var l=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{},c=l.margin,m=void 0===c?0:c,s=l.background,u=void 0===s?"#fff":s,f=l.scrollOffset,p=void 0===f?48:f,g=l.metaClick,v=l.container,h=l.template,y=function(){if(x.original){if(x.original.dispatchEvent(new Event("show")),N=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,S=!0,x.zoomed=function(e){var t=e.getBoundingClientRect(),o=t.top,n=t.left,i=t.width,r=t.height,d=e.cloneNode(),a=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0,l=window.pageXOffset||document.documentElement.scrollLeft||document.body.scrollLeft||0;return d.removeAttribute("id"),d.style.position="absolute",d.style.top=o+a+"px",d.style.left=n+l+"px",d.style.width=i+"px",d.style.height=r+"px",d.style.transform="",d}(x.original),document.body.appendChild(A),O.template){var e=d(O.template)?O.template:document.querySelector(O.template);x.template=document.createElement("div"),x.template.appendChild(e.content.cloneNode(!0)),document.body.appendChild(x.template)}if(document.body.appendChild(x.zoomed),requestAnimationFrame(function(){document.body.classList.add("medium-zoom--open")}),x.original.style.visibility="hidden",x.zoomed.classList.add("medium-zoom-image--open"),x.zoomed.addEventListener("click",z),x.zoomed.addEventListener("transitionend",b),x.original.getAttribute("data-zoom-target")){x.zoomedHd=x.zoomed.cloneNode(),x.zoomedHd.src=x.zoomed.getAttribute("data-zoom-target"),x.zoomedHd.onerror=function(){clearInterval(t),console.error("Unable to reach the zoom image target "+x.zoomedHd.src),x.zoomedHd=null,k()};var t=setInterval(function(){x.zoomedHd.naturalWidth&&(clearInterval(t),x.zoomedHd.classList.add("medium-zoom-image--open"),x.zoomedHd.addEventListener("click",z),document.body.appendChild(x.zoomedHd),k())},10)}else k()}},z=function e(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:0,o=function(){S||!x.original||(x.original.dispatchEvent(new Event("hide")),S=!0,document.body.classList.remove("medium-zoom--open"),x.zoomed.style.transform="",x.zoomedHd&&(x.zoomedHd.style.transform="",x.zoomedHd.removeEventListener("click",e)),x.template&&(x.template.style.transition="opacity 150ms",x.template.style.opacity=0),x.zoomed.removeEventListener("click",e),x.zoomed.addEventListener("transitionend",L))};0<t?setTimeout(o,t):o()},E=function(e){e&&e.target?(x.original=e.target,y()):x.original?z():(x.original=T[0],y())},w=function(e){return(e.metaKey||e.ctrlKey)&&O.metaClick?window.open(e.target.getAttribute("data-original")||e.target.parentNode.href||e.target.src,"_blank"):(e.preventDefault(),void E(e))},b=function e(){S=!1,x.zoomed.removeEventListener("transitionend",e),x.original.dispatchEvent(new Event("shown"))},L=function e(){x.original&&(x.original.style.visibility="",document.body.removeChild(x.zoomed),x.zoomedHd&&document.body.removeChild(x.zoomedHd),document.body.removeChild(A),x.zoomed.classList.remove("medium-zoom-image--open"),x.template&&document.body.removeChild(x.template),S=!1,x.zoomed.removeEventListener("transitionend",e),x.original.dispatchEvent(new Event("hidden")),x.original=null,x.zoomed=null,x.zoomedHd=null,x.template=null)},C=function(){if(!S&&x.original){var e=window.pageYOffset||document.documentElement.scrollTop||document.body.scrollTop||0;Math.abs(N-e)>O.scrollOffset&&z(150)}},H=function(e){n.includes(e.keyCode||e.which)&&z()},k=function(){var t=Math.min;if(x.original){var o,n,i={width:window.innerWidth,height:window.innerHeight,left:0,top:0,right:0,bottom:0};if(O.container)if(O.container instanceof Object)e(i,O.container),o=i.width-i.left-i.right-2*O.margin,n=i.height-i.top-i.bottom-2*O.margin;else{var r=(d(O.container)?O.container:document.querySelector(O.container)).getBoundingClientRect(),a=r.width,l=r.height,c=r.left,m=r.top;e(i,{width:a,height:l,left:c,top:m})}o=o||i.width-2*O.margin,n=n||i.height-2*O.margin;var s=x.zoomedHd||x.original,u=s.naturalWidth,f=void 0===u?o:u,p=s.naturalHeight,g=void 0===p?n:p,v=s.getBoundingClientRect(),h=v.top,y=v.left,z=v.width,E=v.height,w=t(t(f,o)/z,t(g,n)/E)||1,b="scale("+w+") translate3d("+((o-z)/2-y+O.margin+i.left)/w+"px, "+((n-E)/2-h+O.margin+i.top)/w+"px, 0)";x.zoomed.style.transform=b,x.zoomedHd&&(x.zoomedHd.style.transform=b)}},O={margin:m,background:u,scrollOffset:p,metaClick:void 0===g||g,container:v,template:h};a instanceof Object&&e(O,a);var T=function(e){try{return Array.isArray(e)?e.filter(i):(n=e,NodeList.prototype.isPrototypeOf(n)||HTMLCollection.prototype.isPrototypeOf(n)?[].concat(t(e)).filter(i):d(e)?[e].filter(i):"string"==typeof e?[].concat(t(document.querySelectorAll(e))).filter(i):[].concat(t(document.querySelectorAll(o.map(function(e){return e.toLowerCase()}).join(",")))).filter(r))}catch(e){throw new TypeError("The provided selector is invalid.\nExpects a CSS selector, a Node element, a NodeList, an HTMLCollection or an array.\nSee: https://github.com/francoischalifour/medium-zoom")}var n}(a),A=function(e){var t=document.createElement("div");return t.classList.add("medium-zoom-overlay"),t.style.backgroundColor=e,t}(O.background),x={original:null,zoomed:null,zoomedHd:null,template:null},N=0,S=!1;return T.forEach(function(e){e.classList.add("medium-zoom-image"),e.addEventListener("click",w)}),A.addEventListener("click",z),document.addEventListener("scroll",C),document.addEventListener("keyup",H),window.addEventListener("resize",z),{show:E,hide:z,toggle:E,update:function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{};return t.background&&(A.style.backgroundColor=t.background),t.container&&t.container instanceof Object&&(t.container=e({},O.container,t.container)),e(O,t)},addEventListeners:function(e,t){T.forEach(function(o){o.addEventListener(e,t)})},detach:function(){var e=function e(){var t=new Event("detach");T.forEach(function(e){e.classList.remove("medium-zoom-image"),e.removeEventListener("click",w),e.dispatchEvent(t)}),T.splice(0,T.length),A.removeEventListener("click",z),document.removeEventListener("scroll",C),document.removeEventListener("keyup",H),window.removeEventListener("resize",z),x.zoomed&&x.zoomed.removeEventListener("transitionend",e)};x.zoomed?(z(),x.zoomed.addEventListener("transitionend",requestAnimationFrame(e))):e()},images:T,options:O}},l=Object.freeze({default:a});!function(e,t){if("undefined"==typeof document)return t;e=e||"";var o=document.head||document.getElementsByTagName("head")[0],n=document.createElement("style");n.type="text/css",o.appendChild(n),n.styleSheet?n.styleSheet.cssText=e:n.appendChild(document.createTextNode(e))}(".medium-zoom-overlay{position:fixed;top:0;right:0;bottom:0;left:0;opacity:0;transition:opacity .3s;will-change:opacity}.medium-zoom--open .medium-zoom-overlay{cursor:pointer;cursor:zoom-out;opacity:1}.medium-zoom-image{cursor:pointer;cursor:zoom-in;transition:transform .3s}.medium-zoom-image--open{position:relative;cursor:pointer;cursor:zoom-out;will-change:transform}",void 0);var c=l&&a||l;$docsify.plugins=[].concat(function(e){var t;e.doneEach(function(e){t&&t.detach(),t=c("img:not(.emoji):not([data-no-zoom])")})},$docsify.plugins)}();