Mudanças entre as edições de "Widget:Droflax"

De Wiki Gla
Ir para navegação Ir para pesquisar
Linha 1: Linha 1:
<div id="carrinho-container"></div>
<div id="carrinho-container">
  <div class="item-carrinho" id="slot-1">Seleccione una receta</div>
  <div class="item-carrinho" id="slot-2">Seleccione una receta</div>
</div>


<script>
<script>
const receitasSelecionadas = {};
const receitasSelecionadas = [null, null];  
const MAX_RECEITAS = 2;
const MAX_RECEITAS = 2;
let receitas = {};
let receitas = {};
Linha 8: Linha 11:


function selecionarReceita(id) {
function selecionarReceita(id) {
  if (Object.keys(receitasSelecionadas).length >= MAX_RECEITAS && !receitasSelecionadas[id]) {
    alert("Você só pode selecionar até 2 receitas.");
    return;
  }
   const receita = encontrarReceita(id);
   const receita = encontrarReceita(id);
   if (!receita) return;
   if (!receita) return;


   if (!receitasSelecionadas[id]) {
  const jaSelecionada = receitasSelecionadas.find(r => r?.id === id);
     receitasSelecionadas[id] = { ...receita, quantidade: 1 };
   if (jaSelecionada) return; // Já foi selecionada
 
  const posicaoLivre = receitasSelecionadas.findIndex(r => r === null);
  if (posicaoLivre === -1) {
     alert("Você só pode selecionar até 2 receitas.");
    return;
   }
   }


   renderizarSelecao();
   receitasSelecionadas[posicaoLivre] = { ...receita, quantidade: 1 };
  renderizarSelecionadas();
}
}




Linha 144: Linha 149:
}
}


function renderizarSelecao() {
function renderizarSelecionadas() {
   const container = document.getElementById('carrinho-container');
   const container = document.getElementById('carrinho-container');
   container.innerHTML = '';
   container.innerHTML = '';
Linha 150: Linha 155:
   let total = 0;
   let total = 0;


   for (const id in receitasSelecionadas) {
   for (let i = 0; i < 2; i++) {
    const item = receitasSelecionadas[id];
     const item = receitasSelecionadas[i];
    const subtotal = item.preco * item.quantidade;
    total += subtotal;
 
    // Calcular ingredientes totais dessa receita
     const ingredientesMultiplicados = {};
    item.ingredientes.forEach(([nome, quantidade]) => {
      ingredientesMultiplicados[nome] = quantidade * item.quantidade;
    });


     const itemDiv = document.createElement('div');
     const itemDiv = document.createElement('div');
     itemDiv.className = 'item-carrinho';
     itemDiv.className = 'item-carrinho';
     itemDiv.innerHTML = `
 
      <div style="display: flex; align-items: center; gap: 10px;">
     if (!item) {
         <img src="${item.img}" width="64" height="64" />
      itemDiv.innerHTML = `<div class="vazio">Selecione uma receita</div>`;
         <div>
    } else {
           <div class="nome-item" style="font-weight: bold;">${item.nome}</div>
      const subtotal = item.preco * item.quantidade;
      total += subtotal;
 
      itemDiv.innerHTML = `
         <img src="${item.img}" width="40" height="40" />
         <div class="info-carrinho">
           <div class="nome-item">${item.nome}</div>
           <div class="controle-quantidade">
           <div class="controle-quantidade">
             <button onclick="alterarQuantidadeSelecionada('${id}', -1)">−</button>
             <button onclick="alterarQuantidadeIndex(${i}, -1)">−</button>
             <input type="number" value="${item.quantidade}" min="1" onchange="atualizarQuantidadeManualSelecionada('${id}', event)">
             <input type="number" value="${item.quantidade}" min="1" onchange="atualizarQuantidadeManualIndex(${i}, event)">
             <button onclick="alterarQuantidadeSelecionada('${id}', 1)">+</button>
             <button onclick="alterarQuantidadeIndex(${i}, 1)">+</button>
             <button class="btn-100" onclick="alterarQuantidadeSelecionada('${id}', 100, true)">x100</button>
             <button class="btn-100" onclick="definirQuantidade100(${i})">x100</button>
            <button class="remover-item" onclick="removerReceitaSelecionada('${id}')">🗑️</button>
           </div>
           </div>
         </div>
         </div>
         <div class="subtotal" style="margin-left: auto;">
         <div class="subtotal">${subtotal.toLocaleString()} <img src="${berryGif}" width="25" height="25"/></div>
          ${subtotal.toLocaleString()} <img src="${berryGif}" width="25" height="25" />
        <button class="remover-item" onclick="removerReceita(${i})">🗑️</button>
        </div>
      `;
      </div>
     }
      <div style="margin-top: 10px;"><strong>🥣 Ingredientes:</strong></div>
      <div style="display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px;">
        ${Object.entries(ingredientesMultiplicados).map(([nome, qtd]) => `
          <div class="ingrediente-item">${qtd}x ${nome}</div>
        `).join('')}
      </div>
     `;


     container.appendChild(itemDiv);
     container.appendChild(itemDiv);
   }
   }
  document.getElementById('carrinho-total').innerHTML = total > 0
    ? `Total: ${total.toLocaleString()} <img src="${berryGif}" width="25" height="25"/>`
    : '';
}
}


