2024-03-22 23:37:59 +08:00
|
|
|
import axios from '../../../packs/custom_axios.js';
|
|
|
|
import StringWithEllipsis from '../../shared/string_with_ellipsis.vue';
|
2024-04-16 19:33:36 +08:00
|
|
|
import SortFlyout from './helpers/sort_flyout.vue';
|
2024-04-11 15:22:50 +08:00
|
|
|
import Loader from '../loader.vue';
|
2024-04-16 19:33:36 +08:00
|
|
|
import ListEnd from './helpers/list_end.vue';
|
|
|
|
import NoSearchResult from './helpers/no_search_result.vue';
|
|
|
|
import CellTemplate from './helpers/cell_template.vue';
|
|
|
|
import LinkTemplate from './helpers/link_template.vue';
|
2024-05-13 19:46:56 +08:00
|
|
|
import TableHeader from './helpers/table_header.vue';
|
2024-04-11 19:45:29 +08:00
|
|
|
/* global GLOBAL_CONSTANTS I18n */
|
2024-03-22 23:37:59 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
props: {
|
|
|
|
searchUrl: String,
|
|
|
|
query: String,
|
2024-04-05 17:16:58 +08:00
|
|
|
selected: Boolean,
|
|
|
|
filters: Object
|
2024-03-22 23:37:59 +08:00
|
|
|
},
|
|
|
|
components: {
|
2024-04-08 18:54:46 +08:00
|
|
|
StringWithEllipsis,
|
2024-04-11 15:22:50 +08:00
|
|
|
SortFlyout,
|
|
|
|
Loader,
|
2024-04-16 19:33:36 +08:00
|
|
|
NoSearchResult,
|
|
|
|
ListEnd,
|
|
|
|
CellTemplate,
|
2024-05-13 19:46:56 +08:00
|
|
|
LinkTemplate,
|
|
|
|
TableHeader
|
2024-03-22 23:37:59 +08:00
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
2024-04-08 18:54:46 +08:00
|
|
|
sort: 'created_desc',
|
2024-03-22 23:37:59 +08:00
|
|
|
results: [],
|
|
|
|
total: 0,
|
|
|
|
loading: false,
|
|
|
|
page: 1,
|
2024-03-27 19:41:30 +08:00
|
|
|
disabled: false,
|
2024-04-08 02:36:02 +08:00
|
|
|
fullDataLoaded: false,
|
2024-03-22 23:37:59 +08:00
|
|
|
};
|
|
|
|
},
|
|
|
|
watch: {
|
2024-04-29 21:26:08 +08:00
|
|
|
filters() {
|
|
|
|
this.reloadData();
|
|
|
|
},
|
2024-03-22 23:37:59 +08:00
|
|
|
selected() {
|
2024-05-07 16:18:56 +08:00
|
|
|
if (this.selected && !this.fullDataLoaded) {
|
|
|
|
this.reloadData();
|
2024-03-22 23:37:59 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
query() {
|
2024-04-29 21:26:08 +08:00
|
|
|
this.reloadData();
|
2024-03-22 23:37:59 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.loadData();
|
|
|
|
window.addEventListener('scroll', this.handleScroll);
|
|
|
|
},
|
|
|
|
unmounted() {
|
|
|
|
window.removeEventListener('scroll', this.handleScroll);
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
preparedResults() {
|
|
|
|
if (this.selected) {
|
|
|
|
return this.results;
|
|
|
|
}
|
|
|
|
return this.results.slice(0, 4);
|
2024-03-27 18:44:52 +08:00
|
|
|
},
|
|
|
|
viewAll() {
|
|
|
|
return !this.selected && this.total > GLOBAL_CONSTANTS.GLOBAL_SEARCH_PREVIEW_LIMIT;
|
2024-04-08 02:36:02 +08:00
|
|
|
},
|
|
|
|
loaderRows() {
|
2024-04-16 19:33:36 +08:00
|
|
|
return !this.selected ? 4 : 20;
|
2024-04-08 02:36:02 +08:00
|
|
|
},
|
|
|
|
reachedEnd() {
|
2024-04-16 19:33:36 +08:00
|
|
|
return !this.page && this.selected;
|
2024-04-11 15:22:50 +08:00
|
|
|
},
|
|
|
|
showNoSearchResult() {
|
|
|
|
return this.selected && !this.loading && !this.results.length;
|
2024-03-22 23:37:59 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2024-04-11 19:45:29 +08:00
|
|
|
labelName(object) {
|
2024-04-16 19:33:36 +08:00
|
|
|
if (!object) return '';
|
|
|
|
|
2024-04-11 19:45:29 +08:00
|
|
|
if (!object.archived) return object.name;
|
|
|
|
|
|
|
|
return `${I18n.t('labels.archived')} ${object.name}`;
|
|
|
|
},
|
2024-03-22 23:37:59 +08:00
|
|
|
handleScroll() {
|
2024-04-05 17:16:58 +08:00
|
|
|
if (this.loading || !this.selected) return;
|
2024-03-22 23:37:59 +08:00
|
|
|
|
2024-05-16 17:29:26 +08:00
|
|
|
if ((window.innerHeight + window.scrollY) >= document.body.offsetHeight - 50) {
|
2024-03-22 23:37:59 +08:00
|
|
|
if (this.results.length < this.total) {
|
|
|
|
this.loadData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2024-04-08 18:54:46 +08:00
|
|
|
changeSort(sort) {
|
|
|
|
this.sort = sort;
|
|
|
|
this.results = [];
|
|
|
|
this.page = 1;
|
|
|
|
this.loadData();
|
|
|
|
},
|
2024-04-29 21:26:08 +08:00
|
|
|
reloadData() {
|
|
|
|
if (this.query.length > 1) {
|
|
|
|
this.results = [];
|
|
|
|
this.page = 1;
|
|
|
|
this.total = 0;
|
|
|
|
this.fullDataLoaded = false;
|
|
|
|
this.loadData();
|
|
|
|
}
|
|
|
|
},
|
2024-03-22 23:37:59 +08:00
|
|
|
loadData() {
|
2024-04-17 23:01:58 +08:00
|
|
|
if (this.query.length < 2) return;
|
|
|
|
|
2024-05-13 23:11:49 +08:00
|
|
|
if (this.loading && this.page && !(this.selected && !this.fullDataLoaded)) return;
|
2024-03-22 23:37:59 +08:00
|
|
|
|
2024-05-13 23:11:49 +08:00
|
|
|
const fullView = this.selected;
|
|
|
|
const currentPage = this.page;
|
2024-03-22 23:37:59 +08:00
|
|
|
this.loading = true;
|
|
|
|
axios.get(this.searchUrl, {
|
|
|
|
params: {
|
|
|
|
q: this.query,
|
2024-04-08 18:54:46 +08:00
|
|
|
sort: this.sort,
|
2024-04-05 17:16:58 +08:00
|
|
|
filters: this.filters,
|
2024-03-22 23:37:59 +08:00
|
|
|
group: this.group,
|
2024-05-13 23:11:49 +08:00
|
|
|
preview: !fullView,
|
|
|
|
page: currentPage
|
2024-03-22 23:37:59 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
if (this.selected) this.fullDataLoaded = true;
|
2024-05-13 23:11:49 +08:00
|
|
|
|
|
|
|
if (this.selected === fullView && this.page === currentPage) {
|
|
|
|
this.results = this.results.concat(response.data.data);
|
|
|
|
this.total = response.data.meta.total;
|
|
|
|
this.disabled = response.data.meta.disabled;
|
|
|
|
this.loading = false;
|
|
|
|
this.page = response.data.meta.next_page;
|
|
|
|
}
|
2024-03-22 23:37:59 +08:00
|
|
|
})
|
|
|
|
.finally(() => {
|
2024-05-13 23:11:49 +08:00
|
|
|
if (this.selected === fullView) {
|
|
|
|
this.loading = false;
|
|
|
|
this.$emit('updated');
|
|
|
|
}
|
2024-03-22 23:37:59 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|