diff --git a/src/js/browser.js b/src/js/browser.js index e420f77..3f9bbb0 100644 --- a/src/js/browser.js +++ b/src/js/browser.js @@ -23,4 +23,3 @@ const Browser = { } export default Browser - diff --git a/src/js/init.js b/src/js/init.js index 9ff188c..3614c67 100644 --- a/src/js/init.js +++ b/src/js/init.js @@ -40,17 +40,15 @@ export default { // Check if a printable document or object was supplied let args = arguments[0] - if (args === undefined) { - throw new Error('printJS expects at least 1 attribute.') - } + if (args === undefined) throw new Error('printJS expects at least 1 attribute.') + // Process parameters switch (typeof args) { case 'string': params.printable = encodeURI(args) params.fallbackPrintable = params.printable params.type = arguments[1] || params.type break - case 'object': params.printable = args.printable params.fallbackPrintable = typeof args.fallbackPrintable !== 'undefined' ? args.fallbackPrintable : params.printable @@ -79,28 +77,24 @@ export default { throw new Error('Unexpected argument type! Expected "string" or "object", got ' + typeof args) } - if (!params.printable) { - throw new Error('Missing printable information.') - } + // Validate printable + if (!params.printable) throw new Error('Missing printable information.') + // Validate type if (!params.type || typeof params.type !== 'string' || printTypes.indexOf(params.type.toLowerCase()) === -1) { throw new Error('Invalid print type. Available types are: pdf, html, image and json.') } // Check if we are showing a feedback message to the user (useful for large files) - if (params.showModal) { - Modal.show(params) - } - if (params.onLoadingStart) { - params.onLoadingStart() - } + if (params.showModal) Modal.show(params) + + // Check for a print start hook function + if (params.onLoadingStart) params.onLoadingStart() - // To prevent duplication and issues, remove printFrame from the DOM, if it exists + // To prevent duplication and issues, remove any used printFrame from the DOM let usedFrame = document.getElementById(params.frameId) - if (usedFrame) { - usedFrame.parentNode.removeChild(usedFrame) - } + if (usedFrame) usedFrame.parentNode.removeChild(usedFrame) // Create a new iframe or embed element (IE prints blank pdf's if we use iframe) let printFrame @@ -109,7 +103,7 @@ export default { printFrame = document.createElement('iframe') // Hide iframe - printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0;') + printFrame.setAttribute('style', 'visibility: hidden; height: 0; width: 0; position: absolute;') // Set element id printFrame.setAttribute('id', params.frameId) @@ -143,8 +137,6 @@ export default { case 'json': Json.print(params, printFrame) break - default: - throw new Error('Invalid print type. Available types are: pdf, html, image and json.') } } } diff --git a/src/js/json.js b/src/js/json.js index 6e85257..854c940 100644 --- a/src/js/json.js +++ b/src/js/json.js @@ -12,17 +12,13 @@ export default { } // Check if properties were provided - if (!params.properties || typeof params.properties !== 'object') { - throw new Error('Invalid properties array for your JSON data.') - } + if (!params.properties || typeof params.properties !== 'object') throw new Error('Invalid properties array for your JSON data.') // Variable to hold the html string let htmlData = '' - // Check print has header - if (params.header) { - htmlData += '