|
24 | 24 | */ |
25 | 25 |
|
26 | 26 | (function (jsPDFAPI) { |
27 | | -// 'use strict'; |
| 27 | + 'use strict'; |
| 28 | + /*jslint browser:true */ |
| 29 | + /*global document: false */ |
| 30 | + /*predef (jsPDF) */ |
28 | 31 |
|
29 | 32 | var maxLn = 0, |
30 | 33 | lnP = 0, |
|
64 | 67 |
|
65 | 68 | text = document.createElement('font'); |
66 | 69 | text.id = "jsPDFCell"; |
67 | | - text.style = "font-family: ' + fontName + ';font-size:' + fontSize + 'pt;font-style: ' + fontStyle + ';"; |
| 70 | + // error text.style = "font-family: ' + fontName + ';font-size:' + fontSize + 'pt;font-style: ' + fontStyle + ';"; |
68 | 71 | text.innerText = txt; |
69 | 72 |
|
70 | 73 | document.body.appendChild(text); |
|
93 | 96 | }; |
94 | 97 |
|
95 | 98 | jsPDFAPI.cell = function (x, y, w, h, txt, ln) { |
96 | | - if(this.printingHeaderRow !== true && this.lnMod !== 0){ |
| 99 | + this.lnMod = this.lnMod === undefined ? 0 : this.lnMod; |
| 100 | + if (this.printingHeaderRow !== true && this.lnMod !== 0) { |
97 | 101 | ln = ln + this.lnMod; |
98 | 102 | } |
99 | 103 |
|
100 | 104 | if ((((ln * h) + y + (h * 2)) / pages) >= this.internal.pageSize.height && pages === 1 && !newPage) { |
101 | 105 | this.cellAddPage(); |
102 | 106 |
|
103 | | - if(this.printHeaders && this.tableHeaderRow){ |
| 107 | + if (this.printHeaders && this.tableHeaderRow) { |
104 | 108 | this.printHeaderRow(ln); |
105 | | - this.lnMod++; |
106 | | - ln++; |
| 109 | + this.lnMod += 1; |
| 110 | + ln += 1; |
107 | 111 | } |
108 | 112 | if (getMaxLn() === 0) { |
109 | 113 | setMaxLn(Math.round((this.internal.pageSize.height - (h * 2)) / h)); |
110 | 114 | } |
111 | 115 | } else if (newPage && getLastCellPosition().ln !== ln && getLnP() === getMaxLn()) { |
112 | 116 | this.cellAddPage(); |
113 | 117 |
|
114 | | - if(this.printHeaders && this.tableHeaderRow){ |
| 118 | + if (this.printHeaders && this.tableHeaderRow) { |
115 | 119 | this.printHeaderRow(ln); |
116 | | - this.lnMod++; |
117 | | - ln++; |
| 120 | + this.lnMod += 1; |
| 121 | + ln += 1; |
118 | 122 | } |
119 | 123 | } |
120 | 124 |
|
|
151 | 155 | * @type {Function} |
152 | 156 | * @return {String[]} of Object keys |
153 | 157 | */ |
154 | | - jsPDFAPI.getKeys = (typeof Object.keys == 'function') |
155 | | - ? function(object){ |
| 158 | + jsPDFAPI.getKeys = (typeof Object.keys === 'function') |
| 159 | + ? function (object) { |
156 | 160 | if (!object) { |
157 | 161 | return []; |
158 | 162 | } |
159 | 163 | return Object.keys(object); |
160 | 164 | } |
161 | | - : function(object) { |
| 165 | + : function (object) { |
162 | 166 | var keys = [], |
163 | 167 | property; |
164 | 168 |
|
|
177 | 181 | * @param comparisonFn |
178 | 182 | * @returns {*} |
179 | 183 | */ |
180 | | - jsPDFAPI.arrayMax = function(array, comparisonFn) { |
| 184 | + jsPDFAPI.arrayMax = function (array, comparisonFn) { |
181 | 185 | var max = array[0], |
182 | | - i, ln, item; |
| 186 | + i, |
| 187 | + ln, |
| 188 | + item; |
183 | 189 |
|
184 | | - for (i = 0, ln = array.length; i < ln; i++) { |
| 190 | + for (i = 0, ln = array.length; i < ln; i += 1) { |
185 | 191 | item = array[i]; |
186 | 192 |
|
187 | 193 | if (comparisonFn) { |
188 | 194 | if (comparisonFn(max, item) === -1) { |
189 | 195 | max = item; |
190 | 196 | } |
191 | | - } |
192 | | - else { |
| 197 | + } else { |
193 | 198 | if (item > max) { |
194 | 199 | max = item; |
195 | 200 | } |
|
207 | 212 | * @param {Object} [config.autoSize] True to dynamically set the column widths to match the widest cell value |
208 | 213 | * @param {Object} [config.autoStretch] True to force the table to fit the width of the page |
209 | 214 | */ |
210 | | - jsPDFAPI.table = function(data, headers, config){ |
211 | | - |
212 | | - var headerNames = [], headerPrompts = [], header; |
| 215 | + jsPDFAPI.table = function (data, headers, config) { |
| 216 | + |
| 217 | + var headerNames = [], |
| 218 | + headerPrompts = [], |
| 219 | + header, |
| 220 | + autoSize, |
| 221 | + printHeaders, |
| 222 | + autoStretch, |
| 223 | + i, |
| 224 | + ln, |
| 225 | + columnMatrix = {}, |
| 226 | + columnWidths = {}, |
| 227 | + columnData, |
| 228 | + column, |
| 229 | + columnMinWidths = [], |
| 230 | + j, |
| 231 | + tableHeaderConfigs = [], |
| 232 | + model, |
| 233 | + jln; |
213 | 234 |
|
214 | 235 | /** |
215 | 236 | * @property {Number} lnMod |
216 | 237 | * Keep track of the current line number modifier used when creating cells |
217 | 238 | */ |
218 | 239 | this.lnMod = 0; |
219 | 240 |
|
220 | | - if(config){ |
221 | | - var autoSize = config.autoSize || false, |
222 | | - printHeaders = this.printHeaders = config.printHeaders || true, |
223 | | - autoStretch = config.autoStretch || true; |
| 241 | + if (config) { |
| 242 | + autoSize = config.autoSize || false; |
| 243 | + printHeaders = this.printHeaders = config.printHeaders || true; |
| 244 | + autoStretch = config.autoStretch || true; |
224 | 245 | } |
225 | 246 |
|
226 | | - if(!data){ |
227 | | - throw 'No data for PDF table'; |
| 247 | + if (!data) { |
| 248 | + throw 'No data for PDF table'; |
228 | 249 | } |
229 | 250 |
|
230 | 251 | // Set headers |
231 | | - if(headers === undefined || (headers === null)){ |
| 252 | + if (headers === undefined || (headers === null)) { |
232 | 253 |
|
233 | 254 | // No headers defined so we derive from data |
234 | 255 | headerNames = this.getKeys(data[0]); |
235 | 256 |
|
236 | | - } else if (headers[0] && !(typeof headers[0] == 'string')) { |
| 257 | + } else if (headers[0] && (typeof headers[0] !== 'string')) { |
237 | 258 |
|
238 | 259 | // Split header configs into names and prompts |
239 | | - for (var i = 0, ln = headers.length; i < ln; i++) { |
| 260 | + for (i = 0, ln = headers.length; i < ln; i += 1) { |
240 | 261 | header = headers[i]; |
241 | 262 | headerNames.push(header.name); |
242 | 263 | headerPrompts.push(header.prompt); |
|
246 | 267 | headerNames = headers; |
247 | 268 | } |
248 | 269 |
|
249 | | - if(config.autoSize){ |
| 270 | + if (config.autoSize) { |
250 | 271 |
|
251 | 272 | // Create Columns Matrix |
252 | | - var columnMatrix = {}, |
253 | | - columnWidths = {}, |
254 | | - columnData, |
255 | | - column; |
256 | 273 |
|
257 | | - for (var i = 0, ln = headerNames.length; i < ln; i++) { |
| 274 | + for (i = 0, ln = headerNames.length; i < ln; i += 1) { |
258 | 275 | header = headerNames[i]; |
259 | 276 |
|
260 | | - columnMatrix[header] = data.map(function(rec){ |
261 | | - return rec[header]; |
262 | | - }); |
263 | | - |
264 | | - var columnMinWidths = []; |
| 277 | + columnMatrix[header] = data.map( |
| 278 | + function (rec) { |
| 279 | + return rec[header]; |
| 280 | + } |
| 281 | + ); |
265 | 282 |
|
266 | 283 | // get header width |
267 | | - columnMinWidths.push(this.getTextDimensions(headerPrompts[i]||header).w); |
| 284 | + columnMinWidths.push(this.getTextDimensions(headerPrompts[i] || header).w); |
268 | 285 |
|
269 | 286 | column = columnMatrix[header]; |
270 | 287 |
|
271 | 288 | // get cell widths |
272 | | - for (var j = 0, ln = column.length; j < ln; j++) { |
| 289 | + for (j = 0, ln = column.length; j < ln; j += 1) { |
273 | 290 | columnData = column[j]; |
274 | 291 |
|
275 | 292 | columnMinWidths.push(this.getTextDimensions(columnData).w); |
|
282 | 299 |
|
283 | 300 | // -- Construct the table |
284 | 301 |
|
285 | | - if (config.printHeaders){ |
| 302 | + if (config.printHeaders) { |
286 | 303 |
|
287 | 304 | // Construct the header row |
288 | | - var tableHeaderConfigs = []; |
289 | | - |
290 | | - |
291 | | - for (var i = 0, ln = headerNames.length; i < ln; i++) { |
| 305 | + for (i = 0, ln = headerNames.length; i < ln; i += 1) { |
292 | 306 | header = headerNames[i]; |
293 | 307 | tableHeaderConfigs.push([10, 10, columnWidths[header], 25, String(headerPrompts.length ? headerPrompts[i] : header)]); |
294 | 308 | } |
|
301 | 315 | } |
302 | 316 |
|
303 | 317 | // Construct the data rows |
304 | | - var model; |
305 | | - |
306 | | - for (var i = 0, ln = data.length; i < ln; i++) { |
| 318 | + for (i = 0, ln = data.length; i < ln; i += 1) { |
307 | 319 | model = data[i]; |
308 | 320 |
|
309 | | - for (var j = 0, jln = headerNames.length; j < jln; j++) { |
| 321 | + for (j = 0, jln = headerNames.length; j < jln; j += 1) { |
310 | 322 | header = headerNames[j]; |
311 | | - this.cell(10, 10, columnWidths[header], 25, String(model[header]), i+2); |
| 323 | + this.cell(10, 10, columnWidths[header], 25, String(model[header]), i + 2); |
312 | 324 | } |
313 | 325 | } |
314 | 326 |
|
|
321 | 333 | * An array of cell configs that would define a header row: Each config matches the config used by jsPDFAPI.cell |
322 | 334 | * except the ln parameter is excluded |
323 | 335 | */ |
324 | | - jsPDFAPI.setTableHeaderRow = function(config){ |
| 336 | + jsPDFAPI.setTableHeaderRow = function (config) { |
325 | 337 | this.tableHeaderRow = config; |
326 | 338 | }; |
327 | 339 |
|
328 | 340 | /** |
329 | 341 | * Output the store header row |
330 | 342 | * @param lineNumber The line number to output the header at |
331 | 343 | */ |
332 | | - jsPDFAPI.printHeaderRow = function(lineNumber){ |
333 | | - |
334 | | - if (!this.tableHeaderRow){ |
| 344 | + jsPDFAPI.printHeaderRow = function (lineNumber) { |
| 345 | + if (!this.tableHeaderRow) { |
335 | 346 | throw 'Property tableHeaderRow does not exist.'; |
336 | 347 | } |
337 | 348 |
|
338 | | - var tableHeaderCell, tmpArray; |
| 349 | + var tableHeaderCell, |
| 350 | + tmpArray, |
| 351 | + i, |
| 352 | + ln; |
339 | 353 |
|
340 | 354 | this.printingHeaderRow = true; |
341 | 355 |
|
342 | | - for (var i = 0, ln = this.tableHeaderRow.length; i < ln; i++) { |
| 356 | + for (i = 0, ln = this.tableHeaderRow.length; i < ln; i += 1) { |
343 | 357 |
|
344 | 358 | tableHeaderCell = this.tableHeaderRow[i]; |
345 | 359 | tmpArray = [].concat(tableHeaderCell); |
|
350 | 364 | this.printingHeaderRow = false; |
351 | 365 | }; |
352 | 366 |
|
353 | | -})(jsPDF.API); |
| 367 | +}(jsPDF.API)); |
0 commit comments