From 9bc8a553eafa01bf3e6709fc2d8a0afca8225f2e Mon Sep 17 00:00:00 2001 From: Rodrigo Date: Fri, 24 Aug 2018 12:01:19 -0400 Subject: [PATCH] update JSON api to accept properties as strings or objects --- package.json | 2 +- src/js/json.js | 21 ++++++++++++++++----- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 075cb62..1a89123 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "print-js", "homepage": "http://printjs.crabbly.com", "description": "A tiny javascript library to help printing from the web.", - "version": "1.0.47", + "version": "1.0.48", "main": "dist/print.js", "repository": "https://github.com/crabbly/Print.js", "license": "MIT", diff --git a/src/js/json.js b/src/js/json.js index 2950beb..dae1ceb 100644 --- a/src/js/json.js +++ b/src/js/json.js @@ -11,13 +11,24 @@ export default { throw new Error('Invalid javascript data object (JSON).') } - // Check if the repeatTableHeader is boolean + // Validate repeatTableHeader if (typeof params.repeatTableHeader !== 'boolean') { throw new Error('Invalid value for repeatTableHeader attribute (JSON).') } - // Check if properties were provided - if (!params.properties || !Array.isArray(params.properties)) throw new Error('Invalid properties array for your JSON data.') + // 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 + '%;' + } + }) // Variable to hold the html string let htmlData = '' @@ -54,7 +65,7 @@ function jsonToHTML (params) { // Add the table header columns for (let a = 0; a < properties.length; a++) { - htmlData += '' + capitalizePrint(properties[a].displayName) + '' + htmlData += '' + capitalizePrint(properties[a].displayName) + '' } // Add the closing tag for the table header row @@ -88,7 +99,7 @@ function jsonToHTML (params) { } // Add the row contents and styles - htmlData += '' + stringData + '' + htmlData += '' + stringData + '' } // Add the row closing tag