Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Don't load all hierarchical items at once when expanding the tree #1492

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ $hierarchy-view-collapse-icon: url("data:image/svg+xml,<svg xmlns='http://www.w3
background: $mark-bg;
}

.al-hierarchy-more {
margin-bottom: 5px;
}


#collection-context {
ul {
list-style: none;

padding-left: 1.5rem;

ul {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,38 @@
<% elsif paginate? %>
<%# render the first N documents, and let the user expand the remaining if desired %>
<%= helpers.turbo_frame_tag "al-hierarchy-#{@document.id}-sidebar", loading: ('lazy' unless @target_index >= 0), src: hierarchy_path(limit: @maximum_left_gap, key: '-sidebar') %>
<%= tag.turbo_frame id: "al-hierarchy-#{@document.id}-right" do %>
<ul>
<li>
<%= link_to t('arclight.views.show.expand'), hierarchy_path(offset: @maximum_left_gap, key: '-right'), class: 'btn btn-secondary btn-sm' %>
</li>
</ul>
<div id="al-hierarchy-additional-pages">
<% num_pages.each do |page| %>
<% hidden = 'd-none' unless page == 1 %>
<%= tag.turbo_frame class:"#{hidden}", id: "al-hierarchy-#{@document.id}-#{page}-right" do %>
<ul class="al-hierarchy-more" >
<li>
<%= link_to t('arclight.views.show.expand'), hierarchy_path(offset: get_offset(page), limit: @maximum_left_gap, key: "-#{page}-right"),
id: "al-hierarchy-#{@document.id}-#{page}-more", class: 'btn btn-secondary btn-sm' %>
</li>
</ul>
<% end %>
<% end %>
</div>
<% else %>
<%# there aren't enough to bother paginating, so load them all at once %>
<%= helpers.turbo_frame_tag "al-hierarchy-#{@document.id}-sidebar", loading: ('lazy' unless @target_index >= 0), src: hierarchy_path(key: '-sidebar') %>
<% end %>
<script>
(function() {
function showNextLink(e) {
if (e.target.id.startsWith('al-hierarchy') && e.target.id.endsWith('more')) {
const id_pieces = e.target.id.split('-');
const current_page = parseInt(id_pieces[id_pieces.length - 2]);
const next_page = current_page + 1;
const t = e.target.id.replace(`${current_page}-more`, `${next_page}-right`)
document.getElementById(`${t}`).classList.remove('d-none');
}
}

const more_links = document.getElementById('al-hierarchy-additional-pages');
if (more_links !== null) {
more_links.addEventListener('click', showNextLink);
}
})();
</script>
13 changes: 11 additions & 2 deletions app/components/arclight/document_components_hierarchy_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ module Arclight
# Display a document's constituent components with appropriate lazy-loading
# to keep the page load time reasonable.
class DocumentComponentsHierarchyComponent < ViewComponent::Base
# rubocop:disable Metrics/ParameterLists
def initialize(document: nil, target_index: -1, minimum_pagination_size: 20, left_outer_window: 3, maximum_left_gap: 10, window: 10)
# rubocop:disable Metrics/ParameterLists
def initialize(document: nil, target_index: -1, minimum_pagination_size: 200, left_outer_window: 30, maximum_left_gap: 100, window: 100)
super

@document = document
Expand All @@ -21,6 +21,15 @@ def paginate?
@document.number_of_children > @minimum_pagination_size
end

def num_pages
number_of_pages = (@document.number_of_children / @maximum_left_gap.to_f).round
(1..number_of_pages).to_a
end

def get_offset(page)
page * @maximum_left_gap
end

def hierarchy_path(**kwargs)
helpers.hierarchy_solr_document_path(id: @document.id, hierarchy: true, nest_path: params[:nest_path], **kwargs)
end
Expand Down
Loading