Changeset View
Changeset View
Standalone View
Standalone View
swh/web/assets/src/bundles/admin/deposit.js
Show All 9 Lines | if (type === 'display') { | ||||
if (data && data.startsWith('swh')) { | if (data && data.startsWith('swh')) { | ||||
let browseUrl = Urls.browse_swh_id(data); | let browseUrl = Urls.browse_swh_id(data); | ||||
return `<a href="${browseUrl}">${data}</a>`; | return `<a href="${browseUrl}">${data}</a>`; | ||||
} | } | ||||
} | } | ||||
return data; | return data; | ||||
} | } | ||||
function filterDataWithExcludePattern(data, excludePattern) { | |||||
/* Return true if the data is to be filtered, false otherwise. | |||||
Args: | |||||
data (dict): row dict data | |||||
excludePattern (str): pattern to lookup in data columns | |||||
Returns: | |||||
true if the data is to be excluded (because it matches), false otherwise | |||||
*/ | |||||
if (excludePattern === '') { | |||||
return false; // otherwise, everything gets excluded | |||||
} | |||||
for (const key in data) { | |||||
let value = data[key]; | |||||
if ((typeof value === 'string' || value instanceof String) && | |||||
value.search(excludePattern) !== -1) { | |||||
return true; // exclude the data from filtering | |||||
} | |||||
} | |||||
return false; | |||||
} | |||||
export function initDepositAdmin() { | export function initDepositAdmin() { | ||||
let depositsTable; | let depositsTable; | ||||
$(document).ready(() => { | $(document).ready(() => { | ||||
$.fn.dataTable.ext.errMode = 'none'; | $.fn.dataTable.ext.errMode = 'none'; | ||||
depositsTable = $('#swh-admin-deposit-list') | depositsTable = $('#swh-admin-deposit-list') | ||||
.on('error.dt', (e, settings, techNote, message) => { | .on('error.dt', (e, settings, techNote, message) => { | ||||
$('#swh-admin-deposit-list-error').text(message); | $('#swh-admin-deposit-list-error').text(message); | ||||
}) | }) | ||||
.DataTable({ | .DataTable({ | ||||
serverSide: true, | serverSide: true, | ||||
processing: true, | processing: true, | ||||
// let's define the order of table options display | // let's define the order of table options display | ||||
// f: (f)ilter | // f: (f)ilter | ||||
// l: (l)ength changing | // l: (l)ength changing | ||||
// r: p(r)ocessing | // r: p(r)ocessing | ||||
// t: (t)able | // t: (t)able | ||||
// i: (i)nfo | // i: (i)nfo | ||||
// p: (p)agination | // p: (p)agination | ||||
// see https://datatables.net/examples/basic_init/dom.html | // see https://datatables.net/examples/basic_init/dom.html | ||||
dom: '<<f<"#list-exclude">l>rt<"bottom"ip>>', | dom: '<<"d-flex justify-content-between align-items-center"f' + | ||||
'<"#list-exclude">l>rt<"bottom"ip>>', | |||||
// div#list-exclude is a custom filter added next to dataTable | // div#list-exclude is a custom filter added next to dataTable | ||||
// initialization below through js dom manipulation, see | // initialization below through js dom manipulation, see | ||||
// https://datatables.net/examples/advanced_init/dom_toolbar.html | // https://datatables.net/examples/advanced_init/dom_toolbar.html | ||||
ajax: { | ajax: { | ||||
url: Urls.admin_deposit_list(), | url: Urls.admin_deposit_list(), | ||||
// filtering data set depending on the exclude search input | data: d => { | ||||
dataFilter: function(dataResponse) { | d.excludePattern = $('#swh-admin-deposit-list-exclude-filter').val(); | ||||
/* Filter out data returned by the server to exclude entries | |||||
matching the exclude pattern. | |||||
Args | |||||
dataResponse (str): the json response in string | |||||
Returns: | |||||
json response altered (in string) | |||||
*/ | |||||
// | |||||
let data = jQuery.parseJSON(dataResponse); | |||||
let excludePattern = $('#swh-admin-deposit-list-exclude-filter').val(); | |||||
let recordsFiltered = 0; | |||||
let filteredData = []; | |||||
for (const row of data.data) { | |||||
if (filterDataWithExcludePattern(row, excludePattern)) { | |||||
recordsFiltered += 1; | |||||
} else { | |||||
filteredData.push(row); | |||||
} | |||||
} | |||||
// update data values | |||||
data['recordsFiltered'] = recordsFiltered; | |||||
data['data'] = filteredData; | |||||
return JSON.stringify(data); | |||||
} | } | ||||
}, | }, | ||||
columns: [ | columns: [ | ||||
{ | { | ||||
data: 'id', | data: 'id', | ||||
name: 'id' | name: 'id' | ||||
}, | }, | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 109 Lines • Show Last 20 Lines |