bonfire-app/assets/js/bonfire_live.js

42 lines
1.3 KiB
JavaScript
Raw Normal View History

2021-01-05 15:37:12 +00:00
// JS shared with non_live pages
import "./common"
2021-01-05 15:37:12 +00:00
// for JS features & extensions to hook into LiveView
let Hooks = {};
// Semi-boilerplate Phoenix+LiveView...
2020-08-05 07:39:59 +00:00
import {Socket} from "phoenix"
import NProgress from "nprogress"
import {LiveSocket} from "phoenix_live_view"
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
2021-01-05 15:37:12 +00:00
let liveSocket = new LiveSocket("/live", Socket, {
params: { _csrf_token: csrfToken },
2021-01-07 14:49:04 +00:00
dom: {
onBeforeElUpdated(from, to){
if(from.__x){ Alpine.clone(from.__x, to) }
}
},
2021-01-05 15:37:12 +00:00
hooks: Hooks
})
2020-08-05 07:39:59 +00:00
// Show progress bar on live navigation and form submits
window.addEventListener("phx:page-loading-start", info => NProgress.start())
window.addEventListener("phx:page-loading-stop", info => NProgress.done())
// connect if there are any LiveViews on the page
liveSocket.connect()
// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
2020-09-08 05:56:41 +00:00
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
2021-06-11 08:56:43 +00:00
2020-08-05 07:39:59 +00:00
window.liveSocket = liveSocket
2021-01-05 15:37:12 +00:00
2021-05-15 14:28:18 +00:00
// Extensions... # TODO: make this more modular/configurable
2021-06-11 10:52:12 +00:00
import { ExtensionHooks } from "../../deps/bonfire_geolocate/assets/js/extension"
2021-05-15 14:28:18 +00:00
Object.assign(liveSocket.hooks, ExtensionHooks);