katalyst-koi 4.9.4 → 4.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/builds/koi/admin.css.map +1 -1
- data/app/assets/config/koi.js +3 -0
- data/lib/koi/engine.rb +59 -13
- data/lib/koi/form/builder.rb +76 -0
- data/lib/koi/form/elements/document.rb +47 -0
- data/lib/koi/form/elements/file_element.rb +128 -0
- data/lib/koi/form/elements/image.rb +44 -0
- data/lib/koi/form/govuk_extensions.rb +73 -0
- data/lib/koi/form_builder.rb +1 -65
- data/lib/koi/menu.rb +0 -2
- data/lib/koi/middleware/url_redirect.rb +26 -24
- data/lib/koi.rb +7 -24
- metadata +7 -10
- data/config/initializers/content.rb +0 -4
- data/config/initializers/navigation.rb +0 -4
- data/config/initializers/pagy.rb +0 -3
- data/lib/govuk_design_system_formbuilder/concerns/file_element.rb +0 -122
- data/lib/govuk_design_system_formbuilder/elements/document.rb +0 -69
- data/lib/govuk_design_system_formbuilder/elements/image.rb +0 -99
- data/lib/koi/collection.rb +0 -11
- data/lib/koi/extensions.rb +0 -8
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Koi
|
4
|
+
module Form
|
5
|
+
module Elements
|
6
|
+
class Image < GOVUKDesignSystemFormBuilder::Base
|
7
|
+
include FileElement
|
8
|
+
|
9
|
+
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, mime_types:,
|
10
|
+
**kwargs, &)
|
11
|
+
super(builder, object_name, attribute_name, &)
|
12
|
+
|
13
|
+
@mime_types = mime_types
|
14
|
+
@label = label
|
15
|
+
@caption = caption
|
16
|
+
@hint = hint
|
17
|
+
@html_attributes = kwargs.merge(file_input_options)
|
18
|
+
@form_group = form_group
|
19
|
+
end
|
20
|
+
|
21
|
+
def preview
|
22
|
+
options = {}
|
23
|
+
add_option(options, :data, "#{stimulus_controller}_target", "preview")
|
24
|
+
add_option(options, :class, "preview-image")
|
25
|
+
add_option(options, :class, "hidden") unless preview?
|
26
|
+
|
27
|
+
tag.div(**options) do
|
28
|
+
tag.img(src: preview_url, class: "image-thumbnail") + destroy_element_trigger
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def stimulus_controller
|
35
|
+
"image-field"
|
36
|
+
end
|
37
|
+
|
38
|
+
def form_group_class
|
39
|
+
"govuk-image-field"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Koi
|
4
|
+
module Form
|
5
|
+
module GOVUKExtensions
|
6
|
+
# Generates a +div+ element with an +input+ with +type=file+ with a label, optional hint.
|
7
|
+
#
|
8
|
+
# @example A upload field with label as a proc
|
9
|
+
# = f.govuk_document_field :data, label: -> { tag.h3('Upload your document') }
|
10
|
+
#
|
11
|
+
def govuk_document_field(attribute_name,
|
12
|
+
label: {},
|
13
|
+
caption: {},
|
14
|
+
hint: {},
|
15
|
+
form_group: {},
|
16
|
+
mime_types: Koi.config.document_mime_types,
|
17
|
+
**,
|
18
|
+
&)
|
19
|
+
Elements::Document.new(
|
20
|
+
self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
|
21
|
+
).html
|
22
|
+
end
|
23
|
+
|
24
|
+
# Generates a +div+ element to preview uploaded images and an +input+ with +type=file+ with a label, optional hint
|
25
|
+
#
|
26
|
+
# @param attribute_name [Symbol] The name of the attribute
|
27
|
+
# @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+ is
|
28
|
+
# supplied the hint will be wrapped in a +div+ instead of a +span+
|
29
|
+
# @option hint text [String] the hint text
|
30
|
+
# @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
|
31
|
+
# @param label [Hash,Proc] configures or sets the associated label content
|
32
|
+
# @option label text [String] the label text
|
33
|
+
# @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
|
34
|
+
# @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
|
35
|
+
# @option label hidden [Boolean] control the visibility of the label. Hidden labels will still be read by screen
|
36
|
+
# readers
|
37
|
+
# @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
|
38
|
+
# @param caption [Hash] configures or sets the caption content which is inserted above the label
|
39
|
+
# @option caption text [String] the caption text
|
40
|
+
# @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
|
41
|
+
# @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
|
42
|
+
# @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element.
|
43
|
+
# @param form_group [Hash] configures the form group
|
44
|
+
# @option form_group classes [Array,String] sets the form group's classes
|
45
|
+
# @option form_group kwargs [Hash] additional attributes added to the form group
|
46
|
+
# @param & [Block] arbitrary HTML that will be rendered between the hint and the input
|
47
|
+
# @return [ActiveSupport::SafeBuffer] HTML output
|
48
|
+
#
|
49
|
+
# @example An image field with injected content
|
50
|
+
# = f.govuk_image_field :incident_image,
|
51
|
+
# label: { text: 'Attach a picture of the incident' } do
|
52
|
+
#
|
53
|
+
# p.govuk-inset-text
|
54
|
+
# | If you don't know exactly leave this section blank
|
55
|
+
#
|
56
|
+
# @example A image upload field with label as a proc
|
57
|
+
# = f.govuk_image_field :image, label: -> { tag.h3('Upload your image') }
|
58
|
+
#
|
59
|
+
def govuk_image_field(attribute_name,
|
60
|
+
label: {},
|
61
|
+
caption: {},
|
62
|
+
hint: {},
|
63
|
+
form_group: {},
|
64
|
+
mime_types: Koi.config.image_mime_types,
|
65
|
+
**,
|
66
|
+
&)
|
67
|
+
Elements::Image.new(
|
68
|
+
self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
|
69
|
+
).html
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/koi/form_builder.rb
CHANGED
@@ -2,71 +2,7 @@
|
|
2
2
|
|
3
3
|
module Koi
|
4
4
|
class FormBuilder < ActionView::Helpers::FormBuilder
|
5
|
-
delegate_missing_to :@template
|
6
|
-
|
7
5
|
include GOVUKDesignSystemFormBuilder::Builder
|
8
|
-
|
9
|
-
# Generates a submit button for saving admin resources.
|
10
|
-
def admin_save(text = "Save", name: :commit, value: :save, class: "button button--primary", **)
|
11
|
-
button(text, name:, value:, class:, **)
|
12
|
-
end
|
13
|
-
|
14
|
-
# Generates a delete link formatted as a button that will perform a turbo
|
15
|
-
# delete with a confirmation.
|
16
|
-
def admin_delete(text = "Delete", url: nil, confirm: "Are you sure?", data: {}, **)
|
17
|
-
return unless object.persisted?
|
18
|
-
|
19
|
-
link_to(text, url || url_for(action: :destroy),
|
20
|
-
class: "button button--secondary",
|
21
|
-
data: data.reverse_merge(turbo_method: :delete, turbo_confirm: confirm),
|
22
|
-
**)
|
23
|
-
end
|
24
|
-
|
25
|
-
# Generates an archive link formatted as a button that will perform a turbo
|
26
|
-
# delete with a confirmation.
|
27
|
-
def admin_archive(text = "Archive", **)
|
28
|
-
admin_delete(text, **)
|
29
|
-
end
|
30
|
-
|
31
|
-
# Generates a discard changes link formatted as a text button that navigates
|
32
|
-
# the user back to the previous page.
|
33
|
-
def admin_discard(text = "Discard", url: :back, **)
|
34
|
-
link_to(text, url, class: "button button--text", **)
|
35
|
-
end
|
36
|
-
|
37
|
-
# @api internal
|
38
|
-
# @see GOVUKDesignSystemFormBuilder::Builder#govuk_document_field
|
39
|
-
def govuk_document_field(attribute_name, hint: {}, **, &)
|
40
|
-
if hint.is_a?(Hash)
|
41
|
-
max_size = hint.fetch(:max_size, Koi.config.document_size_limit)
|
42
|
-
hint[:text] ||= t("helpers.hint.default.document", max_size: @template.number_to_human_size(max_size))
|
43
|
-
end
|
44
|
-
|
45
|
-
super
|
46
|
-
end
|
47
|
-
|
48
|
-
# @api internal
|
49
|
-
# @see GOVUKDesignSystemFormBuilder::Builder#govuk_image_field
|
50
|
-
def govuk_image_field(attribute_name, hint: {}, **, &)
|
51
|
-
if hint.is_a?(Hash)
|
52
|
-
max_size = hint.fetch(:max_size, Koi.config.image_size_limit)
|
53
|
-
hint[:text] ||= t("helpers.hint.default.document", max_size: @template.number_to_human_size(max_size))
|
54
|
-
end
|
55
|
-
|
56
|
-
super
|
57
|
-
end
|
58
|
-
|
59
|
-
# Use content editor trix setup by default.
|
60
|
-
#
|
61
|
-
# @api internal
|
62
|
-
# @see GOVUKDesignSystemFormBuilder::Builder#govuk_rich_text_area
|
63
|
-
def govuk_rich_text_area(attribute_name, data: {}, **, &)
|
64
|
-
data = data.reverse_merge(
|
65
|
-
direct_upload_url: @template.katalyst_content.direct_uploads_url,
|
66
|
-
controller: "content--editor--trix",
|
67
|
-
action: "trix-initialize->content--editor--trix#trixInitialize",
|
68
|
-
)
|
69
|
-
super
|
70
|
-
end
|
6
|
+
include Koi::Form::Builder
|
71
7
|
end
|
72
8
|
end
|
data/lib/koi/menu.rb
CHANGED
@@ -1,38 +1,40 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Koi
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
module Middleware
|
5
|
+
class UrlRedirect
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
10
|
+
def call(env)
|
11
|
+
status, headers, body = @app.call(env)
|
11
12
|
|
12
|
-
|
13
|
+
current_path = env["REQUEST_URI"]
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
if status.to_i == 404 && (redirect = UrlRewrite.active.find_by(from: current_path))
|
16
|
+
request = ActionDispatch::Request.new(env)
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
# Close the body as we will not use it for a 301 redirect
|
19
|
+
body.close
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
# Return a redirect response
|
22
|
+
[redirect.status_code, { "Location" => new_location(current_path, redirect.to, request) },
|
23
|
+
["#{redirect.from} moved. The document has moved to #{redirect.to}"]]
|
24
|
+
else
|
25
|
+
# Not a 404 or no redirect found, just send the response as is
|
26
|
+
[status, headers, body]
|
27
|
+
end
|
26
28
|
end
|
27
|
-
end
|
28
29
|
|
29
|
-
|
30
|
+
private
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
def new_location(current_path, new_path, request)
|
33
|
+
if %r{\Ahttps{0,1}://}.match?(new_path)
|
34
|
+
new_path
|
35
|
+
else
|
36
|
+
request.original_url.gsub(current_path, new_path)
|
37
|
+
end
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
data/lib/koi.rb
CHANGED
@@ -1,32 +1,13 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "
|
4
|
-
require "
|
5
|
-
require "govuk_design_system_formbuilder/concerns/file_element"
|
6
|
-
require "govuk_design_system_formbuilder/elements/document"
|
7
|
-
require "govuk_design_system_formbuilder/elements/image"
|
8
|
-
require "katalyst/tables"
|
9
|
-
require "pagy"
|
10
|
-
require "stimulus-rails"
|
11
|
-
require "turbo-rails"
|
12
|
-
require "webauthn"
|
3
|
+
require "active_support"
|
4
|
+
require "active_support/rails"
|
13
5
|
|
14
|
-
|
15
|
-
|
16
|
-
require "koi/caching"
|
17
|
-
require "koi/collection"
|
18
|
-
require "koi/config"
|
19
|
-
require "koi/engine"
|
20
|
-
require "koi/extensions"
|
21
|
-
require "koi/release"
|
22
|
-
|
23
|
-
require "katalyst/content"
|
24
|
-
require "katalyst/kpop"
|
25
|
-
require "katalyst/navigation"
|
6
|
+
module Koi
|
7
|
+
extend ActiveSupport::Autoload
|
26
8
|
|
27
|
-
|
9
|
+
autoload :Config
|
28
10
|
|
29
|
-
module Koi
|
30
11
|
extend self
|
31
12
|
|
32
13
|
def config
|
@@ -37,3 +18,5 @@ module Koi
|
|
37
18
|
yield config
|
38
19
|
end
|
39
20
|
end
|
21
|
+
|
22
|
+
require "koi/engine"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: katalyst-koi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.
|
4
|
+
version: 4.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Katalyst Interactive
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-07-
|
11
|
+
date: 2024-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -411,11 +411,8 @@ files:
|
|
411
411
|
- app/views/layouts/koi/frame.html.erb
|
412
412
|
- app/views/layouts/koi/login.html.erb
|
413
413
|
- config/importmap.rb
|
414
|
-
- config/initializers/content.rb
|
415
414
|
- config/initializers/extensions.rb
|
416
415
|
- config/initializers/flipper.rb
|
417
|
-
- config/initializers/navigation.rb
|
418
|
-
- config/initializers/pagy.rb
|
419
416
|
- config/locales/koi.en.yml
|
420
417
|
- config/locales/pagy.en.yml
|
421
418
|
- config/routes.rb
|
@@ -445,18 +442,18 @@ files:
|
|
445
442
|
- lib/generators/koi/admin_views/templates/new.html.erb.tt
|
446
443
|
- lib/generators/koi/admin_views/templates/show.html.erb.tt
|
447
444
|
- lib/generators/koi/helpers/admin_generator_attributes.rb
|
448
|
-
- lib/govuk_design_system_formbuilder/concerns/file_element.rb
|
449
|
-
- lib/govuk_design_system_formbuilder/elements/document.rb
|
450
|
-
- lib/govuk_design_system_formbuilder/elements/image.rb
|
451
445
|
- lib/katalyst/koi.rb
|
452
446
|
- lib/koi.rb
|
453
447
|
- lib/koi/caching.rb
|
454
|
-
- lib/koi/collection.rb
|
455
448
|
- lib/koi/collection/type/archivable.rb
|
456
449
|
- lib/koi/config.rb
|
457
450
|
- lib/koi/engine.rb
|
458
|
-
- lib/koi/extensions.rb
|
459
451
|
- lib/koi/extensions/object_rendering.rb
|
452
|
+
- lib/koi/form/builder.rb
|
453
|
+
- lib/koi/form/elements/document.rb
|
454
|
+
- lib/koi/form/elements/file_element.rb
|
455
|
+
- lib/koi/form/elements/image.rb
|
456
|
+
- lib/koi/form/govuk_extensions.rb
|
460
457
|
- lib/koi/form_builder.rb
|
461
458
|
- lib/koi/menu.rb
|
462
459
|
- lib/koi/menu/builder.rb
|
data/config/initializers/pagy.rb
DELETED
@@ -1,122 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "govuk_design_system_formbuilder"
|
4
|
-
|
5
|
-
module GOVUKDesignSystemFormBuilder
|
6
|
-
module Elements
|
7
|
-
module FileElement
|
8
|
-
extend ActiveSupport::Concern
|
9
|
-
|
10
|
-
using PrefixableArray
|
11
|
-
|
12
|
-
include Traits::Error
|
13
|
-
include Traits::Hint
|
14
|
-
include Traits::Label
|
15
|
-
include Traits::Supplemental
|
16
|
-
include Traits::HTMLAttributes
|
17
|
-
include Traits::HTMLClasses
|
18
|
-
include ActionDispatch::Routing::RouteSet::MountedHelpers
|
19
|
-
|
20
|
-
def html
|
21
|
-
Containers::FormGroup.new(*bound, **default_form_group_options(**@form_group)).html do
|
22
|
-
safe_join([label_element, preview, hint_element, error_element, file, destroy_element, supplemental_content])
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
protected
|
27
|
-
|
28
|
-
def stimulus_controller_actions
|
29
|
-
<<~ACTIONS.gsub(/\s+/, " ").freeze
|
30
|
-
dragover->#{stimulus_controller}#dragover
|
31
|
-
dragenter->#{stimulus_controller}#dragenter
|
32
|
-
dragleave->#{stimulus_controller}#dragleave
|
33
|
-
drop->#{stimulus_controller}#drop
|
34
|
-
ACTIONS
|
35
|
-
end
|
36
|
-
|
37
|
-
def stimulus_controller
|
38
|
-
nil
|
39
|
-
end
|
40
|
-
|
41
|
-
def file
|
42
|
-
previous_input = @builder.hidden_field(@attribute_name, id: nil, value: value.signed_id) if attached?
|
43
|
-
file_input = @builder.file_field(@attribute_name, attributes(@html_attributes))
|
44
|
-
|
45
|
-
safe_join([previous_input, file_input])
|
46
|
-
end
|
47
|
-
|
48
|
-
def destroy_element
|
49
|
-
return if @html_attributes[:optional].blank?
|
50
|
-
|
51
|
-
@builder.fields_for(:"#{@attribute_name}_attachment") do |form|
|
52
|
-
form.hidden_field :_destroy, value: false, data: { "#{stimulus_controller}_target" => "destroy" }
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
def destroy_element_trigger
|
57
|
-
return if @html_attributes[:optional].blank?
|
58
|
-
|
59
|
-
content_tag(:button, "", class: "file-destroy", data: { action: "#{stimulus_controller}#setDestroy" })
|
60
|
-
end
|
61
|
-
|
62
|
-
def preview
|
63
|
-
""
|
64
|
-
end
|
65
|
-
|
66
|
-
def preview_url
|
67
|
-
preview? ? main_app.url_for(value) : ""
|
68
|
-
end
|
69
|
-
|
70
|
-
def preview?
|
71
|
-
attached?
|
72
|
-
end
|
73
|
-
|
74
|
-
def attached?
|
75
|
-
value&.attached? && value.blob.persisted?
|
76
|
-
end
|
77
|
-
|
78
|
-
def value
|
79
|
-
@builder.object.send(@attribute_name)
|
80
|
-
end
|
81
|
-
|
82
|
-
def file_input_options
|
83
|
-
default_file_input_options = options
|
84
|
-
|
85
|
-
add_option(default_file_input_options, :accept, @mime_types.join(","))
|
86
|
-
add_option(default_file_input_options, :data, :action, "change->#{stimulus_controller}#onUpload")
|
87
|
-
|
88
|
-
default_file_input_options
|
89
|
-
end
|
90
|
-
|
91
|
-
def options
|
92
|
-
{
|
93
|
-
id: field_id(link_errors: true),
|
94
|
-
class: classes,
|
95
|
-
aria: { describedby: combine_references(hint_id, error_id, supplemental_id) },
|
96
|
-
}
|
97
|
-
end
|
98
|
-
|
99
|
-
def classes
|
100
|
-
build_classes(%(file-upload), %(file-upload--error) => has_errors?).prefix(brand)
|
101
|
-
end
|
102
|
-
|
103
|
-
def default_form_group_options(**form_group_options)
|
104
|
-
add_option(form_group_options, :class, "govuk-form-group #{form_group_class}")
|
105
|
-
add_option(form_group_options, :data, :controller, stimulus_controller)
|
106
|
-
add_option(form_group_options, :data, :action, stimulus_controller_actions)
|
107
|
-
add_option(form_group_options, :data, :"#{stimulus_controller}_mime_types_value",
|
108
|
-
@mime_types.to_json)
|
109
|
-
|
110
|
-
form_group_options
|
111
|
-
end
|
112
|
-
|
113
|
-
def add_option(options, key, *path)
|
114
|
-
if path.length > 1
|
115
|
-
add_option(options[key] ||= {}, *path)
|
116
|
-
else
|
117
|
-
options[key] = [options[key], *path].compact.join(" ")
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
end
|
@@ -1,69 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "govuk_design_system_formbuilder"
|
4
|
-
|
5
|
-
module GOVUKDesignSystemFormBuilder
|
6
|
-
module Elements
|
7
|
-
class Document < Base
|
8
|
-
include FileElement
|
9
|
-
|
10
|
-
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, mime_types:,
|
11
|
-
**kwargs, &)
|
12
|
-
super(builder, object_name, attribute_name, &)
|
13
|
-
|
14
|
-
@mime_types = mime_types
|
15
|
-
@label = label
|
16
|
-
@caption = caption
|
17
|
-
@hint = hint
|
18
|
-
@html_attributes = kwargs.merge(file_input_options)
|
19
|
-
@form_group = form_group
|
20
|
-
end
|
21
|
-
|
22
|
-
def preview
|
23
|
-
options = {}
|
24
|
-
add_option(options, :data, "#{stimulus_controller}_target", "preview")
|
25
|
-
add_option(options, :class, "preview-file")
|
26
|
-
add_option(options, :class, "hidden") unless preview?
|
27
|
-
|
28
|
-
tag.div(**options) do
|
29
|
-
filename = @builder.object.send(@attribute_name).filename.to_s
|
30
|
-
tag.p(filename, class: "preview-filename") + destroy_element_trigger
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def stimulus_controller
|
37
|
-
"document-field"
|
38
|
-
end
|
39
|
-
|
40
|
-
def form_group_class
|
41
|
-
"govuk-document-field"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
module GOVUKDesignSystemFormBuilder
|
48
|
-
module Builder
|
49
|
-
delegate :config, to: GOVUKDesignSystemFormBuilder
|
50
|
-
|
51
|
-
# Generates a +div+ element with an +input+ with +type=file+ with a label, optional hint.
|
52
|
-
#
|
53
|
-
# @example A upload field with label as a proc
|
54
|
-
# = f.govuk_document_field :data, label: -> { tag.h3('Upload your document') }
|
55
|
-
#
|
56
|
-
def govuk_document_field(attribute_name,
|
57
|
-
label: {},
|
58
|
-
caption: {},
|
59
|
-
hint: {},
|
60
|
-
form_group: {},
|
61
|
-
mime_types: Koi.config.document_mime_types,
|
62
|
-
**,
|
63
|
-
&)
|
64
|
-
Elements::Document.new(
|
65
|
-
self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
|
66
|
-
).html
|
67
|
-
end
|
68
|
-
end
|
69
|
-
end
|
@@ -1,99 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "govuk_design_system_formbuilder"
|
4
|
-
require "govuk_design_system_formbuilder/elements/document"
|
5
|
-
|
6
|
-
module GOVUKDesignSystemFormBuilder
|
7
|
-
module Elements
|
8
|
-
class Image < Base
|
9
|
-
include FileElement
|
10
|
-
|
11
|
-
def initialize(builder, object_name, attribute_name, hint:, label:, caption:, form_group:, mime_types:,
|
12
|
-
**kwargs, &)
|
13
|
-
super(builder, object_name, attribute_name, &)
|
14
|
-
|
15
|
-
@mime_types = mime_types
|
16
|
-
@label = label
|
17
|
-
@caption = caption
|
18
|
-
@hint = hint
|
19
|
-
@html_attributes = kwargs.merge(file_input_options)
|
20
|
-
@form_group = form_group
|
21
|
-
end
|
22
|
-
|
23
|
-
def preview
|
24
|
-
options = {}
|
25
|
-
add_option(options, :data, "#{stimulus_controller}_target", "preview")
|
26
|
-
add_option(options, :class, "preview-image")
|
27
|
-
add_option(options, :class, "hidden") unless preview?
|
28
|
-
|
29
|
-
tag.div **options do
|
30
|
-
tag.img(src: preview_url, class: "image-thumbnail") + destroy_element_trigger
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
private
|
35
|
-
|
36
|
-
def stimulus_controller
|
37
|
-
"image-field"
|
38
|
-
end
|
39
|
-
|
40
|
-
def form_group_class
|
41
|
-
"govuk-image-field"
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
module GOVUKDesignSystemFormBuilder
|
48
|
-
module Builder
|
49
|
-
delegate :config, to: GOVUKDesignSystemFormBuilder
|
50
|
-
|
51
|
-
# Generates a +div+ element to preview uploaded images and an +input+ with +type=file+ with a label, optional hint.
|
52
|
-
#
|
53
|
-
# @param attribute_name [Symbol] The name of the attribute
|
54
|
-
# @param hint [Hash,Proc] The content of the hint. No hint will be added if 'text' is left +nil+. When a +Proc+ is
|
55
|
-
# supplied the hint will be wrapped in a +div+ instead of a +span+
|
56
|
-
# @option hint text [String] the hint text
|
57
|
-
# @option hint kwargs [Hash] additional arguments are applied as attributes to the hint
|
58
|
-
# @param label [Hash,Proc] configures or sets the associated label content
|
59
|
-
# @option label text [String] the label text
|
60
|
-
# @option label size [String] the size of the label font, can be +xl+, +l+, +m+, +s+ or nil
|
61
|
-
# @option label tag [Symbol,String] the label's wrapper tag, intended to allow labels to act as page headings
|
62
|
-
# @option label hidden [Boolean] control the visibility of the label. Hidden labels will still be read by screen
|
63
|
-
# readers
|
64
|
-
# @option label kwargs [Hash] additional arguments are applied as attributes on the +label+ element
|
65
|
-
# @param caption [Hash] configures or sets the caption content which is inserted above the label
|
66
|
-
# @option caption text [String] the caption text
|
67
|
-
# @option caption size [String] the size of the caption, can be +xl+, +l+ or +m+. Defaults to +m+
|
68
|
-
# @option caption kwargs [Hash] additional arguments are applied as attributes on the caption +span+ element
|
69
|
-
# @option kwargs [Hash] kwargs additional arguments are applied as attributes to the +input+ element.
|
70
|
-
# @param form_group [Hash] configures the form group
|
71
|
-
# @option form_group classes [Array,String] sets the form group's classes
|
72
|
-
# @option form_group kwargs [Hash] additional attributes added to the form group
|
73
|
-
# @param block [Block] arbitrary HTML that will be rendered between the hint and the input
|
74
|
-
# @return [ActiveSupport::SafeBuffer] HTML output
|
75
|
-
#
|
76
|
-
# @example An image field with injected content
|
77
|
-
# = f.govuk_image_field :incident_image,
|
78
|
-
# label: { text: 'Attach a picture of the incident' } do
|
79
|
-
#
|
80
|
-
# p.govuk-inset-text
|
81
|
-
# | If you don't know exactly leave this section blank
|
82
|
-
#
|
83
|
-
# @example A image upload field with label as a proc
|
84
|
-
# = f.govuk_image_field :image, label: -> { tag.h3('Upload your image') }
|
85
|
-
#
|
86
|
-
def govuk_image_field(attribute_name,
|
87
|
-
label: {},
|
88
|
-
caption: {},
|
89
|
-
hint: {},
|
90
|
-
form_group: {},
|
91
|
-
mime_types: Koi.config.image_mime_types,
|
92
|
-
**,
|
93
|
-
&)
|
94
|
-
Elements::Image.new(
|
95
|
-
self, object_name, attribute_name, label:, caption:, hint:, form_group:, mime_types:, **, &
|
96
|
-
).html
|
97
|
-
end
|
98
|
-
end
|
99
|
-
end
|
data/lib/koi/collection.rb
DELETED