Skip to content

Commit

Permalink
feat: APPS-2680 html tags and "read more" button for the search resul…
Browse files Browse the repository at this point in the history
…ts (#1152)

* feat: APPS-2680 html tags and "read more" button for the search results

* Allow .html_safe

* APPS-2680: updated JS to not cut description in the middle of the word

* APPS-2680: updated JS to not cut description in the middle of the word and number

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Andy Wallace <andrewwallace@library.ucla.edu>
  • Loading branch information
3 people authored Aug 29, 2024
1 parent 1ccf5b3 commit 398dfdd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 27 deletions.
49 changes: 28 additions & 21 deletions app/assets/javascripts/truncate_description.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
document.addEventListener("turbolinks:load", function() {
var viewMoreButtons = document.querySelectorAll('.view-more')
document.addEventListener("turbolinks:load", function () {
var viewMoreButtons = document.querySelectorAll(".view-more");
viewMoreButtons.forEach(function (el) {
var description = el.previousElementSibling
var originalText = description.innerText
var truncatedText = description.innerText.substr(0, 299)
truncatedText += '…'
var description = el.previousElementSibling;
var originalText = description.innerText;

if (description.innerText.length < 300) {
el.style.display = 'none'
} else {
description.innerText = truncatedText
if (originalText.length < 300) {
el.style.display = "none";
return;
}

el.addEventListener('click', function () {
if (description.classList.contains('description')) {
el.innerHTML = 'Read Less <div class="up-arrow">&raquo;</div>'
description.classList.replace('description', 'description-full')
description.innerText = originalText
var truncatedText = originalText.substr(0, 299);

// To avoid truncation in the middle of the word get the end of the word
// from the the cut part of the description.
var cutString = originalText.substr(299);
truncatedText += cutString.match(/^[a-z0-9]*/i)[0]; //regex gets the end of the word

truncatedText += "…";
description.innerText = truncatedText;

el.addEventListener("click", function () {
if (description.classList.contains("description")) {
el.innerHTML = 'Read Less <div class="up-arrow">&raquo;</div>';
description.classList.replace("description", "description-full");
description.innerText = originalText;
} else {
el.innerHTML = 'Read More <div class="down-arrow">&raquo;</div>'
description.classList.replace('description-full', 'description')
description.innerText = truncatedText
el.innerHTML = 'Read More <div class="down-arrow">&raquo;</div>';
description.classList.replace("description-full", "description");
description.innerText = truncatedText;
}
})
})
})
});
});
});
1 change: 1 addition & 0 deletions app/assets/stylesheets/legacy/ursus/_description.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.view-more {
@extend a;
cursor: pointer;
margin-bottom: 10px;
}

.down-arrow {
Expand Down
7 changes: 3 additions & 4 deletions app/helpers/ursus/catalog_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ def render_truncated_description(args)
truncated_output = String.new
content_tag :div, class: 'truncate-description' do
description = args[:value].first
button = "<span class='view-more' href>Read More <div class='down-arrow'>&raquo;</div></span></br>"
truncated_output << "<div class='description'>#{description}</div>#{button}</br>"
# return truncated_output.html_safe
return description
button = "<div class='view-more' href>Read More <div class='down-arrow'>&raquo;</div></div>"
truncated_output << "<div class='description'>#{description}</div>#{button}"
truncated_output.html_safe # rubocop:disable Rails/OutputSafety
end
end

Expand Down
5 changes: 3 additions & 2 deletions spec/system/search_catalog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
has_model_ssim: ['Work'],
title_tesim: ['Orange Carrot'],
photographer_tesim: ['Bittersweet Tangerine'],
description_tesim: ['Long description Long description Long description Long description Long description Long description']
description_tesim: ['Long description Long description Long description<br>Long description Long description Long description']
}
end

Expand Down Expand Up @@ -147,7 +147,8 @@
# Search for something
fill_in 'q', with: 'carrot'
click_on 'search'
expect(page).not_to have_content('Read More')
expect(page).to have_content('Read More')
expect(page).not_to have_content('<br>')
end

context 'when the sinai? flag is disabled' do
Expand Down

0 comments on commit 398dfdd

Please sign in to comment.