legendary-doc-site/apps/app/assets/js/live.js

31 lines
1,012 B
JavaScript
Raw Normal View History

2021-07-20 22:13:36 +00:00
import { Socket } from "phoenix";
import { LiveSocket } from "phoenix_live_view";
2021-07-20 22:13:36 +00:00
import topbar from "topbar";
2021-06-19 17:26:20 +00:00
2021-07-20 22:13:36 +00:00
let csrfToken = document
.querySelector("meta[name='csrf-token']")
.getAttribute("content");
2021-06-19 17:26:20 +00:00
2021-07-20 22:13:36 +00:00
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
});
2021-06-19 17:26:20 +00:00
// Show progress bar on live navigation and form submits
2021-07-20 22:13:36 +00:00
topbar.config({
barColors: { 0: "#3B82F6" },
shadowColor: "rgba(0, 0, 0, .3)",
});
window.addEventListener("phx:page-loading-start", (info) => topbar.show());
window.addEventListener("phx:page-loading-stop", (info) => topbar.hide());
2021-06-19 17:26:20 +00:00
// Connect if there are any LiveViews on the page
2021-07-20 22:13:36 +00:00
liveSocket.connect();
2021-06-19 17:26:20 +00:00
// Expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000)
// The latency simulator is enabled for the duration of the browser session.
// Call disableLatencySim() to disable:
// >> liveSocket.disableLatencySim()
2021-07-20 22:13:36 +00:00
window.liveSocket = liveSocket;