Advertisement
jargon

Grid Builder.js

Mar 4th, 2025
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function permuteDirections(col, row) {
  2.    
  3.     let entry = jsonData[col]?.[row]; // Avoid errors if jsonData[col][row] is undefined
  4.     if (!entry || !entry.connections) return [0, [], []];
  5.  
  6.     let conn = entry.connections;
  7.    
  8.     let directionCount = 0;
  9.     let directionsDetected = [];
  10.     let directionPairs = [];
  11.  
  12.     for (let [direction, value] of Object.entries(conn)) {
  13.         if (directions.includes(direction)) {
  14.             directionCount++;
  15.             directionsDetected.push(direction);
  16.         }
  17.     }
  18.  
  19.     console.log(entry.text, directionCount, directionsDetected);
  20.  
  21.     if (directionCount >= 2) {
  22.         for (let i = 0; i < directionsDetected.length - 1; i++) {
  23.             for (let j = i + 1; j < directionsDetected.length; j++) {
  24.                 directionPairs.push(`${directionsDetected[i]}-${directionsDetected[j]}`);
  25.             }
  26.         }
  27.     }
  28.  
  29.     return [directionCount, directionsDetected, directionPairs];
  30. }
  31.  
  32. function createArray(columns, rows, defaultValue = false, pad = true) {
  33.     if (pad === true) {
  34.         rows += 1;
  35.         columns += 1;
  36.     }
  37.     return Array.from({ length: rows }, () => Array(columns).fill(defaultValue));
  38. }
  39.  
  40. function gridSetup() {
  41.     jsonData = loadJsonSync(
  42.         `http://${window.location.hostname}/jpsys/Shared/scripts/json/menu/${System}.json`
  43.     );
  44.  
  45.     grid = {};
  46.     mainGrid = document.getElementById("main-grid");
  47.  
  48.     totalColumns = Object.keys(jsonData).length;
  49.     columnRowCounts = {};
  50.  
  51.     for (let col = 1; col <= totalColumns; col++) {
  52.         columnRowCounts[col] = Object.keys(jsonData[col]).length;
  53.     }
  54.  
  55.     maxRows = Math.max(...Object.values(columnRowCounts));
  56.  
  57.     grid = createArray(totalColumns, maxRows, false);
  58.    
  59.     console.log(grid);
  60.     console.log(`Menu Span - Columns: ${totalColumns}, Rows: ${maxRows}`);
  61.  
  62.     for (let col = 1; col <= totalColumns; col++) {
  63.         for (let row = 1; row <= maxRows; row++) {
  64.            
  65.             if (row >= 1 && row <= columnRowCounts[col] && col >= 1 && col <= totalColumns) {
  66.                
  67.                 if (jsonData[col] && jsonData[col][row] !== undefined) {
  68.                    
  69.                     if (!grid[col]) {
  70.                         grid[col] = {};
  71.                     }
  72.                     grid[col][row] = jsonData[col][row];
  73.                 }
  74.             }
  75.             grid[col][row].id = `grid-item-${col}-${row}`;
  76.             console.log(`Processing Column: ${col}, Row: ${row}`);
  77.             console.log(grid[col][row]);
  78.         }
  79.     }
  80.  
  81.     // Ensure all classes are added first before injecting grid items
  82.     for (let row = 1; row <= maxRows; row++) {
  83.         for (let col = 1; col <= totalColumns; col++) {
  84.             FirstPassLayer1(col, row);
  85.             FirstPassLayer2(col, row);
  86.         }
  87.     }
  88.  
  89.     for (let row = 1; row <= maxRows; row++) {
  90.         for (let col = 1; col <= totalColumns; col++) {
  91.             InjectClasses(col, row);
  92.         }
  93.     }
  94.  
  95.     // โœ… Ensure layout fixes are applied **after** grid items are appended
  96.     setTimeout(fixLayout, 100);
  97. }
  98.  
  99. function FirstPassLayer1(col, row) {
  100.    
  101.     if (grid === undefined)
  102.     {
  103.         grid = [ ];
  104.     }
  105.  
  106.     if (grid[col] === undefined)
  107.     {
  108.         grid[col] = [ ];
  109.     }
  110.  
  111.     if (grid[col][row] === undefined)
  112.     {
  113.         grid[col][row] = {text: false, classes: []};
  114.     }
  115.    
  116.     if (!jsonData[col] || jsonData[col][row] === undefined)
  117.     {
  118.         grid[col][row] = {text: false, classes: []};
  119.        
  120.     }else {
  121.         grid[col][row] = jsonData[col][row];
  122.     }
  123.  
  124.     if (grid[col][row] === false)
  125.     {
  126.         grid[col][row] = {text: false, classes: []};
  127.        
  128.     }
  129.  
  130.     if (grid[col][row].classes === undefined) {
  131.         grid[col][row].classes = [];
  132.     }
  133.  
  134.     if (grid[col][row].text === false) {
  135.         // grid[col][row].classes.push("hidden-item");
  136.         grid[col][row].classes.push("grid-item");
  137.     }
  138.     else
  139.     {
  140.         grid[col][row].classes.push("grid-item");
  141.     }
  142.    
  143.     grid[col][row].id = `grid-item-${col}-${row}`;
  144. }
  145.  
  146. function FirstPassLayer2(col, row) {
  147.    
  148.     let entry = jsonData[col][row];
  149.     let conn = entry.connections;
  150.    
  151.     if (!conn) return;
  152.    
  153.     let
  154.     [
  155.         directionCount,
  156.         directionsDetected,
  157.         directionBiets
  158.     ] = permuteDirections ( col, row );
  159.    
  160.     if (directionCount >= 1) {
  161.         directionsDetected.forEach(direction => {
  162.             if (!conn[direction]) return;
  163.             grid[col][row].classes.push("connection");
  164.             grid[col][row].classes.push(`connection-${direction}`);
  165.         });
  166.     }
  167.  
  168.     if (directionCount >= 2) {
  169.         if (conn.type === "t") {
  170.             directionsDetected.forEach(direction => {
  171.                 if (!conn[direction]) return;
  172.                
  173.                 if (['up'].includes(direction)) {
  174.                     let faceSwitch = { '-1': 'left', '0': false, '1': 'right' };
  175.                     let face = faceSwitch[Math.sign(conn[direction] - col).toString()];
  176.  
  177.                     if (face !== false) {
  178.                         grid[col][row].classes.push("connection");
  179.                         grid[col][row].classes.push(`connection-${conn.type}`);
  180.                         grid[col][row].classes.push(`connection-${conn.type}-${face}`);
  181.                     } else {
  182.                         grid[col][row].classes.push("connection");
  183.                         grid[col][row].classes.push(`connection-${direction}`);
  184.                     }
  185.                 }
  186.             });
  187.         }
  188.     }
  189. }
  190.  
  191. function InjectClasses(col, row) {
  192.    
  193.     grid[col][row].id = `grid-item-${col}-${row}`;
  194.    
  195.     let entry = grid[col][row];
  196.        
  197.     let gridItem = document.createElement("div");
  198.    
  199.     // gridItem.classList.add("grid-item");
  200.     gridItem.id = entry.id;
  201.  
  202.     // โœ… Corrected grid-row and grid-column assignment
  203.     gridItem.style.gridRow = `${row}`;
  204.     gridItem.style.gridColumn = `${col}`;
  205.  
  206.     if (entry.text !== false)
  207.     {
  208.         gridItem.innerHTML = entry.text;
  209.     }else
  210.     {
  211.         grid[col][row].styles = [
  212.             "border: 2px solid rgba(0,0,0,0.0)",
  213.             "padding: 5px",
  214.             "background-color: rgba(0,0,0,0.0)",
  215.             "text-align: center",
  216.             "font-size: 14px",
  217.             "display: flex",
  218.             "align-items: center",
  219.             "justify-content: center",
  220.             "width: 70px",
  221.             "height: 30px",
  222.             "position: relative",
  223.             "z-index: 6"
  224.         ];
  225.        
  226.         grid[col][row].styles.forEach(stl => gridItem.style.cssText += stl + "; ");
  227.     }
  228.    
  229.     grid[col][row].classes.forEach(cls => gridItem.classList.add(cls));
  230.  
  231.     let mainGrid = document.getElementById("main-grid");
  232.     mainGrid.appendChild(gridItem);
  233. }
  234.  
  235. function PageSetup() {
  236.     const SystemReferenceUrl = `http://${window.location.hostname}/jpsys/Shared/scripts/php/sysref.php?files`;
  237.     Files = loadJsonSync(SystemReferenceUrl);
  238.  
  239.     const SystemDefaultsUrl = `http://${window.location.hostname}/jpsys/Shared/scripts/json/Defaults.json`;
  240.     Defaults = loadJsonSync(SystemDefaultsUrl);
  241.  
  242.     globalName = Defaults['environment'];
  243.     System = urlParams.get('sys') ?? Defaults['sys'];
  244.  
  245.     if (!Files.includes(System)) {
  246.         System = Defaults['sys'];
  247.     }
  248.  
  249.     menuName = `${globalName} - ${System}`;
  250.     console.log("Document Title:", menuName);
  251.     document.title = menuName;
  252.  
  253.     titleElement = document.querySelector(".container .title");
  254.     if (titleElement) {
  255.         titleElement.textContent = menuName.toUpperCase();
  256.     }
  257.  
  258.     gridSetup();
  259. }
  260.  
  261. function fixLayout() {
  262.     let grid = document.querySelector(".grid");
  263.  
  264.     if (!grid) {
  265.         console.error("Grid element not found!");
  266.         return;
  267.     }
  268.  
  269.     // โœ… Ensure grid has a proper width before applying layout
  270.     grid.style.display = "grid";
  271.     grid.style.width = `calc(${totalColumns} * (70px + 10px * 2))`;
  272.     grid.style.gridTemplateColumns = `repeat(${totalColumns}, calc(70px + 10px * 2))`;
  273.     grid.style.gridTemplateRows = `repeat(${maxRows}, auto)`;
  274.  
  275.     document.querySelectorAll(".grid-item").forEach((item, index) => {
  276.         let col = (index % totalColumns) + 1;
  277.         let row = Math.floor(index / totalColumns) + 1;
  278.  
  279.         item.style.gridColumn = col;
  280.         item.style.gridRow = row;
  281.     });
  282. }
  283.  
  284. PageSetup();
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement