Disable links instead of hiding

Closes #49
This commit is contained in:
cryptogopher 2025-01-03 14:41:18 +01:00
parent 80f05ba45f
commit af152c5e8b
4 changed files with 50 additions and 33 deletions

View File

@ -138,29 +138,28 @@ module ApplicationHelper
end.join.html_safe
end
[:button_to, :link_to, :link_to_unless_current].each do |method_name|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def image_#{method_name}(name, image = nil, options = nil, html_options = {}, &block)
name = name.to_s
name = svg_tag("pictograms/\#{image}") + name if image
html_options[:class] = class_names(
html_options[:class],
'button',
dangerous: html_options[:method] == :delete
)
if html_options[:onclick]&.is_a? Hash
html_options[:onclick] = "return confirm('\#{html_options[:onclick][:confirm]}');"
def image_button_to(name, image = nil, options = nil, html_options = {})
name, html_options = link_or_button_options(:button, name, image, html_options)
button_to name, options, html_options
end
if __method__.start_with?('image_link_to') &&
!(html_options[:onclick] || html_options.dig(:data, :turbo_stream))
name = name + '...'
def image_link_to(name, image = nil, options = nil, html_options = {})
name, html_options = link_or_button_options(:link, name, image, html_options)
link_to name, options, html_options
end
send :#{method_name}, name, options, html_options, &block
DISABLED_ATTRIBUTES = {disabled: true, aria: {disabled: true}, tabindex: -1}
def image_button_to_if(condition, name, image = nil, options = nil, html_options = {})
name, html_options = link_or_button_options(:button, name, image, html_options)
html_options = html_options.deep_merge DISABLED_ATTRIBUTES unless condition
button_to name, options, html_options
end
RUBY_EVAL
def image_link_to_unless_current(name, image = nil, options = nil, html_options = {})
name, html_options = link_or_button_options(:link, name, image, html_options)
html_options = html_options.deep_merge DISABLED_ATTRIBUTES if current_page?(options)
link_to name, options, html_options
end
def render_errors(record)
@ -188,7 +187,25 @@ module ApplicationHelper
"Turbo.renderStreamMessage('#{j(render partial: partial, locals: locals)}'); return false;"
end
def disabled_attributes(disabled)
disabled ? {disabled: true, aria: {disabled: true}, tabindex: -1} : {}
private
def link_or_button_options(type, name, image = nil, html_options)
name = (image ? svg_tag("pictograms/#{image}") : '') + name.to_s
html_options[:class] = class_names(
html_options[:class],
'button',
dangerous: html_options[:method] == :delete
)
if html_options[:onclick]&.is_a? Hash
html_options[:onclick] = "return confirm('\#{html_options[:onclick][:confirm]}');"
end
if type == :link && !(html_options[:onclick] || html_options.dig(:data, :turbo_stream))
name += '...'
end
[name, html_options]
end
end

View File

@ -7,17 +7,17 @@
<td class="actions">
<% unless unit.portable.nil? %>
<% if unit.default? %>
<%= image_button_to t('.import'), 'download-outline', import_default_unit_path(unit),
disabled_attributes(!unit.portable?) %>
<%= image_button_to_if unit.portable?, t('.import'), 'download-outline',
import_default_unit_path(unit) %>
<% end %>
<% if current_user.at_least(:admin) %>
<% if unit.default? %>
<%= image_button_to t('.delete'), 'delete-outline', default_unit_path(unit),
method: :delete, **disabled_attributes(!unit.movable?) %>
<%= image_button_to_if unit.movable?, t('.delete'), 'delete-outline',
default_unit_path(unit), method: :delete %>
<% else %>
<%= image_button_to t('.export'), 'upload-outline', export_default_unit_path(unit),
disabled_attributes(!unit.portable?) %>
<%= image_button_to_if unit.portable?, t('.export'), 'upload-outline',
export_default_unit_path(unit) %>
<% end %>
<% end %>
<% end %>

View File

@ -29,7 +29,7 @@
class: "extendedright" %>
<% if user_signed_in? %>
<%= image_link_to_unless_current(current_user, "account-wrench-outline",
edit_user_registration_path) {} %>
edit_user_registration_path) %>
<% if current_user_disguised? %>
<%= image_link_to t(".revert"), "incognito-off", revert_users_path %>
<% else %>
@ -37,9 +37,9 @@
method: :delete, data: {turbo: false} %>
<% end %>
<% else %>
<%= image_link_to_unless_current(t(:sign_in), "login", new_user_session_path) {} %>
<%= image_link_to_unless_current(t(:sign_in), "login", new_user_session_path) %>
<%= image_link_to_unless_current(t(:register), "account-plus-outline",
new_user_registration_path) {} %>
new_user_registration_path) %>
<% end %>
</header>

View File

@ -17,8 +17,8 @@
id: dom_id(unit, :new, :link), onclick: 'this.blur();', data: {turbo_stream: true} %>
<% end %>
<%= image_button_to t('.destroy'), 'delete-outline', unit_path(unit),
method: :delete, **disabled_attributes(!unit.movable?) %>
<%= image_button_to_if unit.movable?, t('.destroy'), 'delete-outline', unit_path(unit),
method: :delete %>
</td>
<% if unit.movable? %>
<td class="handle" draggable="true">&#x283F</td>