Files
dougal-software/lib/www/client/source/src/main.js

107 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-08-08 23:59:13 +02:00
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import vuetify from './plugins/vuetify'
2020-08-25 13:01:38 +02:00
import vueDebounce from 'vue-debounce'
import { mapMutations } from 'vuex';
import { markdown, markdownInline } from './lib/markdown';
import { geometryAsString } from './lib/utils';
2020-08-08 23:59:13 +02:00
Vue.config.productionTip = false
2020-08-25 13:01:38 +02:00
Vue.use(vueDebounce);
Vue.filter('markdown', markdown);
Vue.filter('markdownInline', markdownInline);
Vue.filter('position', (str, item, opts) =>
str
.replace(/@POS(ITION)?@/g, geometryAsString(item, opts) || "(position unknown)")
.replace(/@DMS@/g, geometryAsString(item, {...opts, dms:true}) || "(position unknown)")
);
// Vue.filter('position', (str, item, opts) => str.replace(/@POS(ITION)?@/, "☺"));
2020-08-08 23:59:13 +02:00
new Vue({
data () {
return {
apiUrl: "/api",
snack: false,
snackText: null,
snackColour: null,
user: null,
wsUrl: "/ws",
ws: null
2020-08-08 23:59:13 +02:00
}
},
methods: {
markdown (value) {
return typeof value == "string"
? marked(value)
: value;
},
showSnack(text, colour = "primary") {
console.log("showSnack", text, colour);
this.snackColour = colour;
this.snackText = text;
this.snack = true;
},
initWs () {
if (this.ws) {
console.log("WebSocket initWs already called");
return;
}
this.ws = new WebSocket(this.wsUrl);
this.ws.addEventListener("message", (ev) => {
const msg = JSON.parse(ev.data);
this.setServerEvent(msg);
});
this.ws.addEventListener("open", (ev) => {
console.log("WebSocket connection open", ev);
2020-09-23 22:18:51 +02:00
this.setServerConnectionState(true);
});
this.ws.addEventListener("close", (ev) => {
console.warn("WebSocket connection closed", ev);
delete this.ws;
this.ws = null;
setTimeout( this.initWs, 5000 );
});
this.ws.addEventListener("error", (ev) => {
console.error("WebSocket connection error", ev);
this.ws.close();
delete this.ws;
this.ws = null;
setTimeout( this.initWs, 60000 );
2020-09-23 22:18:51 +02:00
this.setServerConnectionState(false);
});
},
2020-09-23 22:18:51 +02:00
...mapMutations(['setServerEvent', 'setServerConnectionState'])
},
created () {
2020-09-03 21:18:56 +02:00
this.wsUrl = location.protocol == "https:"
? "wss://"+location.host+this.wsUrl
: "ws://"+location.host+this.wsUrl;
2020-08-08 23:59:13 +02:00
2020-09-03 21:18:56 +02:00
this.$nextTick( () => this.initWs() );
2020-08-08 23:59:13 +02:00
},
router,
store,
vuetify,
render: h => h(App)
}).$mount('#app')