Skip to content

Commit

Permalink
Merge pull request #877 from DaanVanVugt/feature/date-filtering
Browse files Browse the repository at this point in the history
Event start date filtering
  • Loading branch information
fbacall authored Aug 3, 2023
2 parents 3ad2cd4 + b25e712 commit aaa96ce
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 35 deletions.
42 changes: 16 additions & 26 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,24 @@
//= require_self
//= require turbolinks

function updateURLParameter(url, param, paramVal){
var newAdditionalURL = "";
var tempArray = url.split("?");
var baseURL = tempArray[0];
var additionalURL = tempArray[1];
var temp = "";

if (additionalURL) {
tempArray = additionalURL.split("&");
for (var i=0; i<tempArray.length; i++){
if(tempArray[i].split("=")[0] != param){
newAdditionalURL += temp + tempArray[i];
temp = "&";
}
}
}
var rowsTxt = temp + "" + param + "=" + paramVal;
return baseURL + "?" + newAdditionalURL + rowsTxt;
}

function redirect_to_sort_url(){
window.location.replace(
updateURLParameter(
window.location.href,
"sort",
$("#sort").find(":selected").val()
)
url = new URL(window.location.href);
url.searchParams.set(
"sort",
$("#sort").find(":selected").val()
);
window.location.replace(url.toString());
}

function redirect_with_updated_search(param, paramVal) {
url = new URL(window.location.href);
// special case for empty range types
if (param == 'start' && paramVal == '/') {
url.searchParams.delete(param);
} else {
url.searchParams.set(param, paramVal);
}
window.location.replace(url.toString());
}

function reposition_tiles(container, tileClass){
Expand Down
6 changes: 3 additions & 3 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ class Event < ApplicationRecord
end
string :keywords, multiple: true
string :fields, multiple: true
time :start
time :start, trie: true
time :end
time :created_at
time :created_at, trie: true
time :updated_at
string :content_provider do
content_provider.title unless content_provider.nil?
Expand Down Expand Up @@ -175,7 +175,7 @@ def expired?

def self.facet_fields
field_list = %w[ content_provider keywords scientific_topics operations tools fields online event_types
venue city country organizer sponsors target_audience eligibility user node collections ]
start venue city country organizer sponsors target_audience eligibility user node collections ]

field_list.delete('operations') if TeSS::Config.feature['disabled'].include? 'operations'
field_list.delete('scientific_topics') if TeSS::Config.feature['disabled'].include? 'topics'
Expand Down
17 changes: 11 additions & 6 deletions app/views/search/common/_facet_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Variable that should be available

<%
extras = if resource_type.name == 'Event'
[['Most recent', 'new'], ['Earliest', 'early'], ['Latest', 'late']]
[['Earliest', 'early'], ['Latest', 'late'], ['Most recent', 'new']]
elsif resource_type.name == 'Source'
[['Most recent', 'new'], ['Last finished', 'finished']]
else
Expand All @@ -34,13 +34,14 @@ Variable that should be available
</h4>
</li>

<% if TeSS::Config.facets_max_age_list.include?(resource_type.name) %>
<%= render partial: 'search/common/facet_sidebar_max_age' %>
<% end %>
<% boolean_facets, regular_facets = available_facets(resources).partition { |f| f.field_name == :online } %>
<% regular_facets.each do |facet| %>
<% if resource_type.name == 'Event' %>
<%= render partial: 'search/common/facet_sidebar_date_filter', locals: { facet_field: 'start' } %>
<% end %>
<%# We ignore the start facet, since this needs to be always available, not just when there are records%>
<% regular_facets.reject{ |f| f.field_name.to_s == 'start' }.each do |facet| %>
<%= render partial: 'search/common/facet_sidebar_filter', locals: { facet: facet, resources: resources } %>
<% end %>
Expand All @@ -59,6 +60,10 @@ Variable that should be available
enable_text: 'Show archived materials',
disable_text: 'Hide archived materials' } %>
<% end %>
<% if TeSS::Config.facets_max_age_list.include?(resource_type.name) %>
<%= render partial: 'search/common/facet_sidebar_max_age' %>
<% end %>
</ul>

<% if current_user && (current_user.is_curator? || current_user.is_admin?) %>
Expand Down
18 changes: 18 additions & 0 deletions app/views/search/common/_facet_sidebar_date_filter.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<% lb, ub = params[facet_field]&.split('/') %>
<li class="sidebar-group mt-4">
<ul>
<li>
<div class="nav-heading filter-heading">
<span class="icon icon-lg events-icon"></span>
<span><%= t("facets.titles.#{facet_field}", default: facet_field.humanize.singularize) %></span>
<div class="pull-right filter-expand"><i class="icon icon-md expand-icon"></i></div>
</div>
</li>

<li class="nav-item active" style="display: none; font-size: 15px; text-align: center;">
<input type="date" name="<%= facet_field %>_lb" value="<%= lb %>" style="width: 47%;" onchange="redirect_with_updated_search('<%= facet_field %>', this.value + '/' + '<%= ub %>');">
-
<input type="date" name="<%= facet_field %>_ub" value="<%= ub %>" style="width: 47%;" onchange="redirect_with_updated_search('<%= facet_field %>', '<%= lb %>' + '/' + this.value);">
</li>
</ul>
</li>
16 changes: 16 additions & 0 deletions lib/facets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Facets
days_since_scrape: -> (c) { c.method_defined?(:last_scraped) },
elixir: -> (c) { ['Event', 'Material', 'ContentProvider'].include?(c.name) },
max_age: -> (c) { ['Event', 'Material'].include?(c.name) },
start: -> (c) { c.name == 'Event' },
include_hidden: -> (c) { c.method_defined?(:user_requires_approval?) }
}.with_indifferent_access.freeze

Expand All @@ -13,6 +14,7 @@ module Facets
include_expired: -> (value) { value == 'true'},
include_archived: -> (value) { value == 'true'},
max_age: -> (value) { Subscription::FREQUENCY.detect { |f| f[:title] == value }.try(:[], :period) },
start: -> (value) { value&.split('/')&.map {|d| Date.parse(d) rescue nil } },
include_hidden: -> (value) { value == 'true'}
}

Expand Down Expand Up @@ -40,6 +42,20 @@ def max_age(scope, age, _)
end
end

def start(scope, bounds, _)
lb, ub = bounds

sunspot_scoped(scope) do
if lb && ub
with(:start).between(lb..ub)
elsif lb
with(:start).greater_than_or_equal_to(lb)
elsif ub
with(:start).less_than_or_equal_to(ub)
end
end
end

def include_expired(scope, value, _)
sunspot_scoped(scope) { with('end').greater_than(Time.zone.now) } unless value
end
Expand Down

0 comments on commit aaa96ce

Please sign in to comment.