2019-11-21 17:43:56 +00:00
|
|
|
import { Component, Vue } 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';
|
2020-03-04 18:00:03 +00:00
|
|
|
import FacetCategory from './search-results/facet-category.vue';
|
2020-02-27 16:25:03 +00:00
|
|
|
import ActiveFacetCategory from './search-results/active-facet-category.vue';
|
2019-10-18 15:05:56 +00:00
|
|
|
import VsPagination from './search-results/vs-pagination.vue';
|
2019-10-03 16:54:05 +00:00
|
|
|
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,
|
2020-03-04 18:00:03 +00:00
|
|
|
FacetCategory,
|
2020-02-27 16:25:03 +00:00
|
|
|
ActiveFacetCategory,
|
2019-10-18 15:05:56 +00:00
|
|
|
VsPagination
|
2019-09-27 16:02:48 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
export default class App extends Vue {
|
|
|
|
|
2019-10-18 15:05:56 +00:00
|
|
|
results: Array<any> = [];
|
|
|
|
facets: Object = {};
|
|
|
|
searchTerm: string = '';
|
2020-02-27 16:25:03 +00:00
|
|
|
activeFilterCategories: Object = {};
|
2019-10-18 15:05:56 +00:00
|
|
|
pagination: Object = {
|
|
|
|
total: 0,
|
|
|
|
per_page: 2,
|
|
|
|
current_page: 0,
|
|
|
|
// last_page: 0,
|
|
|
|
data: []
|
|
|
|
};
|
|
|
|
loaded = false;
|
|
|
|
numFound: number;
|
2019-10-10 10:58:13 +00:00
|
|
|
|
2019-10-18 15:05:56 +00:00
|
|
|
async onPaginate(start: number): Promise<void> {
|
2019-10-30 10:25:56 +00:00
|
|
|
// console.log(start);
|
2020-02-27 16:25:03 +00:00
|
|
|
var res = await rdrApi.search(this.searchTerm, this.activeFilterCategories, start.toString());
|
2019-10-18 15:05:56 +00:00
|
|
|
this.results = res.response.docs;
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:25:03 +00:00
|
|
|
async onClearFacetCategory(categoryName, alias): Promise<void> {
|
|
|
|
// alert(categoryName);
|
|
|
|
delete this.activeFilterCategories[categoryName];
|
|
|
|
|
|
|
|
var res = await rdrApi.search(this.searchTerm, this.activeFilterCategories);
|
|
|
|
this.results = res.response.docs;
|
|
|
|
this.numFound = res.response.numFound;
|
|
|
|
|
|
|
|
// pagination
|
|
|
|
this.pagination['total'] = res.response.numFound;
|
|
|
|
this.pagination['per_page'] = res.responseHeader.params.rows;
|
|
|
|
this.pagination['current_page'] = 1;
|
|
|
|
this.pagination['data'] = res.response.docs;
|
|
|
|
|
|
|
|
var facet_fields = res.facet_counts.facet_fields;
|
|
|
|
for (var prop in facet_fields) {
|
|
|
|
var facetValues = facet_fields[prop].map((facetValue, i) => {
|
|
|
|
if (i % 2 === 0) {
|
|
|
|
// var rObj = { value: facetValue, count: facet_fields[prop][i + 1] };
|
|
|
|
var rObj:FilterItem;
|
|
|
|
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];
|
|
|
|
//if facet ccategory is reactivated category, deactivate all filter items
|
|
|
|
if (this.propName(this.facets, this.facets[prop]) == alias) {
|
|
|
|
rObj.Active = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rObj = new FilterItem(facetValue, facet_fields[prop][i + 1]);
|
|
|
|
}
|
|
|
|
return rObj;
|
|
|
|
}
|
|
|
|
}).filter(function (el) {
|
|
|
|
return el != null && el.count > 0;
|
|
|
|
});
|
|
|
|
// this.facets.push({ filterName: prop, values: facetValues });
|
|
|
|
this.facets[prop] = facetValues;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private propName (obj, type): string {
|
|
|
|
let stringPropValue = Object.keys(obj).find(key => obj[key] === type);
|
|
|
|
return stringPropValue;
|
|
|
|
}
|
|
|
|
|
2019-10-18 15:05:56 +00:00
|
|
|
async onFilter(filter): Promise<void> {
|
2019-10-10 10:58:13 +00:00
|
|
|
// console.log(filter.value);
|
|
|
|
// if (!this.activeFilterItems.some(e => e.value === filter.value)) {
|
2019-10-18 15:05:56 +00:00
|
|
|
// this.activeFilterItems.push(filter);
|
2020-02-27 16:25:03 +00:00
|
|
|
if (!this.activeFilterCategories.hasOwnProperty(filter.Category)) {
|
|
|
|
this.activeFilterCategories[filter.Category] = Vue.observable([]);
|
2019-10-10 10:58:13 +00:00
|
|
|
}
|
2020-02-27 16:25:03 +00:00
|
|
|
if (!this.activeFilterCategories[filter.Category].some(e => e === filter.value)) {
|
|
|
|
this.activeFilterCategories[filter.Category].push(filter.value);
|
|
|
|
// alert(this.activeFilterCategories[filter.Category]);
|
|
|
|
var res = await rdrApi.search(this.searchTerm, this.activeFilterCategories);
|
2019-10-10 10:58:13 +00:00
|
|
|
this.results = res.response.docs;
|
2019-10-18 15:05:56 +00:00
|
|
|
this.numFound = res.response.numFound;
|
|
|
|
|
|
|
|
// pagination
|
|
|
|
this.pagination['total'] = res.response.numFound;
|
|
|
|
this.pagination['per_page'] = res.responseHeader.params.rows;
|
|
|
|
this.pagination['current_page'] = 1;
|
|
|
|
this.pagination['data'] = 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) {
|
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;
|
2019-10-18 15:05:56 +00:00
|
|
|
} else if (this.facets[prop].some(e => e.value === facetValue)) {
|
2019-10-30 10:25:56 +00:00
|
|
|
// console.log(facetValue + " is included")
|
2019-10-18 15:05:56 +00:00
|
|
|
var indexOfFacetValue = this.facets[prop].findIndex(i => i.value === facetValue);
|
2019-10-30 10:25:56 +00:00
|
|
|
// console.log(indexOfFacetValue);
|
2019-10-15 11:52:52 +00:00
|
|
|
rObj = this.facets[prop][indexOfFacetValue];
|
2019-10-18 15:05:56 +00:00
|
|
|
rObj.count = facet_fields[prop][i + 1];
|
2019-10-15 11:52:52 +00:00
|
|
|
} 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
|
|
|
|
2019-10-18 15:05:56 +00:00
|
|
|
async onSearch(term): Promise<void> {
|
|
|
|
if (term){
|
|
|
|
term = term.trim();
|
|
|
|
} else {
|
|
|
|
term = "*%3A*";
|
|
|
|
}
|
|
|
|
|
2020-02-27 16:25:03 +00:00
|
|
|
this.activeFilterCategories = {};
|
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;
|
2020-02-27 16:25:03 +00:00
|
|
|
var res = await rdrApi.search(this.searchTerm, this.activeFilterCategories);
|
2019-10-03 16:54:05 +00:00
|
|
|
this.results = res.response.docs;
|
2019-10-18 15:05:56 +00:00
|
|
|
this.numFound = res.response.numFound;
|
|
|
|
|
|
|
|
// pagination
|
|
|
|
this.pagination['total'] = res.response.numFound;
|
|
|
|
this.pagination['per_page'] = res.responseHeader.params.rows;
|
|
|
|
this.pagination['current_page'] = 1;
|
|
|
|
this.pagination['data'] = res.response.docs;
|
|
|
|
|
|
|
|
// facets
|
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-10-18 15:05:56 +00:00
|
|
|
// When the window loads, read query parameters and perform search
|
|
|
|
async mounted() {
|
|
|
|
var query = this.getParameterByName("q");
|
|
|
|
if (query) query = query.trim();
|
|
|
|
await this.onSearch("*%3A*");
|
|
|
|
this.loaded = true;
|
|
|
|
}
|
2019-09-27 16:02:48 +00:00
|
|
|
|
2020-04-17 15:55:18 +00:00
|
|
|
private getParameterByName(name: string, url?: string) {
|
2019-10-18 15:05:56 +00:00
|
|
|
if (!url) url = window.location.href;
|
|
|
|
name = name.replace(/[\[\]]/g, "\\$&");
|
|
|
|
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
|
|
|
|
results = regex.exec(url);
|
|
|
|
if (!results) return null;
|
|
|
|
if (!results[2]) return "";
|
|
|
|
return decodeURIComponent(results[2].replace(/\+/g, " "));
|
2019-09-27 16:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|