ash_hq/assets/js/app.js

142 lines
4 KiB
JavaScript
Raw Normal View History

2022-03-21 17:43:24 +13:00
// If you want to use Phoenix channels, run `mix help phx.gen.channel`
// to get started and then uncomment the line below.
// import "./user_socket.js"
// You can include dependencies in two ways.
//
// The simplest option is to put them in assets/vendor and
// import them using relative paths:
//
// import "../vendor/some-package.js"
//
// Alternatively, you can `npm install some-package --prefix assets` and import
// them using a path starting with the package name:
//
// import "some-package"
//
2022-03-28 10:26:35 +13:00
import scrollIntoView from 'smooth-scroll-into-view-if-needed'
2022-03-21 17:43:24 +13:00
// Include phoenix_html to handle method=PUT/DELETE in forms and buttons.
import "phoenix_html"
// Establish Phoenix Socket and LiveView configuration.
import {Socket} from "phoenix"
import {LiveSocket} from "phoenix_live_view"
import topbar from "../vendor/topbar"
2022-03-30 05:12:28 +13:00
import mermaid from "mermaid"
mermaid.init(".mermaid")
2022-03-26 10:17:01 +13:00
const Hooks = {};
Hooks.ColorTheme = {
mounted() {
this.handleEvent('set_theme', (payload) => {
document.documentElement.classList.add(payload.theme);
if(payload.theme === "dark") {
document.documentElement.classList.remove("light");
} else {
document.documentElement.classList.remove("dark");
};
document.cookie = 'theme' + '=' + payload.theme + ';path=/';
})
}
}
2022-03-30 05:12:28 +13:00
Hooks.Docs = {
mounted() {
mermaid.init(".mermaid")
2022-03-30 17:40:17 +13:00
}
2022-03-30 05:12:28 +13:00
}
2022-03-21 17:43:24 +13:00
let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
2022-03-26 10:17:01 +13:00
let liveSocket = new LiveSocket("/live", Socket, {
params: {_csrf_token: csrfToken},
hooks: Hooks,
metadata: {
keydown: (e) => {
return {
key: e.key,
metaKey: e.metaKey
}
}
}
});
2022-03-21 17:43:24 +13:00
2022-03-29 11:05:19 +13:00
// Show progress bar on live navigation and form submits. Only displays if still
// loading after 120 msec
2022-03-21 17:43:24 +13:00
topbar.config({barColors: {0: "#29d"}, shadowColor: "rgba(0, 0, 0, .3)"})
2022-03-29 11:05:19 +13:00
let topBarScheduled = undefined;
window.addEventListener("phx:page-loading-start", () => {
if(!topBarScheduled) {
topBarScheduled = setTimeout(() => topbar.show(), 120);
};
});
window.addEventListener("phx:page-loading-stop", () => {
clearTimeout(topBarScheduled);
topBarScheduled = undefined;
topbar.hide();
});
2022-03-26 10:17:01 +13:00
window.addEventListener("js:focus", e => e.target.focus())
2022-04-01 05:36:44 +13:00
2022-03-21 17:43:24 +13:00
window.addEventListener("phx:js:scroll-to", (e) => {
const target = document.getElementById(e.detail.id);
2022-03-28 10:26:35 +13:00
const boundary = document.getElementById(e.detail.boundary_id);
scrollIntoView(target, {
behavior: 'smooth',
block: 'center',
boundary: boundary
});
});
2022-04-01 05:36:44 +13:00
let scrolled = false;
window.addEventListener("phx:page-loading-stop", ({detail}) => {
if(detail.kind === "initial" && window.location.hash && !scrolled){
let hashEl = document.getElementById(window.location.hash.substring(1));
hashEl && hashEl.scrollIntoView();
scrolled = true;
}
topbar.hide();
})
2022-03-28 10:26:35 +13:00
window.addEventListener("phx:selected-versions", (e) => {
const cookie = Object.keys(e.detail).map((key) => `${key}:${e.detail[key]}`).join(',');
document.cookie = 'selected_versions' + '=' + cookie + ';path=/';
});
2022-03-30 17:40:17 +13:00
window.addEventListener("phx:selected-types", (e) => {
console.log(e.detail);
const cookie = e.detail.types.join(',');
document.cookie = 'selected_types' + '=' + cookie + ';path=/';
});
2022-03-30 18:07:17 +13:00
window.addEventListener("keydown", (event) => {
if(event.metaKey && event.key === "k") {
console.log("here3")
document.getElementById("search-button").click()
}
})
window.addEventListener("keydown", (event) => {
if(event.key === "Escape") {
document.getElementById("close-search").click()
}
})
window.addEventListener("phx:close-search", (_event) => {
document.getElementById("close-search").click()
})
2022-03-21 17:43:24 +13:00
// connect if there are any LiveViews on the page
liveSocket.connect()
2022-03-30 05:12:28 +13:00
2022-03-21 17:43:24 +13:00
// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket