/**
* App eCommerce Category List
*/
'use strict';
// Comment editor
const commentEditor = document.querySelector('.comment-editor');
if (commentEditor) {
new Quill(commentEditor, {
modules: {
toolbar: '.comment-toolbar'
},
placeholder: 'Write a Comment...',
theme: 'snow'
});
}
// Datatable (js)
document.addEventListener('DOMContentLoaded', function (e) {
var dt_category_list_table = document.querySelector('.datatables-category-list');
//select2 for dropdowns in offcanvas
var select2 = $('.select2');
if (select2.length) {
select2.each(function () {
var $this = $(this);
$this.wrap('
').select2({
dropdownParent: $this.parent(),
placeholder: $this.data('placeholder') //for dynamic placeholder
});
});
}
// Customers List Datatable
if (dt_category_list_table) {
var dt_category = new DataTable(dt_category_list_table, {
ajax: assetsPath + 'json/ecommerce-category-list.json', // JSON file to add data
columns: [
// columns according to JSON
{ data: 'id' },
{ data: 'id', orderable: false, render: DataTable.render.select() },
{ data: 'categories' },
{ data: 'total_products' },
{ data: 'total_earnings' },
{ data: 'id' }
],
columnDefs: [
{
// For Responsive
className: 'control',
searchable: false,
orderable: false,
responsivePriority: 1,
targets: 0,
render: function (data, type, full, meta) {
return '';
}
},
{
// For Checkboxes
targets: 1,
orderable: false,
searchable: false,
responsivePriority: 4,
checkboxes: true,
render: function () {
return '';
},
checkboxes: {
selectAllRender: ''
}
},
{
targets: 2,
responsivePriority: 2,
render: function (data, type, full, meta) {
const name = full['categories'];
const categoryDetail = full['category_detail'];
const image = full['cat_image'];
const id = full['id'];
let output;
if (image) {
// For Product image
output = `
`;
} else {
// For Product badge
const stateNum = Math.floor(Math.random() * 6);
const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
const state = states[stateNum];
const initials = (categoryDetail.match(/\b\w/g) || []).slice(0, 2).join('').toUpperCase();
output = `${initials}`;
}
// Creates full output for Categories and Category Detail
const rowOutput = `
${name}
${categoryDetail}
`;
return rowOutput;
}
},
{
// Total products
targets: 3,
responsivePriority: 3,
render: function (data, type, full, meta) {
const total_products = full['total_products'];
return '' + total_products + '
';
}
},
{
// Total Earnings
targets: 4,
orderable: false,
render: function (data, type, full, meta) {
const total_earnings = full['total_earnings'];
return "" + total_earnings + '
`;
}
}
],
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 Category',
text: '_INPUT_'
}
}
]
},
topEnd: {
rowClass: 'row m-3 my-0 justify-content-between',
features: {
pageLength: {
menu: [10, 25, 50, 100],
text: '_MENU_'
},
buttons: [
{
text: `Add Category`,
className: 'add-new btn btn-primary',
attr: {
'data-bs-toggle': 'offcanvas',
'data-bs-target': '#offcanvasEcommerceCategoryList'
}
}
]
}
},
bottomStart: {
rowClass: 'row mx-3 justify-content-between',
features: ['info']
},
bottomEnd: 'paging'
},
language: {
paginate: {
next: '',
previous: '',
first: '',
last: ''
}
},
// For responsive popup
responsive: {
details: {
display: DataTable.Responsive.display.modal({
header: function (row) {
const data = row.data();
return 'Details of ' + data['categories'];
}
}),
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)
? `
| ${col.title}: |
${col.data} |
`
: '';
})
.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 category-list table initialization
setTimeout(() => {
const elementsToModify = [
{ selector: '.dt-buttons .btn', classToRemove: 'btn-secondary' },
{ selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-0' },
{ selector: '.dt-search', classToAdd: 'mb-0 mb-md-6' },
{ selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
{ selector: '.dt-layout-table', classToRemove: 'row mt-2', classToAdd: 'border-top' },
{ selector: '.dt-layout-start', classToAdd: 'px-3 mt-0' },
{ selector: '.dt-layout-end', classToAdd: 'px-3 column-gap-2 mt-0 mb-md-0 mb-4' },
{ selector: '.dt-layout-full', classToAdd: 'table-responsive' }
];
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);
});
//For form validation
(function () {
const eCommerceCategoryListForm = document.getElementById('eCommerceCategoryListForm');
//Add New customer Form Validation
const fv = FormValidation.formValidation(eCommerceCategoryListForm, {
fields: {
categoryTitle: {
validators: {
notEmpty: {
message: 'Please enter category title'
}
}
},
slug: {
validators: {
notEmpty: {
message: 'Please enter slug'
}
}
}
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap5: new FormValidation.plugins.Bootstrap5({
// Use this for enabling/changing valid/invalid class
eleValidClass: 'is-valid',
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()
}
});
})();