app.js (3140B)
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. 21 import "phoenix_html" 22 // Establish Phoenix Socket and LiveView configuration. 23 import {Socket} from "phoenix" 24 import {LiveSocket} from "phoenix_live_view" 25 import {hooks as colocatedHooks} from "phoenix-colocated/daily_tracker" 26 import topbar from "../vendor/topbar" 27 28 const csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content") 29 const liveSocket = new LiveSocket("/live", Socket, { 30 longPollFallbackMs: 2500, 31 params: {_csrf_token: csrfToken}, 32 hooks: {...colocatedHooks}, 33 }) 34 35 // Show progress bar on live navigation and form submits 36 topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"}) 37 window.addEventListener("phx:page-loading-start", _info => topbar.show(300)) 38 window.addEventListener("phx:page-loading-stop", _info => topbar.hide()) 39 40 // connect if there are any LiveViews on the page 41 liveSocket.connect() 42 43 // expose liveSocket on window for web console debug logs and latency simulation: 44 // >> liveSocket.enableDebug() 45 // >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session 46 // >> liveSocket.disableLatencySim() 47 window.liveSocket = liveSocket 48 49 // The lines below enable quality of life phoenix_live_reload 50 // development features: 51 // 52 // 1. stream server logs to the browser console 53 // 2. click on elements to jump to their definitions in your code editor 54 // 55 if (process.env.NODE_ENV === "development") { 56 window.addEventListener("phx:live_reload:attached", ({detail: reloader}) => { 57 // Enable server log streaming to client. 58 // Disable with reloader.disableServerLogs() 59 reloader.enableServerLogs() 60 61 // Open configured PLUG_EDITOR at file:line of the clicked element's HEEx component 62 // 63 // * click with "c" key pressed to open at caller location 64 // * click with "d" key pressed to open at function component definition location 65 let keyDown 66 window.addEventListener("keydown", e => keyDown = e.key) 67 window.addEventListener("keyup", _e => keyDown = null) 68 window.addEventListener("click", e => { 69 if(keyDown === "c"){ 70 e.preventDefault() 71 e.stopImmediatePropagation() 72 reloader.openEditorAtCaller(e.target) 73 } else if(keyDown === "d"){ 74 e.preventDefault() 75 e.stopImmediatePropagation() 76 reloader.openEditorAtDef(e.target) 77 } 78 }, true) 79 80 window.liveReloader = reloader 81 }) 82 } 83