base.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888
  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 #restart-button:hover {
  118. background-color: #404040;
  119. }
  120. .dark #theme-toggle:hover {
  121. background-color: #404040;
  122. }
  123. .dark .bg-gray-50 {
  124. background-color: #1a1a1a;
  125. }
  126. .dark .text-gray-900 {
  127. color: #e5e5e5;
  128. }
  129. .dark .text-gray-400 {
  130. color: #9ca3af;
  131. }
  132. .dark .border-gray-300 {
  133. border-color: #404040;
  134. }
  135. .dark .focus\:ring-offset-2 {
  136. --tw-ring-offset-color: #262626;
  137. }
  138. .dark .shadow-sm {
  139. box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
  140. }
  141. .dark .shadow-lg {
  142. box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.3), 0 4px 6px -2px rgba(0, 0, 0, 0.2);
  143. }
  144. .dark .shadow-xl {
  145. box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.3), 0 10px 10px -5px rgba(0, 0, 0, 0.2);
  146. }
  147. /* Existing styles */
  148. .active-tab {
  149. color: #0c7ff2;
  150. border-color: #0c7ff2;
  151. }
  152. .active-tab .material-icons {
  153. color: #0c7ff2;
  154. }
  155. .inactive-tab {
  156. color: #6b7280;
  157. border-color: transparent;
  158. }
  159. .inactive-tab:hover {
  160. color: #374151;
  161. }
  162. #status-message {
  163. transition: opacity 0.5s ease-in-out, transform 0.5s ease-in-out;
  164. }
  165. /* Switch toggle styles */
  166. .switch {
  167. position: relative;
  168. display: inline-block;
  169. width: 40px;
  170. height: 20px;
  171. }
  172. .switch input {
  173. opacity: 0;
  174. width: 0;
  175. height: 0;
  176. }
  177. .slider {
  178. position: absolute;
  179. cursor: pointer;
  180. top: 0;
  181. left: 0;
  182. right: 0;
  183. bottom: 0;
  184. background-color: #ccc;
  185. transition: .4s;
  186. border-radius: 20px;
  187. }
  188. .slider:before {
  189. position: absolute;
  190. content: "";
  191. height: 16px;
  192. width: 16px;
  193. left: 2px;
  194. bottom: 2px;
  195. background-color: white;
  196. transition: .4s;
  197. border-radius: 50%;
  198. }
  199. input:checked + .slider {
  200. background-color: #0c7ff2;
  201. }
  202. input:checked + .slider:before {
  203. transform: translateX(20px);
  204. }
  205. /* Shadow for top of status bar */
  206. .shadow-lg-top {
  207. box-shadow: 0 -4px 6px -1px rgb(0 0 0 / 0.1), 0 -2px 4px -2px rgb(0 0 0 / 0.1);
  208. }
  209. /* Marquee animation for pattern name on small screens */
  210. @keyframes marquee {
  211. 0% { transform: translateX(0%); }
  212. 50% { transform: translateX(-50%); }
  213. }
  214. .pattern-marquee {
  215. display: inline-block;
  216. min-width: 100%;
  217. animation: marquee 8s linear infinite;
  218. }
  219. @media (min-width: 640px) { /* sm: and up, disable marquee */
  220. .pattern-marquee {
  221. animation: none !important;
  222. transform: none !important;
  223. }
  224. }
  225. {% block additional_styles %}{% endblock %}
  226. </style>
  227. {% block additional_head %}{% endblock %}
  228. </head>
  229. <body
  230. class="bg-gray-50"
  231. style="font-family: 'Plus Jakarta Sans', 'Noto Sans', sans-serif"
  232. >
  233. <div
  234. class="mt-2 w-full text-center px-4 z-50 pointer-events-none absolute"
  235. id="status-message-container"
  236. >
  237. <p
  238. 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"
  239. id="status-message"
  240. ></p>
  241. </div>
  242. <div
  243. class="relative flex size-full min-h-screen flex-col group/design-root overflow-x-hidden"
  244. >
  245. <div class="layout-container flex min-h-full grow flex-col">
  246. <header
  247. 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"
  248. >
  249. <div class="flex items-center gap-3 text-gray-800">
  250. <a href="/" class="flex items-center gap-3 text-gray-800 hover:opacity-80 transition-opacity">
  251. <div class="text-blue-600 w-9 h-9 rounded-full shadow">
  252. <img src="/static/apple-touch-icon.png" alt="Dune Weaver Logo"/>
  253. </div>
  254. <h1
  255. class="text-gray-800 text-xl font-bold leading-tight tracking-tight flex items-center gap-2"
  256. >
  257. {{ app_name or 'Dune Weaver' }}
  258. <span
  259. id="connectionStatusDot"
  260. class="inline-block size-2 rounded-full bg-red-500 ml-2 align-middle"
  261. ></span>
  262. </h1>
  263. </a>
  264. </div>
  265. <div class="flex items-center gap-2">
  266. <!-- Update Available Indicator -->
  267. <button
  268. id="update-indicator"
  269. class="hidden p-1.5 flex rounded-lg hover:bg-green-100 dark:hover:bg-green-900 focus:outline-none focus:ring-2 focus:ring-green-500"
  270. aria-label="Update available"
  271. title="Software update available - Click to view"
  272. >
  273. <span class="material-icons text-green-600 dark:text-green-400 animate-pulse">system_update</span>
  274. </button>
  275. <button
  276. id="theme-toggle"
  277. class="p-1.5 flex rounded-lg hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-blue-500"
  278. aria-label="Toggle dark mode"
  279. >
  280. <span class="material-icons" id="theme-toggle-icon">dark_mode</span>
  281. </button>
  282. <button
  283. id="restart-button"
  284. class="p-1.5 flex rounded-lg hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-amber-500"
  285. aria-label="Restart system"
  286. title="Restart Docker Containers"
  287. >
  288. <span class="material-icons text-amber-600">restart_alt</span>
  289. </button>
  290. <button
  291. id="shutdown-button"
  292. class="p-1.5 flex rounded-lg hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-red-500"
  293. aria-label="Shutdown system"
  294. title="Shutdown System (Raspberry Pi only)"
  295. >
  296. <span class="material-icons text-red-600">power_settings_new</span>
  297. </button>
  298. </div>
  299. </header>
  300. <main class="flex flex-1 justify-center px-4 sm:px-6 lg:px-8 pt-16 sm:pt-20">
  301. {% block content %}{% endblock %}
  302. </main>
  303. <!-- Floating Preview Button -->
  304. <button
  305. id="toggle-preview-modal-btn"
  306. 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"
  307. title="Toggle Pattern Preview"
  308. >
  309. <span class="material-icons text-xl">preview</span>
  310. </button>
  311. <footer
  312. class="fixed bottom-0 left-0 right-0 z-10 bg-white border-t border-gray-200 shadow-t-sm"
  313. >
  314. <nav class="mx-auto flex max-w-5xl justify-around">
  315. <a
  316. 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"
  317. href="/"
  318. >
  319. <span class="material-icons">search</span> Browse
  320. </a>
  321. <a
  322. 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"
  323. href="/playlists"
  324. >
  325. <span class="material-icons">list_alt</span> Playlists
  326. </a>
  327. <a
  328. 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"
  329. href="/table_control"
  330. >
  331. <span class="material-icons">table_chart</span> Table Control
  332. </a>
  333. <a
  334. 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"
  335. href="/led"
  336. >
  337. <span class="material-icons">lightbulb</span> <span id="led-nav-label">LED</span>
  338. </a>
  339. <a
  340. 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"
  341. href="/settings"
  342. >
  343. <span class="material-icons">settings</span> Settings
  344. </a>
  345. </nav>
  346. </footer>
  347. </div>
  348. </div>
  349. {% block scripts %}{% endblock %}
  350. <script src="/static/js/base.js"></script>
  351. <!-- Cache Progress Modal -->
  352. <div id="cacheProgressModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden p-4">
  353. <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md">
  354. <div class="p-6">
  355. <div class="text-center">
  356. <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-2">Initializing Pattern Cache</h2>
  357. <p class="text-gray-600 dark:text-gray-400 mb-4">Preparing your pattern previews...</p>
  358. <div class="mb-4">
  359. <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2">
  360. <div id="cacheProgressBar" class="bg-blue-600 dark:bg-blue-400 h-2 rounded-full transition-all duration-300" style="width: 0%"></div>
  361. </div>
  362. <div class="mt-2 flex justify-between text-sm text-gray-500 dark:text-gray-400">
  363. <span id="cacheProgressText">Starting...</span>
  364. <span id="cacheProgressPercentage">0%</span>
  365. </div>
  366. </div>
  367. <div class="text-sm text-gray-500 dark:text-gray-400">
  368. <p id="cacheCurrentStage">Preparing...</p>
  369. <p id="cacheCurrentFile" class="mt-1 truncate"></p>
  370. </div>
  371. </div>
  372. </div>
  373. </div>
  374. </div>
  375. <!-- Player Preview Modal -->
  376. <div id="playerPreviewModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-40 hidden p-4">
  377. <div class="bg-white rounded-lg shadow-xl w-full max-w-5xl max-h-[95vh] flex flex-col">
  378. <div class="flex items-center justify-between p-4 border-b border-gray-200 flex-shrink-0">
  379. <h2 id="playerPreviewTitle" class="text-xl font-semibold text-gray-800">Pattern Preview</h2>
  380. <button id="closePlayerPreview" class="text-gray-400 hover:text-gray-600 transition-colors">
  381. <span class="material-icons text-2xl">close</span>
  382. </button>
  383. </div>
  384. <!-- Canvas Area -->
  385. <div class="flex-1 p-4 flex justify-center items-center overflow-hidden">
  386. <div class="relative flex items-center justify-center w-full h-full">
  387. <div class="relative max-w-[min(800px,90vw)] max-h-[min(600px,80vh)] aspect-square">
  388. <canvas id="playerPreviewCanvas" width="800" height="800" class="w-full h-full rounded-full border border-gray-300 dark:invert"></canvas>
  389. </div>
  390. </div>
  391. </div>
  392. <!-- Pattern Info and Controls -->
  393. <div class="flex-shrink-0 p-4 border-b border-gray-200 flex items-center gap-4 flex-wrap">
  394. <!-- Pattern Preview Image -->
  395. <div class="flex-shrink-0">
  396. <img id="modal-pattern-preview-img"
  397. class="w-[126px] h-[126px] rounded-full border border-gray-300 object-cover dark:invert" src="">
  398. </div>
  399. <div class="flex-grow w-auto">
  400. <div class="flex items-center justify-between mb-2">
  401. <div class="min-w-0 max-w-[50%]">
  402. <div class="overflow-hidden relative">
  403. <p class="text-lg font-semibold whitespace-nowrap" id="modal-pattern-name">
  404. No pattern playing
  405. </p>
  406. </div>
  407. <p class="text-base text-gray-500" id="modal-eta">
  408. ETA: --:--
  409. </p>
  410. </div>
  411. <div class="flex items-center gap-2 flex-shrink-0">
  412. <button
  413. aria-label="Next"
  414. 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"
  415. id="modal-skip-button"
  416. >
  417. <span class="material-icons text-gray-700">skip_next</span>
  418. </button>
  419. <button
  420. aria-label="Pause"
  421. 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"
  422. id="modal-pause-button"
  423. >
  424. <span class="material-icons text-gray-700">pause</span>
  425. </button>
  426. <button
  427. aria-label="Stop"
  428. 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"
  429. id="modal-stop-button"
  430. >
  431. <span class="material-icons text-gray-700">stop</span>
  432. </button>
  433. </div>
  434. </div>
  435. <div class="w-full bg-gray-300 rounded-full h-2.5 mb-2">
  436. <div
  437. class="bg-blue-600 h-2.5 rounded-full"
  438. id="modal-progress-bar"
  439. style="width: 0%"
  440. ></div>
  441. </div>
  442. <div class="flex items-center justify-between">
  443. <div class="text-base text-gray-500">
  444. Next:
  445. <span class="text-gray-700 font-medium" id="modal-next-pattern">None</span>
  446. </div>
  447. <div class="flex items-center gap-3 text-sm">
  448. <span class="text-gray-500">Speed:</span>
  449. <div class="flex items-center gap-2">
  450. <div class="relative min-w-[4ch] text-center cursor-pointer inline-flex items-center" id="modal-speed-container">
  451. <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>
  452. <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" />
  453. </div>
  454. </div>
  455. </div>
  456. </div>
  457. </div>
  458. </div>
  459. </div>
  460. </div>
  461. <script>
  462. // Function to show status message
  463. function showStatusMessage(message, type = "success") {
  464. const statusContainer = document.getElementById(
  465. "status-message-container"
  466. );
  467. const statusMessage = document.getElementById("status-message");
  468. if (!statusContainer || !statusMessage) return;
  469. // Set message and color based on type
  470. statusMessage.textContent = message;
  471. 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 ${
  472. type === "success"
  473. ? "bg-green-50 text-green-700 border border-green-200"
  474. : type === "error"
  475. ? "bg-red-50 text-red-700 border border-red-200"
  476. : type === "warning"
  477. ? "bg-yellow-50 text-yellow-700 border border-yellow-200"
  478. : "bg-blue-50 text-blue-700 border border-blue-200"
  479. }`;
  480. // Show message with animation
  481. requestAnimationFrame(() => {
  482. statusMessage.classList.remove("opacity-0", "-translate-y-2");
  483. statusMessage.classList.add("opacity-100", "translate-y-0");
  484. });
  485. // Hide message after 5 seconds
  486. setTimeout(() => {
  487. statusMessage.classList.remove("opacity-100", "translate-y-0");
  488. statusMessage.classList.add("opacity-0", "-translate-y-2");
  489. }, 5000);
  490. }
  491. // Initial connection status check
  492. async function checkInitialConnectionStatus() {
  493. try {
  494. const response = await fetch("/serial_status");
  495. if (response.ok) {
  496. const data = await response.json();
  497. const statusDot = document.getElementById("connectionStatusDot");
  498. if (statusDot) {
  499. statusDot.className = `inline-block size-2 rounded-full ml-2 align-middle ${
  500. data.connected ? "bg-green-500" : "bg-red-500"
  501. }`;
  502. }
  503. }
  504. } catch (error) {
  505. console.error("Error checking initial connection status:", error);
  506. }
  507. }
  508. // Check initial status on page load
  509. document.addEventListener("DOMContentLoaded", checkInitialConnectionStatus);
  510. // Player bar toggle logic will be handled by base.js
  511. </script>
  512. <script>
  513. // Cache progress modal functionality
  514. let cacheProgressSocket = null;
  515. let cacheProgressCheckInterval = null;
  516. function initCacheProgressModal() {
  517. const modal = document.getElementById('cacheProgressModal');
  518. const progressBar = document.getElementById('cacheProgressBar');
  519. const progressText = document.getElementById('cacheProgressText');
  520. const progressPercentage = document.getElementById('cacheProgressPercentage');
  521. const currentStage = document.getElementById('cacheCurrentStage');
  522. const currentFile = document.getElementById('cacheCurrentFile');
  523. // Check if cache generation is needed on page load
  524. fetch('/cache-progress')
  525. .then(response => response.json())
  526. .then(data => {
  527. if (data.is_running) {
  528. showCacheProgressModal();
  529. }
  530. })
  531. .catch(error => {
  532. console.error('Error checking initial cache progress:', error);
  533. });
  534. function showCacheProgressModal() {
  535. modal.classList.remove('hidden');
  536. // Connect to WebSocket for real-time updates
  537. const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
  538. const wsUrl = `${protocol}//${window.location.host}/ws/cache-progress`;
  539. cacheProgressSocket = new WebSocket(wsUrl);
  540. cacheProgressSocket.onmessage = function(event) {
  541. const data = JSON.parse(event.data);
  542. if (data.type === 'cache_progress') {
  543. updateCacheProgress(data.data);
  544. }
  545. };
  546. cacheProgressSocket.onclose = function() {
  547. // Fallback to polling if WebSocket fails
  548. startProgressPolling();
  549. };
  550. cacheProgressSocket.onerror = function() {
  551. // Fallback to polling if WebSocket fails
  552. startProgressPolling();
  553. };
  554. }
  555. function startProgressPolling() {
  556. if (cacheProgressCheckInterval) {
  557. clearInterval(cacheProgressCheckInterval);
  558. }
  559. cacheProgressCheckInterval = setInterval(() => {
  560. fetch('/cache-progress')
  561. .then(response => response.json())
  562. .then(data => updateCacheProgress(data))
  563. .catch(error => console.error('Error fetching cache progress:', error));
  564. }, 1000);
  565. }
  566. function updateCacheProgress(data) {
  567. if (!data.is_running && data.stage === 'complete') {
  568. console.log('Initial cache generation completed');
  569. hideCacheProgressModal();
  570. // Trigger cache all prompt after initial cache generation completes
  571. if (typeof onInitialCacheComplete === 'function') {
  572. console.log('Calling onInitialCacheComplete()');
  573. onInitialCacheComplete();
  574. } else {
  575. console.log('onInitialCacheComplete function not found');
  576. }
  577. return;
  578. }
  579. if (!data.is_running && data.stage === 'error') {
  580. progressText.textContent = 'Error occurred';
  581. currentStage.textContent = data.error || 'Unknown error';
  582. currentFile.textContent = '';
  583. return;
  584. }
  585. if (!data.is_running) {
  586. hideCacheProgressModal();
  587. return;
  588. }
  589. // Calculate progress percentage
  590. const percentage = data.total_files > 0 ? Math.round((data.processed_files / data.total_files) * 100) : 0;
  591. // Update progress bar
  592. progressBar.style.width = `${percentage}%`;
  593. progressPercentage.textContent = `${percentage}%`;
  594. // Update text based on stage
  595. let stageText = '';
  596. let progressTextContent = '';
  597. switch (data.stage) {
  598. case 'starting':
  599. stageText = 'Initializing...';
  600. progressTextContent = 'Getting ready...';
  601. break;
  602. case 'metadata':
  603. stageText = 'Processing pattern metadata';
  604. progressTextContent = `${data.processed_files} of ${data.total_files} patterns`;
  605. break;
  606. case 'images':
  607. stageText = 'Generating pattern previews';
  608. progressTextContent = `${data.processed_files} of ${data.total_files} previews`;
  609. break;
  610. default:
  611. stageText = 'Processing...';
  612. progressTextContent = `${data.processed_files} of ${data.total_files} files`;
  613. }
  614. currentStage.textContent = stageText;
  615. progressText.textContent = progressTextContent;
  616. // Update current file
  617. if (data.current_file) {
  618. currentFile.textContent = `Current: ${data.current_file}`;
  619. } else {
  620. currentFile.textContent = '';
  621. }
  622. }
  623. function hideCacheProgressModal() {
  624. modal.classList.add('hidden');
  625. if (cacheProgressSocket) {
  626. cacheProgressSocket.close();
  627. cacheProgressSocket = null;
  628. }
  629. if (cacheProgressCheckInterval) {
  630. clearInterval(cacheProgressCheckInterval);
  631. cacheProgressCheckInterval = null;
  632. }
  633. }
  634. // Expose functions globally for potential external use
  635. window.showCacheProgressModal = showCacheProgressModal;
  636. window.hideCacheProgressModal = hideCacheProgressModal;
  637. }
  638. // Initialize when DOM is loaded
  639. document.addEventListener('DOMContentLoaded', initCacheProgressModal);
  640. </script>
  641. <script>
  642. // Theme switching functionality
  643. document.addEventListener('DOMContentLoaded', function() {
  644. const themeToggle = document.getElementById('theme-toggle');
  645. const themeToggleIcon = document.getElementById('theme-toggle-icon');
  646. // Update icon based on current theme
  647. function updateThemeIcon() {
  648. const isDark = document.documentElement.classList.contains('dark');
  649. themeToggleIcon.textContent = isDark ? 'light_mode' : 'dark_mode';
  650. }
  651. // Initial icon update
  652. updateThemeIcon();
  653. // Theme toggle click handler
  654. themeToggle.addEventListener('click', () => {
  655. const isDark = document.documentElement.classList.toggle('dark');
  656. localStorage.setItem('theme', isDark ? 'dark' : 'light');
  657. updateThemeIcon();
  658. });
  659. });
  660. // Shutdown button functionality
  661. document.addEventListener('DOMContentLoaded', function() {
  662. const shutdownButton = document.getElementById('shutdown-button');
  663. // Shutdown button click handler
  664. shutdownButton.addEventListener('click', async () => {
  665. 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.');
  666. if (!confirmed) return;
  667. try {
  668. showStatusMessage('Initiating shutdown...', 'warning');
  669. const response = await fetch('/api/system/shutdown', { method: 'POST' });
  670. const data = await response.json();
  671. if (data.success) {
  672. showStatusMessage('System is shutting down...', 'success');
  673. } else {
  674. showStatusMessage('Shutdown failed: ' + data.message, 'error');
  675. }
  676. } catch (error) {
  677. showStatusMessage('Failed to shutdown: ' + error.message, 'error');
  678. }
  679. });
  680. });
  681. // Restart button functionality
  682. document.addEventListener('DOMContentLoaded', function() {
  683. const restartButton = document.getElementById('restart-button');
  684. // Restart button click handler
  685. restartButton.addEventListener('click', async () => {
  686. const confirmed = confirm('Are you sure you want to restart the application?\n\nThis will restart the Docker containers.\nThe page will reload automatically when the service is back online.');
  687. if (!confirmed) return;
  688. try {
  689. showStatusMessage('Initiating restart...', 'warning');
  690. const response = await fetch('/api/system/restart', { method: 'POST' });
  691. const data = await response.json();
  692. if (data.success) {
  693. showStatusMessage('System is restarting... Page will reload when ready.', 'success');
  694. // Start checking if the server is back online
  695. setTimeout(() => {
  696. checkServerAndReload();
  697. }, 3000);
  698. } else {
  699. showStatusMessage('Restart failed: ' + data.message, 'error');
  700. }
  701. } catch (error) {
  702. showStatusMessage('Failed to restart: ' + error.message, 'error');
  703. }
  704. });
  705. // Function to check if server is back online and reload
  706. function checkServerAndReload() {
  707. const checkInterval = setInterval(async () => {
  708. try {
  709. const response = await fetch('/api/version', { method: 'GET' });
  710. if (response.ok) {
  711. clearInterval(checkInterval);
  712. showStatusMessage('Server is back online. Reloading...', 'success');
  713. setTimeout(() => {
  714. window.location.reload();
  715. }, 1000);
  716. }
  717. } catch (error) {
  718. // Server not ready yet, keep checking
  719. console.log('Server not ready yet, retrying...');
  720. }
  721. }, 2000);
  722. // Stop checking after 60 seconds
  723. setTimeout(() => {
  724. clearInterval(checkInterval);
  725. }, 60000);
  726. }
  727. });
  728. // Update indicator functionality
  729. document.addEventListener('DOMContentLoaded', async function() {
  730. const updateIndicator = document.getElementById('update-indicator');
  731. if (!updateIndicator) return;
  732. // Check for updates
  733. async function checkForUpdates() {
  734. try {
  735. const response = await fetch('/api/version');
  736. const data = await response.json();
  737. // Show indicator if update is available
  738. if (data.update_available) {
  739. updateIndicator.classList.remove('hidden');
  740. } else {
  741. updateIndicator.classList.add('hidden');
  742. }
  743. } catch (error) {
  744. console.error('Failed to check for updates:', error);
  745. }
  746. }
  747. // Initial check
  748. await checkForUpdates();
  749. // Check every hour
  750. setInterval(checkForUpdates, 3600000);
  751. // Click handler - navigate to settings and scroll to update section
  752. updateIndicator.addEventListener('click', () => {
  753. // Navigate to settings page
  754. window.location.href = '/settings#software-version-section';
  755. });
  756. });
  757. </script>
  758. <!-- Cache All Previews Prompt Modal -->
  759. <div id="cacheAllPromptModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden p-4">
  760. <div class="bg-white dark:bg-gray-800 rounded-lg shadow-xl w-full max-w-md">
  761. <div class="p-6">
  762. <div class="text-center">
  763. <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-2">Cache All Pattern Previews?</h2>
  764. <p class="text-gray-600 dark:text-gray-400 mb-4 max-w-md mx-auto">
  765. Would you like to cache all pattern previews for faster browsing? This will download and store preview images in your browser for instant loading.
  766. </p>
  767. <div class="bg-amber-50 dark:bg-amber-900 p-3 rounded-lg mb-4 text-sm">
  768. <p class="text-amber-700 dark:text-amber-300">
  769. <strong>Note:</strong> This cache is browser-specific. You'll need to repeat this process for each browser you use.
  770. </p>
  771. </div>
  772. <!-- Progress section (hidden initially) -->
  773. <div id="cacheAllProgress" class="mb-4 hidden">
  774. <div class="w-full bg-gray-200 dark:bg-gray-700 rounded-full h-2">
  775. <div id="cacheAllProgressBar" class="bg-blue-600 dark:bg-blue-400 h-2 rounded-full transition-all duration-300" style="width: 0%"></div>
  776. </div>
  777. <div class="mt-2 flex justify-between text-sm text-gray-500 dark:text-gray-400">
  778. <span id="cacheAllProgressText">Starting...</span>
  779. <span id="cacheAllProgressPercentage">0%</span>
  780. </div>
  781. </div>
  782. <!-- Buttons -->
  783. <div id="cacheAllButtons" class="flex gap-3 justify-center">
  784. <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">
  785. Skip for now
  786. </button>
  787. <button id="startCacheAllBtn" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors">
  788. Cache All Previews
  789. </button>
  790. </div>
  791. <!-- Completion message (hidden initially) -->
  792. <div id="cacheAllComplete" class="hidden">
  793. <p class="text-green-600 dark:text-green-400 mb-4">✓ All previews cached successfully!</p>
  794. <button id="closeCacheAllBtn" class="px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors">
  795. Done
  796. </button>
  797. </div>
  798. </div>
  799. </div>
  800. </div>
  801. </div>
  802. </body>
  803. </html>