base.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. // Player status bar functionality - Updated to fix logMessage errors
  2. // Update LED nav label based on provider
  3. async function updateLedNavLabel() {
  4. try {
  5. const response = await fetch('/get_led_config');
  6. if (response.ok) {
  7. const data = await response.json();
  8. const navLabel = document.getElementById('led-nav-label');
  9. if (navLabel) {
  10. if (data.provider === 'wled') {
  11. navLabel.textContent = 'WLED';
  12. } else if (data.provider === 'dw_leds') {
  13. navLabel.textContent = 'DW LEDs';
  14. } else {
  15. navLabel.textContent = 'LED';
  16. }
  17. }
  18. }
  19. } catch (error) {
  20. console.error('Error updating LED nav label:', error);
  21. }
  22. }
  23. // Call on page load
  24. document.addEventListener('DOMContentLoaded', updateLedNavLabel);
  25. // Pattern files cache for improved performance with localStorage persistence
  26. const PATTERN_CACHE_KEY = 'dune_weaver_pattern_files_cache';
  27. const PATTERN_CACHE_EXPIRY = 30 * 60 * 1000; // 30 minutes cache (longer since it persists)
  28. // Function to get cached pattern files or fetch fresh data
  29. async function getCachedPatternFiles(forceRefresh = false) {
  30. const now = Date.now();
  31. // Try to load from localStorage first
  32. if (!forceRefresh) {
  33. try {
  34. const cachedData = localStorage.getItem(PATTERN_CACHE_KEY);
  35. if (cachedData) {
  36. const { files, timestamp } = JSON.parse(cachedData);
  37. if (files && timestamp && (now - timestamp) < PATTERN_CACHE_EXPIRY) {
  38. console.log('Using cached pattern files from localStorage');
  39. return files;
  40. }
  41. }
  42. } catch (error) {
  43. console.warn('Error reading pattern files cache from localStorage:', error);
  44. }
  45. }
  46. try {
  47. console.log('Fetching fresh pattern files from server');
  48. const response = await fetch('/list_theta_rho_files');
  49. if (!response.ok) {
  50. throw new Error(`Failed to fetch pattern files: ${response.status}`);
  51. }
  52. const files = await response.json();
  53. // Store in localStorage
  54. try {
  55. const cacheData = { files, timestamp: now };
  56. localStorage.setItem(PATTERN_CACHE_KEY, JSON.stringify(cacheData));
  57. } catch (error) {
  58. console.warn('Error storing pattern files cache in localStorage:', error);
  59. }
  60. return files;
  61. } catch (error) {
  62. console.error('Error fetching pattern files:', error);
  63. // Try to return any cached data as fallback, even if expired
  64. try {
  65. const cachedData = localStorage.getItem(PATTERN_CACHE_KEY);
  66. if (cachedData) {
  67. const { files } = JSON.parse(cachedData);
  68. if (files) {
  69. console.log('Using expired cached pattern files as fallback');
  70. return files;
  71. }
  72. }
  73. } catch (fallbackError) {
  74. console.warn('Error reading fallback cache:', fallbackError);
  75. }
  76. return [];
  77. }
  78. }
  79. // Function to invalidate pattern files cache
  80. function invalidatePatternFilesCache() {
  81. try {
  82. localStorage.removeItem(PATTERN_CACHE_KEY);
  83. console.log('Pattern files cache invalidated');
  84. } catch (error) {
  85. console.warn('Error invalidating pattern files cache:', error);
  86. }
  87. }
  88. // Helper function to normalize file paths for cross-platform compatibility
  89. function normalizeFilePath(filePath) {
  90. if (!filePath) return '';
  91. // First normalize path separators
  92. let normalized = filePath.replace(/\\/g, '/');
  93. // Remove only the patterns directory prefix, not patterns within the path
  94. if (normalized.startsWith('./patterns/')) {
  95. normalized = normalized.substring(11);
  96. } else if (normalized.startsWith('patterns/')) {
  97. normalized = normalized.substring(9);
  98. }
  99. return normalized;
  100. }
  101. let ws = null;
  102. let reconnectAttempts = 0;
  103. const maxReconnectAttempts = 5;
  104. const reconnectDelay = 3000; // 3 seconds
  105. let isEditingSpeed = false; // Track if user is editing speed
  106. let playerPreviewData = null; // Store the current pattern's preview data for modal
  107. let playerPreviewCtx = null; // Store the canvas context for modal preview
  108. let playerAnimationId = null; // Store animation frame ID for modal
  109. let lastProgress = 0; // Last known progress from backend
  110. let targetProgress = 0; // Target progress to animate towards
  111. let animationStartTime = 0; // Start time of current animation
  112. let animationDuration = 1000; // Duration of interpolation in ms
  113. let smoothAnimationStartTime = 0; // Start time for smooth coordinate animation
  114. let smoothAnimationActive = false; // Whether smooth animation is running
  115. let modalAnimationId = null; // Store animation frame ID for modal
  116. let modalLastProgress = 0; // Last known progress for modal
  117. let modalTargetProgress = 0; // Target progress for modal
  118. let modalAnimationStartTime = 0; // Start time for modal animation
  119. let userDismissedModal = false; // Track if user has manually dismissed the modal
  120. // Function to set modal visibility
  121. function setModalVisibility(show, userAction = false) {
  122. const modal = document.getElementById('playerPreviewModal');
  123. if (!modal) return;
  124. if (show) {
  125. modal.classList.remove('hidden');
  126. } else {
  127. modal.classList.add('hidden');
  128. }
  129. if (userAction) {
  130. userDismissedModal = !show;
  131. }
  132. }
  133. let currentPreviewFile = null; // Track the current file for preview data
  134. function connectWebSocket() {
  135. if (ws) {
  136. ws.close();
  137. }
  138. ws = new WebSocket(`ws://${window.location.host}/ws/status`);
  139. ws.onopen = function() {
  140. console.log("WebSocket connection established");
  141. reconnectAttempts = 0;
  142. };
  143. ws.onclose = function() {
  144. console.log("WebSocket connection closed");
  145. if (reconnectAttempts < maxReconnectAttempts) {
  146. reconnectAttempts++;
  147. setTimeout(connectWebSocket, reconnectDelay);
  148. }
  149. };
  150. ws.onerror = function(error) {
  151. console.error("WebSocket error:", error);
  152. };
  153. ws.onmessage = function(event) {
  154. try {
  155. const data = JSON.parse(event.data);
  156. if (data.type === 'status_update') {
  157. // Update modal status with the full data
  158. syncModalControls(data.data);
  159. // Update speed input field on table control page if it exists
  160. if (data.data && data.data.speed) {
  161. const currentSpeedDisplay = document.getElementById('currentSpeedDisplay');
  162. if (currentSpeedDisplay) {
  163. currentSpeedDisplay.textContent = `${data.data.speed} mm/s`;
  164. }
  165. }
  166. // Update connection status dot using 'connection_status' or fallback to 'connected'
  167. if (data.data.hasOwnProperty('connection_status')) {
  168. updateConnectionStatus(data.data.connection_status);
  169. }
  170. // Check if current file has changed and reload preview data if needed
  171. if (data.data.current_file) {
  172. const newFile = normalizeFilePath(data.data.current_file);
  173. if (newFile !== currentPreviewFile) {
  174. currentPreviewFile = newFile;
  175. // Only preload if modal exists on this page (avoids waste on settings/LED pages)
  176. const modal = document.getElementById('playerPreviewModal');
  177. if (modal) {
  178. loadPlayerPreviewData(data.data.current_file);
  179. }
  180. }
  181. } else {
  182. currentPreviewFile = null;
  183. playerPreviewData = null;
  184. }
  185. // Update progress for modal animation with smooth interpolation
  186. if (playerPreviewData && data.data.progress && data.data.progress.percentage !== null) {
  187. const newProgress = data.data.progress.percentage / 100;
  188. targetProgress = newProgress;
  189. // Update modal if open with smooth animation
  190. const modal = document.getElementById('playerPreviewModal');
  191. if (modal && !modal.classList.contains('hidden')) {
  192. updateModalPreviewSmooth(newProgress);
  193. }
  194. }
  195. // Reset userDismissedModal flag if no pattern is playing
  196. if (!data.data.current_file || !data.data.is_running) {
  197. userDismissedModal = false;
  198. }
  199. }
  200. } catch (error) {
  201. console.error("Error processing WebSocket message:", error);
  202. }
  203. };
  204. }
  205. function updateConnectionStatus(isConnected) {
  206. const statusDot = document.getElementById("connectionStatusDot");
  207. if (statusDot) {
  208. // Update dot color
  209. statusDot.className = `inline-block size-2 rounded-full ml-2 align-middle ${
  210. isConnected ? "bg-green-500" : "bg-red-500"
  211. }`;
  212. }
  213. }
  214. // Setup player preview with expand button
  215. function setupPlayerPreview() {
  216. const previewContainer = document.getElementById('player-pattern-preview');
  217. if (!previewContainer) return;
  218. // Get current background image URL
  219. const currentBgImage = previewContainer.style.backgroundImage;
  220. // Clear container
  221. previewContainer.innerHTML = '';
  222. previewContainer.style.backgroundImage = '';
  223. // Create preview image container
  224. const imageContainer = document.createElement('div');
  225. imageContainer.className = 'relative aspect-square rounded-full overflow-hidden w-full h-full';
  226. // Create image element
  227. const img = document.createElement('img');
  228. img.className = 'w-full h-full object-cover';
  229. // img.alt = 'Pattern Preview';
  230. // Extract URL from background-image CSS
  231. img.src = currentBgImage.replace(/^url\(['"](.+)['"]\)$/, '$1');
  232. // Add expand button overlay
  233. const expandOverlay = document.createElement('div');
  234. expandOverlay.className = 'absolute inset-0 flex items-center justify-center opacity-0 hover:opacity-100 transition-opacity duration-200 cursor-pointer z-20 bg-black bg-opacity-20 hover:bg-opacity-30';
  235. expandOverlay.innerHTML = '<div class="bg-white rounded-full p-3 shadow-lg flex items-center justify-center w-12 h-12"><span class="material-icons text-xl text-gray-800">fullscreen</span></div>';
  236. // Add click handler for expand button
  237. expandOverlay.addEventListener('click', (e) => {
  238. e.stopPropagation();
  239. openPlayerPreviewModal();
  240. });
  241. // Add image and overlay to image container
  242. imageContainer.appendChild(img);
  243. imageContainer.appendChild(expandOverlay);
  244. // Add image container to preview container
  245. previewContainer.appendChild(imageContainer);
  246. }
  247. // Open player preview modal
  248. async function openPlayerPreviewModal() {
  249. try {
  250. const modal = document.getElementById('playerPreviewModal');
  251. const title = document.getElementById('playerPreviewTitle');
  252. const canvas = document.getElementById('playerPreviewCanvas');
  253. const ctx = canvas.getContext('2d');
  254. const toggleBtn = document.getElementById('toggle-preview-modal-btn');
  255. // Set static title
  256. title.textContent = 'Live Pattern Preview';
  257. // Load preview data on-demand if not already loaded (fallback for edge cases)
  258. if (!playerPreviewData && currentPreviewFile) {
  259. // Show loading indicator
  260. title.textContent = 'Loading pattern...';
  261. await loadPlayerPreviewData(`./patterns/${currentPreviewFile}`);
  262. title.textContent = 'Live Pattern Preview';
  263. }
  264. // Show modal and update toggle button
  265. modal.classList.remove('hidden');
  266. // Setup canvas
  267. setupPlayerPreviewCanvas(ctx);
  268. // Draw initial state
  269. drawPlayerPreview(ctx, targetProgress);
  270. } catch (error) {
  271. console.error(`Error opening player preview modal: ${error.message}`);
  272. showStatusMessage('Failed to load pattern for animation', 'error');
  273. }
  274. }
  275. // Setup player preview canvas for modal
  276. function setupPlayerPreviewCanvas(ctx) {
  277. const canvas = ctx.canvas;
  278. const container = canvas.parentElement; // This is the div with max-w and max-h constraints
  279. const modal = document.getElementById('playerPreviewModal');
  280. if (!container || !modal) return;
  281. // Calculate available viewport space directly
  282. const viewportWidth = window.innerWidth;
  283. const viewportHeight = window.innerHeight;
  284. // Calculate maximum canvas size based on viewport and fixed estimates
  285. // Modal uses max-w-5xl (1024px) but we want to be responsive to actual viewport
  286. const modalMaxWidth = Math.min(1024, viewportWidth * 0.9); // Account for modal padding
  287. const modalMaxHeight = viewportHeight * 0.95; // max-h-[95vh]
  288. // Reserve space for modal header (~80px) and controls (~200px) and padding
  289. const reservedSpace = 320; // Header + controls + padding
  290. const availableModalHeight = modalMaxHeight - reservedSpace;
  291. // Calculate canvas constraints (stay within original 800px max, but be responsive)
  292. const maxCanvasSize = Math.min(800, modalMaxWidth - 64, availableModalHeight); // 64px for canvas area padding
  293. // Ensure minimum size
  294. const finalSize = Math.max(200, maxCanvasSize);
  295. // Update container to exact size (override CSS constraints)
  296. container.style.width = `${finalSize}px`;
  297. container.style.height = `${finalSize}px`;
  298. container.style.maxWidth = `${finalSize}px`;
  299. container.style.maxHeight = `${finalSize}px`;
  300. container.style.minWidth = `${finalSize}px`;
  301. container.style.minHeight = `${finalSize}px`;
  302. // Set the internal canvas size for high-DPI rendering
  303. const pixelRatio = (window.devicePixelRatio || 1) * 2;
  304. canvas.width = finalSize * pixelRatio;
  305. canvas.height = finalSize * pixelRatio;
  306. // Set the display size (canvas fills its container)
  307. canvas.style.width = '100%';
  308. canvas.style.height = '100%';
  309. console.log('Canvas resized:', {
  310. viewport: `${viewportWidth}x${viewportHeight}`,
  311. modalMaxWidth,
  312. availableModalHeight,
  313. finalSize: finalSize
  314. });
  315. }
  316. // Get interpolated coordinate at specific progress
  317. function getInterpolatedCoordinate(progress) {
  318. if (!playerPreviewData || playerPreviewData.length === 0) return null;
  319. const totalPoints = playerPreviewData.length;
  320. const exactIndex = progress * (totalPoints - 1);
  321. const index = Math.floor(exactIndex);
  322. const fraction = exactIndex - index;
  323. // Ensure we don't go out of bounds
  324. if (index >= totalPoints - 1) {
  325. return playerPreviewData[totalPoints - 1];
  326. }
  327. if (index < 0) {
  328. return playerPreviewData[0];
  329. }
  330. // Get the two coordinates to interpolate between
  331. const [theta1, rho1] = playerPreviewData[index];
  332. const [theta2, rho2] = playerPreviewData[index + 1];
  333. // Interpolate theta (handle angle wrapping)
  334. let deltaTheta = theta2 - theta1;
  335. if (deltaTheta > Math.PI) deltaTheta -= 2 * Math.PI;
  336. if (deltaTheta < -Math.PI) deltaTheta += 2 * Math.PI;
  337. const interpolatedTheta = theta1 + deltaTheta * fraction;
  338. const interpolatedRho = rho1 + (rho2 - rho1) * fraction;
  339. return [interpolatedTheta, interpolatedRho];
  340. }
  341. // Draw player preview for modal
  342. function drawPlayerPreview(ctx, progress) {
  343. if (!ctx || !playerPreviewData || playerPreviewData.length === 0) return;
  344. const canvas = ctx.canvas;
  345. const pixelRatio = (window.devicePixelRatio || 1) * 2;
  346. const containerSize = canvas.width / pixelRatio;
  347. const center = containerSize / 2;
  348. const scale = (containerSize / 2) - 30;
  349. ctx.save();
  350. // Clear canvas for fresh drawing
  351. ctx.clearRect(0, 0, canvas.width, canvas.height);
  352. // Create circular clipping path
  353. ctx.beginPath();
  354. ctx.arc(canvas.width/2, canvas.height/2, canvas.width/2, 0, Math.PI * 2);
  355. ctx.clip();
  356. // Setup coordinate system for drawing
  357. ctx.scale(pixelRatio, pixelRatio);
  358. // Calculate how many points to draw
  359. const totalPoints = playerPreviewData.length;
  360. const pointsToDraw = Math.floor(totalPoints * progress);
  361. if (pointsToDraw < 2) {
  362. ctx.restore();
  363. return;
  364. }
  365. // Draw the pattern
  366. ctx.beginPath();
  367. ctx.strokeStyle = '#808080';
  368. ctx.lineWidth = 1;
  369. ctx.lineCap = 'round';
  370. ctx.lineJoin = 'round';
  371. // Enable high quality rendering
  372. ctx.imageSmoothingEnabled = true;
  373. ctx.imageSmoothingQuality = 'high';
  374. // Draw pattern lines up to the last complete segment
  375. for (let i = 0; i < pointsToDraw - 1; i++) {
  376. const [theta1, rho1] = playerPreviewData[i];
  377. const [theta2, rho2] = playerPreviewData[i + 1];
  378. const x1 = center + rho1 * scale * Math.cos(theta1);
  379. const y1 = center + rho1 * scale * Math.sin(theta1);
  380. const x2 = center + rho2 * scale * Math.cos(theta2);
  381. const y2 = center + rho2 * scale * Math.sin(theta2);
  382. if (i === 0) {
  383. ctx.moveTo(x1, y1);
  384. }
  385. ctx.lineTo(x2, y2);
  386. }
  387. // Draw the final partial segment to the interpolated position
  388. if (pointsToDraw > 0) {
  389. const interpolatedCoord = getInterpolatedCoordinate(progress);
  390. if (interpolatedCoord && pointsToDraw > 1) {
  391. // Get the last complete coordinate
  392. const [lastTheta, lastRho] = playerPreviewData[pointsToDraw - 1];
  393. const lastX = center + lastRho * scale * Math.cos(lastTheta);
  394. const lastY = center + lastRho * scale * Math.sin(lastTheta);
  395. // Draw line to interpolated position
  396. const [interpTheta, interpRho] = interpolatedCoord;
  397. const interpX = center + interpRho * scale * Math.cos(interpTheta);
  398. const interpY = center + interpRho * scale * Math.sin(interpTheta);
  399. ctx.lineTo(interpX, interpY);
  400. }
  401. }
  402. ctx.stroke();
  403. // Draw current position dot with interpolated position
  404. if (pointsToDraw > 0) {
  405. const interpolatedCoord = getInterpolatedCoordinate(progress);
  406. if (interpolatedCoord) {
  407. const [theta, rho] = interpolatedCoord;
  408. const x = center + rho * scale * Math.cos(theta);
  409. const y = center + rho * scale * Math.sin(theta);
  410. // Draw white border
  411. ctx.beginPath();
  412. ctx.fillStyle = '#ffffff';
  413. ctx.arc(x, y, 7.5, 0, Math.PI * 2);
  414. ctx.fill();
  415. // Draw red dot
  416. ctx.beginPath();
  417. ctx.fillStyle = '#ff0000';
  418. ctx.arc(x, y, 6, 0, Math.PI * 2);
  419. ctx.fill();
  420. }
  421. }
  422. ctx.restore();
  423. }
  424. // Load pattern coordinates for player preview
  425. async function loadPlayerPreviewData(pattern) {
  426. try {
  427. const response = await fetch('/get_theta_rho_coordinates', {
  428. method: 'POST',
  429. headers: { 'Content-Type': 'application/json' },
  430. body: JSON.stringify({ file_name: pattern })
  431. });
  432. if (!response.ok) {
  433. throw new Error(`HTTP error! status: ${response.status}`);
  434. }
  435. const data = await response.json();
  436. if (data.error) {
  437. throw new Error(data.error);
  438. }
  439. playerPreviewData = data.coordinates;
  440. // Store the filename for comparison
  441. playerPreviewData.fileName = normalizeFilePath(pattern);
  442. } catch (error) {
  443. console.error(`Error loading player preview data: ${error.message}`);
  444. playerPreviewData = null;
  445. }
  446. }
  447. // Ultra-smooth animation function for modal
  448. function animateModalPreview() {
  449. const modal = document.getElementById('playerPreviewModal');
  450. if (!modal || modal.classList.contains('hidden')) return;
  451. const canvas = document.getElementById('playerPreviewCanvas');
  452. const ctx = canvas.getContext('2d');
  453. if (!ctx || !playerPreviewData) return;
  454. const currentTime = Date.now();
  455. const elapsed = currentTime - modalAnimationStartTime;
  456. const totalDuration = animationDuration;
  457. // Calculate smooth progress with easing
  458. const rawProgress = Math.min(elapsed / totalDuration, 1);
  459. const easeProgress = rawProgress < 0.5
  460. ? 2 * rawProgress * rawProgress
  461. : 1 - Math.pow(-2 * rawProgress + 2, 2) / 2;
  462. // Interpolate between last and target progress
  463. const currentProgress = modalLastProgress + (modalTargetProgress - modalLastProgress) * easeProgress;
  464. // Draw the pattern up to current progress
  465. drawPlayerPreview(ctx, currentProgress);
  466. // Continue animation if not at target
  467. if (rawProgress < 1) {
  468. modalAnimationId = requestAnimationFrame(animateModalPreview);
  469. }
  470. }
  471. // Update modal preview with smooth animation
  472. function updateModalPreviewSmooth(newProgress) {
  473. if (newProgress === modalTargetProgress) return; // No change needed
  474. // Stop any existing animation
  475. if (modalAnimationId) {
  476. cancelAnimationFrame(modalAnimationId);
  477. }
  478. // Update animation parameters
  479. modalLastProgress = modalTargetProgress;
  480. modalTargetProgress = newProgress;
  481. modalAnimationStartTime = Date.now();
  482. // Start smooth animation
  483. animateModalPreview();
  484. }
  485. // Setup player preview modal events
  486. function setupPlayerPreviewModalEvents() {
  487. const modal = document.getElementById('playerPreviewModal');
  488. const closeBtn = document.getElementById('closePlayerPreview');
  489. const toggleBtn = document.getElementById('toggle-preview-modal-btn');
  490. if (!modal || !closeBtn || !toggleBtn) return;
  491. // Remove any existing event listeners to prevent conflicts
  492. const newToggleBtn = toggleBtn.cloneNode(true);
  493. toggleBtn.parentNode.replaceChild(newToggleBtn, toggleBtn);
  494. // Toggle button click handler
  495. newToggleBtn.addEventListener('click', () => {
  496. const isHidden = modal.classList.contains('hidden');
  497. if (isHidden) {
  498. openPlayerPreviewModal();
  499. } else {
  500. modal.classList.add('hidden');
  501. }
  502. });
  503. // Close modal when clicking close button
  504. closeBtn.addEventListener('click', () => {
  505. setModalVisibility(false, true);
  506. });
  507. // Close modal when clicking outside
  508. modal.addEventListener('click', (e) => {
  509. if (e.target === modal) {
  510. setModalVisibility(false, true);
  511. }
  512. });
  513. // Close modal with Escape key
  514. document.addEventListener('keydown', (e) => {
  515. if (e.key === 'Escape' && !modal.classList.contains('hidden')) {
  516. setModalVisibility(false, true);
  517. }
  518. });
  519. // Setup modal control buttons
  520. setupModalControls();
  521. }
  522. // Handle pause/resume toggle
  523. async function togglePauseResume() {
  524. const pauseButton = document.getElementById('modal-pause-button');
  525. if (!pauseButton) return;
  526. try {
  527. const pauseIcon = pauseButton.querySelector('.material-icons');
  528. const isCurrentlyPaused = pauseIcon.textContent === 'play_arrow';
  529. // Show immediate feedback
  530. showStatusMessage(isCurrentlyPaused ? 'Resuming...' : 'Pausing...', 'info');
  531. const endpoint = isCurrentlyPaused ? '/resume_execution' : '/pause_execution';
  532. const response = await fetch(endpoint, { method: 'POST' });
  533. if (!response.ok) throw new Error(`Failed to ${isCurrentlyPaused ? 'resume' : 'pause'}`);
  534. // Show success message
  535. showStatusMessage(isCurrentlyPaused ? 'Pattern resumed' : 'Pattern paused', 'success');
  536. } catch (error) {
  537. console.error('Error toggling pause:', error);
  538. showStatusMessage('Failed to pause/resume pattern', 'error');
  539. }
  540. }
  541. // Setup modal controls
  542. function setupModalControls() {
  543. const pauseButton = document.getElementById('modal-pause-button');
  544. const skipButton = document.getElementById('modal-skip-button');
  545. const stopButton = document.getElementById('modal-stop-button');
  546. const speedDisplay = document.getElementById('modal-speed-display');
  547. const speedInput = document.getElementById('modal-speed-input');
  548. if (!pauseButton || !skipButton || !stopButton || !speedDisplay || !speedInput) return;
  549. // Pause button click handler
  550. pauseButton.addEventListener('click', togglePauseResume);
  551. // Skip button click handler
  552. skipButton.addEventListener('click', async () => {
  553. try {
  554. // Show immediate feedback
  555. showStatusMessage('Skipping to next pattern...', 'info');
  556. const response = await fetch('/skip_pattern', { method: 'POST' });
  557. if (!response.ok) throw new Error('Failed to skip pattern');
  558. // Show success message
  559. showStatusMessage('Skipped to next pattern', 'success');
  560. } catch (error) {
  561. console.error('Error skipping pattern:', error);
  562. showStatusMessage('Failed to skip pattern', 'error');
  563. }
  564. });
  565. // Stop button click handler
  566. stopButton.addEventListener('click', async () => {
  567. try {
  568. // Show immediate feedback
  569. showStatusMessage('Stopping...', 'info');
  570. const response = await fetch('/stop_execution', { method: 'POST' });
  571. if (!response.ok) throw new Error('Failed to stop pattern');
  572. else {
  573. // Show success message
  574. showStatusMessage('Pattern stopped', 'success');
  575. // Hide modal when stopping
  576. const modal = document.getElementById('playerPreviewModal');
  577. if (modal) modal.classList.add('hidden');
  578. }
  579. } catch (error) {
  580. console.error('Error stopping pattern:', error);
  581. showStatusMessage('Failed to stop pattern', 'error');
  582. }
  583. });
  584. // Speed display click to edit
  585. speedDisplay.addEventListener('click', () => {
  586. isEditingSpeed = true;
  587. speedDisplay.classList.add('hidden');
  588. speedInput.value = speedDisplay.textContent;
  589. speedInput.classList.remove('hidden');
  590. speedInput.focus();
  591. speedInput.select();
  592. });
  593. // Speed input handlers
  594. speedInput.addEventListener('keydown', (e) => {
  595. if (e.key === 'Enter') {
  596. e.preventDefault();
  597. exitModalSpeedEditMode(true);
  598. } else if (e.key === 'Escape') {
  599. e.preventDefault();
  600. exitModalSpeedEditMode(false);
  601. }
  602. });
  603. speedInput.addEventListener('blur', () => {
  604. exitModalSpeedEditMode(true);
  605. });
  606. }
  607. // Exit modal speed edit mode
  608. async function exitModalSpeedEditMode(save = false) {
  609. const speedDisplay = document.getElementById('modal-speed-display');
  610. const speedInput = document.getElementById('modal-speed-input');
  611. if (!speedDisplay || !speedInput) return;
  612. isEditingSpeed = false;
  613. speedInput.classList.add('hidden');
  614. speedDisplay.classList.remove('hidden');
  615. if (save) {
  616. const speed = parseInt(speedInput.value);
  617. if (!isNaN(speed) && speed >= 1 && speed <= 5000) {
  618. try {
  619. const response = await fetch('/set_speed', {
  620. method: 'POST',
  621. headers: { 'Content-Type': 'application/json' },
  622. body: JSON.stringify({ speed: speed })
  623. });
  624. const data = await response.json();
  625. if (data.success) {
  626. speedDisplay.textContent = speed;
  627. showStatusMessage(`Speed set to ${speed} mm/s`, 'success');
  628. } else {
  629. throw new Error(data.detail || 'Failed to set speed');
  630. }
  631. } catch (error) {
  632. console.error('Error setting speed:', error);
  633. showStatusMessage('Failed to set speed', 'error');
  634. }
  635. } else {
  636. showStatusMessage('Please enter a valid speed (1-5000)', 'error');
  637. }
  638. }
  639. }
  640. // Helper function to clean up pattern names
  641. function getCleanPatternName(filePath) {
  642. if (!filePath) return '';
  643. const fileName = normalizeFilePath(filePath);
  644. return fileName.split('/').pop().replace('.thr', '');
  645. }
  646. // Sync modal controls with player status
  647. function syncModalControls(status) {
  648. // Pattern name - clean up to show only filename
  649. const modalPatternName = document.getElementById('modal-pattern-name');
  650. if (modalPatternName && status.current_file) {
  651. modalPatternName.textContent = getCleanPatternName(status.current_file);
  652. }
  653. // Pattern preview image
  654. const modalPatternPreviewImg = document.getElementById('modal-pattern-preview-img');
  655. if (modalPatternPreviewImg && status.current_file) {
  656. const encodedFilename = normalizeFilePath(status.current_file).replace(/[\\/]/g, '--');
  657. const previewUrl = `/preview/${encodedFilename}`;
  658. modalPatternPreviewImg.src = previewUrl;
  659. }
  660. // ETA
  661. const modalEta = document.getElementById('modal-eta');
  662. if (modalEta) {
  663. if (status.progress && status.progress.remaining_time !== null) {
  664. const minutes = Math.floor(status.progress.remaining_time / 60);
  665. const seconds = Math.floor(status.progress.remaining_time % 60);
  666. modalEta.textContent = `ETA: ${minutes}:${seconds.toString().padStart(2, '0')}`;
  667. } else {
  668. modalEta.textContent = 'ETA: calculating...';
  669. }
  670. }
  671. // Progress bar
  672. const modalProgressBar = document.getElementById('modal-progress-bar');
  673. if (modalProgressBar) {
  674. if (status.progress && status.progress.percentage !== null) {
  675. modalProgressBar.style.width = `${status.progress.percentage}%`;
  676. } else {
  677. modalProgressBar.style.width = '0%';
  678. }
  679. }
  680. // Next pattern - clean up to show only filename
  681. const modalNextPattern = document.getElementById('modal-next-pattern');
  682. if (modalNextPattern) {
  683. if (status.playlist && status.playlist.next_file) {
  684. modalNextPattern.textContent = getCleanPatternName(status.playlist.next_file);
  685. } else {
  686. modalNextPattern.textContent = 'None';
  687. }
  688. }
  689. // Pause button
  690. const modalPauseBtn = document.getElementById('modal-pause-button');
  691. if (modalPauseBtn) {
  692. const pauseIcon = modalPauseBtn.querySelector('.material-icons');
  693. if (status.is_paused) {
  694. pauseIcon.textContent = 'play_arrow';
  695. } else {
  696. pauseIcon.textContent = 'pause';
  697. }
  698. }
  699. // Skip button visibility
  700. const modalSkipBtn = document.getElementById('modal-skip-button');
  701. if (modalSkipBtn) {
  702. if (status.playlist && status.playlist.next_file) {
  703. modalSkipBtn.classList.remove('invisible');
  704. } else {
  705. modalSkipBtn.classList.add('invisible');
  706. }
  707. }
  708. // Speed display
  709. const modalSpeedDisplay = document.getElementById('modal-speed-display');
  710. if (modalSpeedDisplay && status.speed && !isEditingSpeed) {
  711. modalSpeedDisplay.textContent = status.speed;
  712. }
  713. }
  714. // Toggle modal visibility
  715. function togglePreviewModal() {
  716. const modal = document.getElementById('playerPreviewModal');
  717. const toggleBtn = document.getElementById('toggle-preview-modal-btn');
  718. if (!modal || !toggleBtn) return;
  719. const isHidden = modal.classList.contains('hidden');
  720. if (isHidden) {
  721. openPlayerPreviewModal();
  722. } else {
  723. setModalVisibility(false, true);
  724. toggleBtn.classList.remove('active-tab');
  725. toggleBtn.classList.add('inactive-tab');
  726. }
  727. }
  728. // Button event handlers
  729. document.addEventListener('DOMContentLoaded', async () => {
  730. try {
  731. // Initialize WebSocket connection
  732. initializeWebSocket();
  733. // Setup player preview modal events
  734. setupPlayerPreviewModalEvents();
  735. console.log('Player initialized successfully');
  736. } catch (error) {
  737. console.error(`Error during initialization: ${error.message}`);
  738. }
  739. });
  740. // Initialize WebSocket connection
  741. function initializeWebSocket() {
  742. connectWebSocket();
  743. }
  744. // Clean up WebSocket when page unloads to prevent memory leaks
  745. window.addEventListener('beforeunload', () => {
  746. if (ws) {
  747. // Disable reconnection before closing
  748. ws.onclose = null;
  749. ws.close();
  750. ws = null;
  751. }
  752. });
  753. // Add resize handler for responsive canvas with debouncing
  754. let resizeTimeout;
  755. window.addEventListener('resize', () => {
  756. const canvas = document.getElementById('playerPreviewCanvas');
  757. const modal = document.getElementById('playerPreviewModal');
  758. if (canvas && modal && !modal.classList.contains('hidden')) {
  759. // Clear previous timeout
  760. clearTimeout(resizeTimeout);
  761. // Debounce resize calls to avoid excessive updates
  762. resizeTimeout = setTimeout(() => {
  763. const ctx = canvas.getContext('2d');
  764. setupPlayerPreviewCanvas(ctx);
  765. drawPlayerPreview(ctx, targetProgress);
  766. }, 16); // ~60fps update rate
  767. }
  768. });
  769. // Handle file changes and reload preview data
  770. function handleFileChange(newFile) {
  771. if (newFile !== currentPreviewFile) {
  772. currentPreviewFile = newFile;
  773. if (newFile) {
  774. loadPlayerPreviewData(`./patterns/${newFile}`);
  775. } else {
  776. playerPreviewData = null;
  777. }
  778. }
  779. }
  780. // Cache All Previews Prompt functionality
  781. let cacheAllInProgress = false;
  782. function shouldShowCacheAllPrompt() {
  783. // Check if we've already shown the prompt
  784. const promptShown = localStorage.getItem('cacheAllPromptShown');
  785. console.log('shouldShowCacheAllPrompt - promptShown:', promptShown);
  786. return !promptShown;
  787. }
  788. function showCacheAllPrompt(forceShow = false) {
  789. console.log('showCacheAllPrompt called, forceShow:', forceShow);
  790. if (!forceShow && !shouldShowCacheAllPrompt()) {
  791. console.log('Cache all prompt already shown, skipping');
  792. return;
  793. }
  794. const modal = document.getElementById('cacheAllPromptModal');
  795. if (modal) {
  796. console.log('Showing cache all prompt modal');
  797. modal.classList.remove('hidden');
  798. // Store whether this was forced (manually triggered)
  799. modal.dataset.manuallyTriggered = forceShow.toString();
  800. } else {
  801. console.log('Cache all prompt modal not found');
  802. }
  803. }
  804. function hideCacheAllPrompt() {
  805. const modal = document.getElementById('cacheAllPromptModal');
  806. if (modal) {
  807. modal.classList.add('hidden');
  808. }
  809. }
  810. function markCacheAllPromptAsShown() {
  811. localStorage.setItem('cacheAllPromptShown', 'true');
  812. }
  813. function initializeCacheAllPrompt() {
  814. const modal = document.getElementById('cacheAllPromptModal');
  815. const skipBtn = document.getElementById('skipCacheAllBtn');
  816. const startBtn = document.getElementById('startCacheAllBtn');
  817. const closeBtn = document.getElementById('closeCacheAllBtn');
  818. if (!modal || !skipBtn || !startBtn || !closeBtn) {
  819. return;
  820. }
  821. // Skip button handler
  822. skipBtn.addEventListener('click', () => {
  823. const wasManuallyTriggered = modal.dataset.manuallyTriggered === 'true';
  824. hideCacheAllPrompt();
  825. // Only mark as shown if it was automatically shown (not manually triggered)
  826. if (!wasManuallyTriggered) {
  827. markCacheAllPromptAsShown();
  828. }
  829. });
  830. // Close button handler (after completion)
  831. closeBtn.addEventListener('click', () => {
  832. const wasManuallyTriggered = modal.dataset.manuallyTriggered === 'true';
  833. hideCacheAllPrompt();
  834. // Always mark as shown after successful completion
  835. if (!wasManuallyTriggered) {
  836. markCacheAllPromptAsShown();
  837. }
  838. });
  839. // Start caching button handler
  840. startBtn.addEventListener('click', async () => {
  841. if (cacheAllInProgress) {
  842. return;
  843. }
  844. cacheAllInProgress = true;
  845. // Hide buttons and show progress
  846. document.getElementById('cacheAllButtons').classList.add('hidden');
  847. document.getElementById('cacheAllProgress').classList.remove('hidden');
  848. try {
  849. await startCacheAllProcess();
  850. // Show completion message
  851. document.getElementById('cacheAllProgress').classList.add('hidden');
  852. document.getElementById('cacheAllComplete').classList.remove('hidden');
  853. } catch (error) {
  854. console.error('Error caching all previews:', error);
  855. // Show error and reset
  856. document.getElementById('cacheAllProgressText').textContent = 'Error occurred during caching';
  857. setTimeout(() => {
  858. hideCacheAllPrompt();
  859. markCacheAllPromptAsShown();
  860. }, 3000);
  861. } finally {
  862. cacheAllInProgress = false;
  863. }
  864. });
  865. }
  866. async function startCacheAllProcess() {
  867. try {
  868. // Get list of patterns using cached function
  869. const patterns = await getCachedPatternFiles();
  870. if (!patterns || patterns.length === 0) {
  871. throw new Error('No patterns found');
  872. }
  873. const progressBar = document.getElementById('cacheAllProgressBar');
  874. const progressText = document.getElementById('cacheAllProgressText');
  875. const progressPercentage = document.getElementById('cacheAllProgressPercentage');
  876. let completed = 0;
  877. const batchSize = 5; // Process in small batches to avoid overwhelming the server
  878. for (let i = 0; i < patterns.length; i += batchSize) {
  879. const batch = patterns.slice(i, i + batchSize);
  880. // Update progress text
  881. progressText.textContent = `Caching previews... (${Math.min(i + batchSize, patterns.length)}/${patterns.length})`;
  882. // Process batch
  883. const batchPromises = batch.map(async (pattern) => {
  884. try {
  885. const previewResponse = await fetch('/preview_thr', {
  886. method: 'POST',
  887. headers: {
  888. 'Content-Type': 'application/json',
  889. },
  890. body: JSON.stringify({ file_name: pattern })
  891. });
  892. if (previewResponse.ok) {
  893. const data = await previewResponse.json();
  894. if (data.preview_url) {
  895. // Pre-load the image to cache it
  896. return new Promise((resolve) => {
  897. const img = new Image();
  898. img.onload = () => resolve();
  899. img.onerror = () => resolve(); // Continue even if image fails
  900. img.src = data.preview_url;
  901. });
  902. }
  903. }
  904. return Promise.resolve();
  905. } catch (error) {
  906. console.warn(`Failed to cache preview for ${pattern}:`, error);
  907. return Promise.resolve(); // Continue with other patterns
  908. }
  909. });
  910. await Promise.all(batchPromises);
  911. completed += batch.length;
  912. // Update progress bar
  913. const progress = Math.round((completed / patterns.length) * 100);
  914. progressBar.style.width = `${progress}%`;
  915. progressPercentage.textContent = `${progress}%`;
  916. // Small delay between batches to prevent overwhelming the server
  917. if (i + batchSize < patterns.length) {
  918. await new Promise(resolve => setTimeout(resolve, 100));
  919. }
  920. }
  921. progressText.textContent = `Completed! Cached ${patterns.length} previews.`;
  922. } catch (error) {
  923. throw error;
  924. }
  925. }
  926. // Function to be called after initial cache generation completes
  927. function onInitialCacheComplete() {
  928. console.log('onInitialCacheComplete called');
  929. // Show the cache all prompt after a short delay
  930. setTimeout(() => {
  931. console.log('Triggering cache all prompt after delay');
  932. showCacheAllPrompt();
  933. }, 1000);
  934. }
  935. // Initialize on DOM load
  936. document.addEventListener('DOMContentLoaded', () => {
  937. initializeCacheAllPrompt();
  938. });
  939. // Make functions available globally for debugging
  940. window.onInitialCacheComplete = onInitialCacheComplete;
  941. window.showCacheAllPrompt = showCacheAllPrompt;
  942. window.testCacheAllPrompt = function() {
  943. console.log('Manual test trigger');
  944. // Clear localStorage for testing
  945. localStorage.removeItem('cacheAllPromptShown');
  946. showCacheAllPrompt();
  947. };