base.html 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <script>
  5. // Immediately set dark mode if needed, before page loads
  6. const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  7. const savedTheme = localStorage.getItem('theme');
  8. if (savedTheme === 'dark' || (!savedTheme && prefersDark)) {
  9. document.documentElement.classList.add('dark');
  10. }
  11. </script>
  12. <meta charset="utf-8" />
  13. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  14. <link crossorigin="" href="https://fonts.gstatic.com/" rel="preconnect" />
  15. <link
  16. as="style"
  17. href="https://fonts.googleapis.com/css2?display=swap&amp;family=Noto+Sans%3Awght%40400%3B500%3B700%3B900&amp;family=Plus+Jakarta+Sans%3Awght%40400%3B500%3B700%3B800"
  18. onload="this.rel='stylesheet'"
  19. rel="stylesheet"
  20. />
  21. <!-- Preload Material Icons fonts for faster loading -->
  22. <link rel="preload" href="/static/fonts/material-icons/MaterialIcons-Regular.woff2" as="font" type="font/woff2" crossorigin>
  23. <link rel="preload" href="/static/fonts/material-icons/MaterialIconsOutlined-Regular.woff2" as="font" type="font/woff2" crossorigin>
  24. <title>{% block title %}{{ app_name or 'Dune Weaver' }}{% endblock %}</title>
  25. <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png">
  26. <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png">
  27. <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png">
  28. <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
  29. <link rel="manifest" href="/static/site.webmanifest?v=2">
  30. <link rel="stylesheet" href="/static/css/tailwind.css">
  31. <link rel="stylesheet" href="/static/css/material-icons.css">
  32. <style>
  33. /* Mobile navigation styles */
  34. @media (max-width: 640px) {
  35. nav.flex {
  36. display: grid;
  37. grid-template-columns: repeat(5, 1fr);
  38. gap: 0;
  39. }
  40. nav.flex a {
  41. display: flex;
  42. flex-direction: column;
  43. align-items: center;
  44. justify-content: center;
  45. text-align: center;
  46. padding: 0.75rem 0;
  47. }
  48. nav.flex a .material-icons {
  49. margin-bottom: 0.25rem;
  50. }
  51. }
  52. /* Dark mode styles */
  53. .dark {
  54. color-scheme: dark;
  55. }
  56. .dark body {
  57. background-color: #1a1a1a;
  58. color: #e5e5e5;
  59. }
  60. .dark header {
  61. background-color: #262626;
  62. border-color: #404040;
  63. }
  64. .dark footer {
  65. background-color: #262626;
  66. border-color: #404040;
  67. }
  68. .dark .inactive-tab {
  69. color: #9ca3af;
  70. }
  71. .dark .inactive-tab:hover {
  72. color: #d1d5db;
  73. }
  74. .dark #player-status-bar-container {
  75. background-color: #262626;
  76. color: #e5e5e5;
  77. }
  78. .dark .bg-gray-100 {
  79. background-color: #262626;
  80. }
  81. .dark .bg-gray-200 {
  82. background-color: #404040;
  83. }
  84. .dark .bg-gray-300 {
  85. background-color: #525252;
  86. }
  87. .dark .text-gray-500 {
  88. color: #9ca3af;
  89. }
  90. .dark .text-gray-700 {
  91. color: #d1d5db;
  92. }
  93. .dark .text-gray-800 {
  94. color: #e5e5e5;
  95. }
  96. .dark .border-gray-200 {
  97. border-color: #404040;
  98. }
  99. .dark .hover\:bg-gray-200:hover {
  100. background-color: #404040;
  101. }
  102. .dark .hover\:bg-gray-300:hover {
  103. background-color: #525252;
  104. }
  105. .dark .hover\:border-gray-300:hover {
  106. border-color: #525252;
  107. }
  108. .dark .hover\:bg-gray-50:hover {
  109. background-color: #262626;
  110. }
  111. .dark .bg-white {
  112. background-color: #262626;
  113. }
  114. .dark #shutdown-button:hover {
  115. background-color: #404040;
  116. }
  117. .dark #theme-toggle:hover {
  118. background-color: #404040;
  119. }
  120. .dark .bg-gray-50 {
  121. background-color: #1a1a1a;
  122. }
  123. .dark .text-gray-900 {
  124. color: #e5e5e5;
  125. }
  126. .dark .text-gray-400 {
  127. color: #9ca3af;
  128. }
  129. .dark .border-gray-300 {
  130. border-color: #404040;
  131. }
  132. .dark .focus\:ring-offset-2 {
  133. --tw-ring-offset-color: #262626;
  134. }
  135. .dark .shadow-sm {
  136. box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
  137. }
  138. .dark .shadow-lg {
  139. box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
  140. }
  141. .dark .shadow-xl {
  142. box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3), 0 10px 10px -5px rgba(0, 0, 0, 0.2);
  143. }
  144. /* Existing styles */
  145. .active-tab {
  146. color: #0c7ff2;
  147. border-color: #0c7ff2;
  148. }
  149. .active-tab .material-icons {
  150. color: #0c7ff2;
  151. }
  152. .inactive-tab {
  153. color: #6b7280;
  154. border-color: transparent;
  155. }
  156. .inactive-tab:hover {
  157. color: #374151;
  158. }
  159. #status-message {
  160. transition: opacity 0.5s ease-in-out, transform 0.5s ease-in-out;
  161. }
  162. /* Switch toggle styles */
  163. .switch {
  164. position: relative;
  165. display: inline-block;
  166. width: 40px;
  167. height: 20px;
  168. }
  169. .switch input {
  170. opacity: 0;
  171. width: 0;
  172. height: 0;
  173. }
  174. .slider {
  175. position: absolute;
  176. cursor: pointer;
  177. top: 0;
  178. left: 0;
  179. right: 0;
  180. bottom: 0;
  181. background-color: #ccc;
  182. transition: .4s;
  183. border-radius: 20px;
  184. }
  185. .slider:before {
  186. position: absolute;
  187. content: "";
  188. height: 16px;
  189. width: 16px;
  190. left: 2px;
  191. bottom: 2px;
  192. background-color: white;
  193. transition: .4s;
  194. border-radius: 50%;
  195. }
  196. input:checked + .slider {
  197. background-color: #0c7ff2;
  198. }
  199. input:checked + .slider:before {
  200. transform: translateX(20px);
  201. }
  202. /* Shadow for top of status bar */
  203. .shadow-lg-top {
  204. box-shadow: 0 -4px 6px -1px rgb(0 0 0 / 0.1), 0 -2px 4px -2px rgb(0 0 0 / 0.1);
  205. }
  206. /* Marquee animation for pattern name on small screens */
  207. @keyframes marquee {
  208. 0% { transform: translateX(0%); }
  209. 50% { transform: translateX(-50%); }
  210. }
  211. .pattern-marquee {
  212. display: inline-block;
  213. min-width: 100%;
  214. animation: marquee 8s linear infinite;
  215. }
  216. @media (min-width: 640px) { /* sm: and up, disable marquee */
  217. .pattern-marquee {
  218. animation: none !important;
  219. transform: none !important;
  220. }
  221. }
  222. {% block additional_styles %}{% endblock %}
  223. </style>
  224. {% block additional_head %}{% endblock %}
  225. </head>
  226. <body
  227. class="bg-gray-50"
  228. style="font-family: 'Plus Jakarta Sans', 'Noto Sans', sans-serif"
  229. >
  230. <div
  231. class="mt-2 w-full text-center px-4 z-50 pointer-events-none absolute"
  232. id="status-message-container"
  233. >
  234. <p
  235. class="text-base font-semibold opacity-0 transform -translate-y-2 transition-all duration-300 ease-in-out px-10 py-2 rounded-lg shadow-lg"
  236. id="status-message"
  237. ></p>
  238. </div>
  239. <div
  240. class="relative flex size-full min-h-screen flex-col group/design-root overflow-x-hidden"
  241. >
  242. <div class="layout-container flex min-h-full grow flex-col">
  243. <header
  244. class="fixed top-0 left-0 right-0 z-10 flex items-center justify-between whitespace-nowrap border-b border-solid border-b-gray-200 bg-white px-4 sm:px-6 md:px-10 py-3 sm:py-4 shadow-sm"
  245. >
  246. <div class="flex items-center gap-3 text-gray-800">
  247. <a href="/" class="flex items-center gap-3 text-gray-800 hover:opacity-80 transition-opacity">
  248. <div class="text-blue-600 w-9 h-9 rounded-full shadow">
  249. <img src="/static/apple-touch-icon.png" alt="Dune Weaver Logo"/>
  250. </div>
  251. <h1
  252. class="text-gray-800 text-xl font-bold leading-tight tracking-tight flex items-center gap-2"
  253. >
  254. {{ app_name or 'Dune Weaver' }}
  255. <span
  256. id="connectionStatusDot"
  257. class="inline-block size-2 rounded-full bg-red-500 ml-2 align-middle"
  258. ></span>
  259. </h1>
  260. </a>
  261. </div>
  262. <div class="flex items-center gap-2">
  263. <button
  264. id="theme-toggle"
  265. class="p-1.5 flex rounded-lg hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-blue-500"
  266. aria-label="Toggle dark mode"
  267. >
  268. <span class="material-icons" id="theme-toggle-icon">dark_mode</span>
  269. </button>
  270. <button
  271. id="shutdown-button"
  272. class="p-1.5 flex rounded-lg hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-red-500"
  273. aria-label="Shutdown system"
  274. title="Shutdown System (Raspberry Pi only)"
  275. >
  276. <span class="material-icons text-red-600">power_settings_new</span>
  277. </button>
  278. </div>
  279. </header>
  280. <main class="flex flex-1 justify-center px-4 sm:px-6 lg:px-8 pt-16 sm:pt-20">
  281. {% block content %}{% endblock %}
  282. </main>
  283. <!-- Floating Preview Button -->
  284. <button
  285. id="toggle-preview-modal-btn"
  286. class="fixed bottom-24 right-4 z-30 bg-blue-600 hover:bg-blue-700 text-white rounded-full shadow-lg w-12 h-12 flex items-center justify-center transition-all focus:outline-none focus:ring-2 focus:ring-blue-500"
  287. title="Toggle Pattern Preview"
  288. >
  289. <span class="material-icons text-xl">preview</span>
  290. </button>
  291. <footer
  292. class="fixed bottom-0 left-0 right-0 z-10 bg-white border-t border-gray-200 shadow-t-sm"
  293. >
  294. <nav class="mx-auto flex max-w-5xl justify-around">
  295. <a
  296. class="{% if request.url.path == '/' %}active-tab{% else %}inactive-tab{% endif %} flex flex-1 flex-col items-center justify-center gap-1 border-b-[3px] {% if request.url.path != '/' %}border-transparent{% endif %} py-3 text-xs font-medium sm:text-sm"
  297. href="/"
  298. >
  299. <span class="material-icons">search</span> Browse
  300. </a>
  301. <a
  302. class="{% if request.url.path == '/playlists' %}active-tab{% else %}inactive-tab{% endif %} flex flex-1 flex-col items-center justify-center gap-1 border-b-[3px] py-3 text-xs font-medium sm:text-sm"
  303. href="/playlists"
  304. >
  305. <span class="material-icons">list_alt</span> Playlists
  306. </a>
  307. <a
  308. class="{% if request.url.path == '/table_control' %}active-tab{% else %}inactive-tab{% endif %} flex flex-1 flex-col items-center justify-center gap-1 border-b-[3px] py-3 text-xs font-medium sm:text-sm"
  309. href="/table_control"
  310. >
  311. <span class="material-icons">table_chart</span> Table Control
  312. </a>
  313. <a
  314. class="{% if request.url.path == '/led' %}active-tab{% else %}inactive-tab{% endif %} flex flex-1 flex-col items-center justify-center gap-1 border-b-[3px] py-3 text-xs font-medium sm:text-sm"
  315. href="/led"
  316. >
  317. <span class="material-icons">lightbulb</span> <span id="led-nav-label">LED</span>
  318. </a>
  319. <a
  320. class="{% if request.url.path == '/settings' %}active-tab{% else %}inactive-tab{% endif %} flex flex-1 flex-col items-center justify-center gap-1 border-b-[3px] py-3 text-xs font-medium sm:text-sm"
  321. href="/settings"
  322. >
  323. <span class="material-icons">settings</span> Settings
  324. </a>
  325. </nav>
  326. </footer>
  327. </div>
  328. </div>
  329. {% block scripts %}{% endblock %}
  330. <script src="/static/js/base.js"></script>
  331. <!-- Cache Progress Modal -->
  332. <div id="cacheProgressModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden p-4">
  333. <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md">
  334. <div class="p-6">
  335. <div class="text-center">
  336. <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-2">Initializing Pattern Cache</h2>
  337. <p class="text-gray-600 dark:text-gray-400 mb-4">Preparing your pattern previews...</p>
  338. <div class="mb-4">
  339. <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2">
  340. <div id="cacheProgressBar" class="bg-blue-600 dark:bg-blue-400 h-2 rounded-full transition-all duration-300" style="width: 0%"></div>
  341. </div>
  342. <div class="mt-2 flex justify-between text-sm text-gray-500 dark:text-gray-400">
  343. <span id="cacheProgressText">Starting...</span>
  344. <span id="cacheProgressPercentage">0%</span>
  345. </div>
  346. </div>
  347. <div class="text-sm text-gray-500 dark:text-gray-400">
  348. <p id="cacheCurrentStage">Preparing...</p>
  349. <p id="cacheCurrentFile" class="mt-1 truncate"></p>
  350. </div>
  351. </div>
  352. </div>
  353. </div>
  354. </div>
  355. <!-- Player Preview Modal -->
  356. <div id="playerPreviewModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-40 hidden p-4">
  357. <div class="bg-white rounded-lg shadow-xl w-full max-w-5xl max-h-[95vh] flex flex-col">
  358. <div class="flex items-center justify-between p-4 border-b border-gray-200 flex-shrink-0">
  359. <h2 id="playerPreviewTitle" class="text-xl font-semibold text-gray-800">Pattern Preview</h2>
  360. <button id="closePlayerPreview" class="text-gray-400 hover:text-gray-600 transition-colors">
  361. <span class="material-icons text-2xl">close</span>
  362. </button>
  363. </div>
  364. <!-- Canvas Area -->
  365. <div class="flex-1 p-4 flex justify-center items-center overflow-hidden">
  366. <div class="relative flex items-center justify-center w-full h-full">
  367. <div class="relative max-w-[min(800px,90vw)] max-h-[min(600px,80vh)] aspect-square">
  368. <canvas id="playerPreviewCanvas" width="800" height="800" class="w-full h-full rounded-full border border-gray-300 dark:invert"></canvas>
  369. </div>
  370. </div>
  371. </div>
  372. <!-- Pattern Info and Controls -->
  373. <div class="flex-shrink-0 p-4 border-b border-gray-200 flex items-center gap-4 flex-wrap">
  374. <!-- Pattern Preview Image -->
  375. <div class="flex-shrink-0">
  376. <img id="modal-pattern-preview-img"
  377. class="w-[126px] h-[126px] rounded-full border border-gray-300 object-cover dark:invert" src="">
  378. </div>
  379. <div class="flex-grow w-auto">
  380. <div class="flex items-center justify-between mb-2">
  381. <div class="min-w-0 max-w-[50%]">
  382. <div class="overflow-hidden relative">
  383. <p class="text-lg font-semibold whitespace-nowrap" id="modal-pattern-name">
  384. No pattern playing
  385. </p>
  386. </div>
  387. <p class="text-base text-gray-500" id="modal-eta">
  388. ETA: --:--
  389. </p>
  390. </div>
  391. <div class="flex items-center gap-2 flex-shrink-0">
  392. <button
  393. aria-label="Next"
  394. class="w-12 aspect-square rounded-full flex items-center justify-center hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-blue-500"
  395. id="modal-skip-button"
  396. >
  397. <span class="material-icons text-gray-700">skip_next</span>
  398. </button>
  399. <button
  400. aria-label="Pause"
  401. class="w-12 aspect-square rounded-full flex items-center justify-center hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-blue-500"
  402. id="modal-pause-button"
  403. >
  404. <span class="material-icons text-gray-700">pause</span>
  405. </button>
  406. <button
  407. aria-label="Stop"
  408. class="w-12 aspect-square rounded-full flex items-center justify-center hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-blue-500"
  409. id="modal-stop-button"
  410. >
  411. <span class="material-icons text-gray-700">stop</span>
  412. </button>
  413. </div>
  414. </div>
  415. <div class="w-full bg-gray-300 rounded-full h-2.5 mb-2">
  416. <div
  417. class="bg-blue-600 h-2.5 rounded-full"
  418. id="modal-progress-bar"
  419. style="width: 0%"
  420. ></div>
  421. </div>
  422. <div class="flex items-center justify-between">
  423. <div class="text-base text-gray-500">
  424. Next:
  425. <span class="text-gray-700 font-medium" id="modal-next-pattern">None</span>
  426. </div>
  427. <div class="flex items-center gap-3 text-sm">
  428. <span class="text-gray-500">Speed:</span>
  429. <div class="flex items-center gap-2">
  430. <div class="relative min-w-[4ch] text-center cursor-pointer inline-flex items-center" id="modal-speed-container">
  431. <span class="inline-block px-2 py-1 text-gray-700 font-medium border border-transparent rounded hover:border-gray-300 hover:bg-gray-50 transition-colors" id="modal-speed-display" title="Click to edit speed">--</span>
  432. <input type="number" min="1" max="5000" step="1" class="w-16 sm:w-20 px-2 py-1 text-sm border border-blue-500 rounded focus:border-blue-500 focus:ring-1 focus:ring-blue-500 text-center hidden" id="modal-speed-input" />
  433. </div>
  434. </div>
  435. </div>
  436. </div>
  437. </div>
  438. </div>
  439. </div>
  440. </div>
  441. <script>
  442. // Function to show status message
  443. function showStatusMessage(message, type = "success") {
  444. const statusContainer = document.getElementById(
  445. "status-message-container"
  446. );
  447. const statusMessage = document.getElementById("status-message");
  448. if (!statusContainer || !statusMessage) return;
  449. // Set message and color based on type
  450. statusMessage.textContent = message;
  451. statusMessage.className = `text-base font-semibold opacity-0 transform -translate-y-2 transition-all duration-300 ease-in-out px-4 py-2 rounded-lg shadow-lg ${
  452. type === "success"
  453. ? "bg-green-50 text-green-700 border border-green-200"
  454. : type === "error"
  455. ? "bg-red-50 text-red-700 border border-red-200"
  456. : type === "warning"
  457. ? "bg-yellow-50 text-yellow-700 border border-yellow-200"
  458. : "bg-blue-50 text-blue-700 border border-blue-200"
  459. }`;
  460. // Show message with animation
  461. requestAnimationFrame(() => {
  462. statusMessage.classList.remove("opacity-0", "-translate-y-2");
  463. statusMessage.classList.add("opacity-100", "translate-y-0");
  464. });
  465. // Hide message after 5 seconds
  466. setTimeout(() => {
  467. statusMessage.classList.remove("opacity-100", "translate-y-0");
  468. statusMessage.classList.add("opacity-0", "-translate-y-2");
  469. }, 5000);
  470. }
  471. // Initial connection status check
  472. async function checkInitialConnectionStatus() {
  473. try {
  474. const response = await fetch("/serial_status");
  475. if (response.ok) {
  476. const data = await response.json();
  477. const statusDot = document.getElementById("connectionStatusDot");
  478. if (statusDot) {
  479. statusDot.className = `inline-block size-2 rounded-full ml-2 align-middle ${
  480. data.connected ? "bg-green-500" : "bg-red-500"
  481. }`;
  482. }
  483. }
  484. } catch (error) {
  485. console.error("Error checking initial connection status:", error);
  486. }
  487. }
  488. // Check initial status on page load
  489. document.addEventListener("DOMContentLoaded", checkInitialConnectionStatus);
  490. // Player bar toggle logic will be handled by base.js
  491. </script>
  492. <script>
  493. // Cache progress modal functionality
  494. let cacheProgressSocket = null;
  495. let cacheProgressCheckInterval = null;
  496. function initCacheProgressModal() {
  497. const modal = document.getElementById('cacheProgressModal');
  498. const progressBar = document.getElementById('cacheProgressBar');
  499. const progressText = document.getElementById('cacheProgressText');
  500. const progressPercentage = document.getElementById('cacheProgressPercentage');
  501. const currentStage = document.getElementById('cacheCurrentStage');
  502. const currentFile = document.getElementById('cacheCurrentFile');
  503. // Check if cache generation is needed on page load
  504. fetch('/cache-progress')
  505. .then(response => response.json())
  506. .then(data => {
  507. if (data.is_running) {
  508. showCacheProgressModal();
  509. }
  510. })
  511. .catch(error => {
  512. console.error('Error checking initial cache progress:', error);
  513. });
  514. function showCacheProgressModal() {
  515. modal.classList.remove('hidden');
  516. // Connect to WebSocket for real-time updates
  517. const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
  518. const wsUrl = `${protocol}//${window.location.host}/ws/cache-progress`;
  519. cacheProgressSocket = new WebSocket(wsUrl);
  520. cacheProgressSocket.onmessage = function(event) {
  521. const data = JSON.parse(event.data);
  522. if (data.type === 'cache_progress') {
  523. updateCacheProgress(data.data);
  524. }
  525. };
  526. cacheProgressSocket.onclose = function() {
  527. // Fallback to polling if WebSocket fails
  528. startProgressPolling();
  529. };
  530. cacheProgressSocket.onerror = function() {
  531. // Fallback to polling if WebSocket fails
  532. startProgressPolling();
  533. };
  534. }
  535. function startProgressPolling() {
  536. if (cacheProgressCheckInterval) {
  537. clearInterval(cacheProgressCheckInterval);
  538. }
  539. cacheProgressCheckInterval = setInterval(() => {
  540. fetch('/cache-progress')
  541. .then(response => response.json())
  542. .then(data => updateCacheProgress(data))
  543. .catch(error => console.error('Error fetching cache progress:', error));
  544. }, 1000);
  545. }
  546. function updateCacheProgress(data) {
  547. if (!data.is_running && data.stage === 'complete') {
  548. console.log('Initial cache generation completed');
  549. hideCacheProgressModal();
  550. // Trigger cache all prompt after initial cache generation completes
  551. if (typeof onInitialCacheComplete === 'function') {
  552. console.log('Calling onInitialCacheComplete()');
  553. onInitialCacheComplete();
  554. } else {
  555. console.log('onInitialCacheComplete function not found');
  556. }
  557. return;
  558. }
  559. if (!data.is_running && data.stage === 'error') {
  560. progressText.textContent = 'Error occurred';
  561. currentStage.textContent = data.error || 'Unknown error';
  562. currentFile.textContent = '';
  563. return;
  564. }
  565. if (!data.is_running) {
  566. hideCacheProgressModal();
  567. return;
  568. }
  569. // Calculate progress percentage
  570. const percentage = data.total_files > 0 ? Math.round((data.processed_files / data.total_files) * 100) : 0;
  571. // Update progress bar
  572. progressBar.style.width = `${percentage}%`;
  573. progressPercentage.textContent = `${percentage}%`;
  574. // Update text based on stage
  575. let stageText = '';
  576. let progressTextContent = '';
  577. switch (data.stage) {
  578. case 'starting':
  579. stageText = 'Initializing...';
  580. progressTextContent = 'Getting ready...';
  581. break;
  582. case 'metadata':
  583. stageText = 'Processing pattern metadata';
  584. progressTextContent = `${data.processed_files} of ${data.total_files} patterns`;
  585. break;
  586. case 'images':
  587. stageText = 'Generating pattern previews';
  588. progressTextContent = `${data.processed_files} of ${data.total_files} previews`;
  589. break;
  590. default:
  591. stageText = 'Processing...';
  592. progressTextContent = `${data.processed_files} of ${data.total_files} files`;
  593. }
  594. currentStage.textContent = stageText;
  595. progressText.textContent = progressTextContent;
  596. // Update current file
  597. if (data.current_file) {
  598. currentFile.textContent = `Current: ${data.current_file}`;
  599. } else {
  600. currentFile.textContent = '';
  601. }
  602. }
  603. function hideCacheProgressModal() {
  604. modal.classList.add('hidden');
  605. if (cacheProgressSocket) {
  606. cacheProgressSocket.close();
  607. cacheProgressSocket = null;
  608. }
  609. if (cacheProgressCheckInterval) {
  610. clearInterval(cacheProgressCheckInterval);
  611. cacheProgressCheckInterval = null;
  612. }
  613. }
  614. // Expose functions globally for potential external use
  615. window.showCacheProgressModal = showCacheProgressModal;
  616. window.hideCacheProgressModal = hideCacheProgressModal;
  617. }
  618. // Initialize when DOM is loaded
  619. document.addEventListener('DOMContentLoaded', initCacheProgressModal);
  620. </script>
  621. <script>
  622. // Theme switching functionality
  623. document.addEventListener('DOMContentLoaded', function() {
  624. const themeToggle = document.getElementById('theme-toggle');
  625. const themeToggleIcon = document.getElementById('theme-toggle-icon');
  626. // Update icon based on current theme
  627. function updateThemeIcon() {
  628. const isDark = document.documentElement.classList.contains('dark');
  629. themeToggleIcon.textContent = isDark ? 'light_mode' : 'dark_mode';
  630. }
  631. // Initial icon update
  632. updateThemeIcon();
  633. // Theme toggle click handler
  634. themeToggle.addEventListener('click', () => {
  635. const isDark = document.documentElement.classList.toggle('dark');
  636. localStorage.setItem('theme', isDark ? 'dark' : 'light');
  637. updateThemeIcon();
  638. });
  639. });
  640. // Shutdown button functionality
  641. document.addEventListener('DOMContentLoaded', function() {
  642. const shutdownButton = document.getElementById('shutdown-button');
  643. // Shutdown button click handler
  644. shutdownButton.addEventListener('click', async () => {
  645. const confirmed = confirm('Are you sure you want to shutdown the system?\n\nNote: This only works on Raspberry Pi.\n\nThis will:\n1. Stop Docker containers\n2. Shut down the system\n\nYou will need physical access to restart it.');
  646. if (!confirmed) return;
  647. try {
  648. showStatusMessage('Initiating shutdown...', 'warning');
  649. const response = await fetch('/api/system/shutdown', { method: 'POST' });
  650. const data = await response.json();
  651. if (data.success) {
  652. showStatusMessage('System is shutting down...', 'success');
  653. } else {
  654. showStatusMessage('Shutdown failed: ' + data.message, 'error');
  655. }
  656. } catch (error) {
  657. showStatusMessage('Failed to shutdown: ' + error.message, 'error');
  658. }
  659. });
  660. });
  661. </script>
  662. <!-- Cache All Previews Prompt Modal -->
  663. <div id="cacheAllPromptModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden p-4">
  664. <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md">
  665. <div class="p-6">
  666. <div class="text-center">
  667. <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-2">Cache All Pattern Previews?</h2>
  668. <p class="text-gray-600 dark:text-gray-400 mb-4 max-w-md mx-auto">
  669. Would you like to cache all pattern previews for faster browsing? This will download and store preview images in your browser for instant loading.
  670. </p>
  671. <div class="bg-amber-50 dark:bg-amber-900 p-3 rounded-lg mb-4 text-sm">
  672. <p class="text-amber-700 dark:text-amber-300">
  673. <strong>Note:</strong> This cache is browser-specific. You'll need to repeat this process for each browser you use.
  674. </p>
  675. </div>
  676. <!-- Progress section (hidden initially) -->
  677. <div id="cacheAllProgress" class="mb-4 hidden">
  678. <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2">
  679. <div id="cacheAllProgressBar" class="bg-blue-600 dark:bg-blue-400 h-2 rounded-full transition-all duration-300" style="width: 0%"></div>
  680. </div>
  681. <div class="mt-2 flex justify-between text-sm text-gray-500 dark:text-gray-400">
  682. <span id="cacheAllProgressText">Starting...</span>
  683. <span id="cacheAllProgressPercentage">0%</span>
  684. </div>
  685. </div>
  686. <!-- Buttons -->
  687. <div id="cacheAllButtons" class="flex gap-3 justify-center">
  688. <button id="skipCacheAllBtn" class="px-4 py-2 text-gray-600 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 transition-colors">
  689. Skip for now
  690. </button>
  691. <button id="startCacheAllBtn" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors">
  692. Cache All Previews
  693. </button>
  694. </div>
  695. <!-- Completion message (hidden initially) -->
  696. <div id="cacheAllComplete" class="hidden">
  697. <p class="text-green-600 dark:text-green-400 mb-4">✓ All previews cached successfully!</p>
  698. <button id="closeCacheAllBtn" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors">
  699. Done
  700. </button>
  701. </div>
  702. </div>
  703. </div>
  704. </div>
  705. </div>
  706. </body>
  707. </html>