yunohost-admin/app/src/components/MessageListGroup.vue

85 lines
1.7 KiB
Vue
Raw Normal View History

<template>
2024-02-24 14:33:11 +01:00
<BListGroup
2024-02-24 18:25:12 +01:00
v-bind="$attrs"
flush
:class="{ 'fixed-height': fixedHeight, bordered: bordered }"
@scroll="onScroll"
>
<YListGroupItem
v-if="limit && messages.length > limit"
2024-02-24 18:25:12 +01:00
variant="info"
v-t="'api.partial_logs'"
/>
<YListGroupItem
2024-02-24 18:25:12 +01:00
v-for="({ color, text }, i) in reducedMessages"
:key="i"
:variant="color"
size="xs"
>
<span v-html="text" />
</YListGroupItem>
2024-02-24 14:33:11 +01:00
</BListGroup>
</template>
<script>
export default {
name: 'MessageListGroup',
props: {
messages: { type: Array, required: true },
fixedHeight: { type: Boolean, default: false },
bordered: { type: Boolean, default: false },
autoScroll: { type: Boolean, default: false },
2024-02-24 18:25:12 +01:00
limit: { type: Number, default: null },
},
2024-02-24 18:25:12 +01:00
data() {
return {
2024-02-24 18:25:12 +01:00
auto: true,
}
},
computed: {
2024-02-24 18:25:12 +01:00
reducedMessages() {
const len = this.messages.length
if (!this.limit || len <= this.limit) {
return this.messages
}
return this.messages.slice(len - this.limit)
2024-02-24 18:25:12 +01:00
},
},
methods: {
2024-02-24 18:25:12 +01:00
scrollToEnd() {
if (!this.auto) return
this.$nextTick(() => {
this.$el.scrollTo(0, this.$el.lastElementChild.offsetTop)
})
},
2024-02-24 18:25:12 +01:00
onScroll({ target }) {
this.auto = target.scrollHeight === target.scrollTop + target.clientHeight
2024-02-24 18:25:12 +01:00
},
},
2024-02-24 18:25:12 +01:00
created() {
if (this.autoScroll) {
2024-03-11 14:44:55 +01:00
this.$watch('messages', this.scrollToEnd, { deep: true })
}
2024-02-24 18:25:12 +01:00
},
}
</script>
<style lang="scss" scoped>
.fixed-height {
max-height: 20vh;
overflow-y: auto;
}
.bordered {
border: $card-border-width solid $card-border-color;
@include border-radius($card-border-radius);
}
</style>