From dff761fc3d1f3cb22fd49df0b171619efced7bea Mon Sep 17 00:00:00 2001 From: Steve Klebanoff Date: Thu, 30 Nov 2017 17:08:48 -0800 Subject: [PATCH] provide custom onLoadingStart and onLoadingEnd hooks --- dist/print.min.js | 2 +- src/js/init.js | 8 ++++++++ src/js/pdf.js | 2 +- src/js/print.js | 3 +++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/dist/print.min.js b/dist/print.min.js index dfa2c6d..ac683f7 100644 --- a/dist/print.min.js +++ b/dist/print.min.js @@ -1 +1 @@ -!function(e){function t(i){if(n[i])return n[i].exports;var r=n[i]={i:i,l:!1,exports:{}};return e[i].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,i){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:i})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="./",t(t.s=10)}([function(e,t,n){"use strict";function i(e,t){if(e.focus(),o.a.isEdge()||o.a.isIE())try{e.contentWindow.document.execCommand("print",!1,null)}catch(t){e.contentWindow.print()}o.a.isIE()||o.a.isEdge()||e.contentWindow.print(),o.a.isIE()&&"pdf"===t.type&&setTimeout(function(){e.parentNode.removeChild(e)},2e3),t.showModal&&a.a.close()}function r(e,t,n){void 0===e.naturalWidth||0===e.naturalWidth?setTimeout(function(){r(e,t,n)},500):i(t,n)}var o=n(1),a=n(3),d={send:function(e,t){document.getElementsByTagName("body")[0].appendChild(t);var n=document.getElementById(e.frameId);"pdf"===e.type&&(o.a.isIE()||o.a.isEdge())?n.setAttribute("onload",i(n,e)):t.onload=function(){if("pdf"===e.type)i(n,e);else{var t=n.contentWindow||n.contentDocument;t.document&&(t=t.document),t.body.innerHTML=e.htmlData,"image"===e.type?r(t.getElementById("printableImage"),n,e):i(n,e)}}}};t.a=d},function(e,t,n){"use strict";var i={isFirefox:function(){return"undefined"!=typeof InstallTrigger},isIE:function(){return-1!==navigator.userAgent.indexOf("MSIE")||!!document.documentMode},isEdge:function(){return!i.isIE()&&!!window.StyleMedia},isChrome:function(){return!!window.chrome&&!!window.chrome.webstore},isSafari:function(){return Object.prototype.toString.call(window.HTMLElement).indexOf("Constructor")>0||-1!==navigator.userAgent.toLowerCase().indexOf("safari")}};t.a=i},function(e,t,n){"use strict";function i(e,t){return'
'+e+"
"}function r(e){return e.charAt(0).toUpperCase()+e.slice(1)}function o(e,t){var n=document.defaultView||window,i=[],r="";if(n.getComputedStyle){i=n.getComputedStyle(e,"");var o=["border","box","break","text-decoration"],a=["clear","display","width","min-width","height","min-height","max-height"];t.honorMarginPadding&&o.push("margin","padding"),t.honorColor&&o.push("color");for(var d=0;d"+e.documentTitle+" "),e.type){case"pdf":if(i.a.isFirefox()||i.a.isEdge()||i.a.isIE()){window.open(e.printable,"_blank").focus(),e.showModal&&r.a.close()}else o.a.print(e,p);break;case"image":d.a.print(e,p);break;case"html":a.a.print(e,p);break;case"json":l.a.print(e,p);break;default:throw new Error("Invalid print type. Available types are: pdf, html, image and json.")}}}},function(e,t,n){"use strict";function i(e){var t=e.printable,i=e.properties,o='
';o+='
';for(var a=0;a'+n.i(r.b)(i[a].displayName||i[a])+"
";o+="
";for(var d=0;d';for(var l=0;l'+t[d][i[l].field||i[l]]+"";o+=""}return o+=""}var r=n(2),o=n(0),a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};t.a={print:function(e,t){if("object"!==a(e.printable))throw new Error("Invalid javascript data object (JSON).");if(!e.properties||"object"!==a(e.properties))throw new Error("Invalid properties array for your JSON data.");var d="";e.header&&(d+='

'+e.header+"

"),d+=i(e),e.htmlData=n.i(r.a)(d,e),o.a.send(e,t)}}},function(e,t,n){"use strict";function i(e,t){t.setAttribute("src",e.printable),o.a.send(e,t)}var r=n(1),o=n(0);t.a={print:function(e,t){if(e.showModal||r.a.isIE()){var n=new window.XMLHttpRequest;n.addEventListener("load",i(e,t)),n.open("GET",window.location.href+e.printable,!0),n.send()}else i(e,t)}}},function(e,t,n){e.exports=n(4)}]); \ No newline at end of file +!function(e){function t(i){if(n[i])return n[i].exports;var o=n[i]={i:i,l:!1,exports:{}};return e[i].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,i){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:i})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="./",t(t.s=10)}([function(e,t,n){"use strict";function i(e,t){if(e.focus(),r.a.isEdge()||r.a.isIE())try{e.contentWindow.document.execCommand("print",!1,null)}catch(t){e.contentWindow.print()}r.a.isIE()||r.a.isEdge()||e.contentWindow.print(),r.a.isIE()&&"pdf"===t.type&&setTimeout(function(){e.parentNode.removeChild(e)},2e3),t.showModal&&a.a.close(),t.onLoadingEnd&&t.onLoadingEnd()}function o(e,t,n){void 0===e.naturalWidth||0===e.naturalWidth?setTimeout(function(){o(e,t,n)},500):i(t,n)}var r=n(1),a=n(3),d={send:function(e,t){document.getElementsByTagName("body")[0].appendChild(t);var n=document.getElementById(e.frameId);"pdf"===e.type&&(r.a.isIE()||r.a.isEdge())?n.setAttribute("onload",i(n,e)):t.onload=function(){if("pdf"===e.type)i(n,e);else{var t=n.contentWindow||n.contentDocument;t.document&&(t=t.document),t.body.innerHTML=e.htmlData,"image"===e.type?o(t.getElementById("printableImage"),n,e):i(n,e)}}}};t.a=d},function(e,t,n){"use strict";var i={isFirefox:function(){return"undefined"!=typeof InstallTrigger},isIE:function(){return-1!==navigator.userAgent.indexOf("MSIE")||!!document.documentMode},isEdge:function(){return!i.isIE()&&!!window.StyleMedia},isChrome:function(){return!!window.chrome&&!!window.chrome.webstore},isSafari:function(){return Object.prototype.toString.call(window.HTMLElement).indexOf("Constructor")>0||-1!==navigator.userAgent.toLowerCase().indexOf("safari")}};t.a=i},function(e,t,n){"use strict";function i(e,t){return'
'+e+"
"}function o(e){return e.charAt(0).toUpperCase()+e.slice(1)}function r(e,t){var n=document.defaultView||window,i=[],o="";if(n.getComputedStyle){i=n.getComputedStyle(e,"");var r=["border","box","break","text-decoration"],a=["clear","display","width","min-width","height","min-height","max-height"];t.honorMarginPadding&&r.push("margin","padding"),t.honorColor&&r.push("color");for(var d=0;d"+e.documentTitle+" "),e.type){case"pdf":if(i.a.isFirefox()||i.a.isEdge()||i.a.isIE()){window.open(e.printable,"_blank").focus(),e.showModal&&o.a.close(),e.onLoadingEnd&&e.onLoadingEnd()}else r.a.print(e,p);break;case"image":d.a.print(e,p);break;case"html":a.a.print(e,p);break;case"json":l.a.print(e,p);break;default:throw new Error("Invalid print type. Available types are: pdf, html, image and json.")}}}},function(e,t,n){"use strict";function i(e){var t=e.printable,i=e.properties,r='
';r+='
';for(var a=0;a'+n.i(o.b)(i[a].displayName||i[a])+"
";r+="
";for(var d=0;d';for(var l=0;l'+t[d][i[l].field||i[l]]+"";r+=""}return r+=""}var o=n(2),r=n(0),a="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};t.a={print:function(e,t){if("object"!==a(e.printable))throw new Error("Invalid javascript data object (JSON).");if(!e.properties||"object"!==a(e.properties))throw new Error("Invalid properties array for your JSON data.");var d="";e.header&&(d+='

'+e.header+"

"),d+=i(e),e.htmlData=n.i(o.a)(d,e),r.a.send(e,t)}}},function(e,t,n){"use strict";function i(e,t){t.setAttribute("src",e.printable),r.a.send(e,t)}var o=n(1),r=n(0);t.a={print:function(e,t){if(e.showModal||e.onLoadingStart||o.a.isIE()){var n=new window.XMLHttpRequest;n.addEventListener("load",i(e,t)),n.open("GET",window.location.origin+e.printable,!0),n.send()}else i(e,t)}}},function(e,t,n){e.exports=n(4)}]); \ No newline at end of file diff --git a/src/js/init.js b/src/js/init.js index 95ed67e..052689b 100644 --- a/src/js/init.js +++ b/src/js/init.js @@ -25,6 +25,8 @@ export default { gridHeaderStyle: 'font-weight: bold;', gridStyle: 'border: 1px solid lightgray; margin-bottom: -1px;', showModal: false, + onLoadingStart: function () {}, + onLoadingEnd: function () {}, modalMessage: 'Retrieving Document...', frameId: 'printJS', htmlData: '', @@ -57,6 +59,8 @@ export default { params.gridHeaderStyle = typeof args.gridHeaderStyle !== 'undefined' ? args.gridHeaderStyle : params.gridHeaderStyle params.gridStyle = typeof args.gridStyle !== 'undefined' ? args.gridStyle : params.gridStyle params.showModal = typeof args.showModal !== 'undefined' ? args.showModal : params.showModal + params.onLoadingStart = typeof args.onLoadingStart !== 'undefined' ? args.onLoadingStart : params.onLoadingStart + params.onLoadingEnd = typeof args.onLoadingEnd !== 'undefined' ? args.onLoadingEnd : params.onLoadingEnd params.modalMessage = typeof args.modalMessage !== 'undefined' ? args.modalMessage : params.modalMessage params.documentTitle = typeof args.documentTitle !== 'undefined' ? args.documentTitle : params.documentTitle break @@ -76,6 +80,9 @@ export default { if (params.showModal) { Modal.show(params) } + if (params.onLoadingStart) { + params.onLoadingStart() + } // To prevent duplication and issues, remove printFrame from the DOM, if it exists let usedFrame = document.getElementById(params.frameId) @@ -111,6 +118,7 @@ export default { win.focus() // Make sure there is no loading modal opened if (params.showModal) Modal.close() + if (params.onLoadingEnd) params.onLoadingEnd() } else { Pdf.print(params, printFrame) } diff --git a/src/js/pdf.js b/src/js/pdf.js index b4e139f..ebfc2c3 100644 --- a/src/js/pdf.js +++ b/src/js/pdf.js @@ -4,7 +4,7 @@ import Print from './print' export default { print: (params, printFrame) => { // If showing feedback to user, pre load pdf files (hacky) - if (params.showModal || Browser.isIE()) { + if (params.showModal || params.onLoadingStart || Browser.isIE()) { let req = new window.XMLHttpRequest() req.addEventListener('load', send(params, printFrame)) req.open('GET', window.location.origin + params.printable, true) diff --git a/src/js/print.js b/src/js/print.js index 74fa27e..0a16b3d 100644 --- a/src/js/print.js +++ b/src/js/print.js @@ -64,6 +64,9 @@ function finishPrint (iframeElement, params) { if (params.showModal) { Modal.close() } + if (params.onLoadingEnd) { + params.onLoadingEnd() + } } function loadImageAndFinishPrint (img, iframeElement, params) {