forked from ontoportal/ontoportal_web_ui
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/Create-a-component-to-display-tree-views' into …
…stage
- Loading branch information
Showing
24 changed files
with
381 additions
and
392 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
class TreeLinkComponent < ViewComponent::Base | ||
include MultiLanguagesHelper | ||
include ComponentsHelper | ||
|
||
def initialize(child:, href:, children_href: , selected_child: , data: {}, muted: false, target_frame: nil) | ||
@child = child | ||
@selected_child = selected_child | ||
@active_style = child.id.eql?(selected_child&.id) && 'active' | ||
#@icons = child.relation_icon(node) | ||
@muted_style = muted ? 'text-muted' : '' | ||
@href = href | ||
@children_link = children_href | ||
if @child.prefLabel.nil? | ||
@pref_label_html = child.id.split('/').last | ||
else | ||
pref_label_lang, @pref_label_html = select_language_label(@child.prefLabel) | ||
pref_label_lang = pref_label_lang.to_s.upcase | ||
@tooltip = pref_label_lang.eql?("@NONE") ? "" : pref_label_lang | ||
end | ||
@data ||= { controller: 'tooltip', 'tooltip-position-value': 'right', turbo: true, 'turbo-frame': target_frame, action: 'click->simple-tree#select'} | ||
|
||
@data.merge!(data) do |_, old, new| | ||
"#{old} #{new}" | ||
end | ||
end | ||
|
||
|
||
# This gives a very hacky short code to use to uniquely represent a class | ||
# based on its parent in a tree. Used for unique ids in HTML for the tree view | ||
def short_uuid | ||
rand(36 ** 8).to_s(36) | ||
end | ||
|
||
# TDOD check where used | ||
def child_id | ||
@child.id.to_s.split('/').last | ||
end | ||
|
||
def open? | ||
@child.expanded? ? 'open' : '' | ||
end | ||
|
||
def border_left | ||
!@child.hasChildren && 'pl-3 tree-border-left' | ||
end | ||
|
||
def li_id | ||
@child.id.eql?('bp_fake_root') ? 'bp_fake_root' : short_uuid | ||
end | ||
|
||
|
||
def open_children_link | ||
return unless @child.hasChildren | ||
if @child.expanded? | ||
tree_close_icon | ||
else | ||
content_tag('turbo_frame', id: "#{child_id}_open_link") do | ||
link_to @children_link, | ||
data: { turbo: true, turbo_frame: "#{child_id + '_childs'}" } do | ||
content_tag(:i, nil, class: "fas fa-chevron-right") | ||
end | ||
end | ||
end | ||
|
||
end | ||
|
||
end |
10 changes: 10 additions & 0 deletions
10
app/components/tree_link_component/tree_link_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
%li{id:li_id , class: open?} | ||
= open_children_link | ||
%a{id: @child.id, data: @data, title: @tooltip, | ||
href: @href, class: "tree-link #{@muted_style} #{@active_style} #{border_left} #{open?}"} | ||
= @pref_label_html | ||
|
||
- if @child.hasChildren && [email protected]? | ||
= render TurboFrameComponent.new(id: "#{child_id}_childs") | ||
- elsif @child.expanded? | ||
= content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
class TreeViewComponent < ViewComponent::Base | ||
include Turbo::FramesHelper | ||
include ComponentsHelper | ||
|
||
renders_many :children, TreeLinkComponent | ||
|
||
def initialize(id:, auto_click: false, sub_tree: false, **html_options) | ||
@id = id | ||
@auto_click = auto_click | ||
@html_options = html_options | ||
@sub_tree = sub_tree | ||
end | ||
|
||
private | ||
|
||
def sub_tree? | ||
@sub_tree | ||
end | ||
|
||
def tree_container(&block) | ||
if sub_tree? | ||
content_tag(:ul, capture(&block), class: 'pl-2 tree-border-left') | ||
else | ||
content_tag(:div, class: 'tree_wrapper hide-if-loading') do | ||
content_tag(:ul, capture(&block), class: 'simpleTree root', data: { controller: 'simple-tree', | ||
'simple-tree-auto-click-value': "#{auto_click?}", | ||
action: 'clicked->history#updateURL' }) | ||
end | ||
end | ||
end | ||
|
||
def auto_click? | ||
@auto_click.to_s | ||
end | ||
|
||
# TDOD check where used | ||
def child_id(child) | ||
child.id.to_s.split('/').last | ||
end | ||
|
||
end | ||
|
9 changes: 9 additions & 0 deletions
9
app/components/tree_view_component/tree_view_component.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
%turbo-frame{id: "#{@id}", **@html_options} | ||
= tree_container do | ||
- children.each do |child| | ||
= child | ||
= content | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.