Нет описания
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /**
  2. * Page User List
  3. */
  4. 'use strict';
  5. // Datatable (js)
  6. document.addEventListener('DOMContentLoaded', function (e) {
  7. let borderColor, bodyBg, headingColor;
  8. borderColor = config.colors.borderColor;
  9. bodyBg = config.colors.bodyBg;
  10. headingColor = config.colors.headingColor;
  11. // Variable declaration for table
  12. const dt_user_table = document.querySelector('.datatables-users'),
  13. userView = baseUrl + 'app/user/view/account',
  14. statusObj = {
  15. 1: { title: 'Pending', class: 'bg-label-warning' },
  16. 2: { title: 'Active', class: 'bg-label-success' },
  17. 3: { title: 'Inactive', class: 'bg-label-secondary' }
  18. };
  19. var select2 = $('.select2');
  20. if (select2.length) {
  21. var $this = select2;
  22. $this.wrap('<div class="position-relative"></div>').select2({
  23. placeholder: 'Select Country',
  24. dropdownParent: $this.parent()
  25. });
  26. }
  27. // Users datatable
  28. if (dt_user_table) {
  29. const dt_user = new DataTable(dt_user_table, {
  30. ajax: assetsPath + 'json/user-list.json', // JSON file to add data
  31. columns: [
  32. // columns according to JSON
  33. { data: 'id' },
  34. { data: 'id', orderable: false, render: DataTable.render.select() },
  35. { data: 'full_name' },
  36. { data: 'role' },
  37. { data: 'current_plan' },
  38. { data: 'billing' },
  39. { data: 'status' },
  40. { data: 'action' }
  41. ],
  42. columnDefs: [
  43. {
  44. // For Responsive
  45. className: 'control',
  46. searchable: false,
  47. orderable: false,
  48. responsivePriority: 2,
  49. targets: 0,
  50. render: function (data, type, full, meta) {
  51. return '';
  52. }
  53. },
  54. {
  55. // For Checkboxes
  56. targets: 1,
  57. orderable: false,
  58. searchable: false,
  59. responsivePriority: 4,
  60. checkboxes: true,
  61. render: function () {
  62. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  63. },
  64. checkboxes: {
  65. selectAllRender: '<input type="checkbox" class="form-check-input">'
  66. }
  67. },
  68. {
  69. targets: 2,
  70. responsivePriority: 3,
  71. render: function (data, type, full, meta) {
  72. var name = full['full_name'];
  73. var email = full['email'];
  74. var image = full['avatar'];
  75. var output;
  76. if (image) {
  77. // For Avatar image
  78. output = '<img src="' + assetsPath + 'img/avatars/' + image + '" alt="Avatar" class="rounded-circle">';
  79. } else {
  80. // For Avatar badge
  81. var stateNum = Math.floor(Math.random() * 6);
  82. var states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
  83. var state = states[stateNum];
  84. var initials = (name.match(/\b\w/g) || []).map(char => char.toUpperCase());
  85. initials = ((initials.shift() || '') + (initials.pop() || '')).toUpperCase();
  86. output = '<span class="avatar-initial rounded-circle bg-label-' + state + '">' + initials + '</span>';
  87. }
  88. // Creates full output for row
  89. var row_output =
  90. '<div class="d-flex justify-content-start align-items-center user-name">' +
  91. '<div class="avatar-wrapper">' +
  92. '<div class="avatar avatar-sm me-4">' +
  93. output +
  94. '</div>' +
  95. '</div>' +
  96. '<div class="d-flex flex-column">' +
  97. '<a href="' +
  98. userView +
  99. '" class="text-heading text-truncate"><span class="fw-medium">' +
  100. name +
  101. '</span></a>' +
  102. '<small>' +
  103. email +
  104. '</small>' +
  105. '</div>' +
  106. '</div>';
  107. return row_output;
  108. }
  109. },
  110. {
  111. targets: 3,
  112. render: function (data, type, full, meta) {
  113. var role = full['role'];
  114. var roleBadgeObj = {
  115. Subscriber: '<i class="icon-base bx bx-crown text-primary me-2"></i>',
  116. Author: '<i class="icon-base bx bx-edit text-warning me-2"></i>',
  117. Maintainer: '<i class="icon-base bx bx-user text-success me-2"></i>',
  118. Editor: '<i class="icon-base bx bx-pie-chart-alt text-info me-2"></i>',
  119. Admin: '<i class="icon-base bx bx-desktop text-danger me-2"></i>'
  120. };
  121. return (
  122. "<span class='text-truncate d-flex align-items-center text-heading'>" +
  123. (roleBadgeObj[role] || '') + // Ensures badge exists for the role
  124. role +
  125. '</span>'
  126. );
  127. }
  128. },
  129. {
  130. // Plans
  131. targets: 4,
  132. render: function (data, type, full, meta) {
  133. const plan = full['current_plan'];
  134. return '<span class="text-heading">' + plan + '</span>';
  135. }
  136. },
  137. {
  138. // User Status
  139. targets: 6,
  140. render: function (data, type, full, meta) {
  141. const status = full['status'];
  142. return (
  143. '<span class="badge ' +
  144. statusObj[status].class +
  145. '" text-capitalized>' +
  146. statusObj[status].title +
  147. '</span>'
  148. );
  149. }
  150. },
  151. {
  152. targets: -1,
  153. title: 'Actions',
  154. searchable: false,
  155. orderable: false,
  156. render: (data, type, full, meta) => {
  157. return `
  158. <div class="d-flex align-items-center">
  159. <a href="javascript:;" class="btn btn-icon delete-record">
  160. <i class="icon-base bx bx-trash icon-md"></i>
  161. </a>
  162. <a href="${userView}" class="btn btn-icon">
  163. <i class="icon-base bx bx-show icon-md"></i>
  164. </a>
  165. <a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown">
  166. <i class="icon-base bx bx-dots-vertical-rounded icon-md"></i>
  167. </a>
  168. <div class="dropdown-menu dropdown-menu-end m-0">
  169. <a href="javascript:;" class="dropdown-item">Edit</a>
  170. <a href="javascript:;" class="dropdown-item">Suspend</a>
  171. </div>
  172. </div>
  173. `;
  174. }
  175. }
  176. ],
  177. select: {
  178. style: 'multi',
  179. selector: 'td:nth-child(2)'
  180. },
  181. order: [[2, 'desc']],
  182. layout: {
  183. topStart: {
  184. rowClass: 'row mx-3 my-0 justify-content-between',
  185. features: [
  186. {
  187. pageLength: {
  188. menu: [10, 25, 50, 100],
  189. text: '_MENU_'
  190. }
  191. }
  192. ]
  193. },
  194. topEnd: {
  195. features: [
  196. {
  197. search: {
  198. placeholder: 'Search User',
  199. text: '_INPUT_'
  200. }
  201. },
  202. {
  203. buttons: [
  204. {
  205. extend: 'collection',
  206. className: 'btn btn-label-secondary dropdown-toggle',
  207. text: '<span class="d-flex align-items-center gap-2"><i class="icon-base bx bx-export icon-sm"></i> <span class="d-none d-sm-inline-block">Export</span></span>',
  208. buttons: [
  209. {
  210. extend: 'print',
  211. text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-printer me-2"></i>Print</span>`,
  212. className: 'dropdown-item',
  213. exportOptions: {
  214. columns: [3, 4, 5, 6, 7],
  215. format: {
  216. body: function (inner, coldex, rowdex) {
  217. if (inner.length <= 0) return inner;
  218. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  219. let result = '';
  220. el.forEach(item => {
  221. if (item.classList && item.classList.contains('user-name')) {
  222. result += item.lastChild.firstChild.textContent;
  223. } else {
  224. result += item.textContent || item.innerText || '';
  225. }
  226. });
  227. return result;
  228. }
  229. }
  230. },
  231. customize: function (win) {
  232. win.document.body.style.color = config.colors.headingColor;
  233. win.document.body.style.borderColor = config.colors.borderColor;
  234. win.document.body.style.backgroundColor = config.colors.bodyBg;
  235. const table = win.document.body.querySelector('table');
  236. table.classList.add('compact');
  237. table.style.color = 'inherit';
  238. table.style.borderColor = 'inherit';
  239. table.style.backgroundColor = 'inherit';
  240. }
  241. },
  242. {
  243. extend: 'csv',
  244. text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-file me-2"></i>Csv</span>`,
  245. className: 'dropdown-item',
  246. exportOptions: {
  247. columns: [3, 4, 5, 6, 7],
  248. format: {
  249. body: function (inner, coldex, rowdex) {
  250. if (inner.length <= 0) return inner;
  251. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  252. let result = '';
  253. el.forEach(item => {
  254. if (item.classList && item.classList.contains('user-name')) {
  255. result += item.lastChild.firstChild.textContent;
  256. } else {
  257. result += item.textContent || item.innerText || '';
  258. }
  259. });
  260. return result;
  261. }
  262. }
  263. }
  264. },
  265. {
  266. extend: 'excel',
  267. text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-export me-2"></i>Excel</span>`,
  268. className: 'dropdown-item',
  269. exportOptions: {
  270. columns: [3, 4, 5, 6, 7],
  271. format: {
  272. body: function (inner, coldex, rowdex) {
  273. if (inner.length <= 0) return inner;
  274. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  275. let result = '';
  276. el.forEach(item => {
  277. if (item.classList && item.classList.contains('user-name')) {
  278. result += item.lastChild.firstChild.textContent;
  279. } else {
  280. result += item.textContent || item.innerText || '';
  281. }
  282. });
  283. return result;
  284. }
  285. }
  286. }
  287. },
  288. {
  289. extend: 'pdf',
  290. text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-pdf me-2"></i>Pdf</span>`,
  291. className: 'dropdown-item',
  292. exportOptions: {
  293. columns: [3, 4, 5, 6, 7],
  294. format: {
  295. body: function (inner, coldex, rowdex) {
  296. if (inner.length <= 0) return inner;
  297. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  298. let result = '';
  299. el.forEach(item => {
  300. if (item.classList && item.classList.contains('user-name')) {
  301. result += item.lastChild.firstChild.textContent;
  302. } else {
  303. result += item.textContent || item.innerText || '';
  304. }
  305. });
  306. return result;
  307. }
  308. }
  309. }
  310. },
  311. {
  312. extend: 'copy',
  313. text: `<i class="icon-base bx bx-copy me-1"></i>Copy`,
  314. className: 'dropdown-item',
  315. exportOptions: {
  316. columns: [3, 4, 5, 6, 7],
  317. format: {
  318. body: function (inner, coldex, rowdex) {
  319. if (inner.length <= 0) return inner;
  320. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  321. let result = '';
  322. el.forEach(item => {
  323. if (item.classList && item.classList.contains('user-name')) {
  324. result += item.lastChild.firstChild.textContent;
  325. } else {
  326. result += item.textContent || item.innerText || '';
  327. }
  328. });
  329. return result;
  330. }
  331. }
  332. }
  333. }
  334. ]
  335. },
  336. {
  337. text: '<i class="icon-base bx bx-plus icon-sm me-0 me-sm-2"></i><span class="d-none d-sm-inline-block">Add New User</span>',
  338. className: 'add-new btn btn-primary',
  339. attr: {
  340. 'data-bs-toggle': 'offcanvas',
  341. 'data-bs-target': '#offcanvasAddUser'
  342. }
  343. }
  344. ]
  345. }
  346. ]
  347. },
  348. bottomStart: {
  349. rowClass: 'row mx-3 justify-content-between',
  350. features: ['info']
  351. },
  352. bottomEnd: 'paging'
  353. },
  354. language: {
  355. sLengthMenu: '_MENU_',
  356. search: '',
  357. searchPlaceholder: 'Search User',
  358. paginate: {
  359. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  360. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  361. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  362. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  363. }
  364. },
  365. // For responsive popup
  366. responsive: {
  367. details: {
  368. display: DataTable.Responsive.display.modal({
  369. header: function (row) {
  370. const data = row.data();
  371. return 'Details of ' + data['full_name'];
  372. }
  373. }),
  374. type: 'column',
  375. renderer: function (api, rowIdx, columns) {
  376. const data = columns
  377. .map(function (col) {
  378. return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
  379. ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
  380. <td>${col.title}:</td>
  381. <td>${col.data}</td>
  382. </tr>`
  383. : '';
  384. })
  385. .join('');
  386. if (data) {
  387. const div = document.createElement('div');
  388. div.classList.add('table-responsive');
  389. const table = document.createElement('table');
  390. div.appendChild(table);
  391. table.classList.add('table');
  392. const tbody = document.createElement('tbody');
  393. tbody.innerHTML = data;
  394. table.appendChild(tbody);
  395. return div;
  396. }
  397. return false;
  398. }
  399. }
  400. },
  401. initComplete: function () {
  402. const api = this.api();
  403. // Helper function to create a select dropdown and append options
  404. const createFilter = (columnIndex, containerClass, selectId, defaultOptionText) => {
  405. const column = api.column(columnIndex);
  406. const select = document.createElement('select');
  407. select.id = selectId;
  408. select.className = 'form-select text-capitalize';
  409. select.innerHTML = `<option value="">${defaultOptionText}</option>`;
  410. document.querySelector(containerClass).appendChild(select);
  411. // Add event listener for filtering
  412. select.addEventListener('change', () => {
  413. const val = select.value ? `^${select.value}$` : '';
  414. column.search(val, true, false).draw();
  415. });
  416. // Populate options based on unique column data
  417. const uniqueData = Array.from(new Set(column.data().toArray())).sort();
  418. uniqueData.forEach(d => {
  419. const option = document.createElement('option');
  420. option.value = d;
  421. option.textContent = d;
  422. select.appendChild(option);
  423. });
  424. };
  425. // Role filter
  426. createFilter(3, '.user_role', 'UserRole', 'Select Role');
  427. // Plan filter
  428. createFilter(4, '.user_plan', 'UserPlan', 'Select Plan');
  429. // Status filter
  430. const statusFilter = document.createElement('select');
  431. statusFilter.id = 'FilterTransaction';
  432. statusFilter.className = 'form-select text-capitalize';
  433. statusFilter.innerHTML = '<option value="">Select Status</option>';
  434. document.querySelector('.user_status').appendChild(statusFilter);
  435. statusFilter.addEventListener('change', () => {
  436. const val = statusFilter.value ? `^${statusFilter.value}$` : '';
  437. api.column(6).search(val, true, false).draw();
  438. });
  439. const statusColumn = api.column(6);
  440. const uniqueStatusData = Array.from(new Set(statusColumn.data().toArray())).sort();
  441. uniqueStatusData.forEach(d => {
  442. const option = document.createElement('option');
  443. option.value = statusObj[d]?.title || d;
  444. option.textContent = statusObj[d]?.title || d;
  445. option.className = 'text-capitalize';
  446. statusFilter.appendChild(option);
  447. });
  448. }
  449. });
  450. //? The 'delete-record' class is necessary for the functionality of the following code.
  451. function deleteRecord(event) {
  452. let row = document.querySelector('.dtr-expanded');
  453. if (event) {
  454. row = event.target.parentElement.closest('tr');
  455. }
  456. if (row) {
  457. dt_user.row(row).remove().draw();
  458. }
  459. }
  460. function bindDeleteEvent() {
  461. const userListTable = document.querySelector('.datatables-users');
  462. const modal = document.querySelector('.dtr-bs-modal');
  463. if (userListTable && userListTable.classList.contains('collapsed')) {
  464. if (modal) {
  465. modal.addEventListener('click', function (event) {
  466. if (event.target.parentElement.classList.contains('delete-record')) {
  467. deleteRecord();
  468. const closeButton = modal.querySelector('.btn-close');
  469. if (closeButton) closeButton.click(); // Simulates a click on the close button
  470. }
  471. });
  472. }
  473. } else {
  474. const tableBody = userListTable?.querySelector('tbody');
  475. if (tableBody) {
  476. tableBody.addEventListener('click', function (event) {
  477. if (event.target.parentElement.classList.contains('delete-record')) {
  478. deleteRecord(event);
  479. }
  480. });
  481. }
  482. }
  483. }
  484. // Initial event binding
  485. bindDeleteEvent();
  486. // Re-bind events when modal is shown or hidden
  487. document.addEventListener('show.bs.modal', function (event) {
  488. if (event.target.classList.contains('dtr-bs-modal')) {
  489. bindDeleteEvent();
  490. }
  491. });
  492. document.addEventListener('hide.bs.modal', function (event) {
  493. if (event.target.classList.contains('dtr-bs-modal')) {
  494. bindDeleteEvent();
  495. }
  496. });
  497. // To remove default btn-secondary in export buttons
  498. $('.dt-buttons > .btn-group > button').removeClass('btn-secondary');
  499. }
  500. // Filter form control to default size
  501. // ? setTimeout used for user-list table initialization
  502. setTimeout(() => {
  503. const elementsToModify = [
  504. { selector: '.dt-buttons .btn', classToRemove: 'btn-secondary' },
  505. { selector: '.dt-search .form-control', classToRemove: 'form-control-sm' },
  506. { selector: '.dt-length .form-select', classToRemove: 'form-select-sm', classToAdd: 'ms-0' },
  507. { selector: '.dt-length', classToAdd: 'mb-md-6 mb-0' },
  508. { selector: '.dt-search', classToAdd: 'mb-md-6 mb-2' },
  509. {
  510. selector: '.dt-layout-end',
  511. classToRemove: 'justify-content-between',
  512. classToAdd: 'd-flex gap-md-4 justify-content-md-between justify-content-center gap-4 flex-wrap mt-0'
  513. },
  514. { selector: '.dt-layout-start', classToAdd: 'mt-0' },
  515. { selector: '.dt-buttons', classToAdd: 'd-flex gap-4 mb-md-0 mb-6' },
  516. { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
  517. { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
  518. ];
  519. // Delete record
  520. elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
  521. document.querySelectorAll(selector).forEach(element => {
  522. if (classToRemove) {
  523. classToRemove.split(' ').forEach(className => element.classList.remove(className));
  524. }
  525. if (classToAdd) {
  526. classToAdd.split(' ').forEach(className => element.classList.add(className));
  527. }
  528. });
  529. });
  530. }, 100);
  531. // Validation & Phone mask
  532. const phoneMaskList = document.querySelectorAll('.phone-mask'),
  533. addNewUserForm = document.getElementById('addNewUserForm');
  534. // Phone Number
  535. if (phoneMaskList) {
  536. phoneMaskList.forEach(function (phoneMask) {
  537. phoneMask.addEventListener('input', event => {
  538. const cleanValue = event.target.value.replace(/\D/g, '');
  539. phoneMask.value = formatGeneral(cleanValue, {
  540. blocks: [3, 3, 4],
  541. delimiters: [' ', ' ']
  542. });
  543. });
  544. registerCursorTracker({
  545. input: phoneMask,
  546. delimiter: ' '
  547. });
  548. });
  549. }
  550. // Add New User Form Validation
  551. const fv = FormValidation.formValidation(addNewUserForm, {
  552. fields: {
  553. userFullname: {
  554. validators: {
  555. notEmpty: {
  556. message: 'Please enter fullname '
  557. }
  558. }
  559. },
  560. userEmail: {
  561. validators: {
  562. notEmpty: {
  563. message: 'Please enter your email'
  564. },
  565. emailAddress: {
  566. message: 'The value is not a valid email address'
  567. }
  568. }
  569. }
  570. },
  571. plugins: {
  572. trigger: new FormValidation.plugins.Trigger(),
  573. bootstrap5: new FormValidation.plugins.Bootstrap5({
  574. // Use this for enabling/changing valid/invalid class
  575. eleValidClass: '',
  576. rowSelector: function (field, ele) {
  577. // field is the field name & ele is the field element
  578. return '.form-control-validation';
  579. }
  580. }),
  581. submitButton: new FormValidation.plugins.SubmitButton(),
  582. // Submit the form when all fields are valid
  583. // defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
  584. autoFocus: new FormValidation.plugins.AutoFocus()
  585. }
  586. });
  587. });