Skip to content

Commit

Permalink
Merge pull request #171 from ResidenciaTICBrisa/refatoracao_js
Browse files Browse the repository at this point in the history
Implementação de testes para o código JS da página do administrador e refatoração parcial do código JS do shortcode
  • Loading branch information
MarcosViniciusG authored Jun 20, 2024
2 parents e76368b + 12fcf14 commit b4abd6e
Show file tree
Hide file tree
Showing 12 changed files with 517 additions and 300 deletions.
Binary file added lgbtq_connect/assets/imgs/custom_marker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lgbtq_connect/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<form method="post">
<div class="search_wrapper">
<input type="text" id="searchInputIndex" placeholder="Pesquise a cidade ou estado...">
<button type="submit" onclick="return searchButtonClicked()" class="button_search">Pesquisar</button>
<button type="button" onclick="pesquisar('searchInputIndex', 'listaResultadosIndex')" class="button_search">Pesquisar</button>
</div>
</form>
<div id="listaResultadosIndex"></div>
Expand Down Expand Up @@ -49,7 +49,7 @@

<div class="search_wrapper">
<input type="text" id="searchInputForm" placeholder="Pesquise a cidade ou estado...">
<button type="button" onclick="return searchButtonClickedForm()" class="button_search">Pesquisar</button>
<button type="button" onclick="pesquisar('searchInputForm', 'listaResultadosForms')" class="button_search">Pesquisar</button>
</div>
<div id="listaResultadosForms"></div>
<div id="mapa_formulario" style="height: 300px;margin-bottom:10px;"></div>
Expand Down
219 changes: 104 additions & 115 deletions lgbtq_connect/assets/js/funcionalidades.js
Original file line number Diff line number Diff line change
@@ -1,137 +1,121 @@
var resultados = []; // Array para armazenar os locais relacionados
var isSearchingIndex = false; // Status de busca no index
var isSearchingForm = false; // Status de busca no form

