@salla.sa/twilight-components
Version:
Salla Web Component
508 lines (504 loc) • 21.8 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import { proxyCustomElement, HTMLElement, h, Fragment, Host } from '@stencil/core/internal/client';
import { d as defineCustomElement$i } from './salla-accordion2.js';
import { d as defineCustomElement$h } from './salla-accordion-body2.js';
import { d as defineCustomElement$g } from './salla-accordion-head2.js';
import { d as defineCustomElement$f } from './salla-booking-field2.js';
import { d as defineCustomElement$e } from './salla-button2.js';
import { d as defineCustomElement$d } from './salla-color-picker2.js';
import { d as defineCustomElement$c } from './salla-conditional-fields2.js';
import { d as defineCustomElement$b } from './salla-datetime-picker2.js';
import { d as defineCustomElement$a } from './salla-file-upload2.js';
import { d as defineCustomElement$9 } from './salla-loading2.js';
import { d as defineCustomElement$8 } from './salla-map2.js';
import { d as defineCustomElement$7 } from './salla-modal2.js';
import { d as defineCustomElement$6 } from './salla-multiple-bundle-product-options-modal2.js';
import { d as defineCustomElement$5 } from './salla-multiple-bundle-product-slider2.js';
import { d as defineCustomElement$4 } from './salla-product-options2.js';
import { d as defineCustomElement$3 } from './salla-progress-bar2.js';
import { d as defineCustomElement$2 } from './salla-skeleton2.js';
import { d as defineCustomElement$1 } from './salla-slider2.js';
const sallaMultipleBundleProductDetailsCss = "";
const SallaMultipleBundleProductDetails = /*@__PURE__*/ proxyCustomElement(class SallaMultipleBundleProductDetails extends HTMLElement {
constructor() {
super();
this.__registerHost();
/** The list of sections belonging to a bundle product. */
this.sections = [];
// store selected product IDs per section (can be string or number)
this.selectedProducts = {};
// Event handler reference for cleanup
this.productSelectedHandler = null;
// handle selecting a product (toggle)
this.onSelectProduct = (sectionId, product) => {
const productId = product.id;
const wasSelected = this.selectedProducts[sectionId]?.has(productId) ?? false;
const section = this.sections.find(s => s.id == sectionId);
if (wasSelected && section && this.isProductSelectionLocked(section)) {
return;
}
if (!wasSelected) {
if (!section) {
return;
}
if (this.getEffectiveMax(section) !== 1 && this.isAtSelectionLimit(section)) {
return;
}
}
this.selectedProducts = {
...this.selectedProducts,
[sectionId]: new Set(this.selectedProducts[sectionId] || []),
};
if (wasSelected) {
this.selectedProducts[sectionId].delete(productId);
this.clearProductFormData(productId, sectionId);
this.clearProductModalOptions(productId, sectionId);
}
else {
const effectiveMax = this.getEffectiveMax(section);
if (effectiveMax === 1) {
this.syncSectionSelection(sectionId, productId);
}
else {
this.selectedProducts[sectionId].add(productId);
}
}
// force re-render
this.selectedProducts = { ...this.selectedProducts };
// still dispatch event
salla.event.dispatch('on-bundle-product-selected', {
id: product.id,
name: product.name,
options: product.options,
wasSelected: wasSelected,
isSelected: !wasSelected,
});
};
// ensure product is selected (only add if not already selected)
this.ensureProductSelected = (sectionId, product) => {
this.selectedProducts = {
...this.selectedProducts,
[sectionId]: new Set(this.selectedProducts[sectionId] || []),
};
const productId = product.id;
if (this.selectedProducts[sectionId].has(productId)) {
return;
}
const section = this.sections.find(s => s.id == sectionId);
if (!section) {
return;
}
const effectiveMax = this.getEffectiveMax(section);
if (effectiveMax === 1) {
this.syncSectionSelection(sectionId, productId);
}
else if (effectiveMax > 1 && this.isAtSelectionLimit(section)) {
return;
}
else {
this.selectedProducts[sectionId].add(productId);
}
this.selectedProducts = { ...this.selectedProducts };
salla.event.dispatch('on-bundle-product-selected', {
id: product.id,
name: product.name,
options: product.options,
});
};
// open product options modal
this.onSelectProductOptions = (product, sectionId) => {
const section = this.sections.find(s => s.id == sectionId);
if (!section || !this.canSelectProductInSection(section, product.id)) {
return;
}
const sectionIndex = this.sections.findIndex(s => s.id == sectionId);
const productIndex = section.products?.findIndex(p => p.id == product.id) ?? 0;
const isProductAlreadySelected = this.selectedProducts[sectionId]?.has(product.id) ?? false;
salla.event.dispatch('multiple-bundle-product-modal::open', {
product,
sectionId,
sectionIndex,
productIndex,
isProductAlreadySelected,
});
};
// Event handlers for bundle slider component
this.handleBundleSliderProductSelected = (event) => {
const { product, sectionId } = event.detail;
this.onSelectProduct(sectionId, product);
};
this.handleBundleSliderProductOptionsSelected = (event) => {
const { product, sectionId } = event.detail;
this.onSelectProductOptions(product, sectionId);
};
}
isSingleProductSection(section) {
return (section.products?.length ?? 0) === 1;
}
getObligatoryMin(section) {
const min = section.obligatory_products;
if (min == null) {
return 0;
}
const numericMin = Number(min);
return Number.isNaN(numericMin) ? 0 : numericMin;
}
isProductSelectionLocked(section) {
return this.isSingleProductSection(section) && this.getObligatoryMin(section) === 1;
}
/** Empty max → cap is the number of products in the section. */
getEffectiveMax(section) {
const productCount = section.products?.length ?? 0;
const max = section.max_obligatory_products;
if (max != null && max > 0) {
return Math.min(max, productCount);
}
return productCount;
}
getSelectedCount(sectionId) {
return this.selectedProducts[sectionId]?.size ?? 0;
}
isAtSelectionLimit(section) {
return this.getSelectedCount(section.id) >= this.getEffectiveMax(section);
}
canSelectProductInSection(section, productId) {
if (this.selectedProducts[section.id]?.has(productId)) {
return true;
}
if (this.getEffectiveMax(section) === 1) {
return true;
}
return !this.isAtSelectionLimit(section);
}
async canSelectBundleProduct(sectionId, productId) {
const section = this.sections.find(s => s.id == sectionId);
if (!section) {
return false;
}
return this.canSelectProductInSection(section, productId);
}
queryProductCheckbox(sectionId, productIndex) {
const selector = `input.s-multiple-bundle-product-checkbox[name="bundle[${sectionId}][${productIndex}][id]"]`;
const form = this.host.closest('form');
const fromForm = form?.querySelector(selector);
if (fromForm) {
return fromForm;
}
for (const slider of this.host.querySelectorAll('salla-multiple-bundle-product-slider')) {
const root = slider.shadowRoot ?? slider;
const input = root.querySelector(selector);
if (input) {
return input;
}
}
return null;
}
dispatchBubblingChange(target) {
requestAnimationFrame(() => {
target.dispatchEvent(new window.Event('change', { bubbles: true }));
});
}
/** Uncheck checkbox, remove form inputs, and reset modal state for one product slot. */
clearSectionProductSlot(sectionId, productId, productIndex) {
const checkbox = this.queryProductCheckbox(sectionId, productIndex);
if (checkbox) {
checkbox.checked = false;
}
const form = this.host.closest('form');
if (form) {
const slotPrefix = `bundle[${sectionId}][${productIndex}]`;
Array.from(form.querySelectorAll('input')).forEach((input) => {
const inSlot = input.name?.startsWith(slotPrefix) ?? false;
if (!inSlot) {
return;
}
if (input.type === 'checkbox' && input.name?.endsWith('][id]')) {
input.checked = false;
return;
}
input.remove();
});
}
this.clearProductModalOptions(productId, sectionId);
}
/** max=1: replace section selection and keep DOM/form in sync. */
syncSectionSelection(sectionId, productId) {
const section = this.sections.find(s => s.id == sectionId);
section?.products?.forEach((product, productIndex) => {
if (product.id != productId) {
this.clearSectionProductSlot(sectionId, product.id, productIndex);
}
});
this.selectedProducts[sectionId] = new Set([productId]);
const selectedIndex = section?.products?.findIndex(p => p.id == productId) ?? -1;
const checkbox = selectedIndex >= 0 ? this.queryProductCheckbox(sectionId, selectedIndex) : null;
if (checkbox) {
checkbox.checked = true;
this.dispatchBubblingChange(checkbox);
}
}
autoSelectSingleProductSections() {
if (!this.sections?.length) {
return;
}
const newSelectedProducts = { ...this.selectedProducts };
let updated = false;
const selectedEvents = [];
for (const section of this.sections) {
if (!this.isProductSelectionLocked(section)) {
continue;
}
const product = section.products?.[0];
if (!product || (!product.unlimited_quantity && (product.quantity ?? 0) <= 0)) {
continue;
}
const sectionId = section.id;
const currentSet = new Set(newSelectedProducts[sectionId] || []);
if (currentSet.has(product.id)) {
continue;
}
currentSet.add(product.id);
newSelectedProducts[sectionId] = currentSet;
updated = true;
selectedEvents.push({ product });
}
if (!updated) {
return;
}
this.selectedProducts = newSelectedProducts;
selectedEvents.forEach(({ product }) => {
salla.event.dispatch('on-bundle-product-selected', {
id: product.id,
name: product.name,
options: product.options,
});
});
}
// Clear form data for a specific product in specific section
clearProductFormData(productId, sectionId) {
if (sectionId != null) {
const section = this.sections.find(s => s.id == sectionId);
const productIndex = section?.products?.findIndex(product => product.id == productId) ?? -1;
if (productIndex >= 0) {
this.clearSectionProductSlot(sectionId, productId, productIndex);
return;
}
}
const form = this.host.closest('form');
if (!form) {
return;
}
const inputsToRemove = form.querySelectorAll(`[data-product-id="${productId}"]`);
inputsToRemove.forEach(input => input.remove());
}
// Clear modal options state for a specific product
clearProductModalOptions(productId, sectionId) {
let sectionIndex = null;
let productIndex = null;
if (sectionId != null) {
sectionIndex = this.sections.findIndex(section => section.id == sectionId);
if (sectionIndex > -1) {
const section = this.sections[sectionIndex];
if (section) {
const foundIndex = section.products?.findIndex(product => product.id == productId);
productIndex = typeof foundIndex === 'number' && foundIndex > -1 ? foundIndex : null;
}
}
}
// Emit event to notify modal to reset its state for this product
salla.event.dispatch('multiple-bundle-product-modal::clear-options', {
productId,
sectionId,
sectionIndex,
productIndex,
});
}
getProgressStatus(section) {
const selectedCount = this.getSelectedCount(section.id);
const effectiveMax = this.getEffectiveMax(section);
if (effectiveMax > 0) {
return `${selectedCount}/${effectiveMax}`;
}
return '0';
}
getSectionSelectionNote(section) {
const min = this.getObligatoryMin(section);
const max = Number(section.max_obligatory_products) || 0;
const hasMin = min > 0;
const hasMax = max > 0;
if (!hasMin && !hasMax) {
return null;
}
if (hasMin && hasMax) {
if (min === max) {
return salla.lang.getWithDefault('pages.products.bundle_select_exact', `اختر ${min} منتجات`, { count: min });
}
return salla.lang.getWithDefault('pages.products.bundle_select_range', `اختر من ${min} إلى ${max} منتجات`, { min, max });
}
if (hasMin) {
return salla.lang.getWithDefault('pages.products.bundle_select_min', `اختر على الأقل ${min} منتجات`, { min });
}
return salla.lang.getWithDefault('pages.products.bundle_select_max', `اختر حتى ${max} منتجات`, { max });
}
handleSectionsChange() {
this.autoSelectSingleProductSections();
}
componentWillLoad() {
this.autoSelectSingleProductSections();
}
renderAccordionHeader(section) {
const selectionNote = this.getSectionSelectionNote(section);
return (h(Fragment, null, h("h2", { slot: "title" }, section?.name), selectionNote ? h("span", { slot: "note" }, selectionNote) : null, h("span", { slot: "progress" }, this.getProgressStatus(section))));
}
componentDidLoad() {
// Listen for product selected event from modal
const modal = this.host.querySelector('salla-multiple-bundle-product-options-modal');
if (modal) {
this.productSelectedHandler = (e) => {
const { productId, sectionId, product, fromModal } = e.detail;
if (fromModal) {
// When called from modal, only add to selection if not already selected
this.ensureProductSelected(sectionId, product || { id: productId });
}
else {
// Normal toggle behavior
this.onSelectProduct(sectionId, product || { id: productId });
}
};
modal.addEventListener('productSelected', this.productSelectedHandler);
}
}
disconnectedCallback() {
// Clean up event listener to prevent memory leaks
if (this.productSelectedHandler) {
const modal = this.host.querySelector('salla-multiple-bundle-product-options-modal');
if (modal) {
modal.removeEventListener('productSelected', this.productSelectedHandler);
}
this.productSelectedHandler = null;
}
}
render() {
return (h(Host, { key: '74b123c432e77263cf0ddcf20ab742885c54d5f4', class: "s-multiple-bundle-product-wrapper" }, h("div", { key: '24bef0958a2a7d29bb4485eac08c7e6b8614a00d', class: "s-multiple-bundle-product-wrapper-sections" }, this.sections.map((section, index) => {
return (h("salla-accordion", { key: section.id, collapsed: index === 0 ? false : true }, h("salla-accordion-head", null, this.renderAccordionHeader(section)), h("salla-accordion-body", null, h("salla-multiple-bundle-product-slider", { section: section, sectionIndex: index, selectedProducts: this.selectedProducts, selectionLimit: this.getEffectiveMax(section), isSelectionLocked: this.isProductSelectionLocked(section), onProductSelected: this.handleBundleSliderProductSelected, onProductOptionsSelected: this.handleBundleSliderProductOptionsSelected }))));
})), h("salla-multiple-bundle-product-options-modal", { key: '556b4e51a4b79dfc00371a07296556fb74111524' })));
}
get host() { return this; }
static get watchers() { return {
"sections": ["handleSectionsChange"]
}; }
static get style() { return sallaMultipleBundleProductDetailsCss; }
}, [0, "salla-multiple-bundle-product-details", {
"sections": [16],
"selectedProducts": [32],
"canSelectBundleProduct": [64]
}, undefined, {
"sections": ["handleSectionsChange"]
}]);
function defineCustomElement() {
if (typeof customElements === "undefined") {
return;
}
const components = ["salla-multiple-bundle-product-details", "salla-accordion", "salla-accordion-body", "salla-accordion-head", "salla-booking-field", "salla-button", "salla-color-picker", "salla-conditional-fields", "salla-datetime-picker", "salla-file-upload", "salla-loading", "salla-map", "salla-modal", "salla-multiple-bundle-product-options-modal", "salla-multiple-bundle-product-slider", "salla-product-options", "salla-progress-bar", "salla-skeleton", "salla-slider"];
components.forEach(tagName => { switch (tagName) {
case "salla-multiple-bundle-product-details":
if (!customElements.get(tagName)) {
customElements.define(tagName, SallaMultipleBundleProductDetails);
}
break;
case "salla-accordion":
if (!customElements.get(tagName)) {
defineCustomElement$i();
}
break;
case "salla-accordion-body":
if (!customElements.get(tagName)) {
defineCustomElement$h();
}
break;
case "salla-accordion-head":
if (!customElements.get(tagName)) {
defineCustomElement$g();
}
break;
case "salla-booking-field":
if (!customElements.get(tagName)) {
defineCustomElement$f();
}
break;
case "salla-button":
if (!customElements.get(tagName)) {
defineCustomElement$e();
}
break;
case "salla-color-picker":
if (!customElements.get(tagName)) {
defineCustomElement$d();
}
break;
case "salla-conditional-fields":
if (!customElements.get(tagName)) {
defineCustomElement$c();
}
break;
case "salla-datetime-picker":
if (!customElements.get(tagName)) {
defineCustomElement$b();
}
break;
case "salla-file-upload":
if (!customElements.get(tagName)) {
defineCustomElement$a();
}
break;
case "salla-loading":
if (!customElements.get(tagName)) {
defineCustomElement$9();
}
break;
case "salla-map":
if (!customElements.get(tagName)) {
defineCustomElement$8();
}
break;
case "salla-modal":
if (!customElements.get(tagName)) {
defineCustomElement$7();
}
break;
case "salla-multiple-bundle-product-options-modal":
if (!customElements.get(tagName)) {
defineCustomElement$6();
}
break;
case "salla-multiple-bundle-product-slider":
if (!customElements.get(tagName)) {
defineCustomElement$5();
}
break;
case "salla-product-options":
if (!customElements.get(tagName)) {
defineCustomElement$4();
}
break;
case "salla-progress-bar":
if (!customElements.get(tagName)) {
defineCustomElement$3();
}
break;
case "salla-skeleton":
if (!customElements.get(tagName)) {
defineCustomElement$2();
}
break;
case "salla-slider":
if (!customElements.get(tagName)) {
defineCustomElement$1();
}
break;
} });
}
defineCustomElement();
export { SallaMultipleBundleProductDetails as S, defineCustomElement as d };