BigW Consortium Gitlab

dropdowns_helper.rb 2.98 KB
Newer Older
Phil Hughes committed
1
module DropdownsHelper
Phil Hughes committed
2
  def dropdown_tag(toggle_text, options: {}, &block)
Phil Hughes committed
3
    content_tag :div, class: "dropdown" 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

Phil Hughes committed
23 24
        output << content_tag(:div, class: "dropdown-content") do
          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 = {})
Phil Hughes committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    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
      output = content_tag(:span, toggle_text, class: "dropdown-toggle-text")
      output << icon('chevron-down')
      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
63
        icon('times', class: 'dropdown-menu-close-icon')
Phil Hughes committed
64 65 66 67 68 69
      end

      title_output.html_safe
    end
  end

Phil Hughes committed
70
  def dropdown_filter(placeholder, search_id: nil)
Phil Hughes committed
71
    content_tag :div, class: "dropdown-input" do
72
      filter_output = search_field_tag search_id, nil, class: "dropdown-input-field", placeholder: placeholder, autocomplete: 'off'
73 74
      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
75 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

      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
101
end