Advertisement
plirof2

SGD v008a1 test js FILTER only

Nov 22nd, 2024
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. document.addEventListener('DOMContentLoaded', () => {
  2.     const gameItems = document.querySelectorAll('.game-item');
  3.     const form = document.getElementById('game-form');
  4.     const fields = <?php echo json_encode(array_keys($fieldDefinitions)); ?>;
  5.     const gameImage = document.getElementById('game-screenshot');
  6.     const gameImageContainer = document.getElementById('game-image');
  7.     const filterInput = document.getElementById('filter-input');
  8.     const filterSelect = document.getElementById('filter-select');
  9.     const toggleRightPanelButton = document.getElementById('toggle-right-panel');
  10.     const rightPanel = document.getElementById('right-panel');
  11.     const games = <?php echo json_encode($games); ?>; // Make games available to JavaScript
  12.  
  13.     // Attach click handlers to game items
  14.     gameItems.forEach(item => {
  15.         item.addEventListener('click', () => {
  16.             // Remove active class from all items
  17.             document.querySelectorAll('.game-item').forEach(i => i.classList.remove('active'));
  18.             item.classList.add('active');
  19.  
  20.             // Load game data into form
  21.             const gameData = JSON.parse(item.dataset.game);
  22.             fields.forEach(field => {
  23.                 const input = form.querySelector(`[name="${field}"]`);
  24.                 if (input) input.value = gameData[field];
  25.             });
  26.             form.querySelector('[name="lineIndex"]').value = item.dataset.index; // Store the index for the form submission
  27.  
  28.             // Load and display the game image
  29.             const pcName = gameData['PC-Name']; // Get the PC-Name from the game data
  30.             const imagePath = `SCRSHOT/${pcName}.gif`; // Create the image path
  31.  
  32.             // Set the image source
  33.             gameImage.src = imagePath;
  34.            
  35.             // Check if the image exists, then display it
  36.             const img = new Image();
  37.             img.onload = () => {
  38.                 gameImage.style.display = 'block'; // Show the image if it loads
  39.                 gameImage.src = imagePath; // Set the image source
  40.             };
  41.             img.onerror = () => {
  42.                 gameImage.style.display = 'none'; // Hide image if it doesn't exist
  43.             };
  44.             img.src = imagePath; // Trigger the image load
  45.             updateGameDirectoryDisplay();//console.log("updateGameDirectoryDisplay();");
  46.         });
  47.     });
  48.  
  49.     // Handle form submission
  50.     form.addEventListener('submit', (e) => {
  51.         e.preventDefault();
  52.         const formData = new FormData(form);
  53.  
  54.         fetch('', {
  55.             method: 'POST',
  56.             body: formData
  57.         })
  58.         .then(response => response.json())
  59.         .then(data => {
  60.             if (data.status === 'success') {
  61.                 // Update the left panel with the updated game
  62.                 const updatedGame = data.updatedGame;
  63.                 const lineIndex = data.lineIndex;
  64.                 const updatedGameItem = document.querySelector(`.game-item[data-index="${lineIndex}"]`);
  65.                 if (updatedGameItem) {
  66.                     updatedGameItem.dataset.game = JSON.stringify(updatedGame);
  67.                     updatedGameItem.querySelector('strong').textContent = updatedGame.Name;
  68.                 }
  69.  
  70.                 alert('Game updated successfully!');
  71.             }
  72.         });
  73.     });
  74.  
  75.     // Filter game list
  76.     filterInput.addEventListener('input', filterGames);
  77.     filterSelect.addEventListener('change', filterGames);
  78.  
  79. function filterGames() {
  80.     const filterValue = filterInput.value.toLowerCase();
  81.     const filterField = filterSelect.value;
  82.  
  83.     const filteredGames = games.filter(game => {
  84.         if (filterField === 'Name') {
  85.             return game.Name.toLowerCase().includes(filterValue);
  86.         } else if (filterField === 'Type') {
  87.             return game.Type.toLowerCase().includes(filterValue);
  88.         } else if (filterField === 'Year') {
  89.             return game.Year.includes(filterValue);
  90.         } else if (filterField === 'FirstLetter') {
  91.             return game.Name.toLowerCase().startsWith(filterValue);
  92.         } else {
  93.             return true; // Show all games if no filter is selected
  94.         }
  95.     });
  96.  
  97.     // Update the game list in the left panel
  98.     const gameList = document.getElementById('game-list');
  99.     gameList.innerHTML = '';
  100.  
  101.     filteredGames.forEach((game, index) => {
  102.         const gameItem = document.createElement('tr');
  103.         gameItem.classList.add('game-item');
  104.         gameItem.dataset.game = JSON.stringify(game);
  105.         gameItem.dataset.index = index;
  106.         gameItem.innerHTML = `
  107.             <td><strong>${game.Name}</strong></td>
  108.             <td>${game.Type}</td>
  109.             <td>${game.Year}</td>
  110.             <td>${game['PC-Name']}</td>
  111.             <td>${game.Author}</td>
  112.         `;
  113.         gameList.appendChild(gameItem);
  114.  
  115.         // Attach click handler to the newly created item
  116.         gameItem.addEventListener('click', () => {
  117.             // Remove active class from all items
  118.             document.querySelectorAll('.game-item').forEach(i => i.classList.remove('active'));
  119.             gameItem.classList.add('active');
  120.  
  121.             // Load game data into form
  122.             const gameData = JSON.parse(gameItem.dataset.game);
  123.             fields.forEach(field => {
  124.                 const input = form.querySelector(`[name="${field}"]`);
  125.                 if (input) input.value = gameData[field];
  126.             });
  127.             form.querySelector('[name="lineIndex"]').value = gameItem.dataset.index; // Store the index for the form submission
  128.  
  129.             // Load and display the game image
  130.             const pcName = gameData['PC-Name']; // Get the PC-Name from the game data
  131.             const imagePath = `SCRSHOT/${pcName}.gif`; // Create the image path
  132.  
  133.             // Set the image source
  134.             gameImage.src = imagePath;
  135.  
  136.             // Check if the image exists, then display it
  137.             const img = new Image();
  138.             img.onload = () => {
  139.                 gameImage.style.display = 'block'; // Show the image if it loads
  140.                 gameImage.src = imagePath; // Set the image source
  141.             };
  142.             img.onerror = () => {
  143.                 gameImage.style.display = 'none'; // Hide image if it doesn't exist
  144.             };
  145.             img.src = imagePath; // Trigger the image load
  146.  
  147.             // Call to update the game directory display based on the PathIndex
  148.             updateGameDirectoryDisplay();
  149.         });
  150.     });
  151. }
  152.  
  153. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement