aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/blocks/CoursewareBlubberThread.vue
blob: 504448d3d15ef5a50fce71287f43aae6eb7a4e1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<template>
    <div class="cw-blubber-thread blubber_thread">
        <ol
            v-show="!loadingThreads && threadComments.length > 0"
            class="cw-blubber-comments comments"
            aria-live="polite"
            ref="commentsRef"
        >
            <courseware-blubber-comment
                v-for="comment in threadComments"
                :key="comment.id"
                :comment-id="comment.id"
                :editing="comment.id === editingComments"
                @answer="answerComment"
                @delete="loadThread(threadId)"
                @editing="editingComment"
            />
        </ol>
        <courseware-companion-box
            v-show="!loadingThreads && threadComments.length === 0"
            class="cw-blubber-thread-empty"
            :msgCompanion="$gettext('Bisher wurde noch nicht diskutiert.')"
            mood="pointing"
        />
        <div v-show="!loadingThreads" class="cw-blubber-thread-add-comment">
            <textarea
                ref="composer"
                v-model="newComment"
                :placeholder="$gettext('Schreiben Sie eine Nachricht…')"
                spellcheck="true"
                @keydown.enter.exact="createComment"
                @keyup.up.exact="editPreviousComment"
            ></textarea>
            <button class="button" @click="createComment">
                {{ $gettext('Absenden') }}
            </button>
        </div>
        <studip-progress-indicator
            v-show="loadingThreads"
            class="cw-loading-indicator-blubber-comment"
            :description="$gettext('Lade Beiträge…')"
        />
    </div>
</template>

<script>
import CoursewareBlubberComment from './CoursewareBlubberComment.vue';
import CoursewareCompanionBox from '../layouts/CoursewareCompanionBox.vue';
import StudipProgressIndicator from '../../StudipProgressIndicator.vue';
import JSUpdater from '@/assets/javascripts/lib/jsupdater.js';
import { mapActions, mapGetters } from 'vuex';

export default {
    name: 'courseware-blubber-thread',
    components: {
        CoursewareBlubberComment,
        CoursewareCompanionBox,
        StudipProgressIndicator
    },
    props: {
        threadId: String,
    },
    data() {
        return {
            newComment: '',
            loadingThreads: true,
            updater: null,
            editingComments: null,
        }
    },
    computed: {
        ...mapGetters({
            blubberThreadById: 'blubber-threads/byId',
            blubberCommentsById: 'blubber-comments/byId',
        }),
        blubberThread() {
            return this.blubberThreadById({ id: this.threadId });
        },
        threadComments() {
            let comments = this.blubberThread?.relationships?.comments?.data;
            if (comments) {
                return comments;
            }
            return [];
        },
        threadTitle() {
            return this.blubberThread?.attributes?.content;
        }
    },
    methods: {
        ...mapActions({
            loadBlubberThread: 'loadBlubberThread',
            createBlubberComment: 'createBlubberComment',
            companionInfo: 'companionInfo',
        }),
        async createComment() {
            if (this.newComment) {
                await this.createBlubberComment({
                    threadId: this.threadId,
                    content: this.newComment
                });
                this.newComment = '';
            } else {
                this.companionInfo({ info: this.$gettext('Leere Beiträge können nicht erstellt werden.') });
            }
        },
        scrollDown() {
            this.$nextTick( () => {
                let ref = this.$refs["commentsRef"];
                if (ref) {
                    ref.scrollTop = ref.scrollHeight;
                }
            });
        },
        async loadThread(threadId) {
            await this.loadBlubberThread({ threadId: threadId});
            this.$emit('threadContent', this.threadTitle);
        },
        answerComment(content) {
            this.newComment = content;
            this.$refs.composer.focus();
        },
        editingComment(event) {
            this.editingComments = event;
        },
        editPreviousComment() {
            const comments = this.threadComments;
            if (comments.length > 0) {
                this.editingComments = comments[comments.length - 1].id;
            }
        }
    },
    mounted() {
        this.$nextTick(async() => {
            if (this.threadId) {
                await this.loadThread(this.threadId);
                this.loadingThreads = false;
                this.scrollDown();
                JSUpdater.register('blubber', () => this.loadThread(this.threadId), { threads: [this.threadId] });
            }
        });
    },
    watch: {
        threadId(newId) {
            if (newId) {
                this.loadThread(newId);
            }
        },
        threadComments(newComments, oldComments) {
            if (newComments.length !== oldComments.length && !this.editingComments) {
                this.scrollDown();
            }
        }
    }
}
</script>