-
Notifications
You must be signed in to change notification settings - Fork 0
/
printd.umd.min.js
1 lines (1 loc) · 2.77 KB
/
printd.umd.min.js
1
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).printd={})}(this,(function(t){"use strict";const e=/^(((http[s]?)|file):)?(\/\/)+([0-9a-zA-Z-_.=?&].+)$/,i=/^((\.|\.\.)?\/)([0-9a-zA-Z-_.=?&]+\/)*([0-9a-zA-Z-_.=?&]+)$/,n=t=>e.test(t)||i.test(t);function s(t,e){const i=t.createElement("style");return i.appendChild(t.createTextNode(e)),i}function r(t,e){const i=t.createElement("link");return i.type="text/css",i.rel="stylesheet",i.href=e,i}function o(t){const e=window.document.createElement("iframe");return e.setAttribute("src","about:blank"),e.setAttribute("style","visibility:hidden;width:0;height:0;position:absolute;z-index:-9999;bottom:0;"),e.setAttribute("width","0"),e.setAttribute("height","0"),e.setAttribute("wmode","opaque"),t.appendChild(e),e}const a={parent:window.document.body,headElements:[],bodyElements:[]};class h{constructor(t){this.isLoading=!1,this.hasEvents=!1,this.opts=[a,t||{}].reduce(((t,e)=>(Object.keys(e).forEach((i=>t[i]=e[i])),t)),{}),this.iframe=o(this.opts.parent)}getIFrame(){return this.iframe}print(t,e,i,o){if(this.isLoading)return;const{contentDocument:a,contentWindow:h}=this.iframe;if(!a||!h)return;if(this.iframe.src="about:blank",this.elCopy=t.cloneNode(!0),!this.elCopy)return;this.isLoading=!0,this.callback=o;const d=h.document;d.open(),d.write('<!DOCTYPE html><html><head><meta charset="utf-8"></head><body></body></html>'),this.addEvents();const{headElements:c,bodyElements:l}=this.opts;Array.isArray(c)&&c.forEach((t=>d.head.appendChild(t))),Array.isArray(l)&&l.forEach((t=>d.body.appendChild(t))),Array.isArray(e)&&e.forEach((t=>{t&&d.head.appendChild(n(t)?r(d,t):s(d,t))})),d.body.appendChild(this.elCopy),Array.isArray(i)&&i.forEach((t=>{if(t){const e=d.createElement("script");n(t)?e.src=t:e.innerText=t,d.body.appendChild(e)}})),d.close()}printURL(t,e){this.isLoading||(this.addEvents(),this.isLoading=!0,this.callback=e,this.iframe.src=t)}onBeforePrint(t){this.onbeforeprint=t}onAfterPrint(t){this.onafterprint=t}launchPrint(t){this.isLoading||t.print()}addEvents(){if(!this.hasEvents){this.hasEvents=!0,this.iframe.addEventListener("load",(()=>this.onLoad()),!1);const{contentWindow:t}=this.iframe;t&&(this.onbeforeprint&&t.addEventListener("beforeprint",this.onbeforeprint),this.onafterprint&&t.addEventListener("afterprint",this.onafterprint))}}onLoad(){if(this.iframe){this.isLoading=!1;const{contentDocument:t,contentWindow:e}=this.iframe;if(!t||!e)return;"function"==typeof this.callback?this.callback({iframe:this.iframe,element:this.elCopy,launchPrint:()=>this.launchPrint(e)}):this.launchPrint(e)}}}t.Printd=h,t.createIFrame=o,t.createLinkStyle=r,t.createStyle=s,t.default=h,Object.defineProperty(t,"__esModule",{value:!0})}));