Skip to content

Commit

Permalink
feat(javascript): Add extendable javascript module
Browse files Browse the repository at this point in the history
  • Loading branch information
mikestreety committed Oct 1, 2024
1 parent 4ffab44 commit 75f3aad
Showing 1 changed file with 145 additions and 0 deletions.
145 changes: 145 additions & 0 deletions Resources/Public/JavaScript/ModuleDataListing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/* global define, TYPO3 */
/* eslint no-unused-vars: "off" */

define([
'jquery',
'datatables.net',
'datatables.net-buttons',
'datatables.net-buttons-print',
'datatables.net-buttons-html5'
], function () {
var ModuleDataListing = {
settings: {
ajaxUrlKey: '',
tableSelector: '#mdl-datatable',
searchButtonSelector: '.mdl-search',

dataTable: {
processing: true,
serverSide: true,
order: [[0, 'desc']],
dom: '<\'form-inline form-inline-spaced\'lf>prtipB',
buttons: [
{
extend: 'csv',
text: 'Download as CSV',
className: 'btn btn-primary'
},
{
extend: 'copy',
text: 'Copy to clipboard',
className: 'btn btn-default'
}
],
pagingType: 'full_numbers',
language: {
emptyTable: 'No data available in table'
},
lengthMenu: [
[
10,
25,
50,
100,
-1
],
[
10,
25,
50,
100,
'All'
]
],
classes: {
sLength: 'form-group',
sLengthSelect: 'form-control input-sm',
sFilter: 'form-group',
sFilterInput: 'form-control input-sm'
}
}
},

config: function(options = {}) {
ModuleDataListing.settings = Object.assign({}, ModuleDataListing.settings, options);
}
};

ModuleDataListing.local = {
get: function(key) {
let storage = localStorage.getItem('ModuleDataListing');
storage = JSON.parse(storage);

return storage[key];
},
set: function(key, data) {
let storage = localStorage.getItem('ModuleDataListing') ?? '';
storage = JSON.parse(storage);

storage[key] = data;

localStorage.setItem('ModuleDataListing', JSON.stringify(storage));
},
}

ModuleDataListing.dataTable = {
init: function(filters) {
const settings = {
search: {
search: ''
},
ajax: {
url: TYPO3.settings.ajaxUrls[ModuleDataListing.settings.ajaxUrlKey],
data: {
filters: filters
}
},
}
const table = $(ModuleDataListing.settings.tableSelector)
.DataTable(
Object.assign({}, ModuleDataListing.settings.dataTable, settings)
);

table.on('search.dt', function () {
// // Get the current URL
// let url = new URL(window.location);
// url.searchParams.set('search', table.search());
// window.history.pushState({}, '', url);
});

return table;
},

destroy: function() {
$(ModuleDataListing.settings.tableSelector).DataTable().destroy();
},

restart: function(filters) {
ModuleDataListing.dataTable.destroy();
ModuleDataListing.dataTable.init(filters);
}
};

ModuleDataListing.filters = {
init: function () {
$(ModuleDataListing.settings.searchButtonSelector).click(function () {
let filters = {}

$('div[data-mdl-filter]').each(function () {
let self = $(this);
let filterData = [];

self.find('input:checked').each(function () {
filterData.push($(this).val());
});

filters[self.data('mdl-filter')] = filterData;
});

ModuleDataListing.dataTable.restart(filters);
});
}
}

return ModuleDataListing;
});

0 comments on commit 75f3aad

Please sign in to comment.