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 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: [], **options ) text = ta(action) if text.nil? && action content = fa_s(icon).concat(" ").concat(tag.span(text, class: "text")) if method != :get options[:method] = method 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.deep_merge!(data: {confirm: confirm}) end link_to(content, url, options.sort.to_h) 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