Module: Tungsten::ButtonHelper

Included in:
FormHelper
Defined in:
app/helpers/tungsten/button_helper.rb

Instance Method Summary collapse

Instance Method Details

#button(text = nil, options = nil, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/helpers/tungsten/button_helper.rb', line 3

def button(text = nil, options = nil, &block)
  if text.is_a?(Hash) || block_given? && options.nil?
    options = text 
    text = nil
  end
  options ||= {}
  tag_name = options[:href].present? ? :a : :button

  options[:class] = button_classes(options)

  options[:data] = {
    toggle: options.delete(:toggle),
    remote: options.delete(:remote),
    'toggle-class' => options.delete(:toggle_class),
    'submit-message' => options.delete(:submit_message),
    'disable-on-submit' => options.delete(:disable_on_submit),
    'disable-with' => options.delete(:disable_with),
  }.merge( options[:data] || {} )

  if options[:confirm]
    options[:dialog] ||= {}
    options[:dialog][:confirm] = options.delete(:confirm)
  end

  if options[:confirm_if]
    options[:dialog][:if] = options.delete(:confirm_if)
  end

  (options.delete(:dialog) || {}).each do |key, val|
    if key.to_s == 'confirm'
      options[:data]["dialog-title"] ||= val
      options[:data]["dialog-confirm"] = true
    else
      options[:data]["dialog-#{key}"] = val
    end
  end

  options[:type] = 'submit' if options[:to]

  if tooltip = options.delete(:tooltip)
    options[:aria_label] ||= tooltip
    options[:class] << ' has-tooltip'
  end

  button_options = {
    role: 'button',
    id: options.delete(:id),
    data: options.delete(:data),
    class: options.delete(:class),
    disabled: options.delete(:disabled),
    type: options.delete(:type) || 'button',
    target: options.delete(:target),
    href: options.delete(:href),
    'aria-label'=> options.delete(:aria_label),
    tabindex: options.delete(:tabindex)
  }

  button =  tag_name, button_options do
    if options[:icon]
      concat (:span, class: 'inline-icon') {
        use_svg(options.delete(:icon), height: '1em', fill: 'currentColor', desc: options.delete(:desc))
      }
    end
    concat (:span, class: 'button-text') { text } if text
    concat capture(&block).html_safe if block_given?
  end

  (:span, class: 'button-wrapper') {
    if options[:to]
      options[:data] = {}
    
      form = button_to 'input', options.delete(:to), options
      form.sub(/<input type="submit".+?>/, button).html_safe
    else
      button
    end
  }
end

#button_classes(options, more = nil) ⇒ Object



175
176
177
178
179
180
181
182
# File 'app/helpers/tungsten/button_helper.rb', line 175

def button_classes(options, more=nil)
  classes = ['button']
  classes << more
  classes << options[:class]
  classes << options.delete(:size)
  classes << 'clear' if options.delete(:clear)
  classes.flatten.compact.join(' ').split(' ').uniq.join(' ').strip
end

#copy_button(target, text = nil, options = {}, &block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/helpers/tungsten/button_helper.rb', line 116

def copy_button(target, text=nil, options={}, &block)

  if text.is_a? Hash
    options = text
    text = nil
  end

  options[:tooltip] ||= "Copy to clipboard"
  options[:class] = button_classes(options, 'copy-button')

  options[:data] ||= {}
  options[:data][:copy_target] = target

  if text.nil?
    icon_button( 'copy', options, &block )
  else
    options[:icon] = 'copy' if options[:icon].nil?
    button( text, options, &block )
  end
end

#destructive_button(text = nil, options = nil, &block) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/tungsten/button_helper.rb', line 93

def destructive_button(text = nil, options = nil, &block)
  if text.is_a?(Hash)
    options = text 
    text = nil
  end
  options ||= {}
  options[:class] = button_classes(options, 'destructive')
  options[:method] = 'delete'
  button(text, options, &block)
end

#destructive_icon_button(icon, options = {}) ⇒ Object



160
161
162
163
# File 'app/helpers/tungsten/button_helper.rb', line 160

def destructive_icon_button(icon, options={})
  options[:class] = button_classes(options, 'destructive')
  icon_button(icon, options)
end

#destructive_primary_button(text = nil, options = nil, &block) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/tungsten/button_helper.rb', line 104

def destructive_primary_button(text = nil, options = nil, &block)
  if text.is_a?(Hash)
    options = text 
    text = nil
  end
  options ||= {}
  options[:class] = button_classes(options, 'primary-destructive')
  options[:type] = 'submit'
  options[:method] = 'delete'
  button(text, options, &block)
end

#destructive_primary_icon_button(icon, options = {}) ⇒ Object



165
166
167
168
# File 'app/helpers/tungsten/button_helper.rb', line 165

def destructive_primary_icon_button(icon, options={})
  options[:class] = button_classes(options, 'primary-destructive')
  icon_button(icon, options)
end

#icon_button(icon, options = {}) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'app/helpers/tungsten/button_helper.rb', line 137

def icon_button(icon, options={})
  options[:class] = button_classes(options, 'icon-button')

  icon_options = {
    title: options.delete(:title),
    width: options.delete(:width),
    height: options.delete(:height),
    color: options.delete(:color),
    fill: options.delete(:fill),
    'fill-opacity' => options.delete(:fill_opacity),
    stroke: options.delete(:stroke),
    desc: options.delete(:desc)
  }

  icon_options[:scale] = true unless icon_options[:width] || icon_options[:height]

  button( options ) do
    concat (:span, class: 'inline-icon') {
      use_svg icon, icon_options
    }
  end
end

#primary_button(text = nil, options = nil, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'app/helpers/tungsten/button_helper.rb', line 82

def primary_button(text = nil, options = nil, &block)
  if text.is_a?(Hash)
    options = text 
    text = nil
  end
  options ||= {}
  options[:class] = button_classes(options, 'primary')
  options[:type] = 'submit'
  button(text, options, &block)
end

#primary_icon_button(icon, options = {}) ⇒ Object



170
171
172
173
# File 'app/helpers/tungsten/button_helper.rb', line 170

def primary_icon_button(icon, options={})
  options[:class] = button_classes(options, 'primary')
  icon_button(icon, options)
end