var ResponsiveTable;
(function(){
var $ = jQuery;
ResponsiveTable = function(spreadsheetKey, columns) {
var self = this;
self.columns = columns;
self.init = function(dataSpreadsheet) {
self.pym = new pym.Child({ polling: 500 });
self.ttop = Tabletop.init({
key: dataSpreadsheet,
callback: self.writeTable,
simpleSheet: true,
debug: false
});
}
self.sortData = function(dataSource) {
dataSource.sort(function(a, b){
var one = a[self.columns[0][0]],
two = b[self.columns[0][0]];
if (!isNaN(Number(one)))
one = Number(one);
if (!isNaN(Number(two)))
two = Number(two);
if(one < two) return -1;
if(one > two) return 1;
return 0;
});
return dataSource
};
self.writeTable = function(dataSource){
dataSource = self.sortData(dataSource);
$('#data').html('
');
self.createTableColumns('#data table');
self.populateTable(dataSource, '#data table');
$('#data table').table().fadeIn();
self.pym.sendHeightToParent();
};
self.createTableColumns = function(table){
var thead = $('
');
$.each(self.columns, function(i, v) {
if (i == 0)
thead.find('tr').append('' + v[1] + ' | ');
else
thead.find('tr').append('' + v[1] + ' | ');
});
$(table).data('columns', self.columns);
$(table).append(thead);
}
self.populateTable = function(dataSource, table) {
var tab = $(table);
columns = $.map($(table).data('columns'), function(d) { return d[0]; });
tab.append('');
$.each(dataSource, function(idx, data) {
var row = $('
');
$.each(data, function(key, val) {
if (columns.indexOf(key) >= 0) {
var cell = $(' | ');
if (columns.indexOf(key) == 0)
cell.data('priority', 'persist');
else
cell.data('priority', columns.indexOf(key));
// Turn urls into actual anchors
if (val.match) {
var matches = val.match(/(https?:\/\/)([\dA-Za-z\.-]+)\.([A-Za-z\.]{2,6})([\/\w \.-]*)*\/?(\?.*)?(\#.*)?/g);
if (matches) {
var replacement = '' + matches[0] + '';
val = val.replace(matches[0], replacement);
}
}
cell.html(val);
row.append(cell);
}
});
tab.find('tbody').append(row);
});
}
$(document).ready(self.init.bind(self, spreadsheetKey));
};
})();