Add better search
This commit is contained in:
parent
3a7501dcc6
commit
4d3fbeb901
@ -63,14 +63,6 @@ customElements.define('local-business-item', class extends Component {
|
||||
`);
|
||||
}
|
||||
|
||||
connectedCallback() {
|
||||
super.connectedCallback();
|
||||
}
|
||||
|
||||
attributeChangedCallback(name, oldV, newV) {
|
||||
super.attributeChangedCallback(name, oldV, newV);
|
||||
}
|
||||
|
||||
get price() {
|
||||
const n = parseInt(this.getAttribute('price'));
|
||||
return isNaN(n) ? 0 : n;
|
||||
@ -103,4 +95,8 @@ customElements.define('local-business-item', class extends Component {
|
||||
const el = this.shadowRoot.querySelector('#img');
|
||||
el.src = v;
|
||||
}
|
||||
|
||||
matches(text) {
|
||||
return !!this.name.match(text)
|
||||
}
|
||||
});
|
||||
|
@ -60,4 +60,14 @@ customElements.define('local-business', class extends Component {
|
||||
set service_id(v) {
|
||||
this.setAttribute('service-id', v);
|
||||
}
|
||||
|
||||
get description() {
|
||||
return Array.from(this.querySelectorAll('[slot=description]')).map(el => el.textContent);
|
||||
}
|
||||
|
||||
matches(regex) {
|
||||
return this.name.match(regex) ||
|
||||
this.description.any(s => s.match(regex)) ||
|
||||
Array.from(this.querySelectorAll('local-business-item')).any(el => el.matches(regex));
|
||||
}
|
||||
});
|
||||
|
@ -38,7 +38,7 @@ customElements.define('local-business-list', class extends Component {
|
||||
</style>
|
||||
<article>
|
||||
<section>
|
||||
<input type="text" id="filter" placeholder="Filtruj" />
|
||||
<input type="text" id="filter" placeholder="Znajdź (wyrażenia regularne są wspierane)" />
|
||||
</section>
|
||||
<section id="items">
|
||||
<slot name="business"></slot>
|
||||
@ -86,8 +86,7 @@ customElements.define('local-business-list', class extends Component {
|
||||
} else {
|
||||
this.setAttribute('filter', value);
|
||||
for (const el of this.querySelectorAll('local-business')) {
|
||||
if (!el.name) continue;
|
||||
if (el.name.includes(value)) {
|
||||
if (el.matches(new RegExp(value))) {
|
||||
el.setAttribute('local-business-visible', 'visible');
|
||||
} else {
|
||||
el.setAttribute('local-business-visible', 'invisible');
|
||||
|
@ -232,4 +232,8 @@ customElements.define('marketplace-offer', class extends Component {
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
matches(regex) {
|
||||
return !!this.description.match(regex)
|
||||
}
|
||||
});
|
||||
|
@ -15,8 +15,32 @@ Object.defineProperties(Object.prototype, {
|
||||
})
|
||||
|
||||
Object.defineProperties(Array.prototype, {
|
||||
'last': { get() { return this[this.length - 1] } },
|
||||
'tail': { get() { return this[this.length - 1] } },
|
||||
'first': { get() { return this[0] } },
|
||||
'head': { get() { return this[0] } },
|
||||
'last': {
|
||||
get() {
|
||||
return this[this.length - 1]
|
||||
}
|
||||
},
|
||||
'tail': {
|
||||
get() {
|
||||
return this[this.length - 1]
|
||||
}
|
||||
},
|
||||
'first': {
|
||||
get() {
|
||||
return this[0]
|
||||
}
|
||||
},
|
||||
'head': {
|
||||
get() {
|
||||
return this[0]
|
||||
}
|
||||
},
|
||||
'any': {
|
||||
value(cb) {
|
||||
for (const el of this) {
|
||||
if (cb(el)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user