upend/ui/src/main.ts

45 lines
1.7 KiB
TypeScript
Raw Normal View History

2021-04-04 17:54:46 +02:00
import { setBasePath, SlInput } from "@shoelace-style/shoelace";
2020-09-25 01:11:04 +02:00
import * as Vue from "vue";
2021-03-24 20:38:04 +01:00
import { DirectiveBinding } from "vue";
import App from "./App.vue";
import router from "./router";
// TODO: Remove when UI settles!
2021-04-04 17:54:46 +02:00
setBasePath(`${window.location.origin}/`);
2020-09-25 01:11:04 +02:00
const app = Vue.createApp(App);
app.use(router);
app.directive("sl-model", {
beforeMount: (element: Element, binding: DirectiveBinding<string>) => {
2021-04-24 21:38:48 +02:00
element.addEventListener("sl-input", (event) => {
2020-09-25 01:11:04 +02:00
const slElement = event?.target as
2021-03-24 20:38:04 +01:00
| typeof SlInput.prototype
| undefined;
2020-09-25 01:11:04 +02:00
const value = slElement?.value;
if (value && binding.instance) {
if (Object.hasOwnProperty.bind(binding.instance)(binding.arg!)) {
(binding.instance as any)[binding.arg!] = value;
} else {
const data = (binding.instance.$data as { [key: string]: unknown });
if (data.hasOwnProperty(binding.arg!)) {
data[binding.arg!] = value;
} else {
console.error(`No property "${binding.arg}" exists on instance.`)
}
}
2020-09-25 01:11:04 +02:00
}
});
},
mounted: (element: SlInput, binding: DirectiveBinding<string>) => {
element.value = (binding.instance as any)[binding.arg!];
},
2020-09-25 01:11:04 +02:00
updated: (element: Element, binding: DirectiveBinding<string>) => {
const slElement = element as typeof SlInput.prototype | undefined;
if (slElement) {
slElement.value = (binding.instance?.$data as { [key: string]: unknown })[binding.arg as string] as string;
}
},
});
2021-03-24 20:38:04 +01:00
app.mount("#app");