1// If you want to use Phoenix channels, run `mix help phx.gen.channel`
2// to get started and then uncomment the line below.
3// import "./user_socket.js"
4
5// You can include dependencies in two ways.
6//
7// The simplest option is to put them in assets/vendor and
8// import them using relative paths:
9//
10// import "../vendor/some-package.js"
11//
12// Alternatively, you can `npm install some-package --prefix assets` and import
13// them using a path starting with the package name:
14//
15// import "some-package"
16//
17// If you have dependencies that try to import CSS, esbuild will generate a separate `app.css` file.
18// To load it, simply add a second `<link>` to your `root.html.heex` file.
19
20// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
21import "phoenix_html"
22import "@fontsource-variable/work-sans"
23// Establish Phoenix Socket and LiveView configuration.
24import {Socket} from "phoenix"
25import {LiveSocket} from "phoenix_live_view"
26import {hooks as colocatedHooks} from "phoenix-colocated/comet"
27import topbar from "../vendor/topbar"
28
29const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
30const liveSocket = new LiveSocket("/live", Socket, {
31 longPollFallbackMs: 2500,
32 params: {_csrf_token: csrfToken},
33 hooks: {...colocatedHooks},
34})
35
36// Show progress bar on live navigation and form submits
37topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
38window.addEventListener("phx:page-loading-start", _info => topbar.show(300))
39window.addEventListener("phx:page-loading-stop", _info => topbar.hide())
40
41// connect if there are any LiveViews on the page
42liveSocket.connect()
43
44// expose liveSocket on window for web console debug logs and latency simulation:
45// >> liveSocket.enableDebug()
46// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
47// >> liveSocket.disableLatencySim()
48window.liveSocket = liveSocket
49
50// The lines below enable quality of life phoenix_live_reload
51// development features:
52//
53// 1. stream server logs to the browser console
54// 2. click on elements to jump to their definitions in your code editor
55//
56if (process.env.NODE_ENV === "development") {
57 window.addEventListener("phx:live_reload:attached", ({detail: reloader}) => {
58 // Enable server log streaming to client.
59 // Disable with reloader.disableServerLogs()
60 reloader.enableServerLogs()
61
62 // Open configured PLUG_EDITOR at file:line of the clicked element's HEEx component
63 //
64 // * click with "c" key pressed to open at caller location
65 // * click with "d" key pressed to open at function component definition location
66 let keyDown
67 window.addEventListener("keydown", e => keyDown = e.key)
68 window.addEventListener("keyup", _e => keyDown = null)
69 window.addEventListener("click", e => {
70 if(keyDown === "c"){
71 e.preventDefault()
72 e.stopImmediatePropagation()
73 reloader.openEditorAtCaller(e.target)
74 } else if(keyDown === "d"){
75 e.preventDefault()
76 e.stopImmediatePropagation()
77 reloader.openEditorAtDef(e.target)
78 }
79 }, true)
80
81 window.liveReloader = reloader
82 })
83}
84