Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**
  2. * App Invoice List (js)
  3. */
  4. 'use strict';
  5. document.addEventListener('DOMContentLoaded', function () {
  6. const dt_invoice_table = document.querySelector('.invoice-list-table');
  7. if (dt_invoice_table) {
  8. const dt_invoice = new DataTable(dt_invoice_table, {
  9. ajax: assetsPath + 'json/invoice-list.json',
  10. columns: [
  11. { data: 'invoice_id' },
  12. { data: 'invoice_id', orderable: false, render: DataTable.render.select() },
  13. { data: 'invoice_id' },
  14. { data: 'invoice_status' },
  15. { data: 'issued_date' },
  16. { data: 'client_name' },
  17. { data: 'total' },
  18. { data: 'balance' },
  19. { data: 'invoice_status' },
  20. { data: 'action' }
  21. ],
  22. columnDefs: [
  23. {
  24. className: 'control',
  25. responsivePriority: 2,
  26. searchable: false,
  27. targets: 0,
  28. render: function () {
  29. return '';
  30. }
  31. },
  32. {
  33. targets: 1,
  34. orderable: false,
  35. searchable: false,
  36. responsivePriority: 4,
  37. render: function () {
  38. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  39. }
  40. },
  41. {
  42. targets: 2,
  43. render: function (data, type, full) {
  44. return `<a href="${baseUrl}app/invoice/preview">#${full['invoice_id']}</a>`;
  45. }
  46. },
  47. {
  48. // Invoice Status with tooltip
  49. targets: 3,
  50. render: function (data, type, full) {
  51. const invoiceStatus = full['invoice_status'];
  52. const balance = full['balance'];
  53. const dueDate = full['due_date'];
  54. const roleBadgeObj = {
  55. Sent: '<span class="badge p-1_5 rounded-pill bg-label-secondary"><i class="icon-base icon-16px bx bx-envelope"></i></span>',
  56. Draft:
  57. '<span class="badge p-1_5 rounded-pill bg-label-primary"><i class="icon-base icon-16px bx bx-folder"></i></span>',
  58. 'Past Due':
  59. '<span class="badge p-1_5 rounded-pill bg-label-danger"><i class="icon-base icon-16px bx bx-error"></i></span>',
  60. 'Partial Payment':
  61. '<span class="badge p-1_5 rounded-pill bg-label-success"><i class="icon-base icon-16px bx bx-check"></i></span>',
  62. Paid: '<span class="badge p-1_5 rounded-pill bg-label-warning"><i class="icon-base icon-16px bx bx-pie-chart-alt"></i></span>',
  63. Downloaded:
  64. '<span class="badge p-1_5 rounded-pill bg-label-info"><i class="icon-base icon-16px bx bx-down-arrow-alt"></i></span>'
  65. };
  66. // Sanitize tooltip content by escaping double quotes
  67. const tooltipContent = `
  68. ${invoiceStatus}<br>
  69. <span class="fw-medium">Balance:</span> ${balance}<br>
  70. <span class="fw-medium">Due Date:</span> ${dueDate}
  71. `.replace(/"/g, '&quot;');
  72. return `
  73. <span class="d-inline-block" data-bs-toggle="tooltip" data-bs-html="true" title="<span>${tooltipContent}">
  74. ${roleBadgeObj[invoiceStatus] || ''}
  75. </span>
  76. </span>
  77. `;
  78. }
  79. },
  80. {
  81. targets: 4,
  82. responsivePriority: 2,
  83. render: function (data, type, full) {
  84. const name = full['client_name'];
  85. const service = full['service'];
  86. const image = full['avatar_image'];
  87. const randNum = Math.floor(Math.random() * 11) + 1;
  88. const userImg = `${randNum}.png`;
  89. let output;
  90. if (image === true) {
  91. output = `<img src="${assetsPath}img/avatars/${userImg}" alt="Avatar" class="rounded-circle">`;
  92. } else {
  93. const stateNum = Math.floor(Math.random() * 6);
  94. const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
  95. const state = states[stateNum];
  96. const initials = (name.match(/\b\w/g) || [])
  97. .slice(0, 2)
  98. .map(letter => letter.toUpperCase())
  99. .join('');
  100. output = `<span class="avatar-initial rounded-circle bg-label-${state}">${initials}</span>`;
  101. }
  102. return `
  103. <div class="d-flex justify-content-start align-items-center">
  104. <div class="avatar-wrapper">
  105. <div class="avatar avatar-sm me-3">
  106. ${output}
  107. </div>
  108. </div>
  109. <div class="d-flex flex-column">
  110. <a href="${baseUrl}pages/profile-user" class="text-heading text-truncate"><span class="fw-medium">${name}</span></a>
  111. <small class="text-truncate">${service}</small>
  112. </div>
  113. </div>
  114. `;
  115. }
  116. },
  117. {
  118. targets: 5,
  119. render: function (data, type, full) {
  120. const total = full['total'];
  121. return `<span class="d-none">${total}</span>$${total}`;
  122. }
  123. },
  124. {
  125. targets: 6,
  126. render: function (data, type, full) {
  127. const dueDate = new Date(full['due_date']);
  128. return `
  129. <span class="d-none">${dueDate.toISOString().slice(0, 10).replace(/-/g, '')}</span>
  130. ${dueDate.toLocaleDateString('en-GB', { day: '2-digit', month: 'short', year: 'numeric' })}
  131. `;
  132. }
  133. },
  134. {
  135. targets: 7,
  136. orderable: false,
  137. render: function (data, type, full) {
  138. const balance = full['balance'];
  139. if (balance === 0) {
  140. return '<span class="badge bg-label-success text-capitalized"> Paid </span>';
  141. } else {
  142. return `<span class="d-none">${balance}</span><span class="text-heading">${balance}</span>`;
  143. }
  144. }
  145. },
  146. {
  147. targets: 8,
  148. visible: false
  149. },
  150. {
  151. targets: -1,
  152. title: 'Actions',
  153. searchable: false,
  154. orderable: false,
  155. render: function () {
  156. return (
  157. '<div class="d-flex align-items-center">' +
  158. '<a href="javascript:;" data-bs-toggle="tooltip" class="btn btn-icon delete-record" data-bs-placement="top" title="Delete"><i class="icon-base bx bx-trash icon-md"></i></a>' +
  159. '<a href="' +
  160. baseUrl +
  161. 'app/invoice/preview" data-bs-toggle="tooltip" class="btn btn-icon" data-bs-placement="top" title="Preview Invoice"><i class="icon-base bx bx-show icon-md"></i></a>' +
  162. '<div class="dropdown">' +
  163. '<a href="javascript:;" class="btn dropdown-toggle hide-arrow btn-icon p-0" data-bs-toggle="dropdown"><i class="icon-base bx bx-dots-vertical-rounded icon-md"></i></a>' +
  164. '<div class="dropdown-menu dropdown-menu-end">' +
  165. '<a href="javascript:;" class="dropdown-item">Download</a>' +
  166. '<a href="' +
  167. baseUrl +
  168. 'app/invoice/edit" class="dropdown-item">Edit</a>' +
  169. '<a href="javascript:;" class="dropdown-item">Duplicate</a>' +
  170. '</div>' +
  171. '</div>'
  172. );
  173. }
  174. }
  175. ],
  176. select: {
  177. style: 'multi',
  178. selector: 'td:nth-child(2)'
  179. },
  180. order: [[2, 'desc']],
  181. displayLength: 10,
  182. layout: {
  183. topStart: {
  184. rowClass: 'row m-3 justify-content-between',
  185. features: [
  186. {
  187. pageLength: {
  188. menu: [10, 25, 50, 100],
  189. text: 'Show_MENU_'
  190. },
  191. buttons: [
  192. {
  193. text: '<i class="icon-base icon-16px bx bx-plus me-md-2"></i><span class="d-md-inline-block d-none">Create Invoice</span>',
  194. className: 'btn btn-primary',
  195. action: function () {
  196. window.location = baseUrl + 'app/invoice/add';
  197. }
  198. }
  199. ]
  200. }
  201. ]
  202. },
  203. topEnd: {
  204. rowClass: 'row mx-3 justify-content-between',
  205. features: [
  206. {
  207. search: {
  208. placeholder: 'Search Invoice',
  209. text: '_INPUT_'
  210. }
  211. }
  212. ]
  213. },
  214. bottomStart: {
  215. rowClass: 'row mx-3 justify-content-between',
  216. features: ['info']
  217. },
  218. bottomEnd: 'paging'
  219. },
  220. language: {
  221. paginate: {
  222. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  223. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  224. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  225. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  226. }
  227. },
  228. responsive: {
  229. details: {
  230. display: DataTable.Responsive.display.modal({
  231. header: function (row) {
  232. const data = row.data();
  233. return 'Details of ' + data['client_name'];
  234. }
  235. }),
  236. type: 'column',
  237. renderer: function (api, rowIdx, columns) {
  238. const data = columns
  239. .map(function (col) {
  240. return col.title !== '' // ? Do not show row in modal popup if title is blank (for check box)
  241. ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
  242. <td>${col.title}:</td>
  243. <td>${col.data}</td>
  244. </tr>`
  245. : '';
  246. })
  247. .join('');
  248. if (data) {
  249. const div = document.createElement('div');
  250. div.classList.add('table-responsive');
  251. const table = document.createElement('table');
  252. div.appendChild(table);
  253. table.classList.add('table');
  254. const tbody = document.createElement('tbody');
  255. tbody.innerHTML = data;
  256. table.appendChild(tbody);
  257. return div;
  258. }
  259. return false;
  260. }
  261. }
  262. },
  263. initComplete: function () {
  264. // Ensure the container for the Invoice Status filter is created
  265. let invoiceStatusContainer = document.querySelector('.invoice_status');
  266. if (!invoiceStatusContainer) {
  267. // Create the container if it doesn't exist
  268. invoiceStatusContainer = document.createElement('div');
  269. invoiceStatusContainer.className = 'invoice_status';
  270. // Append it to a suitable location in your DataTable's layout
  271. // Example: Appending to the filter area (adjust as needed)
  272. const filterArea = document.querySelector('.dt-layout-end');
  273. if (filterArea) {
  274. filterArea.appendChild(invoiceStatusContainer);
  275. }
  276. }
  277. // Adding role filter once the table is initialized
  278. this.api()
  279. .columns(8)
  280. .every(function () {
  281. const column = this;
  282. // Create the dropdown for "Invoice Status"
  283. const select = document.createElement('select');
  284. select.id = 'UserRole';
  285. select.className = 'form-select';
  286. select.innerHTML = '<option value=""> Invoice Status </option>';
  287. // Append the dropdown to the invoice status container
  288. invoiceStatusContainer.appendChild(select);
  289. // Add change event listener to filter the column based on selected value
  290. select.addEventListener('change', function () {
  291. const val = select.value ? `^${select.value}$` : '';
  292. column.search(val, true, false).draw();
  293. });
  294. // Populate the dropdown with unique values from the column data
  295. column
  296. .data()
  297. .unique()
  298. .sort()
  299. .each(function (d) {
  300. const option = document.createElement('option');
  301. option.value = d;
  302. option.className = 'text-capitalize';
  303. option.textContent = d;
  304. select.appendChild(option);
  305. });
  306. });
  307. }
  308. });
  309. function deleteRecord(event) {
  310. let row = document.querySelector('.dtr-expanded');
  311. if (event) {
  312. row = event.target.parentElement.closest('tr');
  313. }
  314. if (row) {
  315. dt_invoice.row(row).remove().draw();
  316. }
  317. }
  318. function bindDeleteEvent() {
  319. const invoiceTable = document.querySelector('.invoice-list-table');
  320. const modal = document.querySelector('.dtr-bs-modal');
  321. if (invoiceTable && invoiceTable.classList.contains('collapsed')) {
  322. if (modal) {
  323. modal.addEventListener('click', function (event) {
  324. if (event.target.parentElement.classList.contains('delete-record')) {
  325. const tooltipInstance = bootstrap.Tooltip.getInstance(event.target.parentElement);
  326. if (tooltipInstance) {
  327. tooltipInstance.dispose();
  328. }
  329. deleteRecord();
  330. const closeButton = modal.querySelector('.btn-close');
  331. if (closeButton) closeButton.click(); // Simulates a click on the close button
  332. }
  333. });
  334. }
  335. } else {
  336. const tableBody = invoiceTable?.querySelector('tbody');
  337. if (tableBody) {
  338. tableBody.addEventListener('click', function (event) {
  339. if (event.target.parentElement.classList.contains('delete-record')) {
  340. const tooltipInstance = bootstrap.Tooltip.getInstance(event.target.parentElement);
  341. if (tooltipInstance) {
  342. tooltipInstance.dispose();
  343. }
  344. deleteRecord(event);
  345. }
  346. });
  347. }
  348. }
  349. }
  350. // Initial event binding
  351. bindDeleteEvent();
  352. // Re-bind events when modal is shown or hidden
  353. document.addEventListener('show.bs.modal', function (event) {
  354. if (event.target.classList.contains('dtr-bs-modal')) {
  355. bindDeleteEvent();
  356. }
  357. });
  358. document.addEventListener('hide.bs.modal', function (event) {
  359. if (event.target.classList.contains('dtr-bs-modal')) {
  360. bindDeleteEvent();
  361. }
  362. });
  363. // Initialize tooltips on each table draw
  364. dt_invoice.on('draw', function () {
  365. const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
  366. tooltipTriggerList.forEach(tooltipTriggerEl => {
  367. new bootstrap.Tooltip(tooltipTriggerEl, {
  368. boundary: document.body
  369. });
  370. });
  371. });
  372. }
  373. // Filter form control to default size
  374. // ? setTimeout used for multilingual table initialization
  375. setTimeout(() => {
  376. const elementsToModify = [
  377. { selector: '.dt-buttons .btn', classToRemove: 'btn-secondary' },
  378. { selector: '.dt-buttons ', classToAdd: 'd-block mb-0 w-auto', classToRemove: 'flex-wrap' },
  379. { selector: '.dt-length', classToAdd: 'd-flex align-items-center mx-2 my-md-5 my-0' },
  380. { selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
  381. { selector: '.dt-search', classToAdd: 'me-sm-0 me-4' },
  382. { selector: '.dt-layout-end .dt-search .form-control', classToRemove: 'form-control-sm' },
  383. {
  384. selector: '.dt-layout-end',
  385. classToRemove: 'justify-content-between ms-auto',
  386. classToAdd:
  387. 'justify-content-md-between justify-content-center d-flex flex-wrap gap-sm-4 mb-sm-0 mb-5 mt-0 pe-md-3 ps-0'
  388. },
  389. {
  390. selector: '.dt-layout-start',
  391. classToRemove: 'd-md-flex justify-content-between',
  392. classToAdd: 'px-3 pe-md-0 mt-0 d-flex justify-content-md-between justify-content-center mt-md-0 mt-5'
  393. },
  394. { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
  395. { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
  396. ];
  397. // Delete record
  398. elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
  399. document.querySelectorAll(selector).forEach(element => {
  400. if (classToRemove) {
  401. classToRemove.split(' ').forEach(className => element.classList.remove(className));
  402. }
  403. if (classToAdd) {
  404. classToAdd.split(' ').forEach(className => element.classList.add(className));
  405. }
  406. });
  407. });
  408. }, 100);
  409. });