Skip to content

Commit 2d48fa4

Browse files
Feature: Add see more items component (#910)
* add list items component to hide container items if more than max items * use list item component in concept details component for synonyms and others * fix show edge case where max items is equal to the items total length * remove an cyclic include raising an exception
1 parent 39208bd commit 2d48fa4

File tree

7 files changed

+41
-13
lines changed

7 files changed

+41
-13
lines changed

app/components/concept_details_component.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ class ConceptDetailsComponent < ViewComponent::Base
1010

1111
attr_reader :concept_properties
1212

13-
def initialize(id:, acronym:, concept_id: nil , properties: nil, top_keys: [], bottom_keys: [], exclude_keys: [])
13+
def initialize(id:, acronym:, concept_id: nil, properties: nil, top_keys: [], bottom_keys: [], exclude_keys: [])
1414
@acronym = acronym
1515
@properties = properties
1616
@top_keys = top_keys
1717
@bottom_keys = bottom_keys
1818
@exclude_keys = exclude_keys
1919
@id = id
20-
@concept_id=concept_id
20+
@concept_id = concept_id
2121

2222
@concept_properties = concept_properties2hash(@properties) if @properties
2323
end
@@ -55,8 +55,8 @@ def row_hash_properties(properties_set, ontology_acronym, &block)
5555
end
5656

5757
out << [
58-
{ th: content_tag(:span, remove_owl_notation(key), title: url, 'data-controller': 'tooltip') },
59-
{ td: content_tag(:span, ajax_links.join.html_safe) }
58+
{ th: content_tag(:span, remove_owl_notation(key), title: url, 'data-controller': 'tooltip') },
59+
{ td: list_items_component(max_items: 5) { |r| ajax_links.map { |val| r.container { val.html_safe } } } }
6060
]
6161
end
6262
out
@@ -81,7 +81,7 @@ def filter_properties(top_keys, bottom_keys, exclude_keys, concept_properties)
8181
private
8282

8383
def link_to_format_modal(format, icon)
84-
link_to_modal(nil, "/ontologies/#{@acronym}/#{escape(@concept_id)}/serialize/#{format}",{ id: "resource_content_#{format}", data: {show_modal_title_value: @concept_id, show_modal_size_value: 'modal-xl'}}) do
84+
link_to_modal(nil, "/ontologies/#{@acronym}/#{escape(@concept_id)}/serialize/#{format}", { id: "resource_content_#{format}", data: { show_modal_title_value: @concept_id, show_modal_size_value: 'modal-xl' } }) do
8585
inline_svg("icons/#{icon}.svg", width: '50px', height: '50px')
8686
end
8787
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
class ListItemsShowMoreComponent < ViewComponent::Base
3+
renders_many :containers
4+
5+
def initialize(max_items: 10)
6+
super
7+
@max_items = max_items - 1
8+
end
9+
10+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
%div{data: {controller: 'reveal-component'}}
2+
- containers[..@max_items].each do |item|
3+
= item
4+
5+
- if (@max_items + 1) < containers.size
6+
- Array(containers[@max_items+1..]).each do |item|
7+
%span{'data-reveal-component-target': "item", class: 'd-none'}
8+
= item
9+
%div{'data-reveal-component-target': "showButton", 'data-action': "click->reveal-component#show" }
10+
%span.btn-link
11+
See more
12+
%div.d-none{'data-reveal-component-target': "hideButton", 'data-action': "click->reveal-component#hide" }
13+
%span.btn-link
14+
See less

app/helpers/components_helper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ def chips_component(id: , name: , label: , value: , checked: false , tooltip: ni
2525
check_input(id: id, name: name, value: value, label: label, checked: checked, disabled: disabled, &block)
2626
end
2727
end
28+
def list_items_component(max_items:, &block)
29+
render ListItemsShowMoreComponent.new(max_items: max_items) do |r|
30+
capture(r, &block)
31+
end
32+
end
2833

2934
def group_chip_component(id: nil, name: , object: , checked: , value: nil, title: nil, disabled: false, &block)
3035
title ||= object["name"]
@@ -111,7 +116,7 @@ def resolvability_check_tag(url)
111116
end
112117

113118
def rounded_button_component(link)
114-
render RoundedButtonComponent.new(link: link, target: '_blank',size: 'small',title: t("components.go_to_api"))
119+
render RoundedButtonComponent.new(link: link, target: '_blank', size: 'small', title: t("components.go_to_api"))
115120
end
116121

117122
def copy_link_to_clipboard(url, show_content: false)

app/helpers/multi_languages_helper.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module MultiLanguagesHelper
2+
include OntologiesHelper, ComponentsHelper
23
def portal_lang
34
session[:locale] || 'en'
45
end
@@ -172,15 +173,15 @@ def display_in_multiple_languages(label)
172173
closable: true)
173174
end
174175

175-
176-
177176
label = label.to_h.reject { |key, _| %i[links context].include?(key) } if label.is_a?(OpenStruct)
178177

179178
if label.is_a?(String)
180179
content_tag(:p, label)
181180
elsif label.is_a?(Array)
182-
content_tag(:div) do
183-
raw(label.map { |x| content_tag(:div, x) }.join)
181+
list_items_component(max_items: show_max) do |r|
182+
label.map do |x|
183+
r.container { content_tag(:span, x, class: style_as_badge ? 'badge bg-secondary' : '').html_safe }
184+
end
184185
end
185186
else
186187
content_tag(:div) do

app/helpers/ontologies_helper.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
require 'iso-639'
22
module OntologiesHelper
3-
include ApplicationHelper
43
REST_URI = $REST_URL
54
API_KEY = $API_KEY
65
LANGUAGE_FILTERABLE_SECTIONS = %w[classes schemes collections instances properties].freeze

app/views/concepts/_details.html.haml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@
1919
- t.add_row({th: t('ontology_details.concept.synonyms')}) do |h|
2020
- h.td do
2121
%div.d-flex
22-
%div.d-flex
23-
= display_in_multiple_languages(@concept.synonym)
22+
= display_in_multiple_languages(@concept.synonym, style_as_badge: true, show_max: 5)
2423
%div.synonym-change-request
2524
= add_synonym_button
2625
= remove_synonym_button

0 commit comments

Comments
 (0)