From 0457e2a010b6acefd9f8c1d8dd727b38057c252e Mon Sep 17 00:00:00 2001 From: kethlinmil Date: Fri, 23 Aug 2024 19:51:51 +0200 Subject: [PATCH 1/4] feat: APPS-2680 html tags and "read more" button for the search results --- app/helpers/ursus/catalog_helper.rb | 3 +-- spec/system/search_catalog_spec.rb | 5 +++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/helpers/ursus/catalog_helper.rb b/app/helpers/ursus/catalog_helper.rb index f1c50b5e3..1cefa281c 100644 --- a/app/helpers/ursus/catalog_helper.rb +++ b/app/helpers/ursus/catalog_helper.rb @@ -29,8 +29,7 @@ def render_truncated_description(args) description = args[:value].first button = "Read More
»

" truncated_output << "
#{description}
#{button}
" - # return truncated_output.html_safe - return description + truncated_output.html_safe end end end diff --git a/spec/system/search_catalog_spec.rb b/spec/system/search_catalog_spec.rb index 3558c3c4e..546866dc0 100644 --- a/spec/system/search_catalog_spec.rb +++ b/spec/system/search_catalog_spec.rb @@ -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
Long description Long description Long description'] } end @@ -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('
') end context 'when the sinai? flag is disabled' do From 39e81879bf05e803e4d5c9dcc04b396b9f0cc82d Mon Sep 17 00:00:00 2001 From: Andy Wallace Date: Fri, 23 Aug 2024 14:27:38 -0700 Subject: [PATCH 2/4] Allow .html_safe --- app/helpers/ursus/catalog_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/helpers/ursus/catalog_helper.rb b/app/helpers/ursus/catalog_helper.rb index 1cefa281c..5130fd5b7 100644 --- a/app/helpers/ursus/catalog_helper.rb +++ b/app/helpers/ursus/catalog_helper.rb @@ -29,6 +29,7 @@ def render_truncated_description(args) description = args[:value].first button = "Read More
»

" truncated_output << "
#{description}
#{button}
" + # rubocop:disable Rails::OutputSafety truncated_output.html_safe end end From 12ac27625c3241700437e35b696cf9d0a36aa1dd Mon Sep 17 00:00:00 2001 From: kethlinmil Date: Mon, 26 Aug 2024 19:31:56 +0200 Subject: [PATCH 3/4] APPS-2680: updated JS to not cut description in the middle of the word --- .../javascripts/truncate_description.js | 49 +++++++++++-------- .../legacy/ursus/_description.scss | 1 + app/helpers/ursus/catalog_helper.rb | 7 ++- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/app/assets/javascripts/truncate_description.js b/app/assets/javascripts/truncate_description.js index 275e32812..212e4c8eb 100644 --- a/app/assets/javascripts/truncate_description.js +++ b/app/assets/javascripts/truncate_description.js @@ -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
»
' - 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-z]*/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
»
'; + description.classList.replace("description", "description-full"); + description.innerText = originalText; } else { - el.innerHTML = 'Read More
»
' - description.classList.replace('description-full', 'description') - description.innerText = truncatedText + el.innerHTML = 'Read More
»
'; + description.classList.replace("description-full", "description"); + description.innerText = truncatedText; } - }) - }) -}) + }); + }); +}); diff --git a/app/assets/stylesheets/legacy/ursus/_description.scss b/app/assets/stylesheets/legacy/ursus/_description.scss index 7fdc85e15..96df0b47b 100644 --- a/app/assets/stylesheets/legacy/ursus/_description.scss +++ b/app/assets/stylesheets/legacy/ursus/_description.scss @@ -1,6 +1,7 @@ .view-more { @extend a; cursor: pointer; + margin-bottom: 10px; } .down-arrow { diff --git a/app/helpers/ursus/catalog_helper.rb b/app/helpers/ursus/catalog_helper.rb index 5130fd5b7..d445fbb91 100644 --- a/app/helpers/ursus/catalog_helper.rb +++ b/app/helpers/ursus/catalog_helper.rb @@ -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 = "Read More
»

" - truncated_output << "
#{description}
#{button}
" - # rubocop:disable Rails::OutputSafety - truncated_output.html_safe + button = "
Read More
»
" + truncated_output << "
#{description}
#{button}" + truncated_output.html_safe # rubocop:disable Rails/OutputSafety end end end From ef1e6c13030cdb5bd1c428f72f10b4ae87dfa24f Mon Sep 17 00:00:00 2001 From: kethlinmil Date: Mon, 26 Aug 2024 19:46:55 +0200 Subject: [PATCH 4/4] APPS-2680: updated JS to not cut description in the middle of the word and number --- app/assets/javascripts/truncate_description.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/assets/javascripts/truncate_description.js b/app/assets/javascripts/truncate_description.js index 212e4c8eb..611f0f41e 100644 --- a/app/assets/javascripts/truncate_description.js +++ b/app/assets/javascripts/truncate_description.js @@ -14,7 +14,7 @@ document.addEventListener("turbolinks:load", function () { // 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-z]*/i)[0]; //regex gets the end of the word + truncatedText += cutString.match(/^[a-z0-9]*/i)[0]; //regex gets the end of the word truncatedText += "…"; description.innerText = truncatedText;