# frozen_string_literal: true module Lipstick module Helpers module FormHelper include ActionView::Helpers::FormTagHelper def field_block(html_opts = {}, &block) add_css_class(html_opts, 'form-group') content_tag('div', html_opts, &block) end # rubocop:disable Style/OptionalBooleanParameter def radio_button_tag(name, value, checked = false, options = {}, &block) content_tag('div', class: 'radio') do content_tag('label') do concat(super) concat(capture(&block)) end end end # rubocop:enable Style/OptionalBooleanParameter def check_box_tag(*, &block) content_tag('div', class: 'checkbox') do content_tag('label') do concat(super) concat(capture(&block)) end end end alias orig_form_tag form_tag def inline_form_tag(url_for_options = {}, options = {}, &block) add_css_class(options, 'form-inline') form_tag(url_for_options, options, &block) end def search_form_tag(filter, url: nil, placeholder: 'Search within these entries') form_tag(url, method: :get) do field_block { search_form_input_tag(filter, placeholder) } end end def search_form_input_tag(filter, placeholder) content_tag('div', class: 'row') do content_tag('div', grouped_search_field( filter, placeholder ), class: 'col-lg-12') end end def search_filter_text_field(filter, placeholder) orig_text_field_tag(:filter, filter, placeholder: placeholder, autocomplete: 'off', class: 'form-control') end def search_button button_tag(type: 'submit') do concat(icon_tag('search')) concat(' Search') end end def grouped_search_field(filter, placeholder) content_tag('div', class: 'input-group') do concat(search_filter_text_field(filter, placeholder)) concat(content_tag('span', search_button, class: 'input-group-btn')) end end def hidden_fields(&block) content_tag('div', style: 'display: none;', &block) end alias orig_text_field_tag text_field_tag def text_field_tag(name, value = nil, opts = {}) add_css_class(opts, 'form-control') super end def text_area_tag(name, content = nil, opts = {}) add_css_class(opts, 'form-control') super end def date_field_tag(name, value = nil, **opts) opts[:class] = "#{opts[:class]} date-picker".strip text_field_tag(name, value, opts) end def select_tag(name, option_tags = nil, **opts) add_css_class(opts, 'form-control') super end def button_tag(content_or_options = nil, options = nil, &block) if content_or_options.is_a?(Hash) content_or_options[:class] ||= 'btn-default' add_css_class(content_or_options, 'btn') super else options ||= {} options[:class] ||= 'btn-default' add_css_class(options, 'btn') super(content_or_options, options, &block) end end def delete_button_tag(url, text: true, **opts) action = text.is_a?(String) ? text : 'Delete' content_tag('div', class: 'btn-group') do concat(delete_dropdown_opener(text && action, **opts)) concat(confirm_delete_dropdown(url, action)) end end def form_for(obj, opts = {}, &block) opts[:builder] = BootstrapFormBuilder super(obj, opts, &block) end # Generates the wrapping code for validating a form. The selector is # passed to jQuery, and must uniquely select the form being validated. # `sym` is the object name when using a `form_for` helper to generate the # form. # # e.g. # <%= # validate_form('#new-test-object', :test_object) do |v| # v.validate_field(:name, ...) # Validate the test_object[name] field # end # %> def validate_form(selector, sym = nil, &block) opts = { type: 'application/vnd.aaf.lipstick.validations+json', 'data-target': selector, class: 'lipstick-validations' } content_tag('script', opts) do validation_json(sym, &block).html_safe end end private def validation_json(sym) v = FormValidationBuilder.new(sym) yield v JSON.generate(v.to_h) end def delete_dropdown_opener(label, **opts) opts = { 'aria-expanded': 'false', 'data-toggle': 'dropdown', type: 'button', 'aria-haspopup': 'true' }.merge(opts) add_css_class(opts, 'btn-small btn-danger dropdown-toggle') button_tag(opts) do concat(icon_tag('trash')) concat(' ') concat(label) end end def confirm_delete_dropdown(url, action) link = link_to("Confirm #{action}", url, class: 'confirm-delete') item = content_tag('li', link) content_tag('ul', item, class: 'dropdown-menu') end def add_css_class(opts, class_name) opts[:class] = "#{opts[:class]} #{class_name}".strip end end end end