summaryrefslogtreecommitdiff
path: root/assets/site.js
blob: 9fc1a442aa3a76e1c411b831dcd949a08371ee56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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()