//Criar uma funcao anonima para nao ser acessivel ao client const KeypadSimplesIP = (function () { // Função para verificar e definir o tema inicial const initialThemeMode = sessionStorage.getItem("themeMode") || "light"; const simplesipStateCall = (initialStateCall) => { let stateCall = initialStateCall; function getStateCall() { return stateCall; } function setStateCall(newStateCall) { stateCall = newStateCall; loadThemeMode(); updateUI(); return stateCall; } return { getStateCall, setStateCall }; }; //definir os states para cada fase da chamada do keypad const stateCurrent = simplesipStateCall("home"); const simplesipButtons = [ "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#", ]; //Listeners para testes============================================== document.addEventListener("DOMContentLoaded", function () { document.getElementById("home").addEventListener("click", () => { stateCurrent.setStateCall("home"); }); document.getElementById("outgoingcall").addEventListener("click", () => { stateCurrent.setStateCall("outgoingcall"); }); document.getElementById("incall").addEventListener("click", () => { stateCurrent.setStateCall("incall"); }); document.getElementById("incomingcall").addEventListener("click", () => { stateCurrent.setStateCall("incomingcall"); }); document.getElementById("online").addEventListener("click", () => { defineStatusAgent("online"); }); document.getElementById("busy").addEventListener("click", () => { defineStatusAgent("busy"); }); document.getElementById("away").addEventListener("click", () => { defineStatusAgent("away"); }); }); //================================================================== //RECEBENDO CHAMADA let otherNumber = "Indefinido"; UASimplesIP.simplesipEvento.on("incomingcall", function (callerNumber) { if (otherNumber) { otherNumber = callerNumber; } stateCurrent.setStateCall("incomingcall"); }); //FAZENDO CHAMADA UASimplesIP.simplesipEvento.on("outgoingcall", function (callerNumber) { if (otherNumber) { otherNumber = callerNumber; } stateCurrent.setStateCall("outgoingcall"); }); //EM CHAMADA UASimplesIP.simplesipEvento.on("incall", function () { stateCurrent.setStateCall("incall"); }); //NA HOME UASimplesIP.simplesipEvento.on("confirmedEnded", function () { stateCurrent.setStateCall("home"); }); UASimplesIP.simplesipEvento.on("home", function () { stateCurrent.setStateCall("home"); }); //================================================================== const simplesipCreateContainer = (themeMode) => { let simplesipContainer = document.querySelector(".simplesipContainer"); // Verifica se já existe um container, e se existir, remove-o if (simplesipContainer) { simplesipContainer.remove(); } simplesipContainer = document.createElement("div"); simplesipContainer.classList.add("simplesipContainer"); simplesipContainer.classList.add(themeMode); simplesipContainer.id = "simplesipContainer"; simplesipCreateTitles(simplesipContainer, themeMode); updateUI = function () { const currentStateCall = stateCurrent.getStateCall(); document.addEventListener("keydown", handleKeyPress); // Ao //cada vez que irei atualizar a tela eu devo remover todo o html do filho // e só assim posso estar criando uma nova tela, para n gerar conflitos e bugs let titles = document.querySelector(".simplesipTitles"); if (titles) { // Remover todos os próximos irmãos de titles let proximoIrmao = titles.nextSibling; while (proximoIrmao) { let irmaoRemovido = proximoIrmao; proximoIrmao = proximoIrmao.nextSibling; irmaoRemovido.parentNode.removeChild(irmaoRemovido); } } switch (currentStateCall) { case "home": simplesipCreateKeypad(simplesipContainer, themeMode); break; case "outgoingcall": simplesipCreateOutgoingCall(simplesipContainer, themeMode); break; case "incomingcall": simplesipCreateIncomingCall(simplesipContainer, themeMode); break; case "incall": simplesipCreateInCall(simplesipContainer, themeMode); restartTimer(); startTimer(); break; case "config": simplesipCreateConfigScreen(simplesipContainer, themeMode); break; default: simplesipCreateKeypad(simplesipContainer, themeMode); break; } document.body.appendChild(simplesipContainer); defineStatusAgent(currentStatus); //sempre atualiza o status do agente ao mudar de tela }; updateUI(); return { simplesipCreateContainer }; }; const simplesipCreateConfigScreen = (container, themeMode) => { document.removeEventListener("keydown", handleKeyPress); const configScreen = document.createElement("div"); configScreen.classList.add("configScreen"); configScreen.classList.add(themeMode); configScreen.id = "configScreen"; const titleConfig = document.createElement("div"); titleConfig.id = "titleConfig"; titleConfig.textContent = "Configurações do usuário"; const formScreen = document.createElement("form"); formScreen.id = "form"; formScreen.method = "post"; formScreen.action = ""; const btnSave = document.createElement("button"); btnSave.classList.add("animationHover"); btnSave.id = "btnSave"; btnSave.type = "submit"; btnSave.textContent = "Conectar"; const btnCancel = document.createElement("button"); btnCancel.classList.add("animationHover"); btnCancel.id = "btnCancel"; btnCancel.type = "button"; btnCancel.textContent = "Cancelar"; btnCancel.addEventListener("click", () => { stateCurrent.setStateCall("home"); }); const divDados = document.createElement("div"); divDados.classList.add("divDados"); const divUnregister = document.createElement("div"); divUnregister.classList.add("divUnregister"); const textoUnregister = document.createElement("p"); textoUnregister.textContent = "Apagar ramal"; const unregisterBtn = document.createElement("button"); unregisterBtn.id = "unregister"; unregisterBtn.addEventListener("click", () => { inputServidor.querySelector("input").value = ""; inputPorta.querySelector("input").value = ""; inputNome.querySelector("input").value = ""; inputRamal.querySelector("input").value = ""; inputSenha.querySelector("input").value = ""; UASimplesIP.config.NOME = ""; UASimplesIP.config.PORTA = ""; UASimplesIP.config.SENHA = ""; UASimplesIP.config.SERVIDOR = ""; UASimplesIP.config.RAMAL = ""; localStorage.clear(); currentStatus = "unknown"; stateCurrent.setStateCall("home"); UASimplesIP.unregister(); }); divUnregister.appendChild(textoUnregister); divUnregister.appendChild(unregisterBtn); // Função para criar elementos