2018-03-29 21:55:26 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module SmartAnnotations
|
2018-04-24 20:53:49 +08:00
|
|
|
class HtmlPreview
|
2018-03-29 21:55:26 +08:00
|
|
|
class << self
|
|
|
|
def html(name, type, object)
|
|
|
|
send("generate_#{type}_snippet", name, object)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
ROUTES = Rails.application.routes.url_helpers
|
|
|
|
|
|
|
|
def generate_prj_snippet(_, object)
|
2020-11-06 18:47:18 +08:00
|
|
|
return "<span class='sa-type'>Prj</span>#{object.name} #{I18n.t('atwho.res.archived')}" if object.archived?
|
|
|
|
|
2020-09-08 00:30:54 +08:00
|
|
|
"<a href='#{ROUTES.project_path(object)}'><span class='sa-type'>Prj</span>#{object.name}</a>"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_exp_snippet(_, object)
|
2020-11-06 18:47:18 +08:00
|
|
|
if object.archived? || object.project.archived?
|
2020-09-21 20:09:11 +08:00
|
|
|
return "<span class='sa-type'>Exp</span>#{object.name} #{I18n.t('atwho.res.archived')}"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
2020-11-06 18:47:18 +08:00
|
|
|
|
2020-09-08 00:30:54 +08:00
|
|
|
"<a href='#{ROUTES.canvas_experiment_path(object)}'><span class='sa-type'>Exp</span>#{object.name}</a>"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_tsk_snippet(_, object)
|
2020-11-06 18:47:18 +08:00
|
|
|
if object.archived? || object.experiment.archived? || object.experiment.project.archived?
|
2020-09-21 20:09:11 +08:00
|
|
|
return "<span class='sa-type'>Tsk</span>#{object.name} #{I18n.t('atwho.res.archived')}"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
2020-11-06 18:47:18 +08:00
|
|
|
|
2018-03-29 21:55:26 +08:00
|
|
|
"<a href='#{ROUTES.protocols_my_module_path(object)}'>" \
|
2020-09-08 00:30:54 +08:00
|
|
|
"<span class='sa-type'>Tsk</span>#{object.name}</a>"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def generate_rep_item_snippet(name, object)
|
2020-09-24 16:36:04 +08:00
|
|
|
if object&.repository
|
2018-05-25 17:41:43 +08:00
|
|
|
repository_name = fetch_repository_name(object)
|
2020-12-15 14:36:49 +08:00
|
|
|
"<a href='#{ROUTES.repository_repository_row_path(object.repository, object)}' " \
|
|
|
|
"class='record-info-link'><span class='sa-type'>#{trim_repository_name(repository_name)}</span>" \
|
|
|
|
"#{object.name} #{object.archived? ? I18n.t('atwho.res.archived') : ''}</a>"
|
|
|
|
else
|
|
|
|
"<span class='sa-type'>Inv</span> #{name} #{I18n.t('atwho.res.deleted')}"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def trim_repository_name(name)
|
2018-05-04 16:44:23 +08:00
|
|
|
splited_name = name.split
|
|
|
|
size = splited_name.size
|
|
|
|
return name.strip.slice(0..2).capitalize if size == 1
|
2020-11-06 18:47:18 +08:00
|
|
|
|
2018-05-04 16:44:23 +08:00
|
|
|
generate_name_from_array(splited_name, size).capitalize
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_name_from_array(names, size)
|
|
|
|
return "#{names[0].slice(0..1)}#{names[1][0]}" if size == 2
|
2020-11-06 18:47:18 +08:00
|
|
|
|
2018-05-04 16:44:23 +08:00
|
|
|
"#{names[0][0]}#{names[1][0]}#{names[2][0]}"
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
2018-05-25 17:41:43 +08:00
|
|
|
|
|
|
|
def fetch_repository_name(object)
|
|
|
|
return object.repository.name if object.repository
|
2020-11-06 18:47:18 +08:00
|
|
|
|
|
|
|
repository = Repository.with_discarded.find_by(id: object.repository_id)
|
2018-05-25 17:41:43 +08:00
|
|
|
return 'Inv' unless repository
|
2020-11-06 18:47:18 +08:00
|
|
|
|
2018-05-25 17:41:43 +08:00
|
|
|
repository.name
|
|
|
|
end
|
2018-03-29 21:55:26 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|