BigW Consortium Gitlab

dropdowns_helper.rb 3.2 KB
Newer Older
Phil Hughes committed
1
module DropdownsHelper
Phil Hughes committed
2
  def dropdown_tag(toggle_text, options: {}, &block)
3
    content_tag :div, class: "dropdown #{options[:wrapper_class] if options.has_key?(:wrapper_class)}" do
Phil Hughes committed
4
      data_attr = { toggle: "dropdown" }
5

Phil Hughes committed
6 7
      if options.has_key?(:data)
        data_attr = options[:data].merge(data_attr)
Phil Hughes committed
8 9
      end

Phil Hughes committed
10
      dropdown_output = dropdown_toggle(toggle_text, data_attr, options)
Phil Hughes committed
11

Phil Hughes committed
12 13
      dropdown_output << content_tag(:div, class: "dropdown-menu dropdown-select #{options[:dropdown_class] if options.has_key?(:dropdown_class)}") do
        output = ""
Phil Hughes committed
14

Phil Hughes committed
15 16
        if options.has_key?(:title)
          output << dropdown_title(options[:title])
Phil Hughes committed
17 18
        end

Phil Hughes committed
19 20
        if options.has_key?(:filter)
          output << dropdown_filter(options[:placeholder])
Phil Hughes committed
21 22
        end

23
        output << content_tag(:div, class: "dropdown-content #{options[:content_class] if options.has_key?(:content_class)}") do
Phil Hughes committed
24
          capture(&block) if block && !options.has_key?(:footer_content)
25 26
        end

27
        if block && options[:footer_content]
Phil Hughes committed
28
          output << content_tag(:div, class: "dropdown-footer") do
29 30
            capture(&block)
          end
Phil Hughes committed
31 32
        end

Phil Hughes committed
33
        output << dropdown_loading
Phil Hughes committed
34 35 36 37 38 39 40

        output.html_safe
      end

      dropdown_output.html_safe
    end
  end
Phil Hughes committed
41

42
  def dropdown_toggle(toggle_text, data_attr, options = {})
43
    default_label = data_attr[:default_label]
Phil Hughes committed
44
    content_tag(:button, class: "dropdown-menu-toggle #{options[:toggle_class] if options.has_key?(:toggle_class)}", id: (options[:id] if options.has_key?(:id)), type: "button", data: data_attr) do
45
      output = content_tag(:span, toggle_text, class: "dropdown-toggle-text #{'is-default' if toggle_text == default_label}")
46
      output << icon('chevron-down')
Phil Hughes committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
      output.html_safe
    end
  end

  def dropdown_title(title, back: false)
    content_tag :div, class: "dropdown-title" do
      title_output = ""

      if back
        title_output << content_tag(:button, class: "dropdown-title-button dropdown-menu-back", aria: { label: "Go back" }, type: "button") do
          icon('arrow-left')
        end
      end

      title_output << content_tag(:span, title)

      title_output << content_tag(:button, class: "dropdown-title-button dropdown-menu-close", aria: { label: "Close" }, type: "button") do
64
        icon('times', class: 'dropdown-menu-close-icon')
Phil Hughes committed
65 66 67 68 69 70
      end

      title_output.html_safe
    end
  end

Phil Hughes committed
71
  def dropdown_filter(placeholder, search_id: nil)
Phil Hughes committed
72
    content_tag :div, class: "dropdown-input" do
73
      filter_output = search_field_tag search_id, nil, class: "dropdown-input-field", placeholder: placeholder, autocomplete: 'off'
74 75
      filter_output << icon('search', class: "dropdown-input-search")
      filter_output << icon('times', class: "dropdown-input-clear js-dropdown-input-clear", role: "button")
Phil Hughes committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101

      filter_output.html_safe
    end
  end

  def dropdown_content(&block)
    content_tag(:div, class: "dropdown-content") do
      if block
        capture(&block)
      end
    end
  end

  def dropdown_footer(&block)
    content_tag(:div, class: "dropdown-footer") do
      if block
        capture(&block)
      end
    end
  end

  def dropdown_loading
    content_tag :div, class: "dropdown-loading" do
      icon('spinner spin')
    end
  end
Phil Hughes committed
102
end