function alterarQuantidadeSelecionada(id, alteracao, substituir = false) {
 
   if (!receitasSelecionadas[id]) return;
function alterarQuantidadeIndex(index, alteracao) {
  if (substituir) {
   if (receitasSelecionadas[index]) {
     receitasSelecionadas[id].quantidade = alteracao;
     receitasSelecionadas[index].quantidade += alteracao;
  } else {
     if (receitasSelecionadas[index].quantidade <= 0) receitasSelecionadas[index].quantidade = 1;
    receitasSelecionadas[id].quantidade += alteracao;
     renderizarSelecionadas();
     if (receitasSelecionadas[id].quantidade < 1) {
      receitasSelecionadas[id].quantidade = 1;
     }
   }
   }
  renderizarSelecao();
}
}


function atualizarQuantidadeManualSelecionada(id, event) {
function atualizarQuantidadeManualIndex(index, event) {
   const novoValor = parseInt(event.target.value);
   const novoValor = parseInt(event.target.value);
   if (!isNaN(novoValor) && novoValor >= 1) {
   if (!isNaN(novoValor) && novoValor >= 1) {
     receitasSelecionadas[id].quantidade = novoValor;
     receitasSelecionadas[index].quantidade = novoValor;
     renderizarSelecao();
     renderizarSelecionadas();
   }
   }
}
}


function removerReceitaSelecionada(id) {
function definirQuantidade100(index) {
   delete receitasSelecionadas[id];
   if (receitasSelecionadas[index]) {
  renderizarSelecao();
    receitasSelecionadas[index].quantidade = 100;
    renderizarSelecionadas();
  }
}
}


function removerReceita(index) {
  receitasSelecionadas[index] = null;
  renderizarSelecionadas();
}


// CSS
// CSS

Edição das 04h04min de 11 de abril de 2025

Seleccione una receta
Seleccione una receta

<script> const receitasSelecionadas = [null, null]; const MAX_RECEITAS = 2; let receitas = {}; const berryGif = "https://wiki.gla.com.br/images/thumb/d/d1/Berrynewgif.gif/32px-Berrynewgif.gif";

function selecionarReceita(id) {

 const receita = encontrarReceita(id);
 if (!receita) return;
 const jaSelecionada = receitasSelecionadas.find(r => r?.id === id);
 if (jaSelecionada) return; // Já foi selecionada
 const posicaoLivre = receitasSelecionadas.findIndex(r => r === null);
 if (posicaoLivre === -1) {
   alert("Você só pode selecionar até 2 receitas.");
   return;
 }
 receitasSelecionadas[posicaoLivre] = { ...receita, quantidade: 1 };
 renderizarSelecionadas();

}


// Função para carregar as receitas a partir do JSON async function cargarReceitas() {

 try {
   const response = await fetch('https://wiki.gla.com.br/index.php?title=Receitas.json&action=raw');
   const data = await response.json();
   
   receitas = {
     baratie: Object.entries(data.baratie).map(([id, item]) => ({
       id,
       nome: item.nome,
       preco: item.cost,
       img: item.thumb,
       ingredientes: item.ingredients || [],
       lvl: item.lvl,
       tempo: item.time,
       descricao: item.description
     })),
     alianca: Object.entries(data.alianca).map(([id, item]) => ({
       id,
       nome: item.nome,
       preco: item.cost,
       img: item.thumb,
       ingredientes: item.ingredients || [],
       lvl: item.lvl,
       tempo: item.time,
       descricao: item.description
     }))
   };
   
   renderizarReceitas();
 } catch (error) {
   console.error('Error al cargar las receitas:', error);
   alert('No se pudieron cargar las receitas. Por favor, inténtalo más tarde.');
 }

}

function renderizarReceitas() {

 renderizarSecao('baratie');
 renderizarSecao('alianca');

}

