1 import {debounce} from "../services/util";
7 this.searchInput = this.$refs.searchInput;
8 this.loadingElem = this.$refs.loading;
9 this.listContainerElem = this.$refs.listContainer;
11 this.localSearchSelector = this.$opts.localSearchSelector;
12 this.url = this.$opts.url;
14 this.elem.addEventListener('show', this.onShow.bind(this));
15 this.searchInput.addEventListener('input', this.onSearch.bind(this));
17 this.runAjaxSearch = debounce(this.runAjaxSearch, 300, false);
25 const input = this.searchInput.value.toLowerCase().trim();
26 if (this.localSearchSelector) {
27 this.runLocalSearch(input);
29 this.toggleLoading(true);
30 this.listContainerElem.innerHTML = '';
31 this.runAjaxSearch(input);
35 runAjaxSearch(searchTerm) {
36 this.loadList(searchTerm);
39 runLocalSearch(searchTerm) {
40 const listItems = this.listContainerElem.querySelectorAll(this.localSearchSelector);
41 for (let listItem of listItems) {
42 const match = !searchTerm || listItem.textContent.toLowerCase().includes(searchTerm);
43 listItem.style.display = match ? 'flex' : 'none';
44 listItem.classList.toggle('hidden', !match);
48 async loadList(searchTerm = '') {
49 this.listContainerElem.innerHTML = '';
50 this.toggleLoading(true);
53 const resp = await window.$http.get(this.getAjaxUrl(searchTerm));
54 this.listContainerElem.innerHTML = resp.data;
59 this.toggleLoading(false);
60 if (this.localSearchSelector) {
65 getAjaxUrl(searchTerm = null) {
70 const joiner = this.url.includes('?') ? '&' : '?';
71 return `${this.url}${joiner}search=${encodeURIComponent(searchTerm)}`;
74 toggleLoading(show = false) {
75 this.loadingElem.style.display = show ? 'block' : 'none';
80 export default DropdownSearch;