aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/blocks/CoursewareTableOfContentsBlock.vue
blob: 868b3eda059afed87b4d5a8efa2c66fde44726e2 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<template>
    <div class="cw-block cw-block-table-of-contents">
        <courseware-default-block :block="block" :canEdit="canEdit" :isTeacher="isTeacher" :preview="true"
            @showEdit="initCurrentData" @storeEdit="storeText" @closeEdit="initCurrentData">
            <template #content>
                <div v-if="childElementsWithTasks.length > 0">
                    <div v-if="currentStyle !== 'tiles' && currentTitle !== ''" class="cw-block-title">
                        {{ currentTitle }}
                    </div>
                    <ul v-if="currentStyle === 'list-details' || currentStyle === 'list'"
                        :class="['cw-block-table-of-contents-' + currentStyle]">
                        <li v-for="child in childElementsWithTasks" :key="child.id">
                            <router-link :to="'/structural_element/' + child.id">
                                <div class="cw-block-table-of-contents-title-box" :class="[child.attributes.payload.color ?? 'studip-blue']">
                                    {{ child.attributes.title }}
                                    <span v-if="child.attributes.purpose === 'task'"> | {{ child.solverName }}</span>
                                    <p v-if="currentStyle === 'list-details'">
                                        {{ child.attributes.payload.description }}
                                    </p>
                                </div>
                            </router-link>
                        </li>
                    </ul>
                    <ul v-if="currentStyle === 'tiles'" class="cw-block-table-of-contents-tiles cw-tiles">
                        <li v-for="child in childElementsWithTasks" :key="child.id">
                            <router-link :to="'/structural_element/' + child.id" :title="child.attributes.purpose === 'task'
                                    ? child.attributes.title + ' | ' + child.solverName
                                    : child.attributes.title
                                ">
                                <courseware-tile tag="div" :color="child.attributes.payload.color ?? 'studip-blue'"
                                    :title="child.attributes.title" :imageUrl="getChildImageUrl(child)"
                                >
                                    <template v-if="child.attributes.purpose === 'task'" #image-overlay>
                                        {{ child.solverName }}
                                    </template>
                                    <template #description>
                                        {{ child.attributes.payload.description }}
                                    </template>
                                    <template #footer>
                                        {{ $ngettext(
                                            '%{length} Seite',
                                            '%{length} Seiten',
                                            countChildChildren(child),
                                            { length: countChildChildren(child) }
                                        ) }}
                                    </template>
                                </courseware-tile>
                            </router-link>
                        </li>
                    </ul>
                </div>
                <courseware-companion-box v-if="viewMode === 'edit' && childElementsWithTasks.length === 0" :msgCompanion="$gettext(
                    'Es sind noch keine Unterseiten vorhanden. ' +
                    'Sobald Sie weitere Unterseiten anlegen, erscheinen diese automatisch hier im Inhaltsverzeichnis.'
                )
                    " mood="pointing" />
            </template>
            <template v-if="canEdit" #edit>
                <form class="default" @submit.prevent="">
                    <label>
                        {{ $gettext('Überschrift') }}
                        <input type="text" v-model="currentTitle" />
                    </label>
                    <label>
                        {{ $gettext('Layout') }}
                        <select v-model="currentStyle">
                            <option value="list">{{ $gettext('Liste') }}</option>
                            <option value="list-details">{{ $gettext('Liste mit Beschreibung') }}</option>
                            <option value="tiles">{{ $gettext('Kacheln') }}</option>
                        </select>
                    </label>
                </form>
            </template>
            <template #info>{{ $gettext('Informationen zum Inhaltsverzeichnis-Block') }}</template>
        </courseware-default-block>
    </div>
</template>

<script>
import BlockComponents from './block-components.js';
import CoursewareTile from '../layouts/CoursewareTile.vue';
import blockMixin from '@/vue/mixins/courseware/block.js';
import { mapActions, mapGetters } from 'vuex';

export default {
    name: 'courseware-table-of-contents-block',
    mixins: [blockMixin],
    components: Object.assign(BlockComponents, { CoursewareTile }),
    props: {
        block: Object,
        canEdit: Boolean,
        isTeacher: Boolean,
    },
    data() {
        return {
            currentTitle: '',
            currentStyle: '',
        };
    },
    computed: {
        ...mapGetters({
            childrenById: 'courseware-structure/children',
            structuralElementById: 'courseware-structural-elements/byId',
            taskById: 'courseware-tasks/byId',
            userById: 'users/byId',
            groupById: 'status-groups/byId',
            viewMode: 'viewMode',
        }),
        structuralElement() {
            return this.structuralElementById({ id: this.$route.params.id });
        },
        childElements() {
            return this.childrenById(this.structuralElement.id).map((id) => this.structuralElementById({ id }));
        },
        title() {
            return this.block?.attributes?.payload?.title;
        },
        style() {
            return this.block?.attributes?.payload?.style;
        },
        childElementsWithTasks() {
            let children = [];
            this.childElements.forEach((element) => {
                if (element.relationships.task.data) {
                    const solverName = this.getSolverName(element.relationships.task.data.id);
                    if (solverName) {
                        element.solverName = solverName;
                        children.push(element);
                    }
                } else {
                    children.push(element);
                }
            });

            return children;
        },
    },
    mounted() {
        this.initCurrentData();
        this.childElements.forEach((element) => {
            if (element.relationships.task.data) {
                const taskId = element.relationships.task.data.id;
                try {
                    this.loadTask({
                        taskId: taskId,
                    });
                } catch {
                    // nothing to do here
                }
            }
        });
    },
    methods: {
        ...mapActions({
            updateBlock: 'updateBlockInContainer',
            loadTask: 'loadTask',
        }),
        initCurrentData() {
            this.currentTitle = this.title;
            this.currentStyle = this.style;
        },
        storeText() {
            let attributes = {};
            attributes.payload = {};
            attributes.payload.title = this.currentTitle;
            attributes.payload.style = this.currentStyle;

            this.updateBlock({
                attributes: attributes,
                blockId: this.block.id,
                containerId: this.block.relationships.container.data.id,
            });
        },
        getChildImageUrl(child) {
            return child.relationships?.image?.meta?.['download-url'];
        },
        countChildChildren(child) {
            return this.childrenById(child.id).length + 1;
        },
        hasImage(child) {
            return child.relationships?.image?.data !== null;
        },

        getSolverName(taskId) {
            const task = this.taskById({ id: taskId });
            if (task) {
                const solver = task.relationships.solver.data;
                if (solver?.type === 'users') {
                    const user = this.userById({ id: solver.id });

                    return user.attributes['formatted-name'];
                }
                if (solver?.type === 'status-groups') {
                    const group = this.groupById({ id: solver.id });

                    return group.attributes.name;
                }
            }
            return null;
        },
    },
};
</script>
<style scoped lang="scss">
@import '../../../../assets/stylesheets/scss/courseware/blocks/table-of-contents';
</style>