Module: Tungsten::ButtonHelper
- Included in:
- FormHelper
- Defined in:
- app/helpers/tungsten/button_helper.rb
Instance Method Summary collapse
- #button(text = nil, options = nil, &block) ⇒ Object
- #button_classes(options, more = nil) ⇒ Object
- #copy_button(target, text = nil, options = {}, &block) ⇒ Object
- #destructive_button(text = nil, options = nil, &block) ⇒ Object
- #destructive_icon_button(icon, options = {}) ⇒ Object
- #destructive_primary_button(text = nil, options = nil, &block) ⇒ Object
- #destructive_primary_icon_button(icon, options = {}) ⇒ Object
- #icon_button(icon, options = {}) ⇒ Object
- #primary_button(text = nil, options = nil, &block) ⇒ Object
- #primary_icon_button(icon, options = {}) ⇒ Object
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 (text = nil, = nil, &block) if text.is_a?(Hash) || block_given? && .nil? = text text = nil end ||= {} tag_name = [:href].present? ? :a : :button [:class] = () [:data] = { toggle: .delete(:toggle), remote: .delete(:remote), 'toggle-class' => .delete(:toggle_class), 'submit-message' => .delete(:submit_message), 'disable-on-submit' => .delete(:disable_on_submit), 'disable-with' => .delete(:disable_with), }.merge( [:data] || {} ) if [:confirm] [:dialog] ||= {} [:dialog][:confirm] = .delete(:confirm) end if [:confirm_if] [:dialog][:if] = .delete(:confirm_if) end (.delete(:dialog) || {}).each do |key, val| if key.to_s == 'confirm' [:data]["dialog-title"] ||= val [:data]["dialog-confirm"] = true else [:data]["dialog-#{key}"] = val end end [:type] = 'submit' if [:to] if tooltip = .delete(:tooltip) [:aria_label] ||= tooltip [:class] << ' has-tooltip' end = { role: 'button', id: .delete(:id), data: .delete(:data), class: .delete(:class), disabled: .delete(:disabled), type: .delete(:type) || 'button', target: .delete(:target), href: .delete(:href), 'aria-label'=> .delete(:aria_label), tabindex: .delete(:tabindex) } = content_tag tag_name, do if [:icon] concat content_tag(:span, class: 'inline-icon') { use_svg(.delete(:icon), height: '1em', fill: 'currentColor', desc: .delete(:desc)) } end concat content_tag(:span, class: 'button-text') { text } if text concat capture(&block).html_safe if block_given? end content_tag(:span, class: 'button-wrapper') { if [:to] [:data] = {} form = 'input', .delete(:to), form.sub(/<input type="submit".+?>/, ).html_safe else 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 (, more=nil) classes = ['button'] classes << more classes << [:class] classes << .delete(:size) classes << 'clear' if .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 (target, text=nil, ={}, &block) if text.is_a? Hash = text text = nil end [:tooltip] ||= "Copy to clipboard" [:class] = (, 'copy-button') [:data] ||= {} [:data][:copy_target] = target if text.nil? ( 'copy', , &block ) else [:icon] = 'copy' if [:icon].nil? ( text, , &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 (text = nil, = nil, &block) if text.is_a?(Hash) = text text = nil end ||= {} [:class] = (, 'destructive') [:method] = 'delete' (text, , &block) end |
#destructive_icon_button(icon, options = {}) ⇒ Object
160 161 162 163 |
# File 'app/helpers/tungsten/button_helper.rb', line 160 def (icon, ={}) [:class] = (, 'destructive') (icon, ) 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 (text = nil, = nil, &block) if text.is_a?(Hash) = text text = nil end ||= {} [:class] = (, 'primary-destructive') [:type] = 'submit' [:method] = 'delete' (text, , &block) end |
#destructive_primary_icon_button(icon, options = {}) ⇒ Object
165 166 167 168 |
# File 'app/helpers/tungsten/button_helper.rb', line 165 def (icon, ={}) [:class] = (, 'primary-destructive') (icon, ) 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, ={}) [:class] = (, 'icon-button') = { title: .delete(:title), width: .delete(:width), height: .delete(:height), color: .delete(:color), fill: .delete(:fill), 'fill-opacity' => .delete(:fill_opacity), stroke: .delete(:stroke), desc: .delete(:desc) } [:scale] = true unless [:width] || [:height] ( ) do concat content_tag(:span, class: 'inline-icon') { use_svg icon, } 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 (text = nil, = nil, &block) if text.is_a?(Hash) = text text = nil end ||= {} [:class] = (, 'primary') [:type] = 'submit' (text, , &block) end |
#primary_icon_button(icon, options = {}) ⇒ Object
170 171 172 173 |
# File 'app/helpers/tungsten/button_helper.rb', line 170 def (icon, ={}) [:class] = (, 'primary') (icon, ) end |