@salla.sa/twilight-components
Version:
Salla Web Component
176 lines (170 loc) • 9.6 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
'use strict';
var index = require('./index-WgjOGof0.js');
var arrowLeft = require('./arrow-left-xSEmRpp6.js');
var arrowRight = `<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 32 32">
<title>arrow-right</title>
<path d="M29.217 15.465c-0.019-0.044-0.056-0.077-0.080-0.119-0.067-0.116-0.139-0.227-0.236-0.317-0.011-0.009-0.013-0.024-0.024-0.033l-10.667-9.333c-0.553-0.484-1.396-0.429-1.881 0.125-0.484 0.555-0.428 1.396 0.127 1.881l7.996 6.997h-20.452c-0.737 0-1.333 0.597-1.333 1.333s0.596 1.333 1.333 1.333h20.452l-7.996 6.997c-0.555 0.485-0.611 1.327-0.127 1.881 0.264 0.3 0.633 0.455 1.004 0.455 0.312 0 0.625-0.109 0.877-0.331l10.667-9.333c0.011-0.009 0.015-0.024 0.024-0.033 0.097-0.091 0.169-0.201 0.236-0.317 0.024-0.041 0.060-0.075 0.080-0.119 0.073-0.163 0.116-0.343 0.116-0.533s-0.043-0.371-0.116-0.535z"></path>
</svg>
`;
const sallaPriceRangeCss = ":host{display:block}.rtl .rtl-range-slider,[dir=rtl] .rtl-range-slider{-webkit-transform:rotateY(180deg);transform:rotateY(180deg)}.rtl .rtl-range-numbers,[dir=rtl] .rtl-range-numbers{-ms-flex-direction:row-reverse;flex-direction:row-reverse}.rtl .form-select,.rtl select,[dir=rtl] .form-select,[dir=rtl] select{background-position:left 0.5rem center;padding-right:12px}.rtl .menu-divide li:not(:last-child):after,[dir=rtl] .menu-divide li:not(:last-child):after{left:0;right:auto}input[type=range]::-webkit-slider-thumb{pointer-events:all;width:24px;height:24px;-webkit-appearance:none}[type=checkbox]:checked{background-size:80%}";
const SallaPriceRange = class {
constructor(hostRef) {
index.registerInstance(this, hostRef);
this.changed = index.createEvent(this, "changed");
this.min = 0;
this.max = 10000;
this.moreThanLabel = "أكثر من";
this.lessThanLabel = "أقل من";
this.toLabel = "الى";
this.fromLabel = "من";
this.typing = false;
this.isMin = false;
this.isRTL = salla.config.get('theme.is_rtl', true);
this.filterValues = [];
}
connectedCallback() {
var _a;
if (this.filtersData && ((_a = this.filtersData) === null || _a === void 0 ? void 0 : _a.price)) {
this.minPrice = this.filtersData.price.min;
this.maxPrice = this.filtersData.price.max;
}
salla.lang.onLoaded(() => {
this.moreThanLabel = salla.lang.getWithDefault('common.elements.more_than', this.moreThanLabel);
this.lessThanLabel = salla.lang.getWithDefault('common.elements.less_than', this.lessThanLabel);
this.toLabel = salla.lang.getWithDefault('common.elements.to', this.toLabel);
this.fromLabel = salla.lang.getWithDefault('common.elements.from', this.fromLabel);
});
//no need to show one option only
if (this.option.values.length == 1) {
return;
}
//here we may receive too many prices, we will group all inputs to
if (this.option.values.length <= 5) {
this.filterValues = this.option.values;
return;
}
const chunkSize = Math.ceil(this.option.values.length / 5);
for (let i = 0; i < this.option.values.length; i += chunkSize) {
this.filterValues.push(this.option.values
.slice(i, i + chunkSize)
.reduce((final, currentValue) => {
final.to = currentValue.to;
final.count += currentValue.count;
return final;
}));
// do whatever
}
}
/**
* reset the price range inputs
*/
async reset() {
//@ts-ignore
this.minInput.value = null;
this.maxInput.value = null;
this.minPrice = null;
this.maxPrice = null;
}
getPriceLabel(filterValue) {
// @ts-ignore
if (isNaN(filterValue.from) || filterValue.from < 1) {
return `${this.lessThanLabel} ${salla.money(filterValue.to)}`;
}
// @ts-ignore
if (isNaN(filterValue.to) || filterValue.to < 1) {
return `${this.moreThanLabel} ${salla.money(filterValue.from)}`;
}
return `${salla.money(filterValue.from)} ${this.toLabel} ${salla.money(filterValue.to)}`;
}
minInputValidation(value) {
if (value && (value > this.max || (this.maxPrice && value > this.maxPrice))) {
// this.minPrice = this.maxPrice;
return;
}
if (value < this.min) {
this.minPrice = this.min;
return;
}
if (value) {
this.minPrice = value;
}
}
maxInputValidation(value) {
if (value && (value < this.min || (this.minPrice && value < this.minPrice))) {
// this.maxPrice = this.minPrice;
return;
}
if (value > this.max) {
this.maxPrice = this.max;
return;
}
}
async changedEventHandler(event) {
salla.helpers.inputDigitsOnly(this.minInput);
salla.helpers.inputDigitsOnly(this.maxInput);
const minInputValue = this.minPrice ? +this.minPrice * 1 : null;
const maxInputValue = this.maxPrice ? +this.maxPrice * 1 : null;
if (!this.maxPrice) {
this.maxPrice = '';
}
if (this.isMin) {
this.minInputValidation(minInputValue);
}
else {
if (!this.minPrice) {
this.minPrice = 0;
}
this.maxInputValidation(maxInputValue);
}
this.isReady && this.changed.emit({
event: event,
option: this.option,
value: { max: this.maxPrice, min: this.minPrice }
});
}
handleMinMaxPrice(event, value) {
//todo:: cover when from is star
this.minPrice = value.from;
this.maxPrice = value.to !== '*' ? value.to : "";
this.changedEventHandler(event);
}
isChecked(filterValue) {
if (!this.minPrice && !this.maxPrice) {
return false;
}
//1 filterValue.from zero or * and this.minPrice not set or zero
//2 filterValue.from == this.minPrice
//@ts-ignore
let isMinEqual = ((filterValue.from < 1 || filterValue.from == '*') && this.minPrice == 0) || filterValue.from == this.minPrice;
//1 filterValue.to == "*" or null
//2 filterValue.to == this.max
let isMaxEqual = filterValue.to == '*' || !filterValue.to || filterValue.to == this.maxPrice;
return isMinEqual && isMaxEqual;
}
handleMinPrice(event) {
this.isMin = true;
const value = event.target.value;
this.minPrice = value === '' ? 0 : value;
}
handleMaxPrice(event) {
this.isMin = false;
const value = event.target.value;
this.maxPrice = value === undefined ? '' : value;
}
render() {
return (index.h(index.Host, { key: '3dbf536c873ce1dfa5a340d38d827a42dc9eefd6' }, this.filterValues.map((filterValue, index$1) => {
return index.h("label", { class: "s-filters-label", htmlFor: `${this.option.key}-${index$1}`, key: index$1 }, index.h("input", { id: `${this.option.key}-${index$1}`, name: "price", type: "radio", checked: this.isChecked(filterValue), class: "s-filters-radio", onChange: e => this.handleMinMaxPrice(e, filterValue) }), index.h("span", { innerHTML: this.getPriceLabel(filterValue) }));
}), index.h("div", { key: 'ba4b1e2b012f2a592a3e3fccec8af85d553e135e', class: "flex justify-center items-center" }, index.h("div", { key: '7aa16587da39ca5ef69df278825979b9b0d7c431', class: "relative max-w-xl w-full" }, index.h("div", { key: '9987a6c8648161cc4ffbffb5c36a74a9e4951c5a', class: "s-price-range-inputs" }, index.h("div", { key: '5522e10855b7d343d7099775ed2d1d6adfd0993f', class: "s-price-range-relative" }, index.h("div", { key: 'cc9ecb09469f4a0244bea50e39d970817a324a82', class: "s-price-range-currency" }, " ", salla.config.currency().symbol), index.h("input", { key: 'd418bc1843338577fae373af8d9d6015f5f027fb', type: "number", maxlength: "5", ref: el => this.minInput = el, onBlur: (e) => this.handleMinPrice(e), value: this.minPrice, placeholder: this.fromLabel, class: "s-price-range-number-input" })), index.h("div", { key: '73bda499ac270dd938ef0c66f732a68925f8a844', class: "s-price-range-gray-text" }, " -"), index.h("div", { key: 'ffd58f181e8274a88d4d9523f7d37fab00c69c06', class: "s-price-range-relative" }, index.h("div", { key: '6ede40d74c8b94f232ec14fecab8ab36185a60db', class: "s-price-range-currency" }, " ", salla.config.currency().symbol), index.h("input", { key: '5922793287d2b9c2b7909ce71960408dcc1553f4', type: "number", maxlength: "5", ref: el => this.maxInput = el, onBlur: (e) => this.handleMaxPrice(e), value: this.maxPrice, placeholder: this.toLabel, class: "s-price-range-number-input", "aria-describedby": "price-currency" })), index.h("salla-button", { key: 'eaa18ffa9c09f4e34e3fd404735cb251e5ca9acc', color: 'gray', shape: 'icon', size: 'small', fill: 'outline', onClick: (event) => this.changedEventHandler(event) }, index.h("span", { key: '904ca86a02c9a59f48cf69e9ff2d1a8abc25b0f6', innerHTML: this.isRTL ? arrowLeft.arrowLeft : arrowRight })))))));
}
componentDidLoad() {
this.isReady = true;
}
};
SallaPriceRange.style = sallaPriceRangeCss;
exports.salla_price_range = SallaPriceRange;
//# sourceMappingURL=salla-price-range.entry.cjs.js.map
//# sourceMappingURL=salla-price-range.cjs.entry.js.map