Skip to content

Commit

Permalink
update JSON api to accept properties as strings or objects
Browse files Browse the repository at this point in the history
  • Loading branch information
crabbly committed Aug 24, 2018
1 parent 57d0c35 commit 9bc8a55
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
21 changes: 16 additions & 5 deletions src/js/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''
Expand Down Expand Up @@ -54,7 +65,7 @@ function jsonToHTML (params) {

// Add the table header columns
for (let a = 0; a < properties.length; a++) {
htmlData += '<th style="width:' + properties.columnSize / properties.length + '%; ' + params.gridHeaderStyle + '">' + capitalizePrint(properties[a].displayName) + '</th>'
htmlData += '<th style="width:' + properties[a].columnSize + ';' + params.gridHeaderStyle + '">' + capitalizePrint(properties[a].displayName) + '</th>'
}

// Add the closing tag for the table header row
Expand Down Expand Up @@ -88,7 +99,7 @@ function jsonToHTML (params) {
}

// Add the row contents and styles
htmlData += '<td style="width:' + properties[n].columnSize / properties.length + '%;' + params.gridStyle + '">' + stringData + '</td>'
htmlData += '<td style="width:' + properties[n].columnSize + params.gridStyle + '">' + stringData + '</td>'
}

// Add the row closing tag
Expand Down

0 comments on commit 9bc8a55

Please sign in to comment.