Nenhuma descrição
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

app-ecommerce-customer-all.js 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /**
  2. * App eCommerce customer all
  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_customer_table = document.querySelector('.datatables-customers'),
  13. select2 = $('.select2'),
  14. customerView = baseUrl + 'app/ecommerce/customer/details/overview';
  15. if (select2.length) {
  16. var $this = select2;
  17. $this.wrap('<div class="position-relative"></div>').select2({
  18. placeholder: 'United States ',
  19. dropdownParent: $this.parent()
  20. });
  21. }
  22. // customers datatable
  23. if (dt_customer_table) {
  24. var dt_customer = new DataTable(dt_customer_table, {
  25. ajax: assetsPath + 'json/ecommerce-customer-all.json', // JSON file to add data
  26. columns: [
  27. // columns according to JSON
  28. { data: '' },
  29. { data: 'id', orderable: false, render: DataTable.render.select() },
  30. { data: 'customer' },
  31. { data: 'customer_id' },
  32. { data: 'country' },
  33. { data: 'order' },
  34. { data: 'total_spent' }
  35. ],
  36. columnDefs: [
  37. {
  38. // For Responsive
  39. className: 'control',
  40. searchable: false,
  41. orderable: false,
  42. responsivePriority: 2,
  43. targets: 0,
  44. render: function (data, type, full, meta) {
  45. return '';
  46. }
  47. },
  48. {
  49. // For Checkboxes
  50. targets: 1,
  51. orderable: false,
  52. searchable: false,
  53. responsivePriority: 3,
  54. checkboxes: true,
  55. render: function () {
  56. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  57. },
  58. checkboxes: {
  59. selectAllRender: '<input type="checkbox" class="form-check-input">'
  60. }
  61. },
  62. {
  63. targets: 2,
  64. responsivePriority: 1,
  65. render: function (data, type, full, meta) {
  66. const name = full['customer'];
  67. const email = full['email'];
  68. const image = full['image'];
  69. let output;
  70. if (image) {
  71. // For Avatar image
  72. output = `
  73. <img src="${assetsPath}img/avatars/${image}" alt="Avatar" class="rounded-circle">
  74. `;
  75. } else {
  76. // For Avatar badge
  77. const stateNum = Math.floor(Math.random() * 6);
  78. const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
  79. const state = states[stateNum];
  80. const initials = (name.match(/\b\w/g) || []).slice(0, 2).join('').toUpperCase();
  81. output = `<span class="avatar-initial rounded-circle bg-label-${state}">${initials}</span>`;
  82. }
  83. // Creates full output for customer name and email
  84. const rowOutput = `
  85. <div class="d-flex justify-content-start align-items-center customer-name">
  86. <div class="avatar-wrapper">
  87. <div class="avatar avatar-sm me-3">${output}</div>
  88. </div>
  89. <div class="d-flex flex-column">
  90. <a href="${customerView}" class="text-heading"><span class="fw-medium">${name}</span></a>
  91. <small>${email}</small>
  92. </div>
  93. </div>`;
  94. return rowOutput;
  95. }
  96. },
  97. {
  98. // customer Role
  99. targets: 3,
  100. render: function (data, type, full, meta) {
  101. const id = full['customer_id'];
  102. return "<span class='text-heading'>#" + id + '</span>';
  103. }
  104. },
  105. {
  106. targets: 4,
  107. render: function (data, type, full, meta) {
  108. const plan = full['country'];
  109. const code = full['country_code'];
  110. const outputCode = code
  111. ? `<i class="icon-base fis fi fi-${code} rounded-circle me-2 icon-lg"></i>`
  112. : `<i class="icon-base fis fi fi-xx rounded-circle me-2 icon-lg"></i>`;
  113. const rowOutput = `
  114. <div class="d-flex justify-content-start align-items-center customer-country">
  115. <div>${outputCode}</div>
  116. <div><span>${plan}</span></div>
  117. </div>`;
  118. return rowOutput;
  119. }
  120. },
  121. {
  122. // customer Status
  123. targets: 5,
  124. render: function (data, type, full, meta) {
  125. const status = full['order'];
  126. return '<span>' + status + '</span>';
  127. }
  128. },
  129. {
  130. // customer Spent
  131. targets: 6,
  132. render: function (data, type, full, meta) {
  133. const spent = full['total_spent'];
  134. return '<span class="fw-medium text-heading">' + spent + '</span>';
  135. }
  136. }
  137. ],
  138. select: {
  139. style: 'multi',
  140. selector: 'td:nth-child(2)'
  141. },
  142. order: [[2, 'desc']],
  143. layout: {
  144. topStart: {
  145. rowClass: 'row m-3 my-0 justify-content-between',
  146. features: [
  147. {
  148. search: {
  149. placeholder: 'Search Order',
  150. text: '_INPUT_'
  151. }
  152. }
  153. ]
  154. },
  155. topEnd: {
  156. features: [
  157. {
  158. pageLength: {
  159. menu: [10, 25, 50, 100],
  160. text: '_MENU_'
  161. }
  162. },
  163. {
  164. buttons: [
  165. {
  166. extend: 'collection',
  167. className: 'btn btn-label-primary dropdown-toggle me-4',
  168. text: '<span class="d-flex align-items-center gap-2"><i class="icon-base bx bx-export icon-xs"></i> <span class="d-none d-sm-inline-block">Export</span></span>',
  169. buttons: [
  170. {
  171. extend: 'print',
  172. text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-printer me-1"></i>Print</span>`,
  173. className: 'dropdown-item',
  174. exportOptions: {
  175. columns: [3, 4, 5, 6],
  176. format: {
  177. body: function (inner, coldex, rowdex) {
  178. if (inner.length <= 0) return inner;
  179. // Check if inner is HTML content
  180. if (inner.indexOf('<') > -1) {
  181. const parser = new DOMParser();
  182. const doc = parser.parseFromString(inner, 'text/html');
  183. // Get all text content
  184. let text = '';
  185. // Handle specific elements
  186. const userNameElements = doc.querySelectorAll('.customer-name');
  187. if (userNameElements.length > 0) {
  188. userNameElements.forEach(el => {
  189. // Get text from nested structure
  190. const nameText =
  191. el.querySelector('.fw-medium')?.textContent ||
  192. el.querySelector('.d-block')?.textContent ||
  193. el.textContent;
  194. text += nameText.trim() + ' ';
  195. });
  196. } else {
  197. // Get regular text content
  198. text = doc.body.textContent || doc.body.innerText;
  199. }
  200. return text.trim();
  201. }
  202. return inner;
  203. }
  204. }
  205. },
  206. customize: function (win) {
  207. win.document.body.style.color = config.colors.headingColor;
  208. win.document.body.style.borderColor = config.colors.borderColor;
  209. win.document.body.style.backgroundColor = config.colors.bodyBg;
  210. const table = win.document.body.querySelector('table');
  211. table.classList.add('compact');
  212. table.style.color = 'inherit';
  213. table.style.borderColor = 'inherit';
  214. table.style.backgroundColor = 'inherit';
  215. }
  216. },
  217. {
  218. extend: 'csv',
  219. text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-file me-1"></i>Csv</span>`,
  220. className: 'dropdown-item',
  221. exportOptions: {
  222. columns: [3, 4, 5, 6],
  223. format: {
  224. body: function (inner, coldex, rowdex) {
  225. if (inner.length <= 0) return inner;
  226. // Parse HTML content
  227. const parser = new DOMParser();
  228. const doc = parser.parseFromString(inner, 'text/html');
  229. let text = '';
  230. // Handle customer-name elements specifically
  231. const userNameElements = doc.querySelectorAll('.customer-name');
  232. if (userNameElements.length > 0) {
  233. userNameElements.forEach(el => {
  234. // Get text from nested structure - try different selectors
  235. const nameText =
  236. el.querySelector('.fw-medium')?.textContent ||
  237. el.querySelector('.d-block')?.textContent ||
  238. el.textContent;
  239. text += nameText.trim() + ' ';
  240. });
  241. } else {
  242. // Handle other elements (status, role, etc)
  243. text = doc.body.textContent || doc.body.innerText;
  244. }
  245. return text.trim();
  246. }
  247. }
  248. }
  249. },
  250. {
  251. extend: 'excel',
  252. text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-export me-1"></i>Excel</span>`,
  253. className: 'dropdown-item',
  254. exportOptions: {
  255. columns: [3, 4, 5, 6],
  256. format: {
  257. body: function (inner, coldex, rowdex) {
  258. if (inner.length <= 0) return inner;
  259. // Parse HTML content
  260. const parser = new DOMParser();
  261. const doc = parser.parseFromString(inner, 'text/html');
  262. let text = '';
  263. // Handle customer-name elements specifically
  264. const userNameElements = doc.querySelectorAll('.customer-name');
  265. if (userNameElements.length > 0) {
  266. userNameElements.forEach(el => {
  267. // Get text from nested structure - try different selectors
  268. const nameText =
  269. el.querySelector('.fw-medium')?.textContent ||
  270. el.querySelector('.d-block')?.textContent ||
  271. el.textContent;
  272. text += nameText.trim() + ' ';
  273. });
  274. } else {
  275. // Handle other elements (status, role, etc)
  276. text = doc.body.textContent || doc.body.innerText;
  277. }
  278. return text.trim();
  279. }
  280. }
  281. }
  282. },
  283. {
  284. extend: 'pdf',
  285. text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-pdf me-1"></i>Pdf</span>`,
  286. className: 'dropdown-item',
  287. exportOptions: {
  288. columns: [3, 4, 5, 6],
  289. format: {
  290. body: function (inner, coldex, rowdex) {
  291. if (inner.length <= 0) return inner;
  292. // Parse HTML content
  293. const parser = new DOMParser();
  294. const doc = parser.parseFromString(inner, 'text/html');
  295. let text = '';
  296. // Handle customer-name elements specifically
  297. const userNameElements = doc.querySelectorAll('.customer-name');
  298. if (userNameElements.length > 0) {
  299. userNameElements.forEach(el => {
  300. // Get text from nested structure - try different selectors
  301. const nameText =
  302. el.querySelector('.fw-medium')?.textContent ||
  303. el.querySelector('.d-block')?.textContent ||
  304. el.textContent;
  305. text += nameText.trim() + ' ';
  306. });
  307. } else {
  308. // Handle other elements (status, role, etc)
  309. text = doc.body.textContent || doc.body.innerText;
  310. }
  311. return text.trim();
  312. }
  313. }
  314. }
  315. },
  316. {
  317. extend: 'copy',
  318. text: `<i class="icon-base bx bx-copy me-1"></i>Copy`,
  319. className: 'dropdown-item',
  320. exportOptions: {
  321. columns: [3, 4, 5, 6],
  322. format: {
  323. body: function (inner, coldex, rowdex) {
  324. if (inner.length <= 0) return inner;
  325. // Parse HTML content
  326. const parser = new DOMParser();
  327. const doc = parser.parseFromString(inner, 'text/html');
  328. let text = '';
  329. // Handle customer-name elements specifically
  330. const userNameElements = doc.querySelectorAll('.customer-name');
  331. if (userNameElements.length > 0) {
  332. userNameElements.forEach(el => {
  333. // Get text from nested structure - try different selectors
  334. const nameText =
  335. el.querySelector('.fw-medium')?.textContent ||
  336. el.querySelector('.d-block')?.textContent ||
  337. el.textContent;
  338. text += nameText.trim() + ' ';
  339. });
  340. } else {
  341. // Handle other elements (status, role, etc)
  342. text = doc.body.textContent || doc.body.innerText;
  343. }
  344. return text.trim();
  345. }
  346. }
  347. }
  348. }
  349. ]
  350. },
  351. {
  352. text: '<span class="d-flex align-items-center gap-2"><i class="icon-base bx bx-plus icon-sm"></i> <span class="d-none d-sm-inline-block">Add Customer</span></span>',
  353. className: 'create-new btn btn-primary',
  354. attr: {
  355. 'data-bs-toggle': 'offcanvas',
  356. 'data-bs-target': '#offcanvasEcommerceCustomerAdd'
  357. }
  358. }
  359. ]
  360. }
  361. ]
  362. },
  363. bottomStart: {
  364. rowClass: 'row mx-3 justify-content-between',
  365. features: ['info']
  366. },
  367. bottomEnd: 'paging'
  368. },
  369. language: {
  370. paginate: {
  371. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  372. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  373. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  374. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  375. }
  376. },
  377. // For responsive popup
  378. responsive: {
  379. details: {
  380. display: DataTable.Responsive.display.modal({
  381. header: function (row) {
  382. const data = row.data();
  383. return 'Details of ' + data['customer'];
  384. }
  385. }),
  386. type: 'column',
  387. renderer: function (api, rowIdx, columns) {
  388. const data = columns
  389. .map(function (col) {
  390. return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
  391. ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
  392. <td>${col.title}:</td>
  393. <td>${col.data}</td>
  394. </tr>`
  395. : '';
  396. })
  397. .join('');
  398. if (data) {
  399. const div = document.createElement('div');
  400. div.classList.add('table-responsive');
  401. const table = document.createElement('table');
  402. div.appendChild(table);
  403. table.classList.add('table');
  404. const tbody = document.createElement('tbody');
  405. tbody.innerHTML = data;
  406. table.appendChild(tbody);
  407. return div;
  408. }
  409. return false;
  410. }
  411. }
  412. }
  413. });
  414. }
  415. // Filter form control to default size
  416. // ? setTimeout used for customer-all table initialization
  417. setTimeout(() => {
  418. const elementsToModify = [
  419. { selector: '.dt-buttons', classToAdd: 'gap-4' },
  420. { selector: '.dt-buttons .btn-group .btn', classToRemove: 'btn-secondary', classToAdd: 'btn-label-secondary' },
  421. { selector: '.dt-buttons .btn-group ~ .btn', classToRemove: 'btn-secondary' },
  422. { selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-0' },
  423. { selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
  424. { selector: '.dt-length', classToAdd: 'mt-0 mt-md-6' },
  425. { selector: '.dt-layout-end', classToAdd: 'gap-2 mt-0 mb-md-0 mb-4' },
  426. { selector: '.dt-layout-start', classToAdd: 'mt-0' },
  427. { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
  428. { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
  429. ];
  430. // Delete record
  431. elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
  432. document.querySelectorAll(selector).forEach(element => {
  433. if (classToRemove) {
  434. classToRemove.split(' ').forEach(className => element.classList.remove(className));
  435. }
  436. if (classToAdd) {
  437. classToAdd.split(' ').forEach(className => element.classList.add(className));
  438. }
  439. });
  440. });
  441. }, 100);
  442. });
  443. // Validation & Phone mask
  444. (function () {
  445. const phoneMaskList = document.querySelectorAll('.phone-mask'),
  446. eCommerceCustomerAddForm = document.getElementById('eCommerceCustomerAddForm');
  447. // Phone Number
  448. if (phoneMaskList) {
  449. phoneMaskList.forEach(function (phoneMask) {
  450. phoneMask.addEventListener('input', event => {
  451. const cleanValue = event.target.value.replace(/\D/g, '');
  452. phoneMask.value = formatGeneral(cleanValue, {
  453. blocks: [3, 3, 4],
  454. delimiters: [' ', ' ']
  455. });
  456. });
  457. registerCursorTracker({
  458. input: phoneMask,
  459. delimiter: ' '
  460. });
  461. });
  462. }
  463. // Add New customer Form Validation
  464. const fv = FormValidation.formValidation(eCommerceCustomerAddForm, {
  465. fields: {
  466. customerName: {
  467. validators: {
  468. notEmpty: {
  469. message: 'Please enter fullname '
  470. }
  471. }
  472. },
  473. customerEmail: {
  474. validators: {
  475. notEmpty: {
  476. message: 'Please enter your email'
  477. },
  478. emailAddress: {
  479. message: 'The value is not a valid email address'
  480. }
  481. }
  482. }
  483. },
  484. plugins: {
  485. trigger: new FormValidation.plugins.Trigger(),
  486. bootstrap5: new FormValidation.plugins.Bootstrap5({
  487. // Use this for enabling/changing valid/invalid class
  488. eleValidClass: '',
  489. rowSelector: function (field, ele) {
  490. // field is the field name & ele is the field element
  491. return '.form-control-validation';
  492. }
  493. }),
  494. submitButton: new FormValidation.plugins.SubmitButton(),
  495. // Submit the form when all fields are valid
  496. // defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
  497. autoFocus: new FormValidation.plugins.AutoFocus()
  498. }
  499. });
  500. })();