import { capitalizePrint, addHeader } from './functions' import Print from './print' export default { print: (params, printFrame) => { // Check if we received proper data if (typeof params.printable !== 'object') { throw new Error('Invalid javascript data object (JSON).') } // Validate repeatTableHeader if (typeof params.repeatTableHeader !== 'boolean') { throw new Error('Invalid value for repeatTableHeader attribute (JSON).') } // Validate properties if (!params.properties || !Array.isArray(params.properties)) { throw new Error('Invalid properties array for your JSON data.') } // We will format the property objects to keep the JSON api compatible with older releases params.properties = params.properties.map(property => { return { field: typeof property === 'object' ? property.field : property, displayName: typeof property === 'object' ? property.displayName : property, columnSize: typeof property === 'object' && property.columnSize ? property.columnSize + ';' : 100 / params.properties.length + '%;' } }) // Create a print container element params.printableElement = document.createElement('div') // Check if we are adding a print header if (params.header) { addHeader(params.printableElement, params) } // Build the printable html data params.printableElement.innerHTML += jsonToHTML(params) // Print the json data Print.send(params, printFrame) } } function jsonToHTML (params) { // Get the row and column data const data = params.printable const properties = params.properties // Create a html table let htmlData = '
| ' + capitalizePrint(properties[a].displayName) + ' | ' } // Add the closing tag for the table header row htmlData += '
|---|
| ' + stringData + ' | ' } // Add the row closing tag htmlData += '