hardlounge/client/components/Windows/Connect.vue

118 lines
2.5 KiB
Vue
Raw Permalink Normal View History

<template>
2019-11-08 18:50:28 +00:00
<NetworkForm :handle-submit="handleSubmit" :defaults="defaults" :disabled="disabled" />
</template>
<script lang="ts">
import {defineComponent, ref} from "vue";
2019-11-16 17:24:03 +00:00
import socket from "../../js/socket";
import {useStore} from "../../js/store";
import NetworkForm, {NetworkFormDefaults} from "../NetworkForm.vue";
export default defineComponent({
name: "Connect",
components: {
NetworkForm,
},
2019-11-08 18:50:28 +00:00
props: {
queryParams: Object,
},
setup(props) {
const store = useStore();
const disabled = ref(false);
const handleSubmit = (data: Record<string, any>) => {
disabled.value = true;
socket.emit("network:new", data);
};
const parseOverrideParams = (params?: Record<string, string>) => {
if (!params) {
return {};
}
const parsedParams: Record<string, any> = {};
2019-11-08 18:50:28 +00:00
for (let key of Object.keys(params)) {
2019-11-23 22:45:04 +00:00
let value = params[key];
2019-11-08 18:50:28 +00:00
// Param can contain multiple values in an array if its supplied more than once
2019-11-23 22:45:04 +00:00
if (Array.isArray(value)) {
value = value[0];
}
2019-11-08 18:50:28 +00:00
// Support `channels` as a compatibility alias with other clients
if (key === "channels") {
key = "join";
}
if (
!Object.prototype.hasOwnProperty.call(
store.state.serverConfiguration?.defaults,
2019-11-08 18:50:28 +00:00
key
)
) {
continue;
}
// When the network is locked, URL overrides should not affect disabled fields
if (
store.state.serverConfiguration?.lockNetwork &&
["name", "host", "port", "tls", "rejectUnauthorized"].includes(key)
2019-11-08 18:50:28 +00:00
) {
continue;
}
if (key === "join") {
value = value
.split(",")
.map((chan) => {
if (!chan.match(/^[#&!+]/)) {
return `#${chan}`;
}
return chan;
})
.join(", ");
}
// Override server provided defaults with parameters passed in the URL if they match the data type
switch (typeof store.state.serverConfiguration?.defaults[key]) {
2019-11-08 18:50:28 +00:00
case "boolean":
2019-11-23 22:45:04 +00:00
if (value === "0" || value === "false") {
parsedParams[key] = false;
} else {
parsedParams[key] = !!value;
}
2019-11-08 18:50:28 +00:00
break;
case "number":
parsedParams[key] = Number(value);
break;
case "string":
parsedParams[key] = String(value);
break;
}
}
return parsedParams;
};
const defaults = ref<Partial<NetworkFormDefaults>>(
Object.assign(
{},
store.state.serverConfiguration?.defaults,
parseOverrideParams(props.queryParams)
)
);
return {
defaults,
disabled,
handleSubmit,
};
},
});
</script>