vue-pure-admin/static/js/print-B-zH8tbv.js

2 lines
2.8 KiB
JavaScript

const s=function(e,n){if(n=n||{},!(this instanceof s))return new s(e,n);this.conf={styleStr:"",setDomHeightArr:[],printBeforeFn:null,printDoneCallBack:null};for(const o in this.conf)o&&n.hasOwnProperty(o)&&(this.conf[o]=n[o]);typeof e=="string"?this.dom=document.querySelector(e):this.dom=this.isDOM(e)?e:e.$el,this.conf.setDomHeightArr&&this.conf.setDomHeightArr.length&&this.setDomHeight(this.conf.setDomHeightArr),this.init()};s.prototype={init:function(){const e=this.getStyle()+this.getHtml();this.writeIframe(e)},extendOptions:function(e,n){for(const o in n)e[o]=n[o];return e},getStyle:function(){let e="";const n=document.querySelectorAll("style,link");for(let o=0;o<n.length;o++)e+=n[o].outerHTML;return e+=`<style>.no-print{display:none;}${this.conf.styleStr}</style>`,e},getHtml:function(){const e=document.querySelectorAll("input"),n=document.querySelectorAll("select"),o=document.querySelectorAll("textarea"),c=document.querySelectorAll("canvas");for(let t=0;t<e.length;t++)e[t].type=="checkbox"||e[t].type=="radio"?e[t].checked==!0?e[t].setAttribute("checked","checked"):e[t].removeAttribute("checked"):(e[t].type=="text",e[t].setAttribute("value",e[t].value));for(let t=0;t<o.length;t++)o[t].type=="textarea"&&(o[t].innerHTML=o[t].value);for(let t=0;t<n.length;t++)if(n[t].type=="select-one"){const r=n[t].children;for(const i in r)r[i].tagName=="OPTION"&&(r[i].selected==!0?r[i].setAttribute("selected","selected"):r[i].removeAttribute("selected"))}for(let t=0;t<c.length;t++){const r=c[t].toDataURL("image/png"),i=document.createElement("img");i.src=r,i.setAttribute("style","max-width: 100%;"),i.className="isNeedRemove",c[t].parentNode.insertBefore(i,c[t].nextElementSibling)}return this.dom.outerHTML},writeIframe:function(e){let n,o;const c=document.createElement("iframe"),t=document.body.appendChild(c);c.id="myIframe",c.setAttribute("style","position:absolute;width:0;height:0;top:-10px;left:-10px;"),n=t.contentWindow||t.contentDocument,o=t.contentDocument||t.contentWindow.document,o.open(),o.write(e),o.close();const r=document.querySelectorAll(".isNeedRemove");for(let l=0;l<r.length;l++)r[l].parentNode.removeChild(r[l]);const i=this;c.onload=function(){i.conf.printBeforeFn&&i.conf.printBeforeFn({doc:o}),i.toPrint(n),setTimeout(function(){document.body.removeChild(c),i.conf.printDoneCallBack&&i.conf.printDoneCallBack()},100)}},toPrint:function(e){try{setTimeout(function(){e.focus();try{e.document.execCommand("print",!1,null)||e.print()}catch(n){e.print()}e.close()},10)}catch(n){console.error(n)}},isDOM:typeof HTMLElement=="object"?function(e){return e instanceof HTMLElement}:function(e){return e&&typeof e=="object"&&e.nodeType===1&&typeof e.nodeName=="string"},setDomHeight(e){e&&e.length&&e.forEach(n=>{document.querySelectorAll(n).forEach(c=>{c.style.height=c.offsetHeight+"px"})})}};export{s as P};