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.runAjaxSearch(input);
34 runAjaxSearch(searchTerm) {
35 this.loadList(searchTerm);
38 runLocalSearch(searchTerm) {
39 const listItems = this.listContainerElem.querySelectorAll(this.localSearchSelector);
40 for (let listItem of listItems) {
41 const match = !searchTerm || listItem.textContent.toLowerCase().includes(searchTerm);
42 listItem.style.display = match ? 'flex' : 'none';
43 listItem.classList.toggle('hidden', !match);
47 async loadList(searchTerm = '') {
48 this.listContainerElem.innerHTML = '';
49 this.toggleLoading(true);
52 const resp = await window.$http.get(this.getAjaxUrl(searchTerm));
53 this.listContainerElem.innerHTML = resp.data;
58 this.toggleLoading(false);
59 if (this.localSearchSelector) {
64 getAjaxUrl(searchTerm = null) {
69 const joiner = this.url.includes('?') ? '&' : '?';
70 return `${this.url}${joiner}search=${encodeURIComponent(searchTerm)}`;
73 toggleLoading(show = false) {
74 this.loadingElem.style.display = show ? 'block' : 'none';
79 export default DropdownSearch;