Skip to content

Commit cfb8ffc

Browse files
authored
Merge pull request #1196 from ElixirTeSS/per-page
Dropdown to adjust page size. Fixes #1195
2 parents e1f4443 + f811c64 commit cfb8ffc

File tree

8 files changed

+69
-38
lines changed

8 files changed

+69
-38
lines changed

app/assets/javascripts/application.js

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,41 @@
4141
//= require ol
4242
//= require ol-geocoder
4343

44-
function redirect_to_sort_url(){
45-
url = new URL(window.location.href);
46-
url.searchParams.set(
47-
"sort",
48-
$("#sort").find(":selected").val()
49-
);
50-
window.location.replace(url.toString());
51-
}
52-
53-
function redirect_with_updated_search(param, paramVal) {
54-
url = new URL(window.location.href);
55-
// special case for empty range types
56-
if (param == 'start' && paramVal == '/') {
57-
url.searchParams.delete(param);
58-
} else {
59-
url.searchParams.set(param, paramVal);
60-
}
61-
window.location.replace(url.toString());
62-
}
63-
64-
function reposition_tiles(container, tileClass){
65-
var $container = $("." + container);
66-
67-
$container.imagesLoaded(function () {
68-
$container.masonry({
69-
// options...
70-
itemSelector: "." + tileClass,
71-
columnWidth: 20
44+
const Index = {
45+
applySorting: function () {
46+
Index.setParam("sort", $("#sort").val());
47+
},
48+
49+
applyPerPage: function () {
50+
Index.setParam("per_page", $("#per_page").val());
51+
},
52+
53+
applyDateParam: function (param, value) {
54+
const url = new URL(window.location.href);
55+
if (param === 'start' && value === '/') {
56+
url.searchParams.delete(param);
57+
} else {
58+
url.searchParams.set(param, value);
59+
}
60+
window.location.replace(url.toString());
61+
},
62+
63+
setParam: function (param, value) {
64+
const url = new URL(window.location.href);
65+
url.searchParams.set(param, value);
66+
window.location.replace(url.toString());
67+
},
68+
69+
repositionTiles(containerSelector, itemSelector) {
70+
const container = $(containerSelector);
71+
container.imagesLoaded(function () {
72+
container.masonry({
73+
// options...
74+
itemSelector: itemSelector,
75+
columnWidth: 20
76+
});
7277
});
73-
});
78+
}
7479
}
7580

7681
// Perform an ajax request to load the calendar and replace the contents
@@ -152,7 +157,7 @@ document.addEventListener("turbolinks:load", function(e) {
152157
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
153158
addTabToFilters(e.target.href.split('#').pop());
154159
// and reposition masonry tiles
155-
reposition_tiles('masonry', 'masonry-brick');
160+
Index.repositionTiles('.masonry', '.masonry-brick');
156161
});
157162

158163
// Manually trigger bootstrap tab history (we should probably remove the dependency and reimplement in a turbolink-compatible way)
@@ -168,12 +173,14 @@ document.addEventListener("turbolinks:load", function(e) {
168173

169174
// Masonry
170175
$(".nav-tabs a").on("shown.bs.tab", function(e) {
171-
reposition_tiles('masonry', 'masonry-brick');
176+
Index.repositionTiles('.masonry', '.masonry-brick');
172177
});
178+
173179
$(window).on("orientationchange", function() {
174-
reposition_tiles("masonry", "masonry-brick");
180+
Index.repositionTiles('.masonry', '.masonry-brick');
175181
});
176-
reposition_tiles("masonry", "masonry-brick");
182+
183+
Index.repositionTiles('.masonry', '.masonry-brick');
177184

178185
new Clipboard(".clipboard-btn");
179186

app/assets/stylesheets/application.scss

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,21 @@ ul.unstyled {
418418
}
419419

420420
.search-results-count {
421-
margin-right: 5px;
421+
margin-right: 1em;
422422
display: inline-block
423423
}
424424

425+
.search-results-per-page {
426+
display: inline-block;
427+
428+
select {
429+
border: none;
430+
padding: 0;
431+
background: transparent;
432+
border-bottom: 1px dashed $text-color;
433+
}
434+
}
435+
425436
.index-display-options {
426437
margin: 5px 0;
427438
}

app/controllers/concerns/searchable_index.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# The concern for searchable index
22
module SearchableIndex
3+
DEFAULT_PAGE_SIZE = 10
4+
PER_PAGE_OPTIONS = [10, 20, 50, 100]
5+
36
extend ActiveSupport::Concern
47

58
included do
@@ -19,7 +22,7 @@ def count
1922
def fetch_resources
2023
if TeSS::Config.solr_enabled
2124
page = page_param.blank? ? 1 : page_param.to_i
22-
per_page = per_page_param.blank? ? 10 : per_page_param.to_i
25+
per_page = per_page_param.blank? ? DEFAULT_PAGE_SIZE : per_page_param.to_i
2326

2427
@search_results = @model.search_and_filter(current_user, @search_params, @facet_params,
2528
page: page, per_page: per_page, sort_by: @sort_by)

app/helpers/application_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,4 +708,9 @@ def omniauth_login_link(provider, config)
708708
method: :post
709709
)
710710
end
711+
712+
def per_page_options_for_select
713+
options_for_select(SearchableIndex::PER_PAGE_OPTIONS.map { |k| [k, k] },
714+
params[:per_page].presence || SearchableIndex::DEFAULT_PAGE_SIZE)
715+
end
711716
end

app/views/search/common/_facet_sidebar_date_filter.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
</li>
1111

1212
<li class="nav-item active" style="display: none; font-size: 15px; text-align: center;">
13-
<input type="date" name="<%= facet_field %>_lb" value="<%= lb %>" style="width: 47%;" onchange="redirect_with_updated_search('<%= facet_field %>', this.value + '/' + '<%= ub %>');">
13+
<input type="date" name="<%= facet_field %>_lb" value="<%= lb %>" style="width: 47%;" onchange="Index.applyDate('<%= facet_field %>', this.value + '/' + '<%= ub %>');">
1414
-
15-
<input type="date" name="<%= facet_field %>_ub" value="<%= ub %>" style="width: 47%;" onchange="redirect_with_updated_search('<%= facet_field %>', '<%= lb %>' + '/' + this.value);">
15+
<input type="date" name="<%= facet_field %>_ub" value="<%= ub %>" style="width: 47%;" onchange="Index.applyDate('<%= facet_field %>', '<%= lb %>' + '/' + this.value);">
1616
</li>
1717
</ul>
1818
</li>

app/views/search/common/_search_panel.html.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
<div class="search-results-count">
2828
<%= pluralize(resources.total, resource_type.model_name.human.downcase ) %> found
2929
</div>
30+
<div class="search-results-per-page form-inline pull-right">
31+
<label for="per_page"><%= t('sidebar.per_page') %></label>
32+
<%= select_tag('per_page', per_page_options_for_select, onchange: 'Index.applyPerPage()', class: 'font-size-sm') %>
33+
</div>
3034
<% end %>
3135
</div>
3236
</div>

app/views/search/common/_sort_by.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ e.g.
2929
<li class="facet-sort-group">
3030
<span class="icon icon-lg sort-icon facet-sort-icon"></span>
3131
<%= select_tag(:sort, options_for_select(options, @sort_by),
32-
onchange: 'redirect_to_sort_url()', class: 'form-control facet-select font-size-sm facet-sort')
32+
onchange: 'Index.applySorting()', class: 'form-control facet-select font-size-sm facet-sort')
3333
%>
3434
</li>
3535
</ul>

config/locales/en.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ en:
910910
values:
911911
show_hidden: 'Show hidden items'
912912
hide_hidden: 'Hide hidden items'
913+
per_page: 'Results per page: '
913914
collections:
914915
show:
915916
curate_materials: "Curate materials"

0 commit comments

Comments
 (0)