From 26722a874e8291e309ee220f28905765c2fe5477 Mon Sep 17 00:00:00 2001 From: alejocruzrcc Date: Thu, 22 Aug 2024 14:22:30 -0500 Subject: [PATCH] pasa funciones coffee de filtro a stimulus --- .../javascripts/sivel2_gen/motor.js.coffee | 49 ---------- .../controllers/filtro_avanzado_controller.js | 96 +++++++++++++++++++ .../casos/_filtro_avanzado.html.erb | 7 +- app/views/sivel2_gen/casos/index.html.erb | 95 +++++++++--------- .../dummy/app/javascript/controllers/index.js | 3 + 5 files changed, 153 insertions(+), 97 deletions(-) create mode 100644 app/javascript/controllers/filtro_avanzado_controller.js diff --git a/app/assets/javascripts/sivel2_gen/motor.js.coffee b/app/assets/javascripts/sivel2_gen/motor.js.coffee index ae576e07f..10f64b46a 100644 --- a/app/assets/javascripts/sivel2_gen/motor.js.coffee +++ b/app/assets/javascripts/sivel2_gen/motor.js.coffee @@ -665,17 +665,6 @@ enviaFormularioContarPost= (root) -> root.elempe = [] ) - $(document).on('click', 'button#boton_filtro_avanzado', (e) -> - if $(this).attr("aria-expanded") == 'true' - $('#botones_finfiltro').removeClass("float-end").addClass("d-flex justify-content-center") - else - $('#botones_finfiltro').removeClass("d-flex justify-content-center").addClass("float-end") - if $('#conscasos_q').is(':visible') - $('#conscasos_q').hide() - else - $('#conscasos_q').show() - ) - Sivel2GenMotorEs6.iniciar() # Elimina acto @@ -785,44 +774,6 @@ enviaFormularioContarPost= (root) -> return event.preventDefault() ) - $(document).on('change', '#filtro_tviolencia_id', (e) -> - tviolencia_id = $(this).val() - select = $(this) - if tviolencia_id != "Z" - b = root.puntomontaje + 'admin/categorias/filtratviolencia' - $.ajax({ - url: b, - data: {tviolencia: tviolencia_id}, - dataType: "json", - success: (datos) -> - div_padre = select.closest("div") - div_grid = div_padre.parent() - div_filtro = div_grid.parent().parent() - input_cat = div_filtro.find("select[id=filtro_categoria_id]") - Msip__Motor.configurarElementoTomSelect(input_cat.val(datos[0])) - error: (jqXHR, texto) -> - alert("Error: " + jqXHR.responseText) - }) - ) - $(document).on('change', '#filtro_categoria_id', (e) -> - categoria_id = $(this).val() - select = $(this) - b = root.puntomontaje + 'admin/categorias/filtratviolencia' - $.ajax({ - url: b, - data: {categorias_seleccionadas: categoria_id}, - dataType: "json", - success: (datos) -> - if datos[0] == false - div_padre = select.closest("div") - div_grid = div_padre.parent() - div_filtro = div_grid.parent().parent() - input_tipo = div_filtro.find("select[id=filtro_tviolencia_id]") - Msip__Motor.configurarElementoTomSelect(input_tipo.val("Z")) - error: (jqXHR, texto) -> - alert("Error: " + jqXHR.responseText) - }) - ) return @sivel2_gen_prepara_eventos_unicos = (root) -> diff --git a/app/javascript/controllers/filtro_avanzado_controller.js b/app/javascript/controllers/filtro_avanzado_controller.js new file mode 100644 index 000000000..f2db74cba --- /dev/null +++ b/app/javascript/controllers/filtro_avanzado_controller.js @@ -0,0 +1,96 @@ +import { Controller } from "@hotwired/stimulus" +// Maneja filrto avanzado de casos con stimulus + +import Msip__Motor from "../msip/motor" + +// Conecta con data-controller="sivel2-gen--filtro-avanzado" +export default class extends Controller { + + static targets = [ + "botonesFin" + ] + + connect() { + console.log("Conexion de controlador filtro avanzado establecida") + } + + abrir(e){ + if (e.target && e.target.id === 'boton_filtro_avanzado') { + var botonFiltro = e.target; + + if (botonFiltro.getAttribute('aria-expanded') === 'true') { + this.botonesFinTarget.classList.remove('float-end'); + this.botonesFinTarget.classList.add('d-flex', 'justify-content-center'); + } else { + this.botonesFinTarget.classList.remove('d-flex', 'justify-content-center'); + this.botonesFinTarget.classList.add('float-end'); + } + } + } + + filtrar_tviolencia(e){ + if (e.target && e.target.id === 'filtro_tviolencia_id') { + var tviolencia_id = e.target.value; + var select = e.target; + + if (tviolencia_id !== "Z") { + var url = window.puntomontaje + 'admin/categorias/filtratviolencia'; + + var xhr = new XMLHttpRequest(); + xhr.open('GET', url + '?tviolencia=' + encodeURIComponent(tviolencia_id), true); + xhr.responseType = 'json'; + + xhr.onreadystatechange = function() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var datos = xhr.response; + var div_padre = select.closest('div'); + var div_grid = div_padre.parentElement; + var div_filtro = div_grid.parentElement.parentElement; + var input_cat = div_filtro.querySelector('select[id=filtro_categoria_id]'); + + //Msip__Motor.configurarElementoTomSelect(input_cat.value = datos[0]); + } else { + alert('Error: ' + xhr.responseText); + } + } + }; + + xhr.send(); + } + } + } + + filtrar_categoria(e){ + if (e.target && e.target.id === 'filtro_categoria_id') { + var categoria_id = e.target.value; + var select = e.target; + var url = window.puntomontaje + 'admin/categorias/filtratviolencia'; + + var xhr = new XMLHttpRequest(); + xhr.open('GET', url + '?categorias_seleccionadas=' + encodeURIComponent(categoria_id), true); + xhr.responseType = 'json'; + + xhr.onreadystatechange = function() { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var datos = xhr.response; + if (datos[0] === false) { + debugger + var div_padre = select.closest('div'); + var div_grid = div_padre.parentElement; + var div_filtro = div_grid.parentElement.parentElement; + var input_tipo = div_filtro.querySelector('select[id=filtro_tviolencia_id]'); + Msip__Motor.configurarElementoTomSelect(input_tipo.value = 'Z'); + } + } else { + alert('Error: ' + xhr.responseText); + } + } + }; + + xhr.send(); + } + } + +} diff --git a/app/views/sivel2_gen/casos/_filtro_avanzado.html.erb b/app/views/sivel2_gen/casos/_filtro_avanzado.html.erb index 95a4e5513..d503bf39b 100644 --- a/app/views/sivel2_gen/casos/_filtro_avanzado.html.erb +++ b/app/views/sivel2_gen/casos/_filtro_avanzado.html.erb @@ -147,6 +147,9 @@ selected: "Z", label: false, include_blank: false + input_html: { + "data-action": "change->sivel2-gen--filtro-avanzado#filtrar_tviolencia", + } %> @@ -169,7 +172,9 @@ }, value_method: :id, :include_hidden => false, - input_html: { :class => "tom-select", multiple: true }, + input_html: { :class => "tom-select", + "data-action": "change->sivel2-gen--filtro-avanzado#filtrar_categoria", + multiple: true }, label: false %> diff --git a/app/views/sivel2_gen/casos/index.html.erb b/app/views/sivel2_gen/casos/index.html.erb index 46354ef1d..5a71aef43 100644 --- a/app/views/sivel2_gen/casos/index.html.erb +++ b/app/views/sivel2_gen/casos/index.html.erb @@ -7,59 +7,60 @@ { remote: true, url: sivel2_gen.casos_path, method: "get" } do |f| -%> -
-
-
- <% if @conscaso.size > 0 && current_usuario && - can?(:new, Sivel2Gen::Caso) %> - <%= render partial: 'msip/modelos/boton_nuevo', - locals: {ruta: sivel2_gen.new_caso_path} %> - <% end %> -
-
- Casos: <%= @conscaso.count %> -
-
- <%= f.input :q, - label: false, - placeholder: 'Búsqueda aproximada' - %> -
-
- -
+ %> +
+
+
+ <% if @conscaso.size > 0 && current_usuario && + can?(:new, Sivel2Gen::Caso) %> + <%= render partial: 'msip/modelos/boton_nuevo', + locals: {ruta: sivel2_gen.new_caso_path} %> + <% end %> +
+
+ Casos: <%= @conscaso.count %>
-
-
-
-
- <%= render partial: 'filtro_avanzado', locals: {f: f} %> -
+
+ <%= f.input :q, + label: false, + placeholder: 'Búsqueda aproximada' + %> +
+
+ +
+
+
+
+
+
+ <%= render partial: 'filtro_avanzado', locals: {f: f} %>
-
-
-
- <%= f.button :submit, 'Filtrar', id: 'sivel2_gen-filtrar-casos', - class: 'btn btn-default', - 'data-turbo': false %> - -
+
+
+
+
+ <%= f.button :submit, 'Filtrar', id: 'sivel2_gen-filtrar-casos', + class: 'btn btn-default', + 'data-turbo': false %> +
+
<% end %>
diff --git a/test/dummy/app/javascript/controllers/index.js b/test/dummy/app/javascript/controllers/index.js index 653df64df..6369071f6 100644 --- a/test/dummy/app/javascript/controllers/index.js +++ b/test/dummy/app/javascript/controllers/index.js @@ -25,6 +25,9 @@ application.register("sivel2-gen--edad", Sivel2Gen__EdadController) import Sivel2Gen__EnviarFichaCasoController from "./sivel2_gen/enviar_ficha_caso_controller" application.register("sivel2-gen--enviar-ficha-caso", Sivel2Gen__EnviarFichaCasoController) +import Sivel2Gen__FiltroAvanzadoController from "./sivel2_gen/filtro_avanzado_controller" +application.register("sivel2-gen--filtro-avanzado", Sivel2Gen__FiltroAvanzadoController) + import Sivel2Gen__MapaosmController from "./sivel2_gen/mapaosm_controller" application.register("sivel2-gen--mapaosm", Sivel2Gen__MapaosmController)