1/*The MIT License (MIT) 2Copyright (c) 2014 https://github.com/kayalshri/ 3Permission is hereby granted, free of charge, to any person obtaining a copy 4of this software and associated documentation files (the "Software"), to deal 5in the Software without restriction, including without limitation the rights 6to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7copies of the Software, and to permit persons to whom the Software is 8furnished to do so, subject to the following conditions: 9The above copyright notice and this permission notice shall be included in 10all copies or substantial portions of the Software. 11THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 12IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 14AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 15LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 16OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 17THE SOFTWARE.*/ 18 19(function ($) { 20 21 //taken from http://stackoverflow.com/questions/283956/is-there-any-way-to-specify-a-suggested-filename-when-using-data-uri#15832569 22 function downloadWithName(uri, name, mime_type) { 23 function eventFire(el, etype) { 24 if (el.fireEvent) { 25 (el.fireEvent('on' + etype)); 26 } else { 27 var evObj = document.createEvent('Events'); 28 evObj.initEvent(etype, true, false); 29 el.dispatchEvent(evObj); 30 } 31 } 32 33 var link = document.createElement('a'); 34 35 if (typeof link.download != "undefined" && !/Edge/.test(navigator.userAgent)) { 36 document.body.appendChild(link); 37 uri = mime_type + ';' + uri; 38 // download attribute is supported
39 link.download = name; 40 link.href = uri; 41 link.target = "_blank"; 42 link.click(); 43 } 44 else { 45 var blob = b64toBlob(uri.replace("base64,", ""), mime_type); 46 navigator.msSaveOrOpenBlob(blob, name); 47 } 48 } 49 50 $.fn.extend({ 51 tableExport: function (options) { 52 var defaults = { 53 separator: ',', 54 ignoreColumn: [], 55 tableName: 'yourTableName', 56 type: 'csv', 57 pdfFontSize: 14, 58 pdfLeftMargin: 0, 59 escape: 'true', 60 htmlContent: 'false', 61 consoleLog: 'false', 62 fileName: 'Download' 63 }; 64 65 var options = $.extend(defaults, options); 66 var el = this; 67 var available_types = ["csv", "txt", "sql", "json", "xml", "excel", "doc", "powerpoint", "png"]; 68 69 var mime_type = ''; 70 var base64data = ''; 71 var file_name = ''; 72 73 if (defaults.type == 'csv' || defaults.type == 'txt') { 74 75 // Header 76 var tdData = ""; 77 $(el).find('thead').find('tr').each(function () { 78 tdData += "\n"; 79 $(this).filter(':visible').find('th').each(function (index, data) { 80 if ($(this).css('display') != 'none') { 81 if (defaults.ignoreColumn.indexOf(index) == -1) { 82 tdData += '"' + parseString($(this)) + '"' + defaults.separator; 83 } 84 } 85 86 }); 87 tdData = $.trim(tdData); 88 tdData = $.trim(tdData).substring(0, tdData.length - 1); 89 }); 90 91 // Row vs Column 92 $(el).find('tbody').find('tr').each(function () { 93 tdData += "\n"; 94 $(this).filter(':visible').find('td').each(function (index, data) { 95 if ($(this).css('display') != 'none') { 96 if (defaults.ignoreColumn.indexOf(index) == -1) { 97 tdData += '"' + parseString($(this)) + '"' + defaults.separator; 98 } 99 } 100 }); 101 //tdData = $.trim(tdData); 102 tdData = $.trim(tdData).substring(0, tdData.length - 1); 103 }); 104 105 //output 106 if (defaults.consoleLog == 'true') { 107 console.log(tdData); 108 } 109 110 base64data = "base64," + $.base64.encode(tdData); 111 mime_type = "data:application/" + defaults.type; 112 file_name = defaults.fileName + defaults.type; 113 } else if (defaults.type == 'sql') { 114 115 // Header 116 var tdData = "INSERT INTO `" + defaults.tableName + "` ("; 117 $(el).find('thead').find('tr').each(function () { 118 119 $(this).filter(':visible').find('th').each(function (index, data) { 120 if ($(this).css('display') != 'none') { 121 if (defaults.ignoreColumn.indexOf(index) == -1) { 122 tdData += '`' + parseString($(this)) + '`,'; 123 } 124 } 125 126 }); 127 tdData = $.trim(tdData); 128 tdData = $.trim(tdData).substring(0, tdData.length - 1); 129 }); 130 tdData += ") VALUES "; 131 // Row vs Column 132 $(el).find('tbody').find('tr').each(function () { 133 tdData += "("; 134 $(this).filter(':visible').find('td').each(function (index, data) { 135 if ($(this).css('display') != 'none') { 136 if (defaults.ignoreColumn.indexOf(index) == -1) { 137 tdData += '"' + parseString($(this)) + '",'; 138 } 139 } 140 }); 141 142 tdData = $.trim(tdData).substring(0, tdData.length - 1); 143 tdData += "),"; 144 }); 145 tdData = $.trim(tdData).substring(0, tdData.length - 1); 146 tdData += ";"; 147 148 //output 149 //console.log(tdData); 150 151 if (defaults.consoleLog == 'true') { 152 console.log(tdData); 153 } 154 155 base64data = "base64," + $.base64.encode(tdData); 156 mime_type = "data:application/" + defaults.type; 157 file_name = defaults.fileName + defaults.type; 158 159 } else if (defaults.type == 'json') { 160 161 var jsonHeaderArray = []; 162 $(el).find('thead').find('tr').each(function () { 163 var tdData = ""; 164 var jsonArrayTd = []; 165 166 $(this).filter(':visible').find('th').each(function (index, data) { 167 if ($(this).css('display') != 'none') { 168 if (defaults.ignoreColumn.indexOf(index) == -1) { 169 jsonArrayTd.push(parseString($(this))); 170 } 171 } 172 }); 173 jsonHeaderArray.push(jsonArrayTd); 174 175 }); 176 177 var jsonArray = []; 178 $(el).find('tbody').find('tr').each(function () { 179 var tdData = ""; 180 var jsonArrayTd = []; 181 182 $(this).filter(':visible').find('td').each(function (index, data) { 183 if ($(this).css('display') != 'none') { 184 if (defaults.ignoreColumn.indexOf(index) == -1) { 185 jsonArrayTd.push(parseString($(this))); 186 } 187 } 188 }); 189 jsonArray.push(jsonArrayTd); 190 191 }); 192 193 var jsonExportArray = []; 194 jsonExportArray.push({ header: jsonHeaderArray, data: jsonArray }); 195 196 //Return as JSON 197 //console.log(JSON.stringify(jsonExportArray)); 198 199 //Return as Array 200 //console.log(jsonExportArray); 201 if (defaults.consoleLog == 'true') { 202 console.log(JSON.stringify(jsonExportArray)); 203 } 204 205 base64data = "base64," + $.base64.encode(jsonExportArray); 206 mime_type = "data:application/" + defaults.type; 207 file_name = defaults.fileName + defaults.type; 208 } else if (defaults.type == 'xml') { 209 210 var xml = '<?xml version="1.0" encoding="utf-8"?>'; 211 xml += '<tabledata><fields>'; 212 213 // Header 214 $(el).find('thead').find('tr').each(function () { 215 $(this).filter(':visible').find('th').each(function (index, data) { 216 if ($(this).css('display') != 'none') { 217 if (defaults.ignoreColumn.indexOf(index) == -1) { 218 xml += "<field>" + parseString($(this)) + "</field>"; 219 } 220 } 221 }); 222 }); 223 xml += '</fields><data>'; 224 225 // Row Vs Column 226 var rowCount = 1; 227 $(el).find('tbody').find('tr').each(function () { 228 xml += '<row id="' + rowCount + '">'; 229 var colCount = 0; 230 $(this).filter(':visible').find('td').each(function (index, data) { 231 if ($(this).css('display') != 'none') { 232 if (defaults.ignoreColumn.indexOf(index) == -1) { 233 xml += "<column-" + colCount + ">" + parseString($(this)) + "</column-" + colCount + ">"; 234 } 235 } 236 colCount++; 237 }); 238 rowCount++; 239 xml += '</row>'; 240 }); 241 xml += '</data></tabledata>' 242 243 if (defaults.consoleLog == 'true') { 244 console.log(xml); 245 } 246 247 base64data = "base64," + $.base64.encode(xml); 248 mime_type = "data:application/" + defaults.type; 249 file_name = defaults.fileName + defaults.type; 250 251 } else if (defaults.type == 'excel' || defaults.type == 'doc' || defaults.type == 'powerpoint') { 252 var excel = "<table>"; 253 // Header 254 $(el).find('thead').find('tr').each(function () { 255 excel += "<tr>"; 256 $(this).find('th').each(function (index, data) { 257 if (defaults.ignoreColumn.indexOf(index) == -1) { 258 excel += "<td>" + parseString($(this)) + "</td>"; 259 } 260 }); 261 excel += '</tr>'; 262 263 }); 264 265 266 // Row Vs Column 267 var rowCount = 1; 268 $(el).find('tbody').find('tr').each(function () { 269 excel += "<tr>"; 270 var colCount = 0; 271 $(this).find('td').each(function (index, data) { 272 if (defaults.ignoreColumn.indexOf(index) == -1) { 273 excel += "<td>" + parseString($(this)) + "</td>"; 274 } 275 colCount++; 276 }); 277 rowCount++; 278 excel += '</tr>'; 279 }); 280 excel += '</table>' 281 282 if (defaults.consoleLog == 'true') { 283 console.log(excel); 284 } 285 286 var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:" + defaults.type + "' xmlns='http://www.w3.org/TR/REC-html40'>"; 287 excelFile += "<head>"; 288 excelFile += "<!--[if gte mso 9]>"; 289 excelFile += "<xml>"; 290 excelFile += "<x:ExcelWorkbook>"; 291 excelFile += "<x:ExcelWorksheets>"; 292 excelFile += "<x:ExcelWorksheet>"; 293 excelFile += "<x:Name>"; 294 excelFile += "{worksheet}"; 295 excelFile += "</x:Name>"; 296 excelFile += "<x:WorksheetOptions>"; 297 excelFile += "<x:DisplayGridlines/>"; 298 excelFile += "</x:WorksheetOptions>"; 299 excelFile += "</x:ExcelWorksheet>"; 300 excelFile += "</x:ExcelWorksheets>"; 301 excelFile += "</x:ExcelWorkbook>"; 302 excelFile += "</xml>"; 303 excelFile += "<![endif]-->"; 304 excelFile += "</head>"; 305 excelFile += "<body>"; 306 excelFile += excel; 307 excelFile += "</body>"; 308 excelFile += "</html>"; 309 310 base64data = "base64," + $.base64.encode(excelFile); 311 mime_type = "data:application/vnd.ms-" + defaults.type; 312 313 switch (defaults.type) { 314 case 'excel': 315 file_name = defaults.fileName + ".xls"; 316 break; 317 case 'doc': 318 file_name = defaults.fileName + ".doc"; 319 break; 320 case 'powerpoint': 321 file_name = defaults.fileName + ".ppt"; 322 break; 323 } 324 325 } else if (defaults.type == 'png') { 326 html2canvas($(el), { 327 onrendered: function (canvas) { 328 var img = canvas.toDataURL("image/png"); 329 window.open(img); 330 331 332 } 333 }); 334 } else if (defaults.type == 'pdf') { 335 336 var doc = new jsPDF('p', 'pt', 'a4', true); 337 doc.setFontSize(defaults.pdfFontSize); 338 339 // Header 340 var startColPosition = defaults.pdfLeftMargin; 341 $(el).find('thead').find('tr').each(function () { 342 $(this).filter(':visible').find('th').each(function (index, data) { 343 if ($(this).css('display') != 'none') { 344 if (defaults.ignoreColumn.indexOf(index) == -1) { 345 var colPosition = startColPosition + (index * 50); 346 doc.text(colPosition, 20, parseString($(this))); 347 } 348 } 349 }); 350 }); 351 352 353 // Row Vs Column 354 var startRowPosition = 20; var page = 1; var rowPosition = 0; 355 $(el).find('tbody').find('tr').each(function (index, data) { 356 rowCalc = index + 1; 357 358 if (rowCalc % 26 == 0) { 359 doc.addPage(); 360 page++; 361 startRowPosition = startRowPosition + 10; 362 } 363 rowPosition = (startRowPosition + (rowCalc * 10)) - ((page - 1) * 280); 364 365 $(this).filter(':visible').find('td').each(function (index, data) { 366 if ($(this).css('display') != 'none') { 367 if (defaults.ignoreColumn.indexOf(index) == -1) { 368 var colPosition = startColPosition + (index * 50); 369 doc.text(colPosition, rowPosition, parseString($(this))); 370 } 371 } 372 373 }); 374 375 }); 376 377 // Output as Data URI 378 doc.output('datauri'); 379 380 } 381 382 if (available_types.indexOf(defaults.type) != -1) { 383 downloadWithName(base64data, file_name, mime_type); //didnt work when I tested in ff25 384 } 385 386 function parseString(data) { 387 388 if (defaults.htmlContent == 'true') { 389 content_data = data.html().trim(); 390 } else { 391 content_data = data.text().trim(); 392 } 393 394 if (defaults.escape == 'true') { 395 content_data = escape(content_data); 396 } 397 398 return content_data; 399 } 400 401 } 402 });
403 404 function b64toBlob(b64Data, contentType, sliceSize) { 405 contentType = contentType || ''; 406 sliceSize = sliceSize || 512; 407 408 var byteCharacters = atob(b64Data); 409 var byteArrays = []; 410 411 for (var offset = 0; offset < byteCharacters.length; offset += sliceSize) { 412 var slice = byteCharacters.slice(offset, offset + sliceSize); 413 414 var byteNumbers = new Array(slice.length); 415 for (var i = 0; i < slice.length; i++) { 416 byteNumbers[i] = slice.charCodeAt(i); 417 } 418 419 var byteArray = new Uint8Array(byteNumbers); 420 421 byteArrays.push(byteArray); 422 } 423 424 var blob = new Blob(byteArrays, { type: contentType }); 425 return blob; 426 } 427})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.