diff options
| author | Mitsuo Tokumori <[email protected]> | 2025-03-08 02:17:52 +0900 |
|---|---|---|
| committer | Mitsuo Tokumori <[email protected]> | 2025-03-08 02:17:52 +0900 |
| commit | d9af103b9a8aed86d6ac834f1240edfb2173ffa0 (patch) | |
| tree | 6b11b47db6201a357277b587561fb429c2451b99 /static/script.js | |
| parent | ec80766af5e2f59f3842b613f271c1100705af5e (diff) | |
| download | masu-d9af103b9a8aed86d6ac834f1240edfb2173ffa0.tar.gz masu-d9af103b9a8aed86d6ac834f1240edfb2173ffa0.tar.bz2 masu-d9af103b9a8aed86d6ac834f1240edfb2173ffa0.zip | |
Restructure js and css into single block modules
Each block is defined by:
* html: front-end elements (modify: templates/index.html) (for now)
* css: front-end design (modify: static/style.css)
* js: front-end code (new file: static/block_name.js)
* python: back-end code (new file: ./block_name.py)
(will move later from the root to an "app" directory (python package)
Diffstat (limited to 'static/script.js')
| -rw-r--r-- | static/script.js | 78 |
1 files changed, 31 insertions, 47 deletions
diff --git a/static/script.js b/static/script.js index f81f9ef..efd6836 100644 --- a/static/script.js +++ b/static/script.js @@ -1,37 +1,35 @@ -import { initTimeVisualizer } from './timeVisualizer.js'; +import { updateTime } from './block_time.js'; +import { updateWeather } from './block_weather.js'; -let polling = true -const blocks = { - time: { interval: 30 * 1000, lastUpdate: 0, update: updateTime }, // 30s - weather: { interval: 30 * 60000, lastUpdate: 0, update: updateWeather } // 30min - // Add more: { interval: X, lastUpdate: 0, update: updateFunction } -}; -let lastPoll = 0; +const config = { + polling: true +} + +function init() { + const blocks = { + time: { interval: 30 * 1000, lastUpdate: 0, update: updateTime }, // 30s + weather: { interval: 30 * 60000, lastUpdate: 0, update: updateWeather } // 30min + // Add more: { interval: X, lastUpdate: 0, update: updateFunction } + }; + + initHeaderControls(blocks) -function updateTime() { - fetch("/time") - .then(res => res.json()) - .then(data => { - document.getElementById("weatherSummary").innerText = data.time; - }); + // Initial load + // Poll every 500ms to check intervals (fast enough for 1s updates, light on CPU) + // maybe the 1s updates should be special case, but for now let's keep it simple + reloadAll(blocks); + setInterval(() => pollUpdates(blocks), 500); } -function updateWeather() { - const city = document.getElementById("city").value; - fetch(`/weather?city=${encodeURIComponent(city)}`) - .then(res => res.json()) - .then(data => { - if (!data) { - document.getElementById("weather-summary").innerText = `Error, "${city}" city not found`; - return - } - document.getElementById("weather-summary").innerText = data.summary; - document.getElementById("weather-icon").src = data.icon_url; - }); +function reloadAll(blocks) { + Object.keys(blocks).forEach(key => { + blocks[key].update(); + blocks[key].lastUpdate = Date.now(); + }); } -function pollUpdates() { - if (!polling) return; +function pollUpdates(blocks) { + if (!config.polling) return; const now = Date.now(); Object.keys(blocks).forEach(key => { const block = blocks[key]; @@ -40,27 +38,13 @@ function pollUpdates() { block.lastUpdate = now; } }); - lastPoll = now; } -function reloadAll() { - Object.keys(blocks).forEach(key => { - blocks[key].update(); - blocks[key].lastUpdate = Date.now(); +function initHeaderControls(blocks) { + document.getElementById("reload").addEventListener("click", () => reloadAll(blocks)); + document.getElementById("pause").addEventListener("change", (e) => { + config.polling = e.target.checked; }); } -// Header controls -document.getElementById("reload").addEventListener("click", reloadAll); -document.getElementById("pause").addEventListener("change", (e) => { - polling = e.target.checked; -}); - -// Initial load -reloadAll(); -// Poll every 500ms to check intervals (fast enough for 1s updates, light on CPU) -// maybe the 1s updates should be special case, but for now let's keep it simple -setInterval(pollUpdates, 500); - -// timeVisualizer -initTimeVisualizer('timeVisualizer'); +init() |
