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

182 lines
5.7 KiB
Vue
Raw Permalink 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"
ref="results"
:result="result"
:resultToReload="resultToReload"
2023-10-19 21:51:18 +08:00
:activeDragResult="activeDragResult"
:userSettingsUrl="userSettingsUrl"
@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"
2023-10-19 21:51:18 +08:00
@result:drag_enter="dragEnter"
/>
</div>
2023-10-26 18:01:48 +08:00
<clipboardPasteModal v-if="showClipboardPasteModal"
:image="pasteImages"
:objects="results"
:objectType="'result'"
:selectedObjectId="firstObjectInViewport()"
@files="uploadFilesToResult"
@cancel="showClipboardPasteModal = false"
/>
</div>
</template>
<script>
import axios from '../../packs/custom_axios.js';
import ResultsToolbar from './results_toolbar.vue';
import Result from './result.vue';
import stackableHeadersMixin from '../mixins/stackableHeadersMixin';
import moduleNameObserver from '../mixins/moduleNameObserver';
2023-09-25 19:58:13 +08:00
import clipboardPasteModal from '../shared/content/attachments/clipboard_paste_modal.vue';
import AssetPasteMixin from '../shared/content/attachments/mixins/paste.js';
2023-10-26 18:01:48 +08:00
export default {
name: 'Results',
components: { ResultsToolbar, Result, clipboardPasteModal },
mixins: [stackableHeadersMixin, moduleNameObserver, AssetPasteMixin],
props: {
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 },
userSettingsUrl: { type: String, required: false }
},
data() {
return {
results: [],
sort: null,
filters: {},
resultToReload: null,
nextPageUrl: null,
loadingPage: false,
activeDragResult: null,
userSettingsUrl: null
};
},
mounted() {
this.userSettingsUrl = document.querySelector('meta[name="user-settings-url"]').getAttribute('content');
window.addEventListener('scroll', this.loadResults, false);
window.addEventListener('scroll', this.initStackableHeaders, false);
this.nextPageUrl = this.url;
this.loadResults();
this.initStackableHeaders();
},
beforeUnmount() {
window.removeEventListener('scroll', this.loadResults, false);
window.removeEventListener('scroll', this.initStackableHeaders, false);
},
methods: {
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;
const params = this.sort ? { ...this.filters, sort: this.sort } : { ...this.filters };
axios.get(this.nextPageUrl, { params }).then((response) => {
this.results = this.results.concat(response.data.data);
this.sort = response.data.meta.sort;
this.nextPageUrl = response.data.links.next;
this.loadingPage = false;
});
}
},
setSort(sort) {
this.sort = sort;
this.resetPageAndReload();
},
setFilters(filters) {
this.filters = filters;
this.resetPageAndReload();
},
createResult() {
axios.post(
`${this.url}`,
{
headers: {
Accept: 'application/json'
}
}
).then(
(response) => {
this.results = [{ newResult: true, ...response.data.data }, ...this.results];
window.scrollTo(0, 0);
}
);
},
expandAll() {
$('.result-wrapper .collapse').collapse('show');
this.toggleCollapsed(false);
},
collapseAll() {
$('.result-wrapper .collapse').collapse('hide');
this.toggleCollapsed(true);
},
toggleCollapsed(newState) {
this.results = this.results.map((result) => ({
...result,
attributes: {
...result.attributes,
collapsed: newState
}
}));
},
removeResult(result_id) {
this.results = this.results.filter((r) => r.id != result_id);
},
dragEnter(id) {
this.activeDragResult = id;
},
uploadFilesToResult(file, resultId) {
this.$refs.results.find((child) => child.result?.id == resultId).uploadFiles(file);
},
firstObjectInViewport() {
const result = $('.result-wrapper:not(.locked)').toArray().find((element) => {
const { top, bottom } = element.getBoundingClientRect();
return bottom > 0 && top < window.innerHeight;
});
return result ? result.dataset.id : null;
}
}
};
</script>