AzuraCast/frontend/vue/components/Common/StreamingLogView.vue

106 lines
2.2 KiB
Vue
Raw Normal View History

2021-10-07 03:00:53 +00:00
<template>
2022-12-29 21:15:05 +00:00
<b-overlay
variant="card"
:show="loading"
>
2022-06-19 22:28:31 +00:00
<b-form-group label-for="modal_scroll_to_bottom">
2022-12-29 21:15:05 +00:00
<b-form-checkbox
id="modal_scroll_to_bottom"
v-model="scrollToBottom"
>
{{ $gettext('Automatically Scroll to Bottom') }}
2022-06-19 22:28:31 +00:00
</b-form-checkbox>
</b-form-group>
2022-12-29 21:15:05 +00:00
<textarea
id="log-view-contents"
ref="$textarea"
2022-12-29 21:15:05 +00:00
class="form-control log-viewer"
spellcheck="false"
readonly
2022-12-30 15:13:37 +00:00
:value="logs"
/>
2021-10-07 03:00:53 +00:00
</b-overlay>
</template>
<script setup>
2023-01-22 00:14:42 +00:00
import {nextTick, ref, toRef, watch} from "vue";
import {useAxios} from "~/vendor/axios";
2023-01-22 00:14:42 +00:00
import {tryOnScopeDispose} from "@vueuse/core";
2023-01-07 01:55:08 +00:00
const props = defineProps({
logUrl: {
type: String,
required: true,
}
});
2023-01-07 01:55:08 +00:00
const loading = ref(false);
const logs = ref('');
const currentLogPosition = ref(null);
const scrollToBottom = ref(true);
const {axios} = useAxios();
const $textarea = ref(); // Template Ref
2023-01-22 00:14:42 +00:00
let updateInterval = null;
const stop = () => {
if (updateInterval) {
clearInterval(updateInterval);
2021-10-07 03:00:53 +00:00
}
};
2023-01-22 00:14:42 +00:00
tryOnScopeDispose(stop);
const updateLogs = () => {
axios({
method: 'GET',
url: props.logUrl,
params: {
position: currentLogPosition.value
}
}).then((resp) => {
if (resp.data.contents !== '') {
logs.value = logs.value + resp.data.contents + "\n";
2023-01-22 00:14:42 +00:00
if (scrollToBottom.value) {
nextTick(() => {
$textarea.value.scrollTop = $textarea.value.scrollHeight;
});
}
}
currentLogPosition.value = resp.data.position;
2023-01-22 00:14:42 +00:00
if (resp.data.eof) {
stop();
}
2023-01-22 00:14:42 +00:00
}).finally(() => {
loading.value = false;
});
};
2023-01-22 00:14:42 +00:00
watch(toRef(props, 'logUrl'), (newLogUrl) => {
loading.value = true;
2023-01-22 00:14:42 +00:00
logs.value = '';
currentLogPosition.value = 0;
if (null === newLogUrl) {
stop();
} else {
updateInterval = setInterval(updateLogs, 2500);
updateLogs();
}
2023-01-22 00:14:42 +00:00
}, {immediate: true});
const getContents = () => {
return logs.value;
};
defineExpose({
getContents
});
2021-10-07 03:00:53 +00:00
</script>