2024-05-17 18:24:08 +00:00
|
|
|
-#
|
|
|
|
Genera un listado de checkboxes entre los que se puede elegir para guardar
|
2024-05-17 18:30:25 +00:00
|
|
|
:ruby
|
2024-05-23 20:47:02 +00:00
|
|
|
id = id_for(base, attribute)
|
2024-05-17 18:30:25 +00:00
|
|
|
name = "#{base}[#{attribute}][]"
|
|
|
|
form_id = "form-#{Nanoid.generate}"
|
2024-05-17 18:24:08 +00:00
|
|
|
|
2024-05-23 20:15:23 +00:00
|
|
|
%div{ data: { controller: 'modal array enter', 'array-original-value': metadata.value.to_json, 'array-new-array-value': site_posts_new_array_value_path(site) } }
|
2024-05-27 19:40:09 +00:00
|
|
|
%template{ data: { target: 'array.placeholder' } }
|
|
|
|
.col.mb-3{ 'aria-hidden': 'true' }
|
|
|
|
%span.placeholder.w-100
|
|
|
|
|
2024-05-17 18:24:08 +00:00
|
|
|
.form-group
|
|
|
|
= hidden_field_tag name, ''
|
2024-05-27 19:38:56 +00:00
|
|
|
.d-flex.align-items-center.justify-content-between
|
|
|
|
= label_tag id, post_label_t(attribute, post: post)
|
|
|
|
= render 'bootstrap/btn', content: t('.edit'), action: 'modal#show'
|
|
|
|
-# Mostramos la lista de valores actuales.
|
2024-05-17 18:24:08 +00:00
|
|
|
|
2024-05-27 19:38:56 +00:00
|
|
|
Al aceptar el modal, se vacía el listado y se completa en base a
|
|
|
|
renderizaciones con HTMX. Para poder hacer eso, tenemos que poder
|
|
|
|
acceder a todos los items dentro del modal (como array.item) y
|
|
|
|
enviar el valor al endpoint que devuelve uno por uno. Esto lo
|
|
|
|
tenemos disponible en Stimulus, pero queremos usar HTMX o técnica
|
|
|
|
similar para poder renderizar del lado del servidor.
|
2024-05-17 18:24:08 +00:00
|
|
|
|
2024-05-27 19:38:56 +00:00
|
|
|
Para poder cancelar, mantenemos el estado original y desactivamos
|
|
|
|
o activamos los ítemes según estén incluidos en esa lista o no.
|
2024-05-27 19:40:09 +00:00
|
|
|
.row.row-cols-1.row-cols-md-2.no-gutters.placeholder-glow{ data: { target: 'array.current' } }
|
2024-05-27 19:38:56 +00:00
|
|
|
- metadata.value.sort_by(&:remove_diacritics).each do |value|
|
|
|
|
= render 'posts/new_array_value', value: value
|
2024-05-17 18:24:08 +00:00
|
|
|
|
|
|
|
= render 'bootstrap/modal', id: id, modal_content_attributes: { class: 'h-100' }, hide_actions: ['array#cancel'] do
|
|
|
|
- content_for :"#{id}_header" do
|
|
|
|
.form-group.flex-grow-1.mb-0
|
|
|
|
= label_tag id, post_label_t(attribute, post: post)
|
2024-05-23 20:15:23 +00:00
|
|
|
%input.form-control{ data: { target: 'array.search', action: 'input->array#search keydown->enter#prevent' }, type: 'search', placeholder: t('.filter') }
|
2024-05-17 18:24:08 +00:00
|
|
|
|
|
|
|
- content_for :"#{id}_body" do
|
|
|
|
.form-group.mb-0{ id: "#{id}_body" }
|
|
|
|
-# Eliminamos las tildes para poder buscar independientemente de cómo se escriba
|
|
|
|
- metadata.values.sort_by(&:remove_diacritics).each do |value|
|
|
|
|
.mb-2{ data: { target: 'array.item', 'searchable-value': value.remove_diacritics.downcase, value: value } }
|
|
|
|
= render 'bootstrap/custom_checkbox', name: name, id: "value-#{Nanoid.generate}", value: value, checked: metadata.value.include?(value), content: value
|
|
|
|
|
|
|
|
- content_for :"#{id}_footer" do
|
|
|
|
.input-group.w-auto.flex-grow-1.my-0
|
2024-05-27 19:44:23 +00:00
|
|
|
%input.form-control{ form: form_id, name: 'value', type: 'text', placeholder: t('.add_new'), required: true }
|
2024-05-17 18:24:08 +00:00
|
|
|
.input-group-append
|
2024-05-23 18:40:00 +00:00
|
|
|
= render 'bootstrap/btn', content: t('.add', layout: ''), form: form_id, type: 'submit', class: 'mb-0 mr-0'
|
2024-05-17 18:24:08 +00:00
|
|
|
= render 'bootstrap/btn', content: t('.accept'), action: 'array#accept modal#hide', class: 'm-0 mr-1'
|
|
|
|
= render 'bootstrap/btn', content: t('.cancel'), action: 'array#cancel modal#hide', class: 'm-0'
|
|
|
|
|
|
|
|
-# Los formularios para HTMX se colocan por fuera del formulario
|
|
|
|
principal, porque HTML5 no soporta formularios anidados. Los campos
|
|
|
|
quedan unidos al formulario por su atributo `id`.
|
|
|
|
|
|
|
|
Al enviar el formulario se obtiene una nueva opción con el valor
|
|
|
|
y se la agrega al final del listado.
|
|
|
|
- content_for :post_form do
|
|
|
|
%form{ id: form_id, 'hx-get': site_posts_new_array_path(site), 'hx-target': "##{id}_body", 'hx-swap': 'beforeend' }
|
|
|
|
%input{ type: 'hidden', name: 'name', value: name }
|
|
|
|
%input{ type: 'hidden', name: 'id', value: form_id }
|