Updates pagination logic for checkbox modal using elastic search on text and relationship filters.

This commit is contained in:
Mateus Machado Luna 2019-04-04 12:40:35 -03:00
parent 0d3e571476
commit 930968e209
2 changed files with 86 additions and 49 deletions

View File

@ -26,15 +26,14 @@
@input="fetchSelectedLabels()"
v-model="activeTab">
<b-tab-item :label="$i18n.get('label_all_terms')">
<div
v-if="!isSearching && !isTaxonomy"
class="modal-card-body tainacan-checkbox-list-container">
<a
v-if="checkboxListOffset"
v-if="isUsingElasticSearch ? lastTermOnFisrtPage != checkboxListOffset : checkboxListOffset"
role="button"
class="tainacan-checkbox-list-page-changer"
@click="beforePage">
@click="previousPage">
<span class="icon">
<i class="tainacan-icon tainacan-icon-previous"/>
</span>
@ -321,7 +320,9 @@
activeTab: 0,
selectedTagsName: {},
isSelectedTermsLoading: false,
isUsingElasticSearch: tainacan_plugin.wp_elasticpress == "1" ? true : false
isUsingElasticSearch: tainacan_plugin.wp_elasticpress == "1" ? true : false,
previousLastTerms: [],
lastTermOnFisrtPage: null
}
},
updated(){
@ -376,23 +377,37 @@
}
return label;
},
beforePage(){
this.checkboxListOffset -= this.maxNumOptionsCheckboxList;
previousPage() {
this.noMorePage = 0;
if(this.checkboxListOffset < 0){
this.checkboxListOffset = 0;
}
this.isCheckboxListLoading = true;
this.getOptions(this.checkboxListOffset);
if (this.isUsingElasticSearch) {
this.previousLastTerms.pop();
if (this.previousLastTerms.length > 0) {
this.getOptions(this.previousLastTerms.pop());
this.previousLastTerms.push(this.checkboxListOffset);
} else {
this.getOptions(0);
}
} else {
this.checkboxListOffset -= this.maxNumOptionsCheckboxList;
if (this.checkboxListOffset < 0)
this.checkboxListOffset = 0;
this.getOptions(this.checkboxListOffset);
}
},
nextPage(){
if(!this.noMorePage) {
nextPage() {
if (this.isUsingElasticSearch)
this.previousLastTerms.push(this.checkboxListOffset);
if (!this.noMorePage && !this.isUsingElasticSearch) {
// LIMIT 0, 20 / LIMIT 19, 20 / LIMIT 39, 20 / LIMIT 59, 20
if(this.checkboxListOffset === this.maxNumOptionsCheckboxList){
this.checkboxListOffset += this.maxNumOptionsCheckboxList-1;
if (this.checkboxListOffset === this.maxNumOptionsCheckboxList){
this.checkboxListOffset += this.maxNumOptionsCheckboxList - 1;
} else {
this.checkboxListOffset += this.maxNumOptionsCheckboxList;
}
@ -402,9 +417,9 @@
this.getOptions(this.checkboxListOffset);
},
getOptions(offset){
getOptions(offset) {
let promise = '';
// Cancels previous Request
if (this.getOptionsValuesCancel != undefined)
this.getOptionsValuesCancel.cancel('Facet search Canceled.');
@ -415,9 +430,19 @@
promise = this.getValuesPlainText( this.metadatum_id, this.optionName, this.isRepositoryLevel, [], offset, this.maxNumOptionsCheckboxList, true);
promise.request
.then(() => {
.then((data) => {
this.isCheckboxListLoading = false;
this.isSearchingLoading = false;
if (this.isUsingElasticSearch) {
this.checkboxListOffset = data.last_term;
if (!this.lastTermOnFisrtPage || this.lastTermOnFisrtPage == this.checkboxListOffset) {
this.lastTermOnFisrtPage = this.checkboxListOffset;
this.previousLastTerms.push(0);
}
}
})
.catch(error => {
this.$console.log(error);

View File

@ -60,10 +60,13 @@ export const filter_type_mixin = {
url = `/facets/${metadatumId}?getSelected=${getSelected}&`;
else
url = `/collection/${this.collection}/facets/${metadatumId}?getSelected=${getSelected}&`;
if (offset != undefined && number != undefined) {
url += `offset=${offset}&number=${number}&`;
}
if (!this.isUsingElasticSearch)
url += `offset=${offset}&number=${number}&`;
else
url += `last_term=${offset}&number=${number}&`;
}
if(search && offset != undefined && number != undefined){
url += `search=${search}&` + qs.stringify(query_items);
@ -77,20 +80,25 @@ export const filter_type_mixin = {
return new Object ({
request:
axios.tainacan.get(url, { cancelToken: source.token })
.then(res => {
this.isLoadingOptions = false;
if (res.data.values)
this.prepareOptionsForPlainText(res.data.values, search, valuesToIgnore, isInCheckboxModal);
else
this.prepareOptionsForPlainText(res.data, search, valuesToIgnore, isInCheckboxModal);
})
.catch((thrown) => {
if (axios.isCancel(thrown)) {
console.log('Request canceled: ', thrown.message);
} else {
new Promise((resolve, reject) => {
axios.tainacan.get(url, { cancelToken: source.token })
.then(res => {
this.isLoadingOptions = false;
}
if (res.data.values)
this.prepareOptionsForPlainText(res.data.values, search, valuesToIgnore, isInCheckboxModal);
else
this.prepareOptionsForPlainText(res.data, search, valuesToIgnore, isInCheckboxModal);
resolve(res.data);
})
.catch((thrown) => {
if (axios.isCancel(thrown)) {
console.log('Request canceled: ', thrown.message);
} else {
this.isLoadingOptions = false;
}
reject(thrown);
})
}),
source: source
});
@ -145,22 +153,26 @@ export const filter_type_mixin = {
return new Object ({
request:
axios.tainacan.get(url + '&fetch_only=thumbnail,title,id&' + qs.stringify(query_items))
.then(res => {
this.isLoadingOptions = false;
if (res.data.values)
this.prepareOptionsForRelationship(res.data.values, search, valuesToIgnore, isInCheckboxModal);
else
this.prepareOptionsForRelationship(res.data, search, valuesToIgnore, isInCheckboxModal);
})
.catch((thrown) => {
if (axios.isCancel(thrown)) {
console.log('Request canceled: ', thrown.message);
} else {
new Promise((resolve, reject) => {
axios.tainacan.get(url + '&fetch_only=thumbnail,title,id&' + qs.stringify(query_items))
.then(res => {
this.isLoadingOptions = false;
if (res.data.values)
this.prepareOptionsForRelationship(res.data.values, search, valuesToIgnore, isInCheckboxModal);
else
this.prepareOptionsForRelationship(res.data, search, valuesToIgnore, isInCheckboxModal);
resolve(res.data);
})
.catch((thrown) => {
if (axios.isCancel(thrown)) {
console.log('Request canceled: ', thrown.message);
} else {
this.isLoadingOptions = false;
}
reject(thrown);
}
})
}),
source: source
});