Created
July 20, 2014 16:51
-
-
Save kylekarpack/8c20cb8aeaef22cb5df4 to your computer and use it in GitHub Desktop.
This file contains 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
(function(){ | |
var cache = {}; | |
this.tmpl = function tmpl(str, data){ | |
// Figure out if we're getting a template, or if we need to | |
// load the template - and be sure to cache the result. | |
var fn = !/\W/.test(str) ? | |
cache[str] = cache[str] || | |
tmpl(document.getElementById(str).innerHTML) : | |
// Generate a reusable function that will serve as a template | |
// generator (and which will be cached). | |
new Function("obj", | |
"var p=[],print=function(){p.push.apply(p,arguments);};" + | |
// Introduce the data as local variables using with(){} | |
"with(obj){p.push('" + | |
// Convert the template into pure JavaScript | |
str.replace(/[\r\t\n]/g, " ") | |
.split("{{").join("\t") | |
.replace(/((^|}})[^\t]*)'/g, "$1\r") | |
.replace(/\t=(.*?)}}/g, "',$1,'") | |
.split("\t").join("');") | |
.split("}}").join("p.push('") | |
.split("\r").join("\\'") | |
+ "');}return p.join('');"); | |
// Provide some basic currying to the user | |
return data ? fn( data ) : fn; | |
}; | |
})(); | |
var t = (function() { | |
//var htmlStore = $("table") | |
//$(".done").css("background","green").css("color","#fff"); | |
var uri = 'data:application/vnd.ms-excel;base64,', | |
template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{{=worksheet}}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>{{for(var i=0; i<tables.length;i++){ }}<table>{{=tables[i]}}</table>{{ } }}</body></html>', | |
IEbase64 = function(s) { return Base64.encode(unescape(encodeURIComponent(s))) }, | |
base64 = function(s) { return window.btoa(unescape(encodeURIComponent(s))) }, | |
format = function(s, c) { return s.replace(/{(\w+)}/g, function(m, p) { return c[p]; }) } | |
return function(tableList, name) { | |
if (!tableList.length>0 && !tableList[0].nodeType) table = document.getElementById(table) | |
var tables = []; | |
for(var i=0; i<tableList.length; i++){tables.push(tableList[i].innerHTML);} | |
var ctx = {worksheet: name || 'Worksheet', tables: tables}; | |
//console.log(base64(tmpl(template, ctx))); | |
//console.log("\n---------------------------------------------------------------------------------------\n") | |
//console.log(IEbase64(tmpl(template, ctx))); | |
window.location.href = uri + IEbase64(tmpl(template, ctx)) | |
} | |
})(); | |
var Base64 = { | |
// private property | |
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", | |
// public method for encoding | |
encode : function (input) { | |
var output = ""; | |
var chr1, chr2, chr3, enc1, enc2, enc3, enc4; | |
var i = 0; | |
input = Base64._utf8_encode(input); | |
while (i < input.length) { | |
chr1 = input.charCodeAt(i++); | |
chr2 = input.charCodeAt(i++); | |
chr3 = input.charCodeAt(i++); | |
enc1 = chr1 >> 2; | |
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); | |
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); | |
enc4 = chr3 & 63; | |
if (isNaN(chr2)) { | |
enc3 = enc4 = 64; | |
} else if (isNaN(chr3)) { | |
enc4 = 64; | |
} | |
output = output + | |
this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + | |
this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); | |
} | |
return output; | |
}, | |
// private method for UTF-8 encoding | |
_utf8_encode : function (string) { | |
string = string.replace(/\r\n/g,"\n"); | |
var utftext = ""; | |
for (var n = 0; n < string.length; n++) { | |
var c = string.charCodeAt(n); | |
if (c < 128) { | |
utftext += String.fromCharCode(c); | |
} | |
else if((c > 127) && (c < 2048)) { | |
utftext += String.fromCharCode((c >> 6) | 192); | |
utftext += String.fromCharCode((c & 63) | 128); | |
} | |
else { | |
utftext += String.fromCharCode((c >> 12) | 224); | |
utftext += String.fromCharCode(((c >> 6) & 63) | 128); | |
utftext += String.fromCharCode((c & 63) | 128); | |
} | |
} | |
return utftext; | |
}, | |
// private method for UTF-8 decoding | |
_utf8_decode : function (utftext) { | |
var string = ""; | |
var i = 0; | |
var c = c1 = c2 = 0; | |
while ( i < utftext.length ) { | |
c = utftext.charCodeAt(i); | |
if (c < 128) { | |
string += String.fromCharCode(c); | |
i++; | |
} | |
else if((c > 191) && (c < 224)) { | |
c2 = utftext.charCodeAt(i+1); | |
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); | |
i += 2; | |
} | |
else { | |
c2 = utftext.charCodeAt(i+1); | |
c3 = utftext.charCodeAt(i+2); | |
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); | |
i += 3; | |
} | |
} | |
return string; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment