2023-01-04 17:22:06 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-01-10 22:14:58 +01:00
|
|
|
module BootstrapHelper
|
|
|
|
def nav_entry(body, path, options = {})
|
|
|
|
options = {
|
2023-01-04 17:22:06 +01:00
|
|
|
badge: nil,
|
2022-01-10 22:14:58 +01:00
|
|
|
badge_color: nil,
|
2023-02-25 15:44:51 +01:00
|
|
|
badge_attr: {},
|
2023-01-04 17:22:06 +01:00
|
|
|
icon: nil,
|
2023-02-08 00:00:50 +01:00
|
|
|
class: "",
|
2023-08-16 21:17:18 +02:00
|
|
|
id: nil,
|
2023-02-08 00:00:50 +01:00
|
|
|
hotkey: nil,
|
2022-01-10 22:14:58 +01:00
|
|
|
}.merge(options)
|
|
|
|
|
|
|
|
classes = [
|
|
|
|
"nav-item",
|
|
|
|
current_page?(path) ? "active" : nil,
|
|
|
|
options[:class]
|
|
|
|
].compact.join(" ")
|
|
|
|
|
|
|
|
unless options[:icon].nil?
|
2023-01-04 17:22:06 +01:00
|
|
|
body = if options[:icon_only]
|
|
|
|
content_tag(:i, "", class: "fa fa-#{options[:icon]}", title: body).to_s
|
|
|
|
else
|
|
|
|
"#{content_tag(:i, '', class: "fa fa-#{options[:icon]}")} #{body}"
|
|
|
|
end
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
2023-10-18 23:26:10 +02:00
|
|
|
if options[:badge].present? || options.dig(:badge_attr, :data)&.key?(:controller)
|
2023-01-04 17:22:06 +01:00
|
|
|
badge_class = [
|
|
|
|
"badge",
|
|
|
|
("badge-#{options[:badge_color]}" unless options[:badge_color].nil?),
|
|
|
|
("badge-pill" if options[:badge_pill])
|
|
|
|
].compact.join(" ")
|
|
|
|
|
2023-10-18 23:26:10 +02:00
|
|
|
body += " #{content_tag(:span, options[:badge], class: badge_class, **options[:badge_attr])}".html_safe # rubocop:disable Rails/OutputSafety
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
|
2023-10-18 23:26:10 +02:00
|
|
|
content_tag(:li, link_to(body.html_safe, path, class: "nav-link", data: { hotkey: options[:hotkey] }), class: classes, id: options[:id]) # rubocop:disable Rails/OutputSafety
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def list_group_item(body, path, options = {})
|
|
|
|
options = {
|
2023-01-04 17:22:06 +01:00
|
|
|
badge: nil,
|
2022-01-10 22:14:58 +01:00
|
|
|
badge_color: nil,
|
2023-10-18 23:26:10 +02:00
|
|
|
class: "",
|
2022-01-10 22:14:58 +01:00
|
|
|
}.merge(options)
|
|
|
|
|
|
|
|
classes = [
|
|
|
|
"list-group-item",
|
|
|
|
"list-group-item-action",
|
|
|
|
current_page?(path) ? "active" : nil,
|
|
|
|
options[:class]
|
|
|
|
].compact.join(" ")
|
|
|
|
|
2023-01-04 17:22:06 +01:00
|
|
|
unless options[:badge].nil? || (options[:badge]).zero?
|
2022-01-10 22:14:58 +01:00
|
|
|
# TODO: make this prettier?
|
2023-10-18 23:26:10 +02:00
|
|
|
badge = content_tag(:span, options[:badge], class: "badge#{
|
2022-01-10 22:14:58 +01:00
|
|
|
" badge-#{options[:badge_color]}" unless options[:badge_color].nil?
|
2023-10-18 23:26:10 +02:00
|
|
|
}",)
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
|
2023-10-18 23:26:10 +02:00
|
|
|
html = if badge
|
|
|
|
"#{body} #{badge}"
|
|
|
|
else
|
|
|
|
body
|
|
|
|
end
|
|
|
|
|
|
|
|
content_tag(:a, html.html_safe, href: path, class: classes) # rubocop:disable Rails/OutputSafety
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def tooltip(body, tooltip_content, placement = "bottom")
|
2024-03-10 15:29:10 +01:00
|
|
|
content_tag(:span, body, { :title => tooltip_content, "data-controller" => "tooltip", "data-bs-placement" => placement })
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def time_tooltip(subject, placement = "bottom")
|
2024-03-02 21:36:29 +01:00
|
|
|
tooltip time_ago_in_words(subject.created_at, scope: "datetime.distance_in_words.short"), localize(subject.created_at, format: :long), placement
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
#
|
2023-01-04 17:49:52 +01:00
|
|
|
def bootstrap_color(color)
|
|
|
|
case color
|
2022-01-10 22:14:58 +01:00
|
|
|
when "error", "alert"
|
|
|
|
"danger"
|
|
|
|
when "notice"
|
|
|
|
"info"
|
|
|
|
else
|
2023-01-04 17:49:52 +01:00
|
|
|
color
|
2022-01-10 22:14:58 +01:00
|
|
|
end
|
|
|
|
end
|
2023-01-04 12:23:47 +01:00
|
|
|
end
|