module BMC::ButtonHelper class << self attr_writer :default_size, :default_style def default_size @default_size ||= :sm end def default_style @default_style ||= :outline_primary end def confirm_attribute @confirm_attribute ||= :"data-turbo-confirm" end def method_attribute @method_attribute ||= :"data-turbo-method" end end def bs_button( # rubocop:disable Metrics/ParameterLists url, method: :get, confirm: nil, text: nil, icon:, action: nil, title: text, btn_size: BMC::ButtonHelper.default_size, btn_style: BMC::ButtonHelper.default_style, add_class: [], helper: :link_to, **options ) text = ta(action) if text.nil? && action content = fa_s(icon).concat(" ").concat(tag.span(text, class: "text")) if method != :get if helper == :link_to options[BMC::ButtonHelper.method_attribute] = method options[:rel] = "nofollow" elsif helper == :button_to options[:method] = method end confirm = true if confirm.nil? end unless options[:class] options[:class] = [ "btn", "btn-#{btn_size}", "btn-#{btn_style.to_s.tr('_', '-')}", ("link-#{action}" if action), *add_class, ] end options[:title] = title unless confirm.nil? confirm = ta(:confirm) if confirm == true options[BMC::ButtonHelper.confirm_attribute] = confirm end public_send(helper, content, url, options.sort.to_h) end def bs_button_to(url, **options) bs_button(url, helper: :button_to, **options) end def new_button(url = url_for(action: :new), **options) options = { :icon => :plus, :action => :new, :btn_style => :success, }.merge(options) bs_button(url, **options) end def read_button(url, **options) options = { :icon => :info_circle, :action => :read, }.merge(options) bs_button(url, **options) end def edit_button(url, **options) options = { :icon => :pencil_alt, :action => :edit, }.merge(options) bs_button(url, **options) end def delete_button(url, **options) options = { :icon => :trash, :action => :delete, :btn_style => :danger, :method => :delete, }.merge(options) bs_button(url, **options) end def print_button(**options) options = { :icon => :print, :action => :print, :onclick => "print(); return false;", }.merge(options) bs_button("#", **options) end def download_button(url, **options) options = { :icon => :cloud_download_alt, :action => :download, :target => :_blank, }.merge(options) bs_button(url, **options) end def export_button(url, **options) ext = url.split(".").last if ext.length <= 4 text = "#{ta(:export)} (#{ext.upcase})" else text = ta(:export) end options = { :icon => :download, :action => :export, :text => text, }.merge(options) download_button(url, **options) end def import_button(url, **options) options = { :icon => :upload, :action => :import, }.merge(options) bs_button(url, **options) end def copy_button(url, **options) options = { :icon => :copy, :action => :copy, }.merge(options) bs_button(url, **options) end end