var loadingGif = $('');
function downloadAsCSV(content, fileName, mimeType) {
var a = document.createElement('a');
mimeType = mimeType || 'application/octet-stream';
if (navigator.msSaveBlob) { // IE10
navigator.msSaveBlob(new Blob([content], {
type: mimeType
}), fileName);
} else if (URL && 'download' in a) { //html5 A[download]
a.href = URL.createObjectURL(new Blob([content], {
type: mimeType
}));
a.setAttribute('download', fileName);
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
location.href = 'data:application/octet-stream,' + encodeURIComponent(content); // only this mime type is supported
}
}
function getCSVContent(tableId) {
var dataDelimiter = ',';
var lineDelimiter = '\n';
var headers = [];
$('#' + tableId + ' thead > tr:eq(0) th').each(function (i, e) { headers.push($(e).text()) })
var csvHeader = headers.join(dataDelimiter) + '\n';
var csvContent = csvHeader;
var rows = [];
$('#' + tableId + ' tbody tr').each(function(index, row) {
if ($(row).css('display') === 'none') // ommit filtered out rows
return;
var rowData = [];
$(row).children().each(function(colIndex, cell) {
rowData.push($(cell).text());
});
rows.push(rowData.join(dataDelimiter));
});
return csvHeader + rows.join(lineDelimiter);
}
function searchTable(tableId, searchKey, colIndicesToSearch, rowsToSkip) {
if (rowsToSkip === undefined) rowsToSkip = [];
tableId = '#' + tableId;
// search cleared, make all visible
if (searchKey === undefined || searchKey === '') {
$(tableId + " tr").each(function(index, elem) {
$(elem).show();
});
return;
}
$(tableId + " tr").each(function(index, elem) {
if (index != 0 && rowsToSkip.indexOf(index) === -1) { // skip column headers
$(elem).children().each(function(colIndex, dataElement) {
if (colIndicesToSearch.indexOf(colIndex) > -1) {
if (isNaN($(dataElement).text())) {
if ($(dataElement).text().toLowerCase().indexOf(searchKey) > -1)
$(elem).show();
else
$(elem).hide();
} else {
if (+$(dataElement).text() === +searchKey)
$(elem).show();
else
$(elem).hide();
}
}
});
}
});
}
function sortTable(table, sortByIndex, reversed) {
/* tableId = '#' + tableId;
var table = $(tableId); */
var rows = table.find('tbody tr').toArray().sort(comparator(sortByIndex));
if (reversed)
rows = rows.reverse()
table.find('tbody').empty();
for (var i = 0; i < rows.length; i++) table.append(rows[i]);
function comparator(keyIndex) {
return function(a, b) {
var valA = cellValue(a, keyIndex);
var valB = cellValue(b, keyIndex);
return $.isNumeric(valA) && $.isNumeric(valB) ? valA - valB : String(valA).localeCompare(valB);
}
}
function cellValue(row, keyIndex) {
return $(row).children('td').eq(keyIndex).text();
}
}
function rigTableHeadersForSort(tableId, skippedColumns) {
$('#' + tableId).find('tr:eq(0) th').click(function() {
if (skippedColumns && skippedColumns.indexOf($(this).index()) > -1) return;
var table = $(this).parents('table').eq(0);
sortTable(table, $(this).index(), !this.asc);
this.asc = !this.asc;
});
}
function decorateTableHeaderSort(tableId, skippedColumns) {
$('#' + tableId).find('tr:eq(0) th').each(function(header, index) {
if (skippedColumns && skippedColumns.indexOf($(this).index()) > -1) return;
$(this).append($(''))
});
}
// removes loading spinner after a certain amt of time
// to handle cases where the page isn't reloaded
function rigLinksAndFormButtons (context) {
context = context || $(document);
var gifContainer = $('').append(loadingGif);
$('a', context).each(function (index, elem) {
var handle = $(elem);
// console.log(handle.attr('href'));
var href = handle.attr('href');
if (href && !(href.indexOf('#',0) === 0)) {
handle.click(function(e) {
if (e.result === undefined || e.result)
handle.after($('').append(loadingGif));
});
}
});
$('button', context).each(function (index, elem) {
var handle = $(elem);
// dont handle download kind buttons
if (handle.text().indexOf('Download') > -1)
return true;
var hasDataTargetAttr = handle.attr('data-target');
var hasOnclickHandler = handle.attr('onclick');
/* var parentStart = handle.parent().offset().left;
var selfStart = handle.offset().left; */
var insertLeft = true;
if (handle.text().indexOf('Remove') > -1) // hackishhh
insertLeft = false;
if (!hasDataTargetAttr && !hasOnclickHandler) {
handle.click(function(e) {
if (e.result === undefined || e.result) {
if (insertLeft)
handle.before(gifContainer);
else
handle.after(gifContainer);
setTimeout(function() { $('.gifContainer').remove() }, 2000);
}
});
}
});
}
function setupTableSearch() {
$('#tag_report thead input[type]').keyup(function() {
if ($(this).val().length >= 0) {
searchTable('tag_report', $(this).val(), [$(this).parent().index()], [0, 1]);
}
});
$('#bulk_action_table thead input[type=text]').keyup(function() {
if ($(this).val().length >= 0) {
searchTable('bulk_action_table', $(this).val(), [$(this).parent().index()], [0, 1]);
}
});
$('#company_status thead input[type=text]').keyup(function() {
if ($(this).val().length >= 0) {
searchTable('company_status', $(this).val(), [$(this).parent().index()], [0, 1]);
}
});
}
function setupTableSort() {
decorateTableHeaderSort('tag_report', []);
rigTableHeadersForSort('tag_report', []);
decorateTableHeaderSort('company_report', []);
rigTableHeadersForSort('company_report', []);
decorateTableHeaderSort('bulk_action_table', [0]);
rigTableHeadersForSort('bulk_action_table', [0]);
decorateTableHeaderSort('company_status', []);
rigTableHeadersForSort('company_status', []);
}
function doBulkAction(tableId, opCode, dataColIndex, callback) {
var payload = {"opcode": opCode};
var data = []
$('#' + tableId + ' tbody tr').each(function (index, row) {
if ($(row).find('input.dashboard_select').attr('checked'))
data.push($(row).children(':eq(' + dataColIndex + ')').children('input').attr('data-value'));
});
payload["data"] = data;
$.ajax({
url: getHost() + '/contentAutomation/admin/action/bulk',
type: 'POST',
data: JSON.stringify(payload),
contentType: "application/json",
dataType: 'json',
success: function(response) {},
complete: function() { if (callback) callback() }
})
console.log(payload);
}
function setUpBulkAction () {
$('#bulk_submit').click(function() {
$(this).attr('disabled','disabled')
$(this).after(loadingGif);
doBulkAction('bulk_action_table', $('#operation_select option:selected').val(), 0, function() { location.reload() } );
});
}
function setUpBulkSelect() {
$('#select_all').click(function(e) {
if ($(e.target).attr('checked')) {
$('#bulk_action_table tbody tr').each(function(index, row) {
if ($(row).css('display') != 'none') {
$(row).find('input.dashboard_select').attr('checked', true);
}
});
} else {
$('#bulk_action_table input.dashboard_select').attr('checked', false);
}
});
}
$(document).ready(function() {
setupTableSearch();
setupTableSort();
setUpBulkSelect();
setUpBulkAction ();
rigLinksAndFormButtons();
});
;;