Files
simple-aircraft-manager-rails/app/javascript/application.js
T
rickyandClaude Opus 5.5 8d5f57da81 Installed app: offline squawks and pre-flight pages, push notifications, shortcuts
Offline
- Flights and squawks saved offline are tagged with their user and only sent
  while that user is signed in; storage is made persistent; Background Sync
  sends them with the app closed (Chromium); unsent count on the app badge
- Report Squawk works offline (photos included, one-time key like flights)
- The Fleet page and, for each of your aircraft, the Overview, W&B, Log
  Flight and Report Squawk pages and photo are saved ahead of time, so the
  app opens and works without signal; copies say when they were saved
- Sign-out clears saved forms, pages and photos (asks first if items wait)
- Offline page rendered by Rails (localized, follows the theme, no inline
  script); service worker version follows the build

Install and notify
- Web Push (web-push gem, VAPID keys from env or storage/vapid_keys.json):
  grounding squawks, newly overdue items (daily, once each), signing
  requests; per-device on/off and test in Profile -> This device; only
  browser push service endpoints accepted
- Manifest shortcuts (Log Flight, Report Squawk via /go/...), lang and
  localized description; install help on Profile; reload notice after a
  release

Also
- Dockerfile: libssl-dev in the build stage (openssl gem); lockfile checksums
- Leftover English and %{_} interpolation names from the localization pass
- Guide and README updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 22:39:42 -05:00

90 lines
3.9 KiB
JavaScript

// Configure your import map in config/importmap.rb.
import "@hotwired/turbo-rails"
import "controllers"
import "chartkick"
import "Chart.bundle"
// Chart text and grid colors follow the light/dark theme.
function styleCharts() {
if (!window.Chart) return
const dark = document.documentElement.dataset.theme === "dark"
window.Chart.defaults.color = dark ? "#a3b1c6" : "#64748b"
window.Chart.defaults.borderColor = dark ? "rgba(148, 163, 184, 0.18)" : "rgba(100, 116, 139, 0.15)"
}
function restyleCharts() {
styleCharts()
if (window.Chartkick) window.Chartkick.eachChart((chart) => chart.redraw())
}
styleCharts()
// Charts can be created before this module runs, so restyle after each page load too.
document.addEventListener("turbo:load", restyleCharts)
window.addEventListener("theme:change", restyleCharts)
// Installable app (PWA): the service worker keeps an offline page, forms and
// pre-flight pages, sends saved items in the background and shows notifications.
if ("serviceWorker" in navigator) {
// A new release's worker takes over open pages: offer a reload (not on the first install).
const hadWorker = !!navigator.serviceWorker.controller
navigator.serviceWorker.addEventListener("controllerchange", () => {
if (hadWorker) document.dispatchEvent(new CustomEvent("app:updated"))
})
// The worker sent saved items in the background: refresh the counts.
navigator.serviceWorker.addEventListener("message", (event) => {
if (event.data?.type === "queue-changed") document.dispatchEvent(new CustomEvent("offline-queue:changed"))
})
window.addEventListener("load", () => navigator.serviceWorker.register("/service-worker.js", { scope: "/" }).catch(() => {}))
}
// Chrome and Android offer to install the app; keep the offer for the button on the profile page.
window.addEventListener("beforeinstallprompt", (event) => {
event.preventDefault()
window.samInstallPrompt = event
document.dispatchEvent(new CustomEvent("install:available"))
})
// Label each table cell with its column header, so phone CSS can show rows as
// cards ("Status: In Service"). Skips tables marked .table-keep.
function labelTableCells(root = document) {
root.querySelectorAll("table.table:not(.table-keep)").forEach((table) => {
const headers = [ ...table.querySelectorAll("thead th") ].map((th) => th.textContent.trim().replace(/\s+/g, " "))
if (headers.length === 0) return
table.querySelectorAll("tbody tr").forEach((row) => {
let column = 0
for (const cell of row.children) {
if (!cell.hasAttribute("data-label")) cell.setAttribute("data-label", headers[column] || "")
column += cell.colSpan || 1
}
})
})
}
document.addEventListener("turbo:load", () => labelTableCells())
document.addEventListener("turbo:frame-load", (event) => labelTableCells(event.target))
// Small behaviours that used to be inline onclick handlers (blocked by the CSP).
document.addEventListener("click", (event) => {
const target = event.target
if (!(target instanceof HTMLInputElement)) return
if (target.matches("[data-select-on-click]")) target.select()
if (target.matches("[data-check-all]")) {
target.closest("table")?.querySelectorAll("tbody input[type=checkbox]").forEach((box) => { box.checked = target.checked })
}
if (target.type === "checkbox") showSelectionActions()
})
// Actions on ticked rows (e.g. "Print selected") appear once a row is ticked:
// data-needs-selection names the form the row checkboxes belong to.
function showSelectionActions() {
document.querySelectorAll("[data-needs-selection]").forEach((el) => {
el.hidden = !document.querySelector(`input[form="${el.dataset.needsSelection}"]:checked`)
})
}
document.addEventListener("turbo:load", showSelectionActions)
document.addEventListener("change", (event) => {
if (event.target.matches?.("[data-submit-on-change]")) event.target.form?.requestSubmit()
})
document.addEventListener("click", (event) => {
if (event.target.closest?.("[data-print]")) window.print()
})