Skip to content

Commit

Permalink
Cambia control de autocompletacion de contacto en org. social. https:…
Browse files Browse the repository at this point in the history
  • Loading branch information
vtamara committed Jul 12, 2023
1 parent fbdf758 commit 63e5c86
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 65 deletions.
107 changes: 107 additions & 0 deletions app/assets/javascripts/msip/AutocompletaAjaxContactos.es6
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

export default class MsipAutocompletaAjaxContactos {
/* No usamos constructor ni this porque en operaElegida sería
* del objeto AutocompletaAjaxExpreg y no esta clase.
* Más bien en esta todo static
*/


// Elije una persona en autocompletación
static operarElegida (eorig, cadpersona, id, otrosop) {
let root = window
msip_arregla_puntomontaje(root)
const cs = id.split(';')
const idPersona = cs[0]
if ([...document.querySelector('#orgsocial_persona').
querySelectorAll('[id$=_attributes_id]')].filter(
e => e.value==idPersona).length > 0) {
window.alert("La misma persona ya está en el listado de contactos");
return
}
let d = '&persona_id=' + idPersona
d += '&ac_orgsocial_persona=true'
const a = root.puntomontaje + 'personas/datos'

window.Rails.ajax({
type: 'GET',
url: a,
data: d,
success: (resp, estado, xhr) => {
const divcp = eorig.target.closest(
'.' + MsipAutocompletaAjaxContactos.claseEnvoltura)
if (divcp == null) {
alert('No se encontró elmento con clase ' +
MsipAutocompletaAjaxContactos.claseEnvoltura)
}
divcp.querySelector('[id$=_attributes_id]').value = resp.id
divcp.querySelector('[id$=_attributes_nombres]').value = resp.nombres
divcp.querySelector('[id$=_attributes_apellidos]').value = resp.apellidos
divcp.querySelector('[id$=_attributes_sexo]').value = resp.sexo
const tdocid = divcp.querySelector('[id$=_attributes_tdocumento_id]')
if (tdocid != null) {
let idop = null
tdocid.childNodes.forEach((o) => {
if (typeof (o.innerText) === 'string' &&
o.innerText === resp.tdocumento) {
idop = o.value
}
})
if (idop != null) {
tdocid.value = idop
}
}
const numdoc = divcp.querySelector('[id$=_numerodocumento]')
if (numdoc != null) {
numdoc.value = resp.numerodocumento
}
const anionac = divcp.querySelector('[id$=_anionac]')
if (anionac != null) {
anionac.value = resp.anionac
}
const mesnac = divcp.querySelector('[id$=_mesnac]')
if (mesnac != null) {
mesnac.value = resp.mesnac
}
const dianac = divcp.querySelector('[id$=_dianac]')
if (dianac != null) {
dianac.value = resp.dianac
}
const cargo = divcp.querySelector('[id$=_cargo]')
if (cargo != null && typeof resp.cargo != 'undefined') {
cargo.value = resp.cargo
}
const correo = divcp.querySelector('[id$=_correo]')
if (correo != null && typeof resp.correo != 'undefined') {
correo.value = resp.correo
}
eorig.target.setAttribute('data-autocompleta', 'no')
eorig.target.removeAttribute('list')
const sel = document.getElementById(
MsipAutocompletaAjaxContactos.idDatalist)
sel.innerHTML = ''
document.dispatchEvent(new Event('msip:autocompletado-contacto'))
},
error: (resp, estado, xhr) => {
window.alert('Error con ajax ' + resp)
}
})
}

static iniciar() {
console.log("MsipAutocompletaAjaxContactos msip")
let url = window.puntomontaje + 'personas.json'
var contactos = new window.AutocompletaAjaxExpreg(
[/^orgsocial_orgsocial_persona_attributes_[0-9]*_persona_attributes_nombres$/],
url,
MsipAutocompletaAjaxContactos.idDatalist,
MsipAutocompletaAjaxContactos.operarElegida
)
contactos.iniciar()
}

}

// Queriamos hacer dentro de MsipAutocompletaAjaxConactos static
// claseEnvoltura = 'campos_persona' pero la versión de bable usada por babel-transpiler, usado por sprockets4 no lo soporta así que:
MsipAutocompletaAjaxContactos.claseEnvoltura = 'campos_persona'
MsipAutocompletaAjaxContactos.idDatalist = 'fuente-contactos-orgsocial'
66 changes: 3 additions & 63 deletions app/assets/javascripts/msip/motor.js.coffee.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#//= require msip/geo
#//= require msip/ubicacionpre
#//= require msip/motor_es5
#//= require msip/AutocompletaAjaxContactos


# HTML
Expand Down Expand Up @@ -670,66 +671,6 @@ if (typeof String.prototype.endsWith != 'function')


