1 import {debounce} from "../services/util";
2 import {transitionHeight} from "../services/animations";
8 this.searchInput = this.$refs.searchInput;
9 this.loadingElem = this.$refs.loading;
10 this.listContainerElem = this.$refs.listContainer;
12 this.localSearchSelector = this.$opts.localSearchSelector;
13 this.url = this.$opts.url;
15 this.elem.addEventListener('show', this.onShow.bind(this));
16 this.searchInput.addEventListener('input', this.onSearch.bind(this));
18 this.runAjaxSearch = debounce(this.runAjaxSearch, 300, false);
26 const input = this.searchInput.value.toLowerCase().trim();
27 if (this.localSearchSelector) {
28 this.runLocalSearch(input);
30 this.toggleLoading(true);
31 this.listContainerElem.innerHTML = '';
32 this.runAjaxSearch(input);
36 runAjaxSearch(searchTerm) {
37 this.loadList(searchTerm);
40 runLocalSearch(searchTerm) {
41 const listItems = this.listContainerElem.querySelectorAll(this.localSearchSelector);
42 for (let listItem of listItems) {
43 const match = !searchTerm || listItem.textContent.toLowerCase().includes(searchTerm);
44 listItem.style.display = match ? 'flex' : 'none';
45 listItem.classList.toggle('hidden', !match);
49 async loadList(searchTerm = '') {
50 this.listContainerElem.innerHTML = '';
51 this.toggleLoading(true);
54 const resp = await window.$http.get(this.getAjaxUrl(searchTerm));
55 const animate = transitionHeight(this.listContainerElem, 80);
56 this.listContainerElem.innerHTML = resp.data;
62 this.toggleLoading(false);
63 if (this.localSearchSelector) {
68 getAjaxUrl(searchTerm = null) {
73 const joiner = this.url.includes('?') ? '&' : '?';
74 return `${this.url}${joiner}search=${encodeURIComponent(searchTerm)}`;
77 toggleLoading(show = false) {
78 this.loadingElem.style.display = show ? 'block' : 'none';
83 export default DropdownSearch;