2019-09-27 16:02:48 +00:00
|
|
|
import { Component, Vue, Prop, Provide } from 'vue-property-decorator';
|
2019-10-03 16:54:05 +00:00
|
|
|
import VsInput from './text-search/vs-input.vue';
|
|
|
|
import VsResults from './search-results/vs-results.vue';
|
|
|
|
import FacetList from './search-results/facet-list.vue'
|
|
|
|
import rdrApi from './search-results/dataservice';
|
2019-10-10 10:58:13 +00:00
|
|
|
import FilterItem from './models/filter-item';
|
2019-09-27 16:02:48 +00:00
|
|
|
|
|
|
|
@Component({
|
2019-10-10 10:58:13 +00:00
|
|
|
components: {
|
2019-10-03 16:54:05 +00:00
|
|
|
VsInput,
|
|
|
|
VsResults,
|
|
|
|
FacetList
|
2019-09-27 16:02:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
export default class App extends Vue {
|
|
|
|
|
|
|
|
results = [];
|
2019-10-15 11:52:52 +00:00
|
|
|
facets = {};
|
2019-10-10 10:58:13 +00:00
|
|
|
searchTerm = '';
|
|
|
|
activeFilterItems = {};
|
|
|
|
|
|
|
|
async onFilter(filter) {
|
|
|
|
// console.log(filter.value);
|
|
|
|
// if (!this.activeFilterItems.some(e => e.value === filter.value)) {
|
|
|
|
// this.activeFilterItems.push(filter);
|
|
|
|
if (!this.activeFilterItems.hasOwnProperty(filter.Category)) {
|
|
|
|
this.activeFilterItems[filter.Category] = [];
|
|
|
|
}
|
|
|
|
if (!this.activeFilterItems[filter.Category].some(e => e === filter.value)) {
|
|
|
|
this.activeFilterItems[filter.Category].push(filter.value);
|
|
|
|
|
|
|
|
var res = await rdrApi.search(this.searchTerm, this.activeFilterItems);
|
|
|
|
this.results = res.response.docs;
|
|
|
|
// this.facets = res.facet_counts.facet_fields;
|
2019-10-15 11:52:52 +00:00
|
|
|
// this.facets = [];
|
2019-10-10 10:58:13 +00:00
|
|
|
var facet_fields = res.facet_counts.facet_fields;
|
|
|
|
for (var prop in facet_fields) {
|
2019-10-15 11:52:52 +00:00
|
|
|
var facetValues = facet_fields[prop].map((facetValue, i) => {
|
2019-10-10 10:58:13 +00:00
|
|
|
if (i % 2 === 0) {
|
2019-10-15 11:52:52 +00:00
|
|
|
// var rObj = { value: facetValue, count: facet_fields[prop][i + 1] };
|
|
|
|
var rObj;
|
|
|
|
if (filter.value == facetValue) {
|
|
|
|
rObj = filter;
|
|
|
|
} else if( this.facets[prop].some(e => e.value === facetValue)) {
|
|
|
|
console.log(facetValue + " is included")
|
|
|
|
var indexOfFacetValue = this.facets[prop].findIndex(i => i.value === facetValue);
|
|
|
|
console.log(indexOfFacetValue);
|
|
|
|
rObj = this.facets[prop][indexOfFacetValue];
|
|
|
|
rObj.count = facet_fields[prop][i + 1];
|
|
|
|
} else {
|
|
|
|
rObj = new FilterItem(facetValue, facet_fields[prop][i + 1]);
|
|
|
|
}
|
2019-10-10 10:58:13 +00:00
|
|
|
return rObj;
|
|
|
|
}
|
|
|
|
}).filter(function (el) {
|
|
|
|
return el != null && el.count > 0;
|
|
|
|
});
|
2019-10-15 11:52:52 +00:00
|
|
|
// this.facets.push({ filterName: prop, values: facetValues });
|
|
|
|
this.facets[prop] = facetValues;
|
2019-10-10 10:58:13 +00:00
|
|
|
}
|
2019-10-15 11:52:52 +00:00
|
|
|
|
2019-10-10 10:58:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 16:02:48 +00:00
|
|
|
|
|
|
|
async onSearch(term) {
|
2019-10-10 10:58:13 +00:00
|
|
|
// console.log(term);
|
|
|
|
// while (this.activeFilterItems.length > 0) {
|
|
|
|
// this.activeFilterItems.pop();
|
|
|
|
// }
|
|
|
|
this.activeFilterItems = {};
|
2019-10-15 11:52:52 +00:00
|
|
|
// while (this.facets.length > 0) {
|
|
|
|
// this.facets.pop();
|
|
|
|
// }
|
|
|
|
this.facets = {};
|
2019-10-10 10:58:13 +00:00
|
|
|
this.searchTerm = term;
|
|
|
|
var res = await rdrApi.search(this.searchTerm, this.activeFilterItems);
|
2019-10-03 16:54:05 +00:00
|
|
|
this.results = res.response.docs;
|
2019-10-10 10:58:13 +00:00
|
|
|
var facet_fields = res.facet_counts.facet_fields;
|
|
|
|
for (var prop in facet_fields) {
|
|
|
|
var facetValues = facet_fields[prop].map((facet, i) => {
|
|
|
|
if (i % 2 === 0) {
|
|
|
|
//var rObj = { value: facet, count: facet_fields[prop][i + 1] };
|
|
|
|
var rObj = new FilterItem(facet, facet_fields[prop][i + 1])
|
|
|
|
return rObj;
|
|
|
|
}
|
|
|
|
}).filter(function (el) {
|
|
|
|
return el != null && el.count > 0;
|
|
|
|
});
|
2019-10-15 11:52:52 +00:00
|
|
|
//this.facets.push({ filterName: prop, values: facetValues });
|
|
|
|
this.facets[prop] = facetValues;
|
2019-10-10 10:58:13 +00:00
|
|
|
}
|
|
|
|
// console.log(this.facets.toString());
|
2019-09-27 16:02:48 +00:00
|
|
|
}
|
2019-10-10 10:58:13 +00:00
|
|
|
|
2019-09-27 16:02:48 +00:00
|
|
|
|
|
|
|
mounted() {
|
2019-10-04 17:00:31 +00:00
|
|
|
// console.log('Component mounted.')
|
2019-09-27 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|