Skip to content

Commit

Permalink
#11: proof of concept for date facet based on year
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Nelson committed Apr 7, 2020
1 parent 3302e8d commit f18a501
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 18 deletions.
50 changes: 42 additions & 8 deletions assets/js/search/facet-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class FacetGroup extends Faceter(LitElement) {
document.addEventListener('searchResultsObtained', this.handleSearchResultsObtained);
document.addEventListener('facetSelected', this.handleFacetSelected);
document.addEventListener('searchStarted', this.handleSearchStarted);
// 1. gather all vivo-search-facets (or @implements vivo-search-facets)
}

disconnectedCallback() {
Expand All @@ -60,32 +61,65 @@ class FacetGroup extends Faceter(LitElement) {
this.waiting = true;
}

handleSearchResultsObtained(e) {
this.waiting = false;
const data = e.detail;
if (!data || !data[this.key]) {
return;
}
this.data = data;
// main override: let values = content.map(v => v.value);
//
removeNotApplicable() {
let grouped = _.groupBy(this.data[this.key].facets, "field");

// little wasteful
let allFacets = document.querySelectorAll(`vivo-search-facets, [implements="vivo-search-facets"]`);
let groupedFacets = _.groupBy(allFacets, "key");
let groupedFacets2 = _.groupBy(groupedFacets[this.key], "field");
//let searches = document.querySelectorAll(`[implements="vivo-search"]`);

// 2. find each vivo-facet (or implements vivo-facet) by this field
// 3. run it's "removeFiltersNoLongerApplicable .... method"
// 4. which would default to just same as below

// NOTE: this removes filters from constructed
// search if they are no longer in search results
//
// This would happen if another facet has been applied and
// narrowed the overall results
this.filters.map(filter => {
let facet = groupedFacets2[filter.field][0];
// first check if we even have any matches (avoid error)
if (grouped[filter.field]) {
let entries = grouped[filter.field][0].entries;
let content = entries.content;
let values = content.map(v => v.value);
;
let values = facet.getValuesFromContent(content);
//let values = this.getValuesFromContent(content);
// remove cause no longer in search results
if (!_.includes(values, filter.value)) {
// FIXME: this assumes data from facet query is same
// if facet.type = 'date-compare' ->
// else
//
/*
Array(7) [
"2017-01-01T00:00:00Z", "2019-01-01T00:00:00Z", "2018-01-01T00:00:00Z",
"2016-01-01T00:00:00Z", "2015-01-01T00:00:00Z", "2014-01-01T00:00:00Z",
"2005-01-01T00:00:00Z" ]
// value [2016 TO 2021] not in array
removing {"field":"publicationDate","value":"[2016 TO 2021]",
"opKey":"BETWEEN","tag":"publicationDate"}
*/
this.removeFilter(filter);
}
}
});

}

handleSearchResultsObtained(e) {
this.waiting = false;
const data = e.detail;
if (!data || !data[this.key]) {
return;
}
this.data = data;
this.removeNotApplicable();
}

handleFacetSelected(e) {
Expand Down
8 changes: 8 additions & 0 deletions assets/js/search/facet.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ class SearchFacet extends LitElement {
this.handleFacetSelected = this.handleFacetSelected.bind(this);
}

// see facet-group, removing filters if not in results
// need a way to map selected filters to search results
getValuesFromContent(content) {
let values = content.map(v => v.value);
return values;
}


handleFacetSelected(e) {
// if span clicked - need parent (note had to add 'value' to multiple 'parents')
let parent = e.target.parentNode;
Expand Down
3 changes: 1 addition & 2 deletions assets/js/search/faceter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ let Faceter = (superclass) => class extends superclass {
this.filters = filters;
}

// TODO: when adding filter - if already one of that field
// turn it into a list (then join(' OR '))?

addFilter(filter) {
// TODO: maybe not send tag if blank (and opKey too)
this.filters.push(
Expand Down
5 changes: 3 additions & 2 deletions assets/js/search/publications/publication-date-facet.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ class PublicationDateFacet extends LitElement {
}

render() {
let theDate = new Date(this.value);
//let theDate = new Date(this.value);
// FIXME: need to be able to send in localization
let display = theDate.toLocaleDateString("en-US");
//let display = theDate.toLocaleDateString("en-US");
let display = this.label;
return html`
<div value=${this.value}
selected="${this.selected}"
Expand Down
29 changes: 24 additions & 5 deletions assets/js/search/publications/publication-date-facets.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,23 @@ class PublicationDateFacets extends Faceter(LitElement) {
constructor() {
super();
this.implements = "vivo-search-facets";
this.opKey = "EQUALS";
//this.opKey = "EQUALS";
this.opKey = "BETWEEN";
}


// NOTE: this has to be an over-ride because trying to match
// incoming search results to applied filters
getValuesFromContent(content) {
console.log("trying to do something different here");
let values = content.map(v => {
let dateValue = new Date(v.value);
let today = new Date();
let range = `[${dateValue.getFullYear()} TO ${today.getFullYear()+1}]`;
return range;
});
return values;
}

render() {
if (!this.data) {
return html``
Expand All @@ -43,15 +57,20 @@ class PublicationDateFacets extends Faceter(LitElement) {
let content = this.data[0].entries.content;

let facetList = content.map(facet => {
let selected = this.inFilters(this.field, facet);
let dateValue = new Date(facet.value);
let today = new Date();
let range = `[${dateValue.getFullYear()} TO ${today.getFullYear()+1}]`;
let fakeFacet = {"value": ""+range+"", "count": facet.count, "__typename": "FacetEntry"};

let selected = this.inFilters(this.field, fakeFacet);
return html`<vivo-publication-date-facet
category="${this.key}"
tag="${this.tag}"
opKey="${this.opKey}"
field="${this.field}"
?selected=${selected}
value="${facet.value}"
label="${facet.value}"
value="${range}"
label="${dateValue.getFullYear()}"
count="${facet.count}">
</vivo-publication-date-facet>`
});
Expand Down
2 changes: 1 addition & 1 deletion assets/js/search/publications/publication-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const publicationQuery = gql`
{ field: "type", exclusionTag: "type" },
{ field: "authors", exclusionTag: "authors" },
{ field: "publisher", exclusionTag: "publisher" },
{ field: "publicationDate", exclusionTag: "publicationDate" },
{ field: "publicationDate", exclusionTag: "publicationDate", type: DATE_YEAR },
]
filters: $filters
paging: {
Expand Down

0 comments on commit f18a501

Please sign in to comment.