@salla.sa/twilight-components
Version:
Salla Web Component
308 lines (307 loc) • 12.5 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import { Host, h } from "@stencil/core";
import arrowLeft from "../../assets/svg/arrow-left.svg";
import arrowRight from "../../assets/svg/arrow-right.svg";
export class SallaPriceRange {
constructor() {
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 (h(Host, { key: '4bbf78038f7e7da06f17d24717726e60b842b272' }, this.filterValues.map((filterValue, index) => {
return h("label", { class: "s-filters-label", htmlFor: `${this.option.key}-${index}`, key: index }, h("input", { id: `${this.option.key}-${index}`, name: "price", type: "radio", checked: this.isChecked(filterValue), class: "s-filters-radio", onChange: e => this.handleMinMaxPrice(e, filterValue) }), h("span", { innerHTML: this.getPriceLabel(filterValue) }));
}), h("div", { key: '97253ed3be349943cf2595557328a987739b3086', class: "flex justify-center items-center" }, h("div", { key: 'f4d087f5bef0c07cf01dc071613dc3932bd96670', class: "relative max-w-xl w-full" }, h("div", { key: 'f806be59df222454c8c709d637203a6e5c6d1fb0', class: "s-price-range-inputs" }, h("div", { key: 'a86e781533ee820428de9a669cb0737ac3ce33d6', class: "s-price-range-relative" }, h("div", { key: '7bf5e038d4eaac20831a866ed478bf2f66694abe', class: "s-price-range-currency" }, " ", salla.config.currency().symbol), h("input", { key: 'b58f77102c6833adc859d4f345d64f9269549e7c', 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" })), h("div", { key: '2f3a516a52253fadeecaf6e86b14ee202cd67d41', class: "s-price-range-gray-text" }, " -"), h("div", { key: 'cd4bdc28326a4294c06e5c77c23858900af3799b', class: "s-price-range-relative" }, h("div", { key: 'b1a1f86fd82a201050ce5036bbdc55faaf9ced54', class: "s-price-range-currency" }, " ", salla.config.currency().symbol), h("input", { key: '7e598948746a4260278b59ec7110b0acef60a89c', 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" })), h("salla-button", { key: 'a4b33af3ae0353cda9e76c3103aae16f767d37de', color: 'gray', shape: 'icon', size: 'small', fill: 'outline', onClick: (event) => this.changedEventHandler(event) }, h("span", { key: '4e7084ac446928a2f840a80234187b4366428d0b', innerHTML: this.isRTL ? arrowLeft : arrowRight })))))));
}
componentDidLoad() {
this.isReady = true;
}
static get is() { return "salla-price-range"; }
static get originalStyleUrls() {
return {
"$": ["salla-price-range.scss"]
};
}
static get styleUrls() {
return {
"$": ["salla-price-range.css"]
};
}
static get properties() {
return {
"minPrice": {
"type": "any",
"attribute": "min-price",
"mutable": true,
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Minimum price threshold value"
},
"getter": false,
"setter": false,
"reflect": false
},
"maxPrice": {
"type": "any",
"attribute": "max-price",
"mutable": true,
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Maximum price threshold value"
},
"getter": false,
"setter": false,
"reflect": false
},
"option": {
"type": "unknown",
"attribute": "option",
"mutable": false,
"complexType": {
"original": "Filter",
"resolved": "Filter",
"references": {
"Filter": {
"location": "import",
"path": "../salla-filters/interfaces",
"id": "src/components/salla-filters/interfaces.ts::Filter"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Product price range filter option object instance"
},
"getter": false,
"setter": false
},
"filtersData": {
"type": "any",
"attribute": "filters-data",
"mutable": false,
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Currently selected price filter data"
},
"getter": false,
"setter": false,
"reflect": true
}
};
}
static get states() {
return {
"min": {},
"max": {},
"priceOptions": {},
"moreThanLabel": {},
"lessThanLabel": {},
"toLabel": {},
"fromLabel": {},
"typing": {},
"isMin": {},
"isRTL": {}
};
}
static get events() {
return [{
"method": "changed",
"name": "changed",
"bubbles": true,
"cancelable": true,
"composed": true,
"docs": {
"tags": [],
"text": "Custome event emitted when there is a change in price input."
},
"complexType": {
"original": "any",
"resolved": "any",
"references": {}
}
}];
}
static get methods() {
return {
"reset": {
"complexType": {
"signature": "() => Promise<void>",
"parameters": [],
"references": {
"Promise": {
"location": "global",
"id": "global::Promise"
}
},
"return": "Promise<void>"
},
"docs": {
"text": "reset the price range inputs",
"tags": []
}
}
};
}
}
//# sourceMappingURL=salla-price-range.js.map