Add better search

This commit is contained in:
eraden 2022-07-29 19:41:37 +02:00
parent 3a7501dcc6
commit 4d3fbeb901
5 changed files with 48 additions and 15 deletions

View File

@ -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() { get price() {
const n = parseInt(this.getAttribute('price')); const n = parseInt(this.getAttribute('price'));
return isNaN(n) ? 0 : n; return isNaN(n) ? 0 : n;
@ -103,4 +95,8 @@ customElements.define('local-business-item', class extends Component {
const el = this.shadowRoot.querySelector('#img'); const el = this.shadowRoot.querySelector('#img');
el.src = v; el.src = v;
} }
matches(text) {
return !!this.name.match(text)
}
}); });

View File

@ -60,4 +60,14 @@ customElements.define('local-business', class extends Component {
set service_id(v) { set service_id(v) {
this.setAttribute('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));
}
}); });

View File

@ -38,7 +38,7 @@ customElements.define('local-business-list', class extends Component {
</style> </style>
<article> <article>
<section> <section>
<input type="text" id="filter" placeholder="Filtruj" /> <input type="text" id="filter" placeholder="Znajdź (wyrażenia regularne są wspierane)" />
</section> </section>
<section id="items"> <section id="items">
<slot name="business"></slot> <slot name="business"></slot>
@ -86,8 +86,7 @@ customElements.define('local-business-list', class extends Component {
} else { } else {
this.setAttribute('filter', value); this.setAttribute('filter', value);
for (const el of this.querySelectorAll('local-business')) { for (const el of this.querySelectorAll('local-business')) {
if (!el.name) continue; if (el.matches(new RegExp(value))) {
if (el.name.includes(value)) {
el.setAttribute('local-business-visible', 'visible'); el.setAttribute('local-business-visible', 'visible');
} else { } else {
el.setAttribute('local-business-visible', 'invisible'); el.setAttribute('local-business-visible', 'invisible');

View File

@ -232,4 +232,8 @@ customElements.define('marketplace-offer', class extends Component {
} }
return ''; return '';
} }
matches(regex) {
return !!this.description.match(regex)
}
}); });

View File

@ -15,8 +15,32 @@ Object.defineProperties(Object.prototype, {
}) })
Object.defineProperties(Array.prototype, { Object.defineProperties(Array.prototype, {
'last': { get() { return this[this.length - 1] } }, 'last': {
'tail': { get() { return this[this.length - 1] } }, get() {
'first': { get() { return this[0] } }, return this[this.length - 1]
'head': { get() { return this[0] } }, }
},
'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;
}
}
}) })