mirror of
https://github.com/pure-admin/vue-pure-admin.git
synced 2025-06-08 01:17:23 +08:00
2 lines
2.8 KiB
JavaScript
2 lines
2.8 KiB
JavaScript
const c=function(t,o){if(o=o||{},!(this instanceof c))return new c(t,o);this.conf={styleStr:"",setDomHeightArr:[],echartDomArr:[],printBeforeFn:null,printDoneCallBack:null};for(const n in this.conf)n&&o.hasOwnProperty(n)&&(this.conf[n]=o[n]);typeof t=="string"?this.dom=document.querySelector(t):this.dom=this.isDOM(t)?t:t.$el,this.conf.setDomHeightArr&&this.conf.setDomHeightArr.length&&this.setDomHeight(this.conf.setDomHeightArr),this.init()};c.prototype={init:function(){const t=this.getStyle()+this.getHtml();this.writeIframe(t)},extendOptions:function(t,o){for(const n in o)t[n]=o[n];return t},getStyle:function(){let t="";const o=document.querySelectorAll("style,link");for(let n=0;n<o.length;n++)t+=o[n].outerHTML;return t+=`<style>.no-print{display:none;}${this.conf.styleStr}</style>`,t},getHtml:function(){const t=document.querySelectorAll("input"),o=document.querySelectorAll("select"),n=document.querySelectorAll("textarea");for(let e=0;e<t.length;e++)t[e].type=="checkbox"||t[e].type=="radio"?t[e].checked==!0?t[e].setAttribute("checked","checked"):t[e].removeAttribute("checked"):(t[e].type=="text",t[e].setAttribute("value",t[e].value));for(let e=0;e<n.length;e++)n[e].type=="textarea"&&(n[e].innerHTML=n[e].value);for(let e=0;e<o.length;e++)if(o[e].type=="select-one"){const r=o[e].children;for(const i in r)r[i].tagName=="OPTION"&&(r[i].selected==!0?r[i].setAttribute("selected","selected"):r[i].removeAttribute("selected"))}return this.dom.outerHTML},writeIframe:function(t){let o,n;const e=document.createElement("iframe"),r=document.body.appendChild(e);e.id="myIframe",e.setAttribute("style","position:absolute;width:0;height:0;top:-10px;left:-10px;"),o=r.contentWindow||r.contentDocument,n=r.contentDocument||r.contentWindow.document,n.open(),n.write(t),n.close();const i=this;e.onload=function(){i.conf.printBeforeFn&&i.conf.printBeforeFn({doc:n}),i.drawEchartImg(n).then(()=>{i.toPrint(o),setTimeout(function(){document.body.removeChild(e),i.conf.printDoneCallBack&&i.conf.printDoneCallBack()},100)})}},drawEchartImg(t){return new Promise(o=>{this.conf.echartDomArr&&this.conf.echartDomArr.length>0&&this.conf.echartDomArr.forEach(n=>{const e=t.querySelector("#"+n.$el.id),r=new Image,i=e.offsetWidth+"px",s=e.offsetHeight+"px";r.style.width=i,r.style.height=s,r.src=n.imgSrc,e.innerHTML="",e.appendChild(r)}),o()})},toPrint:function(t){try{setTimeout(function(){t.focus();try{t.document.execCommand("print",!1,null)||t.print()}catch{t.print()}t.close()},10)}catch(o){console.error(o)}},isDOM:typeof HTMLElement=="object"?function(t){return t instanceof HTMLElement}:function(t){return t&&typeof t=="object"&&t.nodeType===1&&typeof t.nodeName=="string"},setDomHeight(t){t&&t.length&&t.forEach(o=>{document.querySelectorAll(o).forEach(e=>{e.style.height=e.offsetHeight+"px"})})}};export{c as P};
|