Gudu0

UMT Save_Load JS

May 5th, 2025 (edited)
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | Source Code | 0 0
  1. import { calculateTotalCost } from './utility.js';
  2.  
  3. function setupSaveLoadHandlers() {
  4. document.getElementById('saveButton').addEventListener('click', saveGridState);
  5. document.getElementById('load-button').addEventListener('click', () => {
  6. document.getElementById('load-file').click();
  7. });
  8.  
  9. document.getElementById('load-file').addEventListener('change', (event) => {
  10. const file = event.target.files[0];
  11. if (!file) return;
  12.  
  13. const reader = new FileReader();
  14. reader.onload = (e) => {
  15. try {
  16. const json = e.target.result;
  17. loadGridState(json);
  18. } catch (err) {
  19. console.error("Failed to load grid:", err);
  20. }
  21. };
  22. reader.readAsText(file);
  23. });
  24. }
  25.  
  26.  
  27. // Function to save the current grid state
  28. function saveGridState() {
  29. const rows = document.querySelectorAll('.grid tr');
  30. const data = Array.from(rows).map(row => {
  31. return Array.from(row.children).map(cell => {
  32. const tile = cell.querySelector('.tile');
  33. if (!tile.dataset.machineId) return null; // Skip empty tiles
  34. return {
  35. backgroundImage: tile.style.backgroundImage,
  36. backgroundPosition: tile.style.backgroundPosition,
  37. rotation: tile.dataset.rotationCount || '0',
  38. machineId: tile.dataset.machineId || ''
  39. };
  40. });
  41. });
  42.  
  43. // Flatten and remove nulls (empty tiles)
  44. const filteredData = data.map(row => row.map(cell => cell || null));
  45.  
  46. const blob = new Blob([JSON.stringify(filteredData)], { type: 'application/json' });
  47.  
  48. const inputName = document.getElementById('filename')?.value.trim() || 'grid';
  49. const filename = inputName.endsWith('.json') ? inputName : `${inputName}.json`;
  50.  
  51. const link = document.createElement('a');
  52. link.href = URL.createObjectURL(blob);
  53. link.download = filename;
  54. link.click();
  55. }
  56.  
  57.  
  58. // Function to load a saved grid state
  59. function loadGridState(jsonString) {
  60. const data = JSON.parse(jsonString);
  61. const rows = document.querySelectorAll('.grid tr');
  62.  
  63. // Reset rows 0–29 only
  64. rows.forEach((row, rowIndex) => {
  65. if (rowIndex >= 30) return;
  66. Array.from(row.children).forEach(cell => {
  67. const tile = cell.querySelector('.tile');
  68. if (!tile) return;
  69. tile.style.backgroundImage = "url('Images/Black_Square32.jpg')";
  70. tile.style.backgroundPosition = "0px 0px";
  71. tile.dataset.rotationCount = '0';
  72. tile.dataset.machineId = '';
  73. tile.style.transform = 'rotate(0deg)';
  74. });
  75. });
  76.  
  77. // Load saved data into rows 0–29 only
  78. data.forEach((rowData, row) => {
  79. if (row >= 30) return;
  80. rowData.forEach((cellData, col) => {
  81. if (!cellData) return;
  82.  
  83. const rowEl = rows[row];
  84. if (!rowEl) return;
  85.  
  86. const cell = rowEl.children[col];
  87. if (!cell) return;
  88.  
  89. const tile = cell.querySelector('.tile');
  90. if (!tile) return;
  91.  
  92. tile.style.backgroundImage = cellData.backgroundImage;
  93. tile.style.backgroundPosition = cellData.backgroundPosition;
  94. tile.dataset.rotationCount = cellData.rotation;
  95. tile.dataset.machineId = cellData.machineId;
  96. tile.style.transform = `rotate(${cellData.rotation * 90}deg)`;
  97. });
  98. });
  99.  
  100. calculateTotalCost();
  101. }
  102.  
  103.  
  104.  
  105. export { saveGridState, loadGridState, setupSaveLoadHandlers }
Advertisement
Add Comment
Please, Sign In to add comment