Skip to content

Commit

Permalink
add extern_link argument to ajax_link_chip to enforce external or not
Browse files Browse the repository at this point in the history
  • Loading branch information
syphax-bouazzouni committed Nov 7, 2024
1 parent 4f538a9 commit 2afeb07
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
14 changes: 7 additions & 7 deletions app/components/label_fetcher_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@
class LabelFetcherComponent < ViewComponent::Base
include UrlsHelper, Turbo::FramesHelper, ModalHelper

def initialize(id:, label: nil, link: nil, ajax_src: nil, open_in_modal: false, target: nil)
def initialize(id:, label: nil, link: nil, ajax_src: nil, open_in_modal: false, target: nil, external: false)
super
@id = id
@link = link
@ajax_src = ajax_src
@open_in_modal = open_in_modal
@target = target
@external = external
@label = label

if internal_link?(id, label)
if external_link?
@link = id
@target ||= '_blank'
@label = id
else
@label = label
@target ||= '_top'
end

end

def internal_link?(id, label)
label.nil? || label.eql?(id)
def external_link?
(@label.nil? || @label.eql?(@id)) && @external
end

def label_fetcher_container(&block)
Expand All @@ -47,7 +47,7 @@ def label_fetcher_container(&block)
end

def link_with_icon
if @id.eql?(@label)
if external_link?
ExternalLinkTextComponent.new(text: @label).call
else
InternalLinkTextComponent.new(text: @label).call
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def show_label

label = helpers.main_language_label(collection_label)
link = collection_path(collection_id: params[:id], ontology_id: params[:ontology_id], language: request_lang)
render(inline: helpers.ajax_link_chip(params[:id], label, link), layout: false)
render(inline: helpers.ajax_link_chip(params[:id], label, link, external: collection.blank?), layout: false)
end

def show_members
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/concepts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ def index
def show_label
cls_id = params[:concept] || params[:id]
ont_id = params[:ontology]
label = helpers.main_language_label(concept_label(ont_id, cls_id))
pref_label = concept_label(ont_id, cls_id)
cls = @ontology.explore.single_class({ language: request_lang, include: 'prefLabel' }, cls_id)
label = helpers.main_language_label(pref_label)
link = concept_path(cls_id, ont_id, request_lang)

render(inline: helpers.ajax_link_chip(cls_id, label, link), layout: nil)
render(inline: helpers.ajax_link_chip(cls_id, label, link, external: cls.nil? || cls.errors), layout: nil)
end

def show_definition
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/label_xl_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def show_label
label_xl_label = params[:id] if label_xl_label.nil? || label_xl_label.empty?
label = helpers.main_language_label(label_xl_label)
link = "/ajax/label_xl/?id=#{escape(params[:id])}&ontology=#{params[:ontology_id]}&cls_id=#{escape(params[:cls_id])}"
render(inline: helpers.ajax_link_chip(params[:id], label, link, open_in_modal: true), layout: false)
render(inline: helpers.ajax_link_chip(params[:id], label, link, open_in_modal: true, external: label_xl.blank?), layout: false)
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/schemes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def show_label
scheme_label = scheme_label.nil? || scheme_label.empty? ? params[:id] : scheme_label
label = helpers.main_language_label(scheme_label)
link = scheme_path(scheme_id: params[:id], ontology_id: params[:ontology_id])
render(inline: helpers.ajax_link_chip(params[:id], label, link), layout: false)
render(inline: helpers.ajax_link_chip(params[:id], label, link, external: scheme.blank?), layout: false)
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/components_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ def tree_component(root, selected, target_frame:, sub_tree: false, id: nil, auto
end
end

def ajax_link_chip(id, label = nil, link = nil, open_in_modal: false, ajax_src: nil, target: '_blank')
render LabelFetcherComponent.new(id: id, label: label, link: link, open_in_modal: open_in_modal, ajax_src: ajax_src, target: target)
def ajax_link_chip(id, label = nil, link = nil, external: false, open_in_modal: false, ajax_src: nil, target: '_blank')
render LabelFetcherComponent.new(id: id, label: label, link: link, open_in_modal: open_in_modal, ajax_src: ajax_src, target: target, external: external)
end

def chart_component(title: '', type:, labels:, datasets:, index_axis: 'x', show_legend: false)
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/concepts_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def concept_label(ont_id, cls_id)
@ontology ||= LinkedData::Client::Models::Ontology.find_by_acronym(ont_id).first
ontology_not_found(ont_id) if @ontology.nil? || @ontology.errors
cls = @ontology.explore.single_class({language: request_lang, include: 'prefLabel'}, cls_id)
cls.prefLabel || cls_id
cls&.prefLabel || cls_id
end

def concept_id_param_exist?(params)
Expand Down

0 comments on commit 2afeb07

Please sign in to comment.