Skip to content

Commit

Permalink
Moved the link dialog partials into view components
Browse files Browse the repository at this point in the history
Try to migrate the link dialog partials into view component to make it easier extendable and lesser repetitive.
  • Loading branch information
sascha-karnatz committed Mar 4, 2024
1 parent 84d7b22 commit 953fe29
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 146 deletions.
38 changes: 38 additions & 0 deletions app/components/alchemy/admin/link_dialog/anchor_tab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

module Alchemy
module Admin
module LinkDialog
class AnchorTab < BaseTab
def title
Alchemy.t("link_overlay_tab_label.anchor")
end

def name
:anchor
end

def fields
[
anchor_select,
title_input
]
end

def message
content_tag("p", Alchemy.t(:anchor_link_headline))
end

private

def anchor_select
label = label_tag("anchor_link", Alchemy.t(:anchor), class: "control-label")
select = select_tag(:anchor_link,
options_for_select([[Alchemy.t("Please choose"), ""]]),
is: "alchemy-select")
content_tag("div", label + select, class: "input select")
end
end
end
end
end
61 changes: 61 additions & 0 deletions app/components/alchemy/admin/link_dialog/base_tab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# frozen_string_literal: true

module Alchemy
module Admin
module LinkDialog
class BaseTab < ViewComponent::Base
include BaseHelper

def title
raise ArgumentError, "The tab needs to have a title"
end

def name
raise ArgumentError, "The tab needs to have a name"
end

def fields
[]
end

def message
nil
end

def call
content = message ? render_message(:info, message) : ""
content += content_tag("div", content_tag("ul"), id: "errors", class: "errors")
content += fields.join("").html_safe + submit_button

form = content_tag("form", content.html_safe, {"data-link-form-type": name})

panel_name = "overlay_tab_#{name}_link"
options = {slot: "nav", panel: panel_name}

content_tag("sl-tab", title, options) +
content_tag("sl-tab-panel", form, name: panel_name)
end

private

def title_input
input_name = "#{name}_link_title"
label = label_tag(input_name, Alchemy.t(:link_title), class: "control-label")
input = text_field_tag input_name, "", class: "link_title"
content_tag("div", label + input, class: "input text")
end

def target_select
select_name = "#{name}_link_target"
label = label_tag(select_name, Alchemy.t("Open Link in"), class: "control-label")
select = select_tag(select_name, options_for_select(Alchemy::Page.link_target_options, @target), class: "link_target")
content_tag("div", label + select, class: "input select")
end

def submit_button
content_tag("div", button_tag(Alchemy.t(:apply)), {class: "submit"})
end
end
end
end
end
39 changes: 39 additions & 0 deletions app/components/alchemy/admin/link_dialog/external_tab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# frozen_string_literal: true

module Alchemy
module Admin
module LinkDialog
class ExternalTab < BaseTab
def title
Alchemy.t("link_overlay_tab_label.external")
end

def name
:external
end

def fields
[
url_input,
title_input,
target_select
]
end

def message
content_tag("h3", Alchemy.t(:enter_external_link)) +
content_tag("p", Alchemy.t(:external_link_notice_1)) +
content_tag("p", Alchemy.t(:external_link_notice_2))
end

private

def url_input
label = label_tag("external_link", "URL", class: "control-label")
input = text_field_tag "external_link", ""
content_tag("div", label + input, class: "input text")
end
end
end
end
end
48 changes: 48 additions & 0 deletions app/components/alchemy/admin/link_dialog/file_tab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

module Alchemy
module Admin
module LinkDialog
class FileTab < BaseTab
delegate :alchemy, to: :helpers

def title
Alchemy.t("link_overlay_tab_label.file")
end

def name
:file
end

def fields
[
attachment_select,
title_input,
target_select
]
end

def message
content_tag("h3", Alchemy.t(:choose_file_to_link))
end

private

def attachments
@_attachments ||= Attachment.all.collect { |f|
[f.name, alchemy.download_attachment_path(id: f.id, name: f.slug)]
}
end

def attachment_select
label = label_tag("file_link", Alchemy.t(:file), class: "control-label")
select = select_tag "file_link",
options_for_select(attachments),
prompt: Alchemy.t("Please choose"),
is: "alchemy-select"
content_tag("div", label + select, class: "input select")
end
end
end
end
end
46 changes: 46 additions & 0 deletions app/components/alchemy/admin/link_dialog/internal_tab.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

module Alchemy
module Admin
module LinkDialog
class InternalTab < BaseTab
def title
Alchemy.t("link_overlay_tab_label.internal")
end

def name
:internal
end

def fields
[
page_select,
dom_id_select,
title_input,
target_select
]
end

def message
content_tag("h3", Alchemy.t(:internal_link_headline)) +
content_tag("p", Alchemy.t(:internal_link_page_elements_explanation))
end

private

def page_select
label = label_tag("internal_link", Alchemy.t(:page), class: "control-label")
input = text_field_tag("internal_link", "", id: "internal_link", class: "alchemy_selectbox full_width")
content_tag("div", label + input, class: "input select")
end

def dom_id_select
label = label_tag("element_anchor", Alchemy.t(:anchor), class: "control-label")
options = {id: "element_anchor", class: "alchemy_selectbox full_width", disabled: true, placeholder: Alchemy.t("Select a page first")}
input = text_field_tag("element_anchor", nil, options)
content_tag("div", label + input, class: "input select")
end
end
end
end
end
3 changes: 0 additions & 3 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,6 @@ def destroy
end

def link
@attachments = Attachment.all.collect { |f|
[f.name, download_attachment_path(id: f.id, name: f.slug)]
}
end

def fold
Expand Down
22 changes: 0 additions & 22 deletions app/views/alchemy/admin/pages/_anchor_link.html.erb

This file was deleted.

31 changes: 0 additions & 31 deletions app/views/alchemy/admin/pages/_external_link.html.erb

This file was deleted.

31 changes: 0 additions & 31 deletions app/views/alchemy/admin/pages/_file_link.html.erb

This file was deleted.

35 changes: 0 additions & 35 deletions app/views/alchemy/admin/pages/_internal_link.html.erb

This file was deleted.

Loading

0 comments on commit 953fe29

Please sign in to comment.