function renderizarSecao(secao) {

 const container = document.getElementById(`receitas-${secao}`);
 if (!container) return;
 container.innerHTML = ;
 receitas[secao].forEach(receita => {
   const card = document.createElement('div');
   card.className = 'receita-card';
   card.setAttribute('data-id', receita.id);
   card.innerHTML = `
     <button class="info-btn" onclick="mostrarModalReceita(this)" 
       data-descricao="${encodeURIComponent(receita.descricao)}"
       data-lvl="${receita.lvl}"
       data-tempo="${receita.tempo}">i</button>
     <img src="${receita.img}" width="48" height="48" />
${receita.nome}
${receita.preco.toLocaleString()} <img src="${berryGif}" alt="Berry" width="25" height="25" style="transform: translateY(-1px);"/>
     <button class="add-to-cart-btn" onclick="selecionarReceita('${receita.id}')">Selecionar</button>
   `;
   container.appendChild(card);
 });

}

function mostrarModalReceita(button) {

 const descricao = decodeURIComponent(button.getAttribute('data-descricao'));
 const lvl = button.getAttribute('data-lvl');
 const tempo = button.getAttribute('data-tempo');
 const descricaoFormatada = descricao
   .replace(/\n/g, '
') .replace(/Tempo de recarga/g, '
Tempo de recarga');
 const modalHTML = `
 `;
 
 document.body.insertAdjacentHTML('beforeend', modalHTML);
 document.addEventListener('keydown', (e) => e.key === 'Escape' && cerrarModal(e));

}

function cerrarModal(event) {

 event.preventDefault();
 const modal = document.querySelector('.modal-overlay');
 if (modal) {
   modal.classList.remove('active');
   setTimeout(() => modal.remove(), 300);
 }

}

function encontrarReceita(id) {

 for (const secao in receitas) {
   const receita = receitas[secao].find(r => r.id === id);
   if (receita) return receita;
 }
 return null;

}

function esvaziarCarrinho() {

 for (const id in carrinho) {
   delete carrinho[id];
   const receitaCard = document.querySelector(`[data-id="${id}"]`);
   if (receitaCard) {
     receitaCard.style.backgroundColor = ;
   }
 }
 renderizarCarrinho();
 const detalhesContainer = document.getElementById('detalhes-carrinho');
 if (detalhesContainer) {
   detalhesContainer.innerHTML = ;
 }

}

function renderizarSelecionadas() {

 const container = document.getElementById('carrinho-container');
 container.innerHTML = ;
 let total = 0;
 for (let i = 0; i < 2; i++) {
   const item = receitasSelecionadas[i];
   const itemDiv = document.createElement('div');
   itemDiv.className = 'item-carrinho';
   if (!item) {

itemDiv.innerHTML = `

Selecione uma receita

`;

   } else {
     const subtotal = item.preco * item.quantidade;
     total += subtotal;
     itemDiv.innerHTML = `
       <img src="${item.img}" width="40" height="40" />
${item.nome}
           <button onclick="alterarQuantidadeIndex(${i}, -1)">−</button>
           <input type="number" value="${item.quantidade}" min="1" onchange="atualizarQuantidadeManualIndex(${i}, event)">
           <button onclick="alterarQuantidadeIndex(${i}, 1)">+</button>
           <button class="btn-100" onclick="definirQuantidade100(${i})">x100</button>
${subtotal.toLocaleString()} <img src="${berryGif}" width="25" height="25"/>
       <button class="remover-item" onclick="removerReceita(${i})">🗑️</button>
     `;
   }
   container.appendChild(itemDiv);
 }
 document.getElementById('carrinho-total').innerHTML = total > 0
   ? `Total: ${total.toLocaleString()} <img src="${berryGif}" width="25" height="25"/>`
   : ;

}


function alterarQuantidadeIndex(index, alteracao) {

 if (receitasSelecionadas[index]) {
   receitasSelecionadas[index].quantidade += alteracao;
   if (receitasSelecionadas[index].quantidade <= 0) receitasSelecionadas[index].quantidade = 1;
   renderizarSelecionadas();
 }

}

function atualizarQuantidadeManualIndex(index, event) {

 const novoValor = parseInt(event.target.value);
 if (!isNaN(novoValor) && novoValor >= 1) {
   receitasSelecionadas[index].quantidade = novoValor;
   renderizarSelecionadas();
 }

}

function definirQuantidade100(index) {

 if (receitasSelecionadas[index]) {
   receitasSelecionadas[index].quantidade = 100;
   renderizarSelecionadas();
 }

}

function removerReceita(index) {

 receitasSelecionadas[index] = null;
 renderizarSelecionadas();

}

// CSS document.head.insertAdjacentHTML('beforeend', ` <style>

 .receitas-container {
   display: flex;
   flex-wrap: wrap;
   gap: 16px;
   margin-bottom: 24px;
 }
 .nome-item, .subtotal{
   user-select: none;
 }
 
 .receita-card {
   width: 96px;
   text-align: center;
   border: 1px solid #ccc;
   border-radius: 8px;
   padding: 8px;
   background-color: #fff;
   box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
   transition: transform 0.2s;
   user-select: none;
   position: relative;
 }
 .modal-overlay {
   position: fixed;
   top: 0;
   left: 0;
   right: 0;
   bottom: 0;
   background-color: rgba(0, 0, 0, 0.5);
   display: flex;
   justify-content: center;
   align-items: center;
   z-index: 1000;
   opacity: 0;
   visibility: hidden;
   transition: all 0.3s ease;
 }
 .modal-overlay.active {
   opacity: 1;
   visibility: visible;
 }
 .modal-content {
   background-color: white;
   padding: 20px;
   border-radius: 8px;
   width: 300px;
   position: relative;
   box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
 }
 .close-modal {
   position: absolute;
   top: 10px;
   right: 10px;
   font-size: 20px;
   cursor: pointer;
   background: none;
   border: none;
 }
 .info-btn {
   position: absolute;
   top: 5px;
   right: 5px;
   background: #4393ff;
   color: white;
   border: none;
   border-radius: 50%;
   width: 20px;
   height: 20px;
   font-size: 12px;
   cursor: pointer;
   display: flex;
   justify-content: center;
   align-items: center;
 }
 .receita-card:hover {
   transform: scale(1.05);
 }
 .receita-nome {
   font-weight: bold;
   margin: 4px 0;
   min-height: 3em; /* força ocupar duas linhas mesmo se o texto for curto */
   line-height: 1.5em;
   display: flex;
   align-items: center;
   justify-content: center;
   text-align: center;
   font-size: 88%;
 }
 .receita-preco {
   margin-bottom: 6px;
 }
 
 .item-carrinho {
   display: flex;
   gap: 12px;
   padding: 8px;
   border: 1px solid #ccc;
   border-radius: 8px;
   margin-bottom: 8px;
   width: 44.5%;
   margin-inline: 1%;
   flex-direction: column;
   align-items: center;
 }
 
 .info-carrinho {
   flex: 1;
 }
 
 .controle-quantidade {
   display: flex;
   align-items: center;
   gap: 4px;
   margin-top: 4px;
 }
 
 .controle-quantidade input {
   width: 50px;
   text-align: center;
   padding: 2px;
 }
 
 .controle-quantidade button {
   width: 24px;
   height: 24px;
 }
 
 .btn-100 {
   width: auto !important;
   padding: 0 4px;
   margin-left: 4px;
   font-size: 12px;
 }
 
 .remover-item {
   background: none;
   border: none;
   cursor: pointer;
   font-size: 16px;
 }
 .ingredientes-grid {
   display: grid;
   grid-template-columns: repeat(2, 1fr); /* Fuerza 2 columnas */
   gap: 8px;
   margin-top: 5px;
   margin-bottom: 10px;
 }
 .ingrediente-item {
   background-color: #f9f9f9;
   border: 1px solid #ddd;
   border-radius: 4px;
   padding: 6px 8px;
   font-size: 14px;
   text-align: center;
   white-space: normal;
   overflow: hidden;
   text-overflow: unset;
   overflow-wrap: break-word;
   align-content: center;
 }
 
 .acao-carrinho {
   padding: 8px 16px;
   background-color: #4393ff;
   color: white;
   border: none;
   border-radius: 4px;
   cursor: pointer;
 }
 
 .acao-carrinho:hover {
   background-color: #3275c7;
 }
 #carrinho-container {
   display: flex;
   flex-direction: row;
   flex-wrap: wrap;
   justify-content: center;
   margin-top: 1.5%;
 }
 .receita-card.highlighted {
   background-color: lightgreen;
 }
 .detalhe-receita ul {
   padding-left: 20px;
 }
 .detalhe-receita li {
   margin-bottom: 3px;
 }
 .detalhe-receita {
   width: 16%;
   justify-items: anchor-center;
 }
 .receitas-wrapper{
   display: flex;
   flex-direction: column;
   width: 48%;
 }
 .full-receitas {
   display: flex;
   flex-direction: row;
   flex-wrap: nowrap;
 }
 @media screen and (max-width: 1366px) {
   .item-carrinho {
     width: 29.3%;
   }
 }
 @media (max-width: 768px) {
   .item-carrinho {
     width: 43%;
   }
 }
 @media only screen and (min-width: 1280px) and (max-width: 1280px) and (min-height: 1024px) and (max-height: 1024px) {
   .item-carrinho {
     width: 32%;
   }
 }

</style> `);

// Carregar as receitas ao iniciar document.addEventListener('DOMContentLoaded', cargarReceitas); </script>