scinote-web/app/javascript/vue/results/results.vue

137 lines
4 KiB
Vue
Raw Normal View History

<template>
<div class="results-wrapper">
2023-09-25 19:58:13 +08:00
<ResultsToolbar
ref="resultsToolbar"
:sort="sort"
2023-09-06 20:58:59 +08:00
:canCreate="canCreate == 'true'"
:archived="archived == 'true'"
:active_url="active_url"
:archived_url="archived_url"
2023-09-25 19:58:13 +08:00
:headerSticked="headerSticked"
:moduleName="moduleName"
2023-08-23 17:59:21 +08:00
@setSort="setSort"
@setFilters="setFilters"
@newResult="createResult"
@expandAll="expandAll"
@collapseAll="collapseAll"
2023-08-31 21:50:35 +08:00
class="my-4"
2023-08-23 17:59:21 +08:00
/>
<div class="results-list">
<Result v-for="result in results" :key="result.id"
:result="result"
:resultToReload="resultToReload"
@result:elements:loaded="resultToReload = null"
@result:move_element="reloadResult"
@result:attachments:loaded="resultToReload = null"
@result:move_attachment="reloadResult"
2023-09-06 20:58:59 +08:00
@result:duplicated="resetPageAndReload"
@result:archived="removeResult"
@result:deleted="removeResult"
@result:restored="removeResult"
/>
</div>
</div>
</template>
<script>
import axios from '../../packs/custom_axios.js';
import ResultsToolbar from './results_toolbar.vue';
import Result from './result.vue';
2023-09-25 19:58:13 +08:00
import stackableHeadersMixin from '../mixins/stackableHeadersMixin';
import moduleNameObserver from '../mixins/moduleNameObserver';
export default {
name: 'Results',
components: { ResultsToolbar, Result },
2023-09-25 19:58:13 +08:00
mixins: [stackableHeadersMixin, moduleNameObserver],
props: {
2023-09-06 20:58:59 +08:00
url: { type: String, required: true },
canCreate: { type: String, required: true },
archived: { type: String, required: true },
active_url: { type: String, required: true },
archived_url: { type: String, required: true }
},
data() {
return {
results: [],
2023-08-23 17:59:21 +08:00
sort: 'created_at_desc',
filters: {},
resultToReload: null,
nextPageUrl: null,
2023-09-25 19:58:13 +08:00
loadingPage: false,
}
},
mounted() {
window.addEventListener('scroll', this.loadResults, false);
2023-09-25 19:58:13 +08:00
window.addEventListener('scroll', this.initStackableHeaders, false);
this.nextPageUrl = this.url;
this.loadResults();
2023-09-25 19:58:13 +08:00
this.initStackableHeaders();
},
beforeDestroy() {
window.removeEventListener('scroll', this.loadResults, false);
2023-09-25 19:58:13 +08:00
window.removeEventListener('scroll', this.initStackableHeaders, false);
},
methods: {
2023-09-25 19:58:13 +08:00
getHeader() {
return this.$refs.resultsToolbar.$refs.resultsHeaderToolbar;
},
reloadResult(result) {
this.resultToReload = result;
},
resetPageAndReload(){
this.nextPageUrl = this.url;
this.results = [];
this.$nextTick(() => {
this.loadResults();
});
},
loadResults() {
if (this.nextPageUrl === null || this.loadingPage) return;
if (window.scrollY + window.innerHeight >= document.body.scrollHeight - 20) {
this.loadingPage = true;
axios.get(this.nextPageUrl, { params: { sort: this.sort, ...this.filters } }).then((response) => {
this.results = this.results.concat(response.data.data);
this.nextPageUrl = response.data.links.next;
this.loadingPage = false;
});
}
},
setSort(sort) {
this.sort = sort;
this.resetPageAndReload();
},
2023-08-23 17:59:21 +08:00
setFilters(filters) {
this.filters = filters;
this.resetPageAndReload();
2023-08-23 17:59:21 +08:00
},
createResult() {
axios.post(
`${this.url}`,
{
headers: {
'Accept': 'application/json'
}
}
).then(
(response) => {
this.results = [response.data.data, ...this.results];
window.scrollTo(0, 0);
}
);
},
expandAll() {
$('.result-wrapper .collapse').collapse('show')
},
collapseAll() {
$('.result-wrapper .collapse').collapse('hide')
2023-09-06 20:58:59 +08:00
},
removeResult(result_id) {
2023-09-13 20:34:49 +08:00
this.results = this.results.filter((r) => r.id != result_id);
}
}
}
</script>