const _$ = window.$
function SetCookie (cookieName, cookieValue, nDays) {
const today = new Date()
const expire = new Date()
if (nDays === null || nDays === 0) nDays = 1
expire.setTime(today.getTime() + 3600000 * 24 * nDays)
document.cookie = cookieName + '=' + escape(cookieValue) +
';expires=' + expire.toGMTString()
}
function ReadCookie (cookieName) {
const theCookie = ' ' + document.cookie
let ind = theCookie.indexOf(' ' + cookieName + '=')
if (ind === -1) ind = theCookie.indexOf(';' + cookieName + '=')
if (ind === -1 || cookieName === '') return ''
let ind1 = theCookie.indexOf(';', ind + 1)
if (ind1 === -1) ind1 = theCookie.length
return unescape(theCookie.substring(ind + cookieName.length + 2, ind1))
}
function insertWaitAjax (element) {
// jQuery(element).append('
')
_$(element).append('
')
}
function DispositivoSearch (opts) {
_$(function () {
let formData = {}
let containerDs = _$('body').children('#container_ds')
if (containerDs.length > 0) {
_$(containerDs).remove()
}
containerDs = _$('')
_$('body').prepend(containerDs)
const fields = _$('[data-sapl-ta="DispositivoSearch"]')
fields.each(function () {
const field = _$(this)
const dataTypeSelection = field.attr('data-type-selection')
const dataField = field.attr('data-field')
const dataFunction = field.attr('data-function')
const onChangeFieldSelects = function (event) {
if (dataTypeSelection === 'checkbox') {
const tas = field.find('input[name="ta_select_all"]') // tas - Textos Articulados
tas.off()
tas.on('change', function (event) {
_$(this).closest('ul').find('input[name="' + dataField + '"]').prop('checked', this.checked)
// _$(this).prop('checked', false)
})
} else {
const dpts = field.find('input')
dpts.off()
dpts.attr('type', 'hidden')
_$('')
.insertBefore(dpts)
.append(_$('×'))
.on('click', function () {
if (_$(this).closest('ul').find('li').length === 2) {
_$(this).closest('ul').remove()
} else {
_$(this).closest('li').remove()
}
})
}
}
onChangeFieldSelects()
const onChangeParamTA = function (event) {
const tipoTa = _$('select[name="tipo_ta"]').val()
const tipoModel = _$('select[name="tipo_model"]').val()
const numTa = _$('input[name="num_ta"]').val()
const anoTa = _$('input[name="ano_ta"]').val()
let tipoResultado = _$('input[name="tipo_resultado"]:checked').val()
const rotuloDispositivo = _$('input[name="rotulo_dispositivo"]').val()
const textoDispositivo = _$('input[name="texto_dispositivo"]').val()
const maxResults = _$('select[name="max_results"]').val()
let url = ''
if (rotuloDispositivo.length > 0 || textoDispositivo.length > 0) {
_$('input[name="tipo_resultado"]').prop('disabled', false)
_$('input[name="tipo_resultado"]').each((idx, element) => {
element.parentElement.classList.remove('disabled')
})
_$('input[name="tipo_resultado"]').closest('#div_id_tipo_resultado').css('opacity', '1')
} else {
_$('input[name="tipo_resultado"]').filter('[value="False"]').prop('checked', true)
_$('input[name="tipo_resultado"]').prop('disabled', true)
_$('input[name="tipo_resultado"]').each((idx, element) => {
element.parentElement.classList.add('disabled')
})
_$('input[name="tipo_resultado"]').closest('#div_id_tipo_resultado').css('opacity', '0.3')
tipoResultado = 'False'
}
formData = {
tipo_ta: tipoTa,
tipo_model: tipoModel,
num_ta: numTa,
ano_ta: anoTa,
texto: textoDispositivo,
rotulo: rotuloDispositivo,
tipo_resultado: tipoResultado,
max_results: maxResults,
data_type_selection: dataTypeSelection,
data_field: dataField,
data_function: dataFunction
}
window.localStorage.setItem('dispositivo_search_form_data', JSON.stringify(formData))
url = '/ta/search_fragment_form'
_$('.result-busca-dispositivo').html('')
insertWaitAjax('.result-busca-dispositivo')
_$.get(url, formData).done(function (data) {
_$('.result-busca-dispositivo').html(data)
// OptionalCustomFrontEnd().init()
if (dataTypeSelection === 'checkbox') {
const tas = _$('.result-busca-dispositivo').find('input[name="ta_select_all"]')
tas.off()
tas.on('change', function (event) {
_$(this).closest('ul').find('input[name="' + dataField + '"]').prop('checked', this.checked)
})
}
})
}
const onKeyPressRotuloBuscaTextual = function (event) {
const rotuloDispositivo = _$('input[name="rotulo_dispositivo"]').val()
const textoDispositivo = _$('input[name="texto_dispositivo"]').val()
// let tipoResultado = _$('input[name="tipo_resultado"]:checked').val()
if (rotuloDispositivo.length > 0 || textoDispositivo.length > 0) {
_$('input[name="tipo_resultado"]').prop('disabled', false)
_$('input[name="tipo_resultado"]').each((idx, element) => {
element.parentElement.classList.remove('disabled')
})
_$('input[name="tipo_resultado"]').closest('#div_id_tipo_resultado').css('opacity', '1')
} else {
_$('input[name="tipo_resultado"]').filter('[value="False"]').prop('checked', true)
_$('input[name="tipo_resultado"]').prop('disabled', true)
_$('input[name="tipo_resultado"]').each((idx, element) => {
element.parentElement.classList.add('disabled')
})
_$('input[name="tipo_resultado"]').closest('#div_id_tipo_resultado').css('opacity', '0.3')
// tipoResultado = 'False'
}
}
let buttonDs = field.children('#buttonDs')
if (buttonDs.length > 0) {
_$(buttonDs).remove()
}
buttonDs = _$('')
field.prepend(buttonDs)
const btnOpenSearch = _$('