diff --git a/dist/print.min.js b/dist/print.min.js index b46309e..438833f 100644 --- a/dist/print.min.js +++ b/dist/print.min.js @@ -1 +1 @@ -!function e(t,r,a){function n(o,d){if(!r[o]){if(!t[o]){var s="function"==typeof require&&require;if(!d&&s)return s(o,!0);if(i)return i(o,!0);var p=new Error("Cannot find module '"+o+"'");throw p.code="MODULE_NOT_FOUND",p}var l=r[o]={exports:{}};t[o][0].call(l.exports,function(e){var r=t[o][1][e];return n(r?r:e)},l,l.exports,e,t,r,a)}return r[o].exports}for(var i="function"==typeof require&&require,o=0;o'+e+""}function o(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);return e}function d(e){return e.charAt(0).toUpperCase()+e.slice(1)}Object.defineProperty(r,"__esModule",{value:!0}),r.PrintJS=void 0;var s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e};r.print=n;var p=e("./browser"),l=a(p),m=["pdf","html","image","json"],c={printable:null,type:"pdf",header:null,maxWidth:800,font:"TimesNewRoman",font_size:"12pt",honorMarginPadding:!0,honorColor:!1,properties:null,showModal:!1,modalMessage:"Retrieving Document...",frameId:"printJS",border:!0,htmlData:""},u=void 0,f=void 0,h=void 0,y=r.PrintJS=function(){var e=arguments[0],t=this;switch(t.params=o({},c),s(e[0])){case"string":t.params.printable=encodeURI(e[0]),t.params.type=e[1]||c.type;break;case"object":t.params.printable=e[0].printable,t.params.type=e[0].type||c.type,t.params.frameId=e[0].frameId||c.frameId,t.params.header=e[0].header||c.header,t.params.maxWidth=e[0].maxWidth||c.maxWidth,t.params.font=e[0].font||c.font,t.params.font_size=e[0].font_size||c.font_size,t.params.honorMarginPadding="undefined"!=typeof e[0].honorMarginPadding?e[0].honorMarginPadding:c.honorMarginPadding,t.params.properties=e[0].properties||c.properties,t.params.showModal="undefined"!=typeof e[0].showModal?e[0].showModal:c.showModal,t.params.modalMessage=e[0].modalMessage||c.modalMessage;break;default:throw new Error('Unexpected argument type! Expected "string" or "object", got '+s(e[0]))}t.validateInput(),t.params.showModal&&t.showModal();var r=document.getElementById(t.params.frameId);r&&r.parentNode.removeChild(r),l.isIE()&&"pdf"===t.params.type?(t.printFrame=document.createElement("embed"),t.printFrame.setAttribute("type","application/pdf"),t.printFrame.setAttribute("style","width:0px;height:0px;")):(t.printFrame=document.createElement("iframe"),t.printFrame.setAttribute("style","display:none;")),t.printFrame.setAttribute("id",t.params.frameId),"pdf"!==t.params.type&&(t.printFrame.srcdoc="")};y.prototype.pdf=function(){var e=this;e.params.showModal&&!l.isIE()?!function(){var t=document.createElement("img");t.src=e.params.printable;var r=new Promise(function(e,r){function a(){t.complete&&(window.clearInterval(n),e("PrintJS: PDF loaded. Read to print."))}var n=setInterval(a,100)});r.then(function(t){e.printFrame.setAttribute("src",e.params.printable),e.print()})}():(e.printFrame.setAttribute("src",e.params.printable),e.print())},y.prototype.image=function(){function e(){var e=document.createElement("div");if(e.setAttribute("style","width:100%"),l.isFirefox()){var a=document.createElement("canvas");a.setAttribute("width",t.width),a.setAttribute("height",t.height);var n=a.getContext("2d");n.drawImage(t,0,0),t.setAttribute("src",a.toDataURL("JPEG",1))}e.appendChild(t),r.params.header&&r.addHeader(e),r.params.htmlData=e.outerHTML,r.print()}var t=document.createElement("img");t.setAttribute("style","width:100%;"),t.src=this.params.printable;var r=this;if(l.isIE())e();else{var a=new Promise(function(e,r){function a(){t.complete&&(window.clearInterval(n),e("PrintJS: Image loaded. Read to print."))}var n=setInterval(a,100)});a.then(function(t){e()})}},y.prototype.html=function(){var e=document.getElementById(this.params.printable);if(!e)return window.console.error("Invalid HTML element id: "+this.params.printable),!1;var t=document.createElement("div");t.appendChild(e.cloneNode(!0)),t.setAttribute("style","display:none;"),t.setAttribute("id","printJS-html"),e.parentNode.appendChild(t),t=document.getElementById("printJS-html"),t.setAttribute("style",this.collectStyles(t)+"margin:0 !important;");var r=t.children;this.loopNodesCollectStyles(r),this.params.header&&this.addHeader(t),t.parentNode.removeChild(t),this.params.htmlData=i(t.innerHTML),this.print()},y.prototype.json=function(){if("object"!==s(this.params.printable))throw new Error("Invalid javascript data object (JSON).");if(!this.params.properties||"object"!==s(this.params.properties))throw new Error("Invalid properties array for your JSON data.");var e="";this.params.header&&(e+='

'+this.params.header+"

"),e+=this.jsonToHTML(),this.params.htmlData=i(e),this.print()},y.prototype.print=function(){function e(){if(a.focus(),l.isIE()&&"pdf"!==r.params.type)try{a.contentWindow.document.execCommand("print",!1,null)}catch(e){a.contentWindow.print()}else a.contentWindow.print();r.params.showModal&&r.disablePrintModal()}function t(){"undefined"==typeof a.print?setTimeout(function(){t()},1e3):(a.print(),setTimeout(function(){a.parentNode.removeChild(a)},2e3))}var r=this;document.getElementsByTagName("body")[0].appendChild(r.printFrame);var a=document.getElementById(r.params.frameId);l.isIE()&&"pdf"===r.params.type?t():r.printFrame.onload=function(){if("pdf"===r.params.type)e();else{var t=a.contentWindow||a.contentDocument;t.document&&(t=t.document),t.body.innerHTML=r.params.htmlData,e()}}},y.prototype.collectStyles=function(e){var t=document.defaultView||window,r=[],a="";if(t.getComputedStyle){r=t.getComputedStyle(e,"");for(var n=0;n';r+='
';for(var a=0;a'+d(t[a])+"
";r+="";for(var n=0;n'+e[n][t[i]]+"";r+=""}return r+=""},y.prototype.validateInput=function(){if(!this.params.printable)throw new Error("Missing printable information.");if(!this.params.type||"string"!=typeof this.params.type||m.indexOf(this.params.type.toLowerCase())===-1)throw new Error("Invalid print type. Available types are: pdf, html, image and json.")},y.prototype.showModal=function(){var e="font-family:sans-serif; display:table; text-align:center; font-weight:300; font-size:30px; left:0; top:0;position:fixed; z-index: 9990;color: #0460B5; width: 100%; height: 100%; background-color:rgba(255,255,255,.9);transition: opacity .3s ease;",t=document.createElement("div");t.setAttribute("style",e),t.setAttribute("id","printJS-Modal");var r=document.createElement("div");r.setAttribute("style","display:table-cell; vertical-align:middle; padding-bottom:100px;");var a=document.createElement("div");a.setAttribute("class","printClose"),a.setAttribute("id","printClose"),r.appendChild(a);var n=document.createElement("span");n.setAttribute("class","printSpinner"),r.appendChild(n);var i=document.createTextNode(this.params.modalMessage);r.appendChild(i),t.appendChild(r),document.getElementsByTagName("body")[0].appendChild(t);var o=this;document.getElementById("printClose").addEventListener("click",function(){o.disablePrintModal()})},y.prototype.disablePrintModal=function(){var e=document.getElementById("printJS-Modal");e.parentNode.removeChild(e)}},{"./browser":1}],3:[function(e,t,r){"use strict";var a=e("./js/print");window.printJS=a.print},{"./js/print":2}]},{},[3]); \ No newline at end of file +!function e(t,r,a){function n(o,s){if(!r[o]){if(!t[o]){var d="function"==typeof require&&require;if(!s&&d)return d(o,!0);if(i)return i(o,!0);var p=new Error("Cannot find module '"+o+"'");throw p.code="MODULE_NOT_FOUND",p}var l=r[o]={exports:{}};t[o][0].call(l.exports,function(e){var r=t[o][1][e];return n(r?r:e)},l,l.exports,e,t,r,a)}return r[o].exports}for(var i="function"==typeof require&&require,o=0;o'+e+""}function n(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r]);return e}function i(e){return e.charAt(0).toUpperCase()+e.slice(1)}var o="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e},s=e("./browser"),d=["pdf","html","image","json"],p={printable:null,type:"pdf",header:null,maxWidth:800,font:"TimesNewRoman",font_size:"12pt",honorMarginPadding:!0,honorColor:!1,properties:null,showModal:!1,modalMessage:"Retrieving Document...",frameId:"printJS",border:!0,htmlData:""},l=void 0,m=void 0,c=void 0;t.exports=function(){if(void 0===arguments[0])return window.console.error("printJS expects at least 1 attribute."),!1;var e=new h(arguments);switch(l="max-width: "+e.params.maxWidth+"px !important;"+e.params.font_size+" !important;",m="font-family:"+e.params.font+" !important; font-size: "+e.params.font_size+" !important; width:100%;",c="font-weight:300;",e.params.type){case"pdf":if(s.isFirefox()){var t=window.open(e.params.printable,"_blank");t.focus(),e.params.showModal&&e.disablePrintModal()}else e.pdf();break;case"image":e.image();break;case"html":e.html();break;case"json":e.json();break;default:throw new Error("Invalid print type. Available types are: pdf, html, image and json.")}};var h=function(){var e=arguments[0],t=this;switch(t.params=n({},p),o(e[0])){case"string":t.params.printable=encodeURI(e[0]),t.params.type=e[1]||p.type;break;case"object":t.params.printable=e[0].printable,t.params.type=e[0].type||p.type,t.params.frameId=e[0].frameId||p.frameId,t.params.header=e[0].header||p.header,t.params.maxWidth=e[0].maxWidth||p.maxWidth,t.params.font=e[0].font||p.font,t.params.font_size=e[0].font_size||p.font_size,t.params.honorMarginPadding="undefined"!=typeof e[0].honorMarginPadding?e[0].honorMarginPadding:p.honorMarginPadding,t.params.properties=e[0].properties||p.properties,t.params.showModal="undefined"!=typeof e[0].showModal?e[0].showModal:p.showModal,t.params.modalMessage=e[0].modalMessage||p.modalMessage;break;default:throw new Error('Unexpected argument type! Expected "string" or "object", got '+o(e[0]))}t.validateInput(),t.params.showModal&&t.showModal();var r=document.getElementById(t.params.frameId);r&&r.parentNode.removeChild(r),s.isIE()&&"pdf"===t.params.type?(t.printFrame=document.createElement("embed"),t.printFrame.setAttribute("type","application/pdf"),t.printFrame.setAttribute("style","width:0px;height:0px;")):(t.printFrame=document.createElement("iframe"),t.printFrame.setAttribute("style","display:none;")),t.printFrame.setAttribute("id",t.params.frameId),"pdf"!==t.params.type&&(t.printFrame.srcdoc="")};h.prototype.pdf=function(){var e=this;e.params.showModal&&!s.isIE()?!function(){var t=document.createElement("img");t.src=e.params.printable;var r=new Promise(function(e,r){function a(){t.complete&&(window.clearInterval(n),e("PrintJS: PDF loaded. Read to print."))}var n=setInterval(a,100)});r.then(function(t){e.printFrame.setAttribute("src",e.params.printable),e.print()})}():(e.printFrame.setAttribute("src",e.params.printable),e.print())},h.prototype.image=function(){function e(){var e=document.createElement("div");if(e.setAttribute("style","width:100%"),s.isFirefox()){var a=document.createElement("canvas");a.setAttribute("width",t.width),a.setAttribute("height",t.height);var n=a.getContext("2d");n.drawImage(t,0,0),t.setAttribute("src",a.toDataURL("JPEG",1))}e.appendChild(t),r.params.header&&r.addHeader(e),r.params.htmlData=e.outerHTML,r.print()}var t=document.createElement("img");t.setAttribute("style","width:100%;"),t.src=this.params.printable;var r=this;if(s.isIE())e();else{var a=new Promise(function(e,r){function a(){t.complete&&(window.clearInterval(n),e("PrintJS: Image loaded. Read to print."))}var n=setInterval(a,100)});a.then(function(t){e()})}},h.prototype.html=function(){var e=document.getElementById(this.params.printable);if(!e)return window.console.error("Invalid HTML element id: "+this.params.printable),!1;var t=document.createElement("div");t.appendChild(e.cloneNode(!0)),t.setAttribute("style","display:none;"),t.setAttribute("id","printJS-html"),e.parentNode.appendChild(t),t=document.getElementById("printJS-html"),t.setAttribute("style",this.collectStyles(t)+"margin:0 !important;");var r=t.children;this.loopNodesCollectStyles(r),this.params.header&&this.addHeader(t),t.parentNode.removeChild(t),this.params.htmlData=a(t.innerHTML),this.print()},h.prototype.json=function(){if("object"!==o(this.params.printable))throw new Error("Invalid javascript data object (JSON).");if(!this.params.properties||"object"!==o(this.params.properties))throw new Error("Invalid properties array for your JSON data.");var e="";this.params.header&&(e+='

'+this.params.header+"

"),e+=this.jsonToHTML(),this.params.htmlData=a(e),this.print()},h.prototype.print=function(){function e(){if(a.focus(),s.isIE()&&"pdf"!==r.params.type)try{a.contentWindow.document.execCommand("print",!1,null)}catch(e){a.contentWindow.print()}else a.contentWindow.print();r.params.showModal&&r.disablePrintModal()}function t(){"undefined"==typeof a.print?setTimeout(function(){t()},1e3):(a.print(),setTimeout(function(){a.parentNode.removeChild(a)},2e3))}var r=this;document.getElementsByTagName("body")[0].appendChild(r.printFrame);var a=document.getElementById(r.params.frameId);s.isIE()&&"pdf"===r.params.type?t():r.printFrame.onload=function(){if("pdf"===r.params.type)e();else{var t=a.contentWindow||a.contentDocument;t.document&&(t=t.document),t.body.innerHTML=r.params.htmlData,e()}}},h.prototype.collectStyles=function(e){var t=document.defaultView||window,r=[],a="";if(t.getComputedStyle){r=t.getComputedStyle(e,"");for(var n=0;n';r+='
';for(var a=0;a'+i(t[a])+"
";r+="";for(var n=0;n'+e[n][t[o]]+"";r+=""}return r+=""},h.prototype.validateInput=function(){if(!this.params.printable)throw new Error("Missing printable information.");if(!this.params.type||"string"!=typeof this.params.type||d.indexOf(this.params.type.toLowerCase())===-1)throw new Error("Invalid print type. Available types are: pdf, html, image and json.")},h.prototype.showModal=function(){var e="font-family:sans-serif; display:table; text-align:center; font-weight:300; font-size:30px; left:0; top:0;position:fixed; z-index: 9990;color: #0460B5; width: 100%; height: 100%; background-color:rgba(255,255,255,.9);transition: opacity .3s ease;",t=document.createElement("div");t.setAttribute("style",e),t.setAttribute("id","printJS-Modal");var r=document.createElement("div");r.setAttribute("style","display:table-cell; vertical-align:middle; padding-bottom:100px;");var a=document.createElement("div");a.setAttribute("class","printClose"),a.setAttribute("id","printClose"),r.appendChild(a);var n=document.createElement("span");n.setAttribute("class","printSpinner"),r.appendChild(n);var i=document.createTextNode(this.params.modalMessage);r.appendChild(i),t.appendChild(r),document.getElementsByTagName("body")[0].appendChild(t);var o=this;document.getElementById("printClose").addEventListener("click",function(){o.disablePrintModal()})},h.prototype.disablePrintModal=function(){var e=document.getElementById("printJS-Modal");e.parentNode.removeChild(e)}},{"./browser":1}],3:[function(e,t,r){"use strict";window.printJS=e("./js/print")},{"./js/print":2}]},{},[3]); \ No newline at end of file diff --git a/src/js/browser.js b/src/js/browser.js index fb5443d..342f368 100644 --- a/src/js/browser.js +++ b/src/js/browser.js @@ -1,21 +1,23 @@ // Firefox 1.0+ -export function isFirefox () { - return typeof InstallTrigger !== 'undefined' -} +module.exports = { + isFirefox() { + return typeof InstallTrigger !== 'undefined' + }, -// Internet Explorer 6-11 -export function isIE () { - return !!document.documentMode -} + // Internet Explorer 6-11 + isIE() { + return !!document.documentMode + } -// Opera 8.0+ -// let isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0 + // Opera 8.0+ + // let isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0 -// At least Safari 3+: "[object HTMLElementConstructor]" -// let isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 + // At least Safari 3+: "[object HTMLElementConstructor]" + // let isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 -// Edge 20+ -// let isEdge = !isIE && !!window.StyleMedia + // Edge 20+ + // let isEdge = !isIE && !!window.StyleMedia -// Chrome 1+ -// let isChrome = !!window.chrome && !!window.chrome.webstore + // Chrome 1+ + // let isChrome = !!window.chrome && !!window.chrome.webstore +} \ No newline at end of file diff --git a/src/js/print.js b/src/js/print.js index 3c4c057..ed300fb 100644 --- a/src/js/print.js +++ b/src/js/print.js @@ -1,6 +1,6 @@ 'use strict' -import * as browser from './browser' +let browser = require('./browser') let printTypes = ['pdf', 'html', 'image', 'json'] @@ -23,7 +23,7 @@ let defaultParams = { let printFriendlyElement, bodyStyle, headerStyle -export function print () { +module.exports = function() { // check if a printable document or object was supplied if (arguments[0] === undefined) { window.console.error('printJS expects at least 1 attribute.') @@ -68,7 +68,7 @@ export function print () { } // printJS class -export let PrintJS = function () { +let PrintJS = function () { let args = arguments[0] let print = this diff --git a/src/print.js b/src/print.js index 7b221ce..bad0655 100644 --- a/src/print.js +++ b/src/print.js @@ -8,6 +8,4 @@ * https://github.com/crabbly/Print.js/blob/master/LICENSE */ -import { print } from './js/print' - -window.printJS = print +window.printJS = require('./js/print')