Created
April 9, 2015 23:26
-
-
Save ityonemo/00fe07380fd28b556d1d to your computer and use it in GitHub Desktop.
readable and marginally commented version of http://scripts.shpg.org/TableThis.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var $$; | |
| if(!Array.prototype.forEach){ | |
| Array.prototype.forEach = function(fun){ | |
| var len=this.length; | |
| if(typeof fun!=="function"){ | |
| throw new TypeError(); | |
| } | |
| var thisp = arguments[1]; | |
| for(var i=0;i<len;i++){ | |
| if(i in this){ | |
| fun.call(thisp,this[i],i,this); | |
| } | |
| } | |
| }; | |
| } | |
| if(typeof String.prototype.trim!=='function'){ | |
| String.prototype.trim = function(){ | |
| return this.replace(/^\s+|\s+$/g,''); | |
| } | |
| } | |
| function init(){ | |
| $$=jQuery; | |
| //MOUSEENTER CODE FOR ALL TABLES | |
| $$('table').mouseenter( | |
| function(){ | |
| //store a few variables to be accessed within the closure without getting confused by "this" | |
| var pos=$$(this).offset(); | |
| var width=$$(this).width(); | |
| var height=$$(this).height(); | |
| $$(this).css("cursor","pointer"); | |
| //create a new div, and make it a 'table-overlay' | |
| //NB: Jquery passed HTML code creates a new element on the DOM | |
| var tableOverlay=$$("<div></div>").addClass('table-overlay'); | |
| tableOverlay.offset(pos).width(width).height(height); | |
| tableOverlay.css( | |
| { "position":"absolute", | |
| "background":"#000", | |
| "background":"rgba(0,0,0,0.4)", | |
| "z-index":100000, | |
| "pointer-events":"none", | |
| "cursor":"pointer", | |
| "display":"table"} | |
| ); | |
| var tableText=$$("<div></div>").addClass('table-text'); | |
| tableText.text('Click to download table as CSV file').css( | |
| { "color":"white", | |
| "pointer-events":"none", | |
| "display":"table-cell", | |
| "text-align":"center", | |
| "vertical-align":"middle", | |
| "font-size":"26px", | |
| "font-family":"Arial", | |
| "font-weight":"800"} | |
| ); | |
| tableOverlay=tableOverlay.append(tableText); | |
| $$('body').append(tableOverlay); | |
| }); | |
| //MOUSELEAVE CODE FOR ALL TABLES | |
| //it suffices to destroy the table-overlay object | |
| $$('table').mouseleave( | |
| function(){ | |
| $$('div.table-overlay').remove(); | |
| }); | |
| //CLICK CODE: BASICALLY FORWARD THIS TO THE 'tableData' function which gets defined later... | |
| $$('table').click( | |
| function(){ | |
| var table=$$(this); | |
| tableData(table); | |
| }) | |
| } | |
| function csvDownload(str,utf){ | |
| var a=document.createElement('a'); | |
| if(utf) | |
| {a.href='data:text/csv;charset=utf-16,'+ str;} | |
| else {a.href='data:text/csv,'+ str;} | |
| a.target='_blank'; | |
| a.download="tabledownload.csv"; | |
| document.body.appendChild(a); | |
| a.click(); | |
| } | |
| function utf16Checker(str){ | |
| var re=/%u\d/; | |
| var string=escape(str); | |
| var res=string.search(re); | |
| return(res!==-1); | |
| } | |
| function tableData(table){ | |
| var tr=table.find('> tr, > tbody > tr'); | |
| var tableString=""; | |
| var tableData = $$.map(tr,function(tr){ | |
| var row=[$$.map($$(tr).children(),function(td){return $$(td).text();})]; | |
| return row;} | |
| ); | |
| tableData.forEach(function(entry){ | |
| entry.forEach(function(i){ | |
| var re=new RegExp(" ","g"), rdq=new RegExp('"',"g"), rlb=new RegExp("\n","g"); | |
| i=i.trim(); | |
| //URL escaping | |
| i=i.replace(re,"%20"); | |
| i=i.replace(rdq,'\"'); | |
| i=i.replace(rlb,'%0D'); | |
| tableString+='"'+ i+'",';} | |
| ) | |
| tableString=tableString.substring(0,tableString.length- 1); | |
| tableString+="%0A";} | |
| ); | |
| if(utf16Checker(tableString)){ | |
| csvDownload(tableString,true); | |
| alert("NOTE: Because this table contains special characters, the data has been downloaded in UTF-16 format. \n\nThis may cause compatibility issues with some spreadsheet programs."); | |
| }else{csvDownload(tableString,false);} | |
| } | |
| //initialize this baby | |
| init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment