Skip to content

Commit

Permalink
Merge branch 'feature/Create-a-component-to-display-tree-views' into …
Browse files Browse the repository at this point in the history
…stage
  • Loading branch information
syphax-bouazzouni committed Dec 15, 2023
2 parents 3977206 + 0367c4f commit e89d2b7
Show file tree
Hide file tree
Showing 24 changed files with 381 additions and 392 deletions.
17 changes: 17 additions & 0 deletions app/assets/stylesheets/bioportal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,7 @@ div.tree_error {

#bd ul.simpleTree li{
margin-left:-10px;
padding: 5px 0 0 10px;
}

#bd ul.simpleTree{
Expand All @@ -668,6 +669,22 @@ div.tree_error {
overflow:auto;
border: 1px solid #444444;
*/
a.tree-link {
display: inline-block;
padding: 5px;
}

a.tree-link.active {
cursor: default;
background-color: var(--light-color);
font-weight: bold;
border-radius: 2px;
}

.tree-border-left{
border-left: 2px dotted #eee;
margin-left: 6px;
}
}
.simpleTree li {
list-style: none;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
current_page: @current_page, next_page: @next_page) do |c|

%div
%ul.simpleTree{data:{controller: 'simple-tree','simple-tree': { 'auto-click-value': auto_click? }, action: 'clicked->history#updateURL'}}
%li.root
%ul
= content
= render TreeViewComponent.new(id: nil, auto_click: auto_click?) do
= content

- c.error do
%div.text-wrap
Expand Down
69 changes: 69 additions & 0 deletions app/components/tree_link_component.rb
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 app/components/tree_link_component/tree_link_component.html.haml
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
44 changes: 44 additions & 0 deletions app/components/tree_view_component.rb
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

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




111 changes: 9 additions & 102 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,6 @@ def to_param(name) # Paramaterizes URLs without encoding
end
end

def undo_param(name) #Undo Paramaterization
unless name.nil?
name.to_s.gsub('_'," ")
end
end

def bp_config_json
# For config settings, see
# config/bioportal_config.rb
Expand All @@ -188,22 +182,6 @@ def bp_config_json
config.to_json
end

def remote_file_exists?(url)
begin
url = URI.parse(url)

if url.kind_of?(URI::FTP)
check = check_ftp_file(url)
else
check = check_http_file(url)
end

rescue
return false
end

check
end

def rest_url
# Split the URL into protocol and path parts
Expand Down Expand Up @@ -308,15 +286,6 @@ def redirect_to_home # Redirect to Home Page
redirect_to "/"
end

def redirect_to_history # Redirects to the correct tab through the history system
if session[:redirect].nil?
redirect_to_home
else
tab = find_tab(session[:redirect][:ontology])
session[:redirect]=nil
redirect_to uri_url(:ontology=>tab.ontology_id,:conceptid=>tab.concept)
end
end

def redirect_new_api(class_view = false)
# Hack to make ontologyid and conceptid work in addition to id and ontology params
Expand Down Expand Up @@ -373,24 +342,6 @@ def authorize_and_redirect
end
end

# Verifies that a user owns an object
def authorize_owner(id=nil)
if id.nil?
id = params[:id].to_i
end

id.map! {|i| i.to_i} if id.kind_of?(Array)

if session[:user].nil?
redirect_to_home
else
if id.kind_of?(Array)
redirect_to_home if !session[:user].admin? && !id.include?(session[:user].id.to_i)
else
redirect_to_home if !session[:user].admin? && !session[:user].id.to_i.eql?(id)
end
end
end

def authorize_admin
admin = session[:user] && session[:user].admin?
Expand All @@ -405,41 +356,7 @@ def ontology_restricted?(acronym)
restrict_downloads = $NOT_DOWNLOADABLE
restrict_downloads.include? acronym
end
# updates the 'history' tab with the current selected concept
def update_tab(ontology, concept)
array = session[:ontologies] || []
found = false
for item in array
if item.ontology_id.eql?(ontology.id)
item.concept=concept
found=true
end
end

unless found
array << History.new(ontology.id, ontology.name, ontology.acronym, concept)
end

session[:ontologies]=array
end

# Removes a 'history' tab
def remove_tab(ontology_id)
array = session[:ontologies]
array.delete(find_tab(ontology_id))
session[:ontologies]=array
end

# Returns a specific 'history' tab
def find_tab(ontology_id)
array = session[:ontologies]
for item in array
if item.ontology_id.eql?(ontology_id)
return item
end
end
return nil
end

def check_delete_mapping_permission(mappings)
# ensure mappings is an Array of mappings (some calls may provide only a single mapping instance)
Expand All @@ -463,13 +380,13 @@ def using_captcha?
def get_class(params)

lang = request_lang

if @ontology.flat?

ignore_concept_param = params[:conceptid].nil? ||
params[:conceptid].empty? ||
params[:conceptid].eql?("root") ||
params[:conceptid].eql?("bp_fake_root")
params[:conceptid].empty? ||
params[:conceptid].eql?("root") ||
params[:conceptid].eql?("bp_fake_root")
if ignore_concept_param
# Don't display any classes in the tree
@concept = LinkedData::Client::Models::Class.new
Expand All @@ -491,19 +408,19 @@ def get_class(params)
# not ignoring 'bp_fake_root' here
include = 'prefLabel,hasChildren,obsolete'
ignore_concept_param = params[:conceptid].nil? ||
params[:conceptid].empty? ||
params[:conceptid].eql?("root")
params[:conceptid].empty? ||
params[:conceptid].eql?("root")
if ignore_concept_param
# get the top level nodes for the root
# TODO_REV: Support views? Replace old view call: @ontology.top_level_classes(view)
@roots = @ontology.explore.roots(concept_schemes: params[:concept_schemes])
@roots = @ontology.explore.roots(concept_schemes: params[:concept_schemes])
if @roots.nil? || @roots.empty?
LOG.add :debug, "Missing @roots for #{@ontology.acronym}"
classes = @ontology.explore.classes.collection
@concept = classes.first.explore.self(full: true) if classes.first
return
end

@root = LinkedData::Client::Models::Class.new(read_only: true)
@root.children = @roots.sort{|x,y| (x.prefLabel || "").downcase <=> (y.prefLabel || "").downcase}

Expand Down Expand Up @@ -569,17 +486,7 @@ def get_simplified_ontologies_hash()
return simple_ontologies
end

def get_ontology_details(ont_uri)
# Note the simplify_ontology_model will cache individual ontology data.
begin
ont_model = LinkedData::Client::Models::Ontology.find(ont_uri)
ont = simplify_ontology_model(ont_model)
rescue Exception => e
LOG.add :error, e.message
return nil
end
return ont
end


def simplify_classes(classes)
# Simplify the classes batch service data for the UI
Expand Down
Loading

0 comments on commit e89d2b7

Please sign in to comment.