|
1 | 1 | /* global jQuery3, luxon, tableDataProxy */ |
2 | 2 | jQuery3(document).ready(function () { |
3 | | - bindTables(); |
4 | | -}); |
5 | | - |
6 | | -/** |
7 | | - * Binds all tables that have the class 'data-table' to a new JQuery DataTables instance. |
8 | | - */ |
9 | | -function bindTables() { |
10 | 3 | /** |
11 | | - * Creates the data table instance for the specified table element. |
| 4 | + * Binds all tables that have the class 'data-table' to a new JQuery DataTables instance. |
12 | 5 | */ |
13 | | - function createDataTable(table) { |
14 | | - const defaultConfiguration = { |
15 | | - language: { |
16 | | - emptyTable: 'Loading - please wait ...' |
17 | | - }, |
18 | | - deferRender: true, |
19 | | - pagingType: 'numbers', // page number button only |
20 | | - order: [[1, 'asc']], // default order, if not persisted yet |
21 | | - columnDefs: [ |
22 | | - { |
23 | | - targets: 'nosort', // All columns with the '.nosort' class in the <th> |
24 | | - orderable: false |
| 6 | + function bindTables() { |
| 7 | + /** |
| 8 | + * Creates the data table instance for the specified table element. |
| 9 | + */ |
| 10 | + function createDataTable(table) { |
| 11 | + const defaultConfiguration = { |
| 12 | + language: { |
| 13 | + emptyTable: 'Loading - please wait ...' |
25 | 14 | }, |
26 | | - { |
27 | | - targets: 'date', // All columns with the '.date' class in the <th> |
28 | | - render: function (data, type, _row, _meta) { |
29 | | - if (type === 'display') { |
30 | | - if (data === 0) { |
31 | | - return '-'; |
| 15 | + deferRender: true, |
| 16 | + pagingType: 'numbers', // page number button only |
| 17 | + order: [[1, 'asc']], // default order, if not persisted yet |
| 18 | + columnDefs: [ |
| 19 | + { |
| 20 | + targets: 'nosort', // All columns with the '.nosort' class in the <th> |
| 21 | + orderable: false |
| 22 | + }, |
| 23 | + { |
| 24 | + targets: 'date', // All columns with the '.date' class in the <th> |
| 25 | + render: function (data, type, _row, _meta) { |
| 26 | + if (type === 'display') { |
| 27 | + if (data === 0) { |
| 28 | + return '-'; |
| 29 | + } |
| 30 | + var dateTime = luxon.DateTime.fromMillis(data * 1000); |
| 31 | + return '<span data-toggle="tooltip" data-placement="bottom" title="' |
| 32 | + + dateTime.toLocaleString(luxon.DateTime.DATETIME_SHORT) + '">' |
| 33 | + + dateTime.toRelative({locale: 'en'}) + '</span>'; |
| 34 | + } |
| 35 | + else { |
| 36 | + return data; |
32 | 37 | } |
33 | | - var dateTime = luxon.DateTime.fromMillis(data * 1000); |
34 | | - return '<span data-toggle="tooltip" data-placement="bottom" title="' |
35 | | - + dateTime.toLocaleString(luxon.DateTime.DATETIME_SHORT) + '">' |
36 | | - + dateTime.toRelative({locale: 'en'}) + '</span>'; |
37 | | - } else { |
38 | | - return data; |
39 | 38 | } |
40 | 39 | } |
41 | | - } |
42 | | - ], |
43 | | - columns: JSON.parse(table.attr('data-columns-definition')) |
44 | | - }; |
45 | | - const tableConfiguration = JSON.parse(table.attr('data-table-configuration')); |
46 | | - // overwrite/merge the default configuration with values from the provided table configuration |
47 | | - const dataTable = table.DataTable(Object.assign(defaultConfiguration, tableConfiguration)); |
| 40 | + ], |
| 41 | + columns: JSON.parse(table.attr('data-columns-definition')) |
| 42 | + }; |
| 43 | + const tableConfiguration = JSON.parse(table.attr('data-table-configuration')); |
| 44 | + // overwrite/merge the default configuration with values from the provided table configuration |
| 45 | + const dataTable = table.DataTable(Object.assign(defaultConfiguration, tableConfiguration)); |
48 | 46 |
|
49 | | - // add the buttons to the top of the table |
50 | | - if (tableConfiguration.buttons) { |
51 | | - dataTable |
52 | | - .buttons() |
53 | | - .container() |
54 | | - .addClass('float-none mb-3') |
55 | | - .prependTo(jQuery3(dataTable.table().container()).closest('.table-responsive')); |
56 | | - } |
| 47 | + // add the buttons to the top of the table |
| 48 | + if (tableConfiguration.buttons) { |
| 49 | + dataTable |
| 50 | + .buttons() |
| 51 | + .container() |
| 52 | + .addClass('float-none mb-3') |
| 53 | + .prependTo(jQuery3(dataTable.table().container()).closest('.table-responsive')); |
| 54 | + } |
57 | 55 |
|
58 | | - return dataTable; |
59 | | - } |
| 56 | + return dataTable; |
| 57 | + } |
60 | 58 |
|
61 | | - /** |
62 | | - * Loads the content for the specified table element via an Ajax call. |
63 | | - */ |
64 | | - function loadTableData(table, dataTable) { |
65 | | - if (!table[0].hasAttribute('isLoaded')) { |
66 | | - table.attr('isLoaded', 'true'); |
67 | | - tableDataProxy.getTableRows(table.attr('id'), function (t) { |
68 | | - (function () { |
69 | | - const model = JSON.parse(t.responseObject()); |
70 | | - dataTable.rows.add(model).draw(); |
71 | | - jQuery3('[data-toggle="tooltip"]').tooltip(); |
72 | | - })(); |
73 | | - }); |
| 59 | + /** |
| 60 | + * Loads the content for the specified table element via an Ajax call. |
| 61 | + */ |
| 62 | + function loadTableData(table, dataTable) { |
| 63 | + if (!table[0].hasAttribute('isLoaded')) { |
| 64 | + table.attr('isLoaded', 'true'); |
| 65 | + tableDataProxy.getTableRows(table.attr('id'), function (t) { |
| 66 | + (function () { |
| 67 | + const model = JSON.parse(t.responseObject()); |
| 68 | + dataTable.rows.add(model).draw(); |
| 69 | + jQuery3('[data-toggle="tooltip"]').tooltip(); |
| 70 | + })(); |
| 71 | + }); |
| 72 | + } |
74 | 73 | } |
75 | | - } |
76 | 74 |
|
77 | | - const allTables = jQuery3('table.data-table'); |
| 75 | + const allTables = jQuery3('table.data-table'); |
78 | 76 |
|
79 | | - function toggleDetailsColumnIcon(tr, from, to) { |
80 | | - const svg = tr.find("use"); |
81 | | - const current = svg.attr("href"); |
82 | | - svg.attr("href", current.replace(from, to)) |
83 | | - } |
| 77 | + function toggleDetailsColumnIcon(tr, from, to) { |
| 78 | + const svg = tr.find("use"); |
| 79 | + const current = svg.attr("href"); |
| 80 | + svg.attr("href", current.replace(from, to)) |
| 81 | + } |
84 | 82 |
|
85 | | - allTables.each(function () { |
86 | | - const table = jQuery3(this); |
87 | | - const id = table.attr('id'); |
88 | | - const dataTable = createDataTable(table); |
| 83 | + allTables.each(function () { |
| 84 | + const table = jQuery3(this); |
| 85 | + const id = table.attr('id'); |
| 86 | + const dataTable = createDataTable(table); |
89 | 87 |
|
90 | | - // Add event listener for opening and closing details |
91 | | - table.on('click', 'div.details-control', function () { |
92 | | - const tr = jQuery3(this).parents('tr'); |
93 | | - const row = dataTable.row(tr); |
| 88 | + // Add event listener for opening and closing details |
| 89 | + table.on('click', 'div.details-control', function () { |
| 90 | + const tr = jQuery3(this).parents('tr'); |
| 91 | + const row = dataTable.row(tr); |
| 92 | + |
| 93 | + if (row.child.isShown()) { |
| 94 | + row.child.hide(); |
| 95 | + tr.removeClass('shown'); |
| 96 | + toggleDetailsColumnIcon(tr, "minus-circle", "plus-circle"); |
| 97 | + } |
| 98 | + else { |
| 99 | + row.child(jQuery3(this).data('description')).show(); |
| 100 | + tr.addClass('shown'); |
| 101 | + toggleDetailsColumnIcon(tr, "plus-circle", "minus-circle"); |
| 102 | + } |
| 103 | + }); |
94 | 104 |
|
95 | | - if (row.child.isShown()) { |
96 | | - row.child.hide(); |
97 | | - tr.removeClass('shown'); |
98 | | - toggleDetailsColumnIcon(tr, "minus-circle", "plus-circle"); |
| 105 | + if (table.is(":visible")) { |
| 106 | + loadTableData(table, dataTable); |
99 | 107 | } |
100 | 108 | else { |
101 | | - row.child(jQuery3(this).data('description')).show(); |
102 | | - tr.addClass('shown'); |
103 | | - toggleDetailsColumnIcon(tr, "plus-circle", "minus-circle"); |
| 109 | + table.on('becameVisible', function () { |
| 110 | + loadTableData(table, dataTable); |
| 111 | + }); |
104 | 112 | } |
105 | | - }); |
106 | 113 |
|
107 | | - if (table.is(":visible")) { |
108 | | - loadTableData(table, dataTable); |
109 | | - } |
110 | | - else { |
111 | | - table.on('becameVisible', function () { |
112 | | - loadTableData(table, dataTable); |
| 114 | + // Add event listener that stores the order a user selects |
| 115 | + table.on('order.dt', function () { |
| 116 | + const order = table.DataTable().order(); |
| 117 | + localStorage.setItem(id + '#orderBy', order[0][0]); |
| 118 | + localStorage.setItem(id + '#orderDirection', order[0][1]); |
113 | 119 | }); |
114 | | - } |
115 | | - |
116 | | - // Add event listener that stores the order a user selects |
117 | | - table.on('order.dt', function () { |
118 | | - const order = table.DataTable().order(); |
119 | | - localStorage.setItem(id + '#orderBy', order[0][0]); |
120 | | - localStorage.setItem(id + '#orderDirection', order[0][1]); |
121 | | - }); |
122 | 120 |
|
123 | | - /** |
124 | | - * Restores the order of every table by reading the local storage of the browser. |
125 | | - * If no order has been stored yet, the table is skipped. |
126 | | - * Also saves the default length of the number of table columns. |
127 | | - */ |
128 | | - const orderBy = localStorage.getItem(id + '#orderBy'); |
129 | | - const orderDirection = localStorage.getItem(id + '#orderDirection'); |
130 | | - if (orderBy && orderDirection) { |
131 | | - const order = [orderBy, orderDirection]; |
132 | | - try { |
133 | | - dataTable.order(order).draw(); |
134 | | - } |
135 | | - catch (ignore) { // TODO: find a way to determine the number of columns here |
136 | | - dataTable.order([[1, 'asc']]).draw(); |
| 121 | + /** |
| 122 | + * Restores the order of every table by reading the local storage of the browser. |
| 123 | + * If no order has been stored yet, the table is skipped. |
| 124 | + * Also saves the default length of the number of table columns. |
| 125 | + */ |
| 126 | + const orderBy = localStorage.getItem(id + '#orderBy'); |
| 127 | + const orderDirection = localStorage.getItem(id + '#orderDirection'); |
| 128 | + if (orderBy && orderDirection) { |
| 129 | + const order = [orderBy, orderDirection]; |
| 130 | + try { |
| 131 | + dataTable.order(order).draw(); |
| 132 | + } |
| 133 | + catch (ignore) { // TODO: find a way to determine the number of columns here |
| 134 | + dataTable.order([[1, 'asc']]).draw(); |
| 135 | + } |
137 | 136 | } |
138 | | - } |
139 | 137 |
|
140 | | - // Store paging size |
141 | | - table.on('length.dt', function (e, settings, len) { |
142 | | - localStorage.setItem(id + '#table-length', len); |
| 138 | + // Store paging size |
| 139 | + table.on('length.dt', function (e, settings, len) { |
| 140 | + localStorage.setItem(id + '#table-length', len); |
| 141 | + }); |
| 142 | + const storedLength = localStorage.getItem(id + '#table-length'); |
| 143 | + if (jQuery3.isNumeric(storedLength)) { |
| 144 | + dataTable.page.len(storedLength).draw(); |
| 145 | + } |
143 | 146 | }); |
144 | | - const storedLength = localStorage.getItem(id + '#table-length'); |
145 | | - if (jQuery3.isNumeric(storedLength)) { |
146 | | - dataTable.page.len(storedLength).draw(); |
147 | | - } |
148 | | - }); |
149 | | -} |
| 147 | + } |
| 148 | + |
| 149 | + bindTables(); |
| 150 | +}); |
0 commit comments