diff options
Diffstat (limited to 'assets/site.js')
-rw-r--r-- | assets/site.js | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/assets/site.js b/assets/site.js index 2a1bdd6..9fc1a44 100644 --- a/assets/site.js +++ b/assets/site.js @@ -1,6 +1,36 @@ import * as Turbo from "@hotwired/turbo" import { Application } from "stimulus" import { definitions } from 'stimulus:./controllers'; +import {Socket} from "phoenix" +import {LiveSocket} from "phoenix_live_view" +import { DateTime } from "luxon"; const app = Application.start(); app.load(definitions); + +let Hooks = {} + +Hooks.AutoFocus = { + mounted() { + this.el.focus(); + } +} + +Hooks.NaiveDateTimeUTC = { + mounted() { + var intl = Intl.DateTimeFormat().resolvedOptions(); + var tz = document.body.dataset.tz || intl.timeZone || "UTC"; + var locale = document.body.dataset.locale || intl.locale || "en"; + + var style = DateTime[this.el.dataset.timeFormat]; + var date = DateTime.fromISO(this.el.dateTime, { zone: 'utc' }).setZone(tz).setLocale(locale); + this.el.innerText = date.toLocaleString(style); + } +} + +let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") +let liveSocket = new LiveSocket("/live", Socket, {hooks: Hooks, params: {_csrf_token: csrfToken}}) + +// Connect if there are any LiveViews on the page +liveSocket.connect() + |