| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- /**
- * App eCommerce customer all
- */
-
- 'use strict';
-
- // Datatable (js)
- document.addEventListener('DOMContentLoaded', function (e) {
- let borderColor, bodyBg, headingColor;
-
- borderColor = config.colors.borderColor;
- bodyBg = config.colors.bodyBg;
- headingColor = config.colors.headingColor;
-
- // Variable declaration for table
- const dt_customer_table = document.querySelector('.datatables-customers'),
- select2 = $('.select2'),
- customerView = baseUrl + 'app/ecommerce/customer/details/overview';
- if (select2.length) {
- var $this = select2;
- $this.wrap('<div class="position-relative"></div>').select2({
- placeholder: 'United States ',
- dropdownParent: $this.parent()
- });
- }
-
- // customers datatable
- if (dt_customer_table) {
- var dt_customer = new DataTable(dt_customer_table, {
- ajax: assetsPath + 'json/ecommerce-customer-all.json', // JSON file to add data
- columns: [
- // columns according to JSON
- { data: '' },
- { data: 'id', orderable: false, render: DataTable.render.select() },
- { data: 'customer' },
- { data: 'customer_id' },
- { data: 'country' },
- { data: 'order' },
- { data: 'total_spent' }
- ],
- columnDefs: [
- {
- // For Responsive
- className: 'control',
- searchable: false,
- orderable: false,
- responsivePriority: 2,
- targets: 0,
- render: function (data, type, full, meta) {
- return '';
- }
- },
- {
- // For Checkboxes
- targets: 1,
- orderable: false,
- searchable: false,
- responsivePriority: 3,
- checkboxes: true,
- render: function () {
- return '<input type="checkbox" class="dt-checkboxes form-check-input">';
- },
- checkboxes: {
- selectAllRender: '<input type="checkbox" class="form-check-input">'
- }
- },
- {
- targets: 2,
- responsivePriority: 1,
- render: function (data, type, full, meta) {
- const name = full['customer'];
- const email = full['email'];
- const image = full['image'];
- let output;
-
- if (image) {
- // For Avatar image
- output = `
- <img src="${assetsPath}img/avatars/${image}" alt="Avatar" class="rounded-circle">
- `;
- } else {
- // For Avatar badge
- const stateNum = Math.floor(Math.random() * 6);
- const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
- const state = states[stateNum];
- const initials = (name.match(/\b\w/g) || []).slice(0, 2).join('').toUpperCase();
-
- output = `<span class="avatar-initial rounded-circle bg-label-${state}">${initials}</span>`;
- }
- // Creates full output for customer name and email
- const rowOutput = `
- <div class="d-flex justify-content-start align-items-center customer-name">
- <div class="avatar-wrapper">
- <div class="avatar avatar-sm me-3">${output}</div>
- </div>
- <div class="d-flex flex-column">
- <a href="${customerView}" class="text-heading"><span class="fw-medium">${name}</span></a>
- <small>${email}</small>
- </div>
- </div>`;
- return rowOutput;
- }
- },
- {
- // customer Role
- targets: 3,
- render: function (data, type, full, meta) {
- const id = full['customer_id'];
-
- return "<span class='text-heading'>#" + id + '</span>';
- }
- },
- {
- targets: 4,
- render: function (data, type, full, meta) {
- const plan = full['country'];
- const code = full['country_code'];
-
- const outputCode = code
- ? `<i class="icon-base fis fi fi-${code} rounded-circle me-2 icon-lg"></i>`
- : `<i class="icon-base fis fi fi-xx rounded-circle me-2 icon-lg"></i>`;
-
- const rowOutput = `
- <div class="d-flex justify-content-start align-items-center customer-country">
- <div>${outputCode}</div>
- <div><span>${plan}</span></div>
- </div>`;
- return rowOutput;
- }
- },
- {
- // customer Status
- targets: 5,
- render: function (data, type, full, meta) {
- const status = full['order'];
-
- return '<span>' + status + '</span>';
- }
- },
- {
- // customer Spent
- targets: 6,
- render: function (data, type, full, meta) {
- const spent = full['total_spent'];
-
- return '<span class="fw-medium text-heading">' + spent + '</span>';
- }
- }
- ],
- select: {
- style: 'multi',
- selector: 'td:nth-child(2)'
- },
- order: [[2, 'desc']],
- layout: {
- topStart: {
- rowClass: 'row m-3 my-0 justify-content-between',
- features: [
- {
- search: {
- placeholder: 'Search Order',
- text: '_INPUT_'
- }
- }
- ]
- },
- topEnd: {
- features: [
- {
- pageLength: {
- menu: [10, 25, 50, 100],
- text: '_MENU_'
- }
- },
- {
- buttons: [
- {
- extend: 'collection',
- className: 'btn btn-label-primary dropdown-toggle me-4',
- 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>',
- buttons: [
- {
- extend: 'print',
- text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-printer me-1"></i>Print</span>`,
- className: 'dropdown-item',
- exportOptions: {
- columns: [3, 4, 5, 6],
- format: {
- body: function (inner, coldex, rowdex) {
- if (inner.length <= 0) return inner;
-
- // Check if inner is HTML content
- if (inner.indexOf('<') > -1) {
- const parser = new DOMParser();
- const doc = parser.parseFromString(inner, 'text/html');
-
- // Get all text content
- let text = '';
-
- // Handle specific elements
- const userNameElements = doc.querySelectorAll('.customer-name');
- if (userNameElements.length > 0) {
- userNameElements.forEach(el => {
- // Get text from nested structure
- const nameText =
- el.querySelector('.fw-medium')?.textContent ||
- el.querySelector('.d-block')?.textContent ||
- el.textContent;
- text += nameText.trim() + ' ';
- });
- } else {
- // Get regular text content
- text = doc.body.textContent || doc.body.innerText;
- }
-
- return text.trim();
- }
-
- return inner;
- }
- }
- },
- customize: function (win) {
- win.document.body.style.color = config.colors.headingColor;
- win.document.body.style.borderColor = config.colors.borderColor;
- win.document.body.style.backgroundColor = config.colors.bodyBg;
- const table = win.document.body.querySelector('table');
- table.classList.add('compact');
- table.style.color = 'inherit';
- table.style.borderColor = 'inherit';
- table.style.backgroundColor = 'inherit';
- }
- },
- {
- extend: 'csv',
- text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-file me-1"></i>Csv</span>`,
- className: 'dropdown-item',
- exportOptions: {
- columns: [3, 4, 5, 6],
- format: {
- body: function (inner, coldex, rowdex) {
- if (inner.length <= 0) return inner;
-
- // Parse HTML content
- const parser = new DOMParser();
- const doc = parser.parseFromString(inner, 'text/html');
-
- let text = '';
-
- // Handle customer-name elements specifically
- const userNameElements = doc.querySelectorAll('.customer-name');
- if (userNameElements.length > 0) {
- userNameElements.forEach(el => {
- // Get text from nested structure - try different selectors
- const nameText =
- el.querySelector('.fw-medium')?.textContent ||
- el.querySelector('.d-block')?.textContent ||
- el.textContent;
- text += nameText.trim() + ' ';
- });
- } else {
- // Handle other elements (status, role, etc)
- text = doc.body.textContent || doc.body.innerText;
- }
-
- return text.trim();
- }
- }
- }
- },
- {
- extend: 'excel',
- text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-export me-1"></i>Excel</span>`,
- className: 'dropdown-item',
- exportOptions: {
- columns: [3, 4, 5, 6],
- format: {
- body: function (inner, coldex, rowdex) {
- if (inner.length <= 0) return inner;
-
- // Parse HTML content
- const parser = new DOMParser();
- const doc = parser.parseFromString(inner, 'text/html');
-
- let text = '';
-
- // Handle customer-name elements specifically
- const userNameElements = doc.querySelectorAll('.customer-name');
- if (userNameElements.length > 0) {
- userNameElements.forEach(el => {
- // Get text from nested structure - try different selectors
- const nameText =
- el.querySelector('.fw-medium')?.textContent ||
- el.querySelector('.d-block')?.textContent ||
- el.textContent;
- text += nameText.trim() + ' ';
- });
- } else {
- // Handle other elements (status, role, etc)
- text = doc.body.textContent || doc.body.innerText;
- }
-
- return text.trim();
- }
- }
- }
- },
- {
- extend: 'pdf',
- text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-pdf me-1"></i>Pdf</span>`,
- className: 'dropdown-item',
- exportOptions: {
- columns: [3, 4, 5, 6],
- format: {
- body: function (inner, coldex, rowdex) {
- if (inner.length <= 0) return inner;
-
- // Parse HTML content
- const parser = new DOMParser();
- const doc = parser.parseFromString(inner, 'text/html');
-
- let text = '';
-
- // Handle customer-name elements specifically
- const userNameElements = doc.querySelectorAll('.customer-name');
- if (userNameElements.length > 0) {
- userNameElements.forEach(el => {
- // Get text from nested structure - try different selectors
- const nameText =
- el.querySelector('.fw-medium')?.textContent ||
- el.querySelector('.d-block')?.textContent ||
- el.textContent;
- text += nameText.trim() + ' ';
- });
- } else {
- // Handle other elements (status, role, etc)
- text = doc.body.textContent || doc.body.innerText;
- }
-
- return text.trim();
- }
- }
- }
- },
- {
- extend: 'copy',
- text: `<i class="icon-base bx bx-copy me-1"></i>Copy`,
- className: 'dropdown-item',
- exportOptions: {
- columns: [3, 4, 5, 6],
- format: {
- body: function (inner, coldex, rowdex) {
- if (inner.length <= 0) return inner;
-
- // Parse HTML content
- const parser = new DOMParser();
- const doc = parser.parseFromString(inner, 'text/html');
-
- let text = '';
-
- // Handle customer-name elements specifically
- const userNameElements = doc.querySelectorAll('.customer-name');
- if (userNameElements.length > 0) {
- userNameElements.forEach(el => {
- // Get text from nested structure - try different selectors
- const nameText =
- el.querySelector('.fw-medium')?.textContent ||
- el.querySelector('.d-block')?.textContent ||
- el.textContent;
- text += nameText.trim() + ' ';
- });
- } else {
- // Handle other elements (status, role, etc)
- text = doc.body.textContent || doc.body.innerText;
- }
-
- return text.trim();
- }
- }
- }
- }
- ]
- },
- {
- 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>',
- className: 'create-new btn btn-primary',
- attr: {
- 'data-bs-toggle': 'offcanvas',
- 'data-bs-target': '#offcanvasEcommerceCustomerAdd'
- }
- }
- ]
- }
- ]
- },
- bottomStart: {
- rowClass: 'row mx-3 justify-content-between',
- features: ['info']
- },
- bottomEnd: 'paging'
- },
- language: {
- paginate: {
- next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
- previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
- first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
- last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
- }
- },
- // For responsive popup
- responsive: {
- details: {
- display: DataTable.Responsive.display.modal({
- header: function (row) {
- const data = row.data();
- return 'Details of ' + data['customer'];
- }
- }),
- type: 'column',
- renderer: function (api, rowIdx, columns) {
- const data = columns
- .map(function (col) {
- return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
- ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
- <td>${col.title}:</td>
- <td>${col.data}</td>
- </tr>`
- : '';
- })
- .join('');
-
- if (data) {
- const div = document.createElement('div');
- div.classList.add('table-responsive');
- const table = document.createElement('table');
- div.appendChild(table);
- table.classList.add('table');
- const tbody = document.createElement('tbody');
- tbody.innerHTML = data;
- table.appendChild(tbody);
- return div;
- }
- return false;
- }
- }
- }
- });
- }
-
- // Filter form control to default size
- // ? setTimeout used for customer-all table initialization
- setTimeout(() => {
- const elementsToModify = [
- { selector: '.dt-buttons', classToAdd: 'gap-4' },
- { selector: '.dt-buttons .btn-group .btn', classToRemove: 'btn-secondary', classToAdd: 'btn-label-secondary' },
- { selector: '.dt-buttons .btn-group ~ .btn', classToRemove: 'btn-secondary' },
- { selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-0' },
- { selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
- { selector: '.dt-length', classToAdd: 'mt-0 mt-md-6' },
- { selector: '.dt-layout-end', classToAdd: 'gap-2 mt-0 mb-md-0 mb-4' },
- { selector: '.dt-layout-start', classToAdd: 'mt-0' },
- { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
- { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
- ];
-
- // Delete record
- elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
- document.querySelectorAll(selector).forEach(element => {
- if (classToRemove) {
- classToRemove.split(' ').forEach(className => element.classList.remove(className));
- }
- if (classToAdd) {
- classToAdd.split(' ').forEach(className => element.classList.add(className));
- }
- });
- });
- }, 100);
- });
-
- // Validation & Phone mask
- (function () {
- const phoneMaskList = document.querySelectorAll('.phone-mask'),
- eCommerceCustomerAddForm = document.getElementById('eCommerceCustomerAddForm');
-
- // Phone Number
- if (phoneMaskList) {
- phoneMaskList.forEach(function (phoneMask) {
- phoneMask.addEventListener('input', event => {
- const cleanValue = event.target.value.replace(/\D/g, '');
- phoneMask.value = formatGeneral(cleanValue, {
- blocks: [3, 3, 4],
- delimiters: [' ', ' ']
- });
- });
- registerCursorTracker({
- input: phoneMask,
- delimiter: ' '
- });
- });
- }
- // Add New customer Form Validation
- const fv = FormValidation.formValidation(eCommerceCustomerAddForm, {
- fields: {
- customerName: {
- validators: {
- notEmpty: {
- message: 'Please enter fullname '
- }
- }
- },
- customerEmail: {
- validators: {
- notEmpty: {
- message: 'Please enter your email'
- },
- emailAddress: {
- message: 'The value is not a valid email address'
- }
- }
- }
- },
- plugins: {
- trigger: new FormValidation.plugins.Trigger(),
- bootstrap5: new FormValidation.plugins.Bootstrap5({
- // Use this for enabling/changing valid/invalid class
- eleValidClass: '',
- rowSelector: function (field, ele) {
- // field is the field name & ele is the field element
- return '.form-control-validation';
- }
- }),
- submitButton: new FormValidation.plugins.SubmitButton(),
- // Submit the form when all fields are valid
- // defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
- autoFocus: new FormValidation.plugins.AutoFocus()
- }
- });
- })();
|