function searchButtonClicked() {
if (!isSearchingIndex) {
isSearchingIndex = true;
var searchTerm = document.getElementById('searchInputIndex').value;
searchLocations(searchTerm, 'listaResultadosIndex');
class Pesquisador {
constructor(listaId) {
this.listaId = listaId;
this.flag = false;
}
return false;
}

function searchButtonClickedForm() {
if (!isSearchingForm) {
isSearchingForm = true;
var searchTerm = document.getElementById('searchInputForm').value;
searchLocations(searchTerm, 'listaResultadosForms');
}
return false;
}
pesquisarLocalizacoes(query) {
if(this.flag) {
return;
}

this.flag = true;

function searchLocations(query, resultListId) {
resultados = [];
var apiUrl = 'https://nominatim.openstreetmap.org/search?format=json&q=' + encodeURIComponent(query);
fetch(apiUrl)
.then(response => response.json())
.then(data => {
data.forEach(location => {
resultados.push({
display_name: location.display_name,
lat: location.lat,
lon: location.lon
let resultados = [];
const self = this;
let apiUrl = 'https://nominatim.openstreetmap.org/search?format=json&q=' + encodeURIComponent(query);
fetch(apiUrl)
.then(response => response.json())
.then(data => {
data.forEach(location => {
resultados.push({
display_name: location.display_name,
lat: location.lat,
lon: location.lon
});
});
});
imprimirResultados(resultados, resultListId);
self.imprimirResultados(resultados);

// Atualiza o estado da busca após a conclusão
if (resultListId === 'listaResultadosIndex') {
isSearchingIndex = false; // Marca a busca na página inicial como concluída
} else if (resultListId === 'listaResultadosForms') {
isSearchingForm = false; // Marca a busca no formulário como concluída
}
})
.catch(error => {
console.error('Erro ao buscar locais:', error);
self.flag = false; // Marca a busca no formulário como concluída
})
.catch(error => {
console.error('Erro ao buscar locais:', error);

// Em caso de erro, atualiza o estado da busca para permitir novas buscas
if (resultListId === 'listaResultadosIndex') {
isSearchingIndex = false; // Marca a busca na página inicial como concluída
} else if (resultListId === 'listaResultadosForms') {
isSearchingForm = false; // Marca a busca no formulário como concluída
}
});
}
self.flag = false; // Marca a busca no formulário como concluída
});
}

function imprimirResultados(resultados, resultListId) {
var listaResultadosOcultados = [];
var listaResultados = document.getElementById(resultListId);
listaResultados.innerHTML = '';
var count = 0;
var div = document.createElement('div');
resultados.forEach(resultado => {
var divResultado = document.createElement('div');
divResultado.classList.add('celula_resultado');
divResultado.style.borderRadius = '3px';
divResultado.style.margin = '5px 5px 5px 0px';
divResultado.style.cursor = 'pointer';
divResultado.innerHTML = '<img src="https://i.imgur.com/4ZnmAxk.png" width="20px" height="20px">' + resultado.display_name;
divResultado.addEventListener('click', function () {
changeMapLocation(resultado.lat, resultado.lon);
imprimirResultados(resultados) {
let listaResultadosOcultados = [];
const listaResultados = document.getElementById(this.listaId);
listaResultados.innerHTML = '';
let count = 0;
const div = document.createElement('div');
resultados.forEach(resultado => {
const divResultado = document.createElement('div');
divResultado.classList.add('celula_resultado');
divResultado.style.borderRadius = '3px';
divResultado.style.margin = '5px 5px 5px 0px';
divResultado.style.cursor = 'pointer';
divResultado.innerHTML = '<img src="https://i.imgur.com/4ZnmAxk.png" width="20px" height="20px">' + resultado.display_name;
divResultado.addEventListener('click', function () {
if (pagina) {
pagina.mapa.mudarLocalizacao([resultado.lat, resultado.lon]);
}
});
count += 1;
if(count <=5){
div.appendChild(divResultado);
} else {
divResultado.style.display = 'none';
listaResultadosOcultados.push(divResultado);
div.appendChild(divResultado);
}
});
count += 1;
if(count <=5){
div.appendChild(divResultado);
} else {
divResultado.style.display = 'none';
listaResultadosOcultados.push(divResultado);
div.appendChild(divResultado);
}
});


listaResultados.appendChild(div);
listaResultados.appendChild(div);

if (count > 5){
// Adicionando botão "Ver Mais"
var verMaisButton = document.createElement('button');
verMaisButton.textContent = 'Ver Mais';
verMaisButton.setAttribute('type', 'button');
verMaisButton.setAttribute('class', 'ver');
verMaisButton.addEventListener('click', function() {
MostrarMaisResultados();
});
if (count > 5){
// Adicionando botão "Ver Mais"
const verMaisButton = document.createElement('button');
verMaisButton.textContent = 'Ver Mais';
verMaisButton.setAttribute('type', 'button');
verMaisButton.setAttribute('class', 'ver');
verMaisButton.addEventListener('click', function() {
MostrarMaisResultados();
});

listaResultados.appendChild(verMaisButton);
listaResultados.appendChild(verMaisButton);

// Adicionando botão "Ver Menos"
var verMenosButton = document.createElement('button');
verMenosButton.textContent = 'Ver Menos';
verMenosButton.setAttribute('type', 'button');
verMenosButton.setAttribute('class', 'ver');
verMenosButton.addEventListener('click', function() {
MostrarMenosResultados();
});
verMenosButton.style.display = 'none';
// Adicionando botão "Ver Menos"
const verMenosButton = document.createElement('button');
verMenosButton.textContent = 'Ver Menos';
verMenosButton.setAttribute('type', 'button');
verMenosButton.setAttribute('class', 'ver');
verMenosButton.addEventListener('click', function() {
MostrarMenosResultados();
});
verMenosButton.style.display = 'none';

listaResultados.appendChild(verMenosButton);
listaResultados.appendChild(verMenosButton);

// Função para mostrar mais resultados
function MostrarMaisResultados() {
listaResultadosOcultados.forEach(resultado => {
resultado.style.display = 'block';
});
verMaisButton.style.display = 'none';
verMenosButton.style.display = 'block';
}
// Função para mostrar mais resultados
function MostrarMaisResultados() {
listaResultadosOcultados.forEach(resultado => {
resultado.style.display = 'block';
});
verMaisButton.style.display = 'none';
verMenosButton.style.display = 'block';
}

// Função para mostrar menos resultados
function MostrarMenosResultados() {
listaResultadosOcultados.forEach(resultado => {
resultado.style.display = 'none';
});
verMaisButton.style.display = 'block';
verMenosButton.style.display = 'none';
// Função para mostrar menos resultados
function MostrarMenosResultados() {
listaResultadosOcultados.forEach(resultado => {
resultado.style.display = 'none';
});
verMaisButton.style.display = 'block';
verMenosButton.style.display = 'none';
}
}
}
}

function changeMapLocation(latitude, longitude) {
if (pagina)
{
pagina.mapa.mudarLocalizacao([latitude, longitude]);
}
function pesquisar(id, listaId) {
const el = document.getElementById(id);
let query = el.value;
const pesquisador = new Pesquisador(listaId)
pesquisador.pesquisarLocalizacoes(query);
}

function mostrarOutro() {
Expand All @@ -158,4 +142,9 @@ function updateSelectValue(){
}
}

document.getElementById("meu_formulario").addEventListener("submit",updateSelectValue);
document.getElementById("meu_formulario").addEventListener("submit",updateSelectValue);

// Exporta as classes
module.exports = {
Pesquisador
};
4 changes: 0 additions & 4 deletions lgbtq_connect/assets/js/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ class Mapa {
this.mapa.remove()
this.mapa = null;
}

static sum(a, b) {
return a+b;
}
}

class Pagina {
Expand Down
Loading

0 comments on commit b4abd6e

Please sign in to comment.