# PERSONA
# Elije una persona en autocompletación
@msip_autocompleta_persona = (label, id, divcp, root) ->
msip_arregla_puntomontaje(root)
cs = id.split(";")
id_persona = cs[0]
pl = []
ini = 0
for i in [0..cs.length] by 1
t = parseInt(cs[i])
pl[i] = label.substring(ini, ini + t)
ini = ini + t + 1
# pl[1] cnom, pl[2] es cape, pl[3] es cdoc
d = "&id_persona=" + id_persona
d += "&ac_orgsocial_persona=" + true
a = root.puntomontaje + 'personas/datos'
$.ajax({
url: a,
data: d,
dataType: "json"
}).fail( (jqXHR, texto) ->
alert("Error con ajax " + texto)
).done( (e, r) ->
divcp.find('[id$=_attributes_id]').val(e.id)
divcp.find('[id$=_attributes_nombres]').val(e.nombres)
divcp.find('[id$=_attributes_apellidos]').val(e.apellidos)
divcp.find('[id$=_attributes_sexo]').val(e.sexo)
tdocid = divcp.find('[id$=_attributes_tdocumento_id] option:contains(' + e.tdocumento + ')').val()
divcp.find('[id$=_tdocumento_id]').val(tdocid)
divcp.find('[id$=_numerodocumento]').val(e.numerodocumento)
divcp.find('[id$=_anionac]').val(e.anionac)
divcp.find('[id$=_mesnac]').val(e.mesnac)
divcp.find('[id$=_dianac]').val(e.dianac)
divcp.find('[id$=_cargo]').val(e.cargo)
divcp.find('[id$=_correo]').val(e.correo)
)
return

# Busca persona por nombre, apellido o identificación
# s es objeto con foco donde se busca persona
@msip_busca_persona_nombre = (s) ->
root = window
msip_arregla_puntomontaje(root)
cnom = s.attr('id')
v = $("#" + cnom).data('autocompleta')
if (v != 1 && v != "no")
$("#" + cnom).data('autocompleta', 1)
divcp = s.closest('.campos_persona')
if (typeof divcp == 'undefined' || divcp.length == 0)
alert('No se ubico .campos_persona')
return
$("#" + cnom).autocomplete({
source: root.puntomontaje + "personas.json",
minLength: 2,
select: ( event, ui ) ->
if (ui.item)
msip_autocompleta_persona(ui.item.value, ui.item.id, divcp, root)
event.stopPropagation()
event.preventDefault()
})
return

# MSIP

Expand Down Expand Up @@ -1086,6 +1027,8 @@ if (typeof String.prototype.endsWith != 'function')
$(ns).attr('data-toggle', 'tooltip')
)

MsipAutocompletaAjaxContactos.iniciar()

# Pone colores de acuerdo al tema
msip_ajax_recibe_json(root, 'temausuario', {}, msip_pone_tema)

Expand Down Expand Up @@ -1116,6 +1059,3 @@ if (typeof String.prototype.endsWith != 'function')

return na;

$(document).on('focusin', 'input[id^=orgsocial_orgsocial_persona_attributes][id$=nombres]', (e) ->
msip_busca_persona_nombre($(this))
)
2 changes: 2 additions & 0 deletions app/views/msip/orgsociales/_campo_orgsocial_persona.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<div id="<%= div_id %>" class="orgsocial_persona">
<label class="string optional control-label" for="tabla_<%= div_id %>"><%= t('Contactos en la organización social') %></label>
<table id="tabla_<%= div_id %>" class="table table-bordered">
<datalist id='fuente-contactos-orgsocial'>
</datalist>
<thead>
<tr>
<th scope="col"><%= t('Nombres') %></th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
ac = {'data-autocompleta' => 'no' }
if (!p.object || !p.object.nombres || p.object.nombres == 'N' || p.object.nombres == '')
ph = t('Buscar')
ac = { }
ac = { autocomplete: 'off',
list: 'fuente-contactos-orgsocial'
}
end %>
<%= p.input :nombres, placeholder: ph,
value_method: lambda {|t| '' if t.nombres == 'N'},
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/app/javascript/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import 'chosen-js/chosen.jquery'; // Cuadros de seleccion potenciados
import 'bootstrap-datepicker'
import 'bootstrap-datepicker/dist/locales/bootstrap-datepicker.es.min.js'

import {AutocompletaAjaxExpreg} from '@pasosdejesus/autocompleta_ajax'
window.AutocompletaAjaxExpreg = AutocompletaAjaxExpreg

let esperarRecursosSprocketsYDocumento = function (resolver) {
if (typeof window.puntomontaje == 'undefined') {
setTimeout(esperarRecursosSprocketsYDocumento, 5, resolver)
Expand Down
3 changes: 2 additions & 1 deletion test/dummy/db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2628,6 +2628,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20230404025025'),
('20230504084246'),
('20230613111532'),
('20230622205530');
('20230622205530'),
('20230712163859');


1 change: 1 addition & 0 deletions test/dummy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@fortawesome/fontawesome-free": "^5.11.2",
"@hotwired/stimulus": "^3.0.1",
"@hotwired/turbo-rails": "^7.1.0",
"@pasosdejesus/autocompleta_ajax": "https://gitlab.com/pasosdeJesus/autocompleta_ajax.git",
"@popperjs/core": "^2.9.1",
"@rails/ujs": "^7.0.1",
"bootstrap": "^5.1.0",
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,10 @@
"@jridgewell/resolve-uri" "3.1.0"
"@jridgewell/sourcemap-codec" "1.4.14"

"@pasosdejesus/autocompleta_ajax@https://gitlab.com/pasosdeJesus/autocompleta_ajax.git":
version "0.9.9-a32"
resolved "https://gitlab.com/pasosdeJesus/autocompleta_ajax.git#c460a93c41ef66b862965b673615d95e5f059458"

"@popperjs/core@^2.9.1":
version "2.11.8"
resolved "https://registry.yarnpkg.com/@popperjs/core/-/core-2.11.8.tgz#6b79032e760a0899cd4204710beede972a3a185f"
Expand Down

0 comments on commit 63e5c86

Please sign in to comment.