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