ModalEdit.vue 16.6 KB
Newer Older
1
<template>
Richard Blechinger's avatar
Richard Blechinger committed
2
3
4
    <div>
        <b-modal
                ref="modalEmissionManagerEdit"
5
                :title="$t('scheduleEditor.titleEdit', { show: selectedShow.name })"
Richard Blechinger's avatar
Richard Blechinger committed
6
7
8
                size="lg"
        >
            <div v-if="timeslot && loaded.schedule">
9
10
11
12
13
14
15
16
                <p
                    class="tw-mb-0"
                    v-html="$t('scheduleEditor.timeslotRuns', {
                      dstart: this.prettyDate(timeslot.start),
                      tstart: this.prettyTime(timeslot.start),
                      tend: this.prettyTime(timeslot.end),
                    })"
                />
Richard Blechinger's avatar
Richard Blechinger committed
17
18
19

                <div v-if="schedule.rrule === 1">
                    <p>
20
                        {{ $t('scheduleEditor.singleEmission') }}
Richard Blechinger's avatar
Richard Blechinger committed
21
                        <span v-if="!loaded.scheduleTimeslots">
22
23
24
25
26
27
                            <br>
                            <img
                                  src="/assets/radio.gif"
                                  :alt="$t('loading')"
                            >
                        </span>
Richard Blechinger's avatar
Richard Blechinger committed
28
                        <span v-else>
29
30
31
32
33
34
35
36
37
                            <span
                                v-if="scheduleTimeslots.length > 1"
                                v-html="'<br>' + $t('scheduleEditor.coexistingTimeslot', {
                                    dstart: timeslot.start === scheduleTimeslots[0].start ? this.prettyDate(this.scheduleTimeslots[1].start) : this.prettyDate(this.scheduleTimeslots[0].start),
                                    tstart: timeslot.start === scheduleTimeslots[0].start ? this.prettyTime(this.scheduleTimeslots[1].start) : this.prettyTime(this.scheduleTimeslots[0].start),
                                    tend: timeslot.start === scheduleTimeslots[0].start ? this.prettyTime(this.scheduleTimeslots[1].end) : this.prettyTime(this.scheduleTimeslots[0].end),
                                })"
                            />
                        </span>
Richard Blechinger's avatar
Richard Blechinger committed
38
39
40
                    </p>
                </div>
                <div v-else>
41
42
43
44
                    <p v-html="$t('scheduleEditor.recurringSchedule', {
                        rrule: this.rruleRender(this.schedule.rrule),
                        until: this.prettyDate(this.schedule.until),
                    })" />
Richard Blechinger's avatar
Richard Blechinger committed
45
46
47
48
49
50

                    <b-table
                            id="emission-table"
                            striped
                            :per-page="perPage"
                            :current-page="currentPage"
51
52
53
54
                            :fields="[
                                { key: 'start', label: this.$t('scheduleEditor.start') },
                                { key: 'end', label: this.$t('scheduleEditor.end') }
                            ]"
Richard Blechinger's avatar
Richard Blechinger committed
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
                            :items="scheduleTimeslots"
                            :busy="!loaded.scheduleTimeslots"
                            :tbody-tr-class="trClass"
                    >
                        <template v-slot:cell(start)="data">
                            {{ prettyDateTime(data.item.start) }}
                        </template>

                        <template v-slot:cell(end)="data">
                            {{ prettyDateTime(data.item.end) }}
                        </template>
                    </b-table>

                    <div class="tw-w-full tw-flex tw-justify-end">
                        <b-pagination
                                v-model="currentPage"
                                :total-rows="scheduleTimeslots.length"
                                :per-page="perPage"
                                aria-controls="emission-table"
                        />
                    </div>
                </div>
            </div>
            <div v-else>
                <img
                        src="/assets/radio.gif"
81
                        :alt="$t('loading')"
Richard Blechinger's avatar
Richard Blechinger committed
82
83
84
85
86
                >
            </div>

            <hr>

87
            <h4>{{ $t('scheduleEditor.addRepetition') }}</h4>
Richard Blechinger's avatar
Richard Blechinger committed
88
89
90
            <b-row>
                <b-col cols="6">
                    <label class="tw-leading-loose">
91
                        {{ $t('scheduleEditor.whenToRepeat') }}
Richard Blechinger's avatar
Richard Blechinger committed
92
93
94
95
96
97
98

                        <b-form-select
                                v-model="repetitionRule"
                                :options="repetitionOptions"
                        />
                    </label>

99
                    <b-checkbox
100
101
                            v-if="repetitionRule !== 4"
                            v-model="useSameTime"
102
                    >
103
                        {{ $t('scheduleEditor.useSameTime') }}
Richard Blechinger's avatar
Richard Blechinger committed
104
105
106
107
108
109
110
                    </b-checkbox>
                </b-col>
                <b-col
                        v-if="!useSameTime"
                        cols="6"
                >
                    <label class="tw-leading-loose">
111
112
                        {{ $t('scheduleEditor.repeatAt') }}
                        <b-form-input type="time"/>
Richard Blechinger's avatar
Richard Blechinger committed
113
114
115
116
117
118
119
                    </label>
                </b-col>
            </b-row>

            <b-row
                    v-if="repetitionRule === 3"
                    class="tw-mt-4"
120
            >
Richard Blechinger's avatar
Richard Blechinger committed
121
122
                <b-col cols="6">
                    <label class="tw-leading-loose">
123
                        {{ $t('scheduleEditor.addNoOfDays') }}
Richard Blechinger's avatar
Richard Blechinger committed
124
125
126
127
128
129
130
131

                        <b-form-input
                                v-model="addNoOfDays"
                                type="number"
                        />
                    </label>

                    <b-checkbox v-model="onlyBusinessDays">
132
                        {{ $t('scheduleEditor.onlyBusinessDays') }}
Richard Blechinger's avatar
Richard Blechinger committed
133
134
135
136
137
138
139
140
141
142
143
                    </b-checkbox>
                </b-col>
            </b-row>

            <b-row class="mt-4">
                <b-col>
                    <b-button
                            variant="primary"
                            size="sm"
                            @click="createRepetitionSchedule"
                    >
144
                        {{ $t('scheduleEditor.addRepetition') }}
Richard Blechinger's avatar
Richard Blechinger committed
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
                    </b-button>
                </b-col>
            </b-row>

            <template v-slot:modal-footer>
                <div
                        v-if="loaded.scheduleTimeslots"
                        class="tw-w-full tw-flex tw-justify-between tw-items-center"
                >
                    <div class="tw-space-x-2">
                        <b-button
                                variant="danger"
                                size="sm"
                                @click="deleteFullSchedule(schedule.id)"
                        >
160
161
162
                            <span v-if="scheduleTimeslots.length === 1">{{ $t('scheduleEditor.delete.delete') }}</span>
                            <span v-else-if="schedule.rrule === 1">{{ $t('scheduleEditor.delete.both') }}</span>
                            <span v-else>{{ $t('scheduleEditor.delete.scheduleTimeslots') }}</span>
Richard Blechinger's avatar
Richard Blechinger committed
163
164
165
166
167
168
169
170
                        </b-button>

                        <b-button
                                v-if="schedule.rrule > 1 && scheduleTimeslots.length > 1"
                                variant="danger"
                                size="sm"
                                @click="deleteSingleTimeslot(schedule.id, timeslot.id)"
                        >
171
                            {{ $t('scheduleEditor.delete.timeslot') }}
Richard Blechinger's avatar
Richard Blechinger committed
172
173
174
175
176
177
178
179
                        </b-button>

                        <b-button
                                v-if="schedule.rrule > 1 && scheduleTimeslots.length > 1"
                                variant="danger"
                                size="sm"
                                @click="deleteAllFutureTimeslots(schedule.id, timeslot.id)"
                        >
180
                            {{ $t('scheduleEditor.delete.allTimeslots') }}
Richard Blechinger's avatar
Richard Blechinger committed
181
182
183
184
185
186
                        </b-button>
                    </div>
                </div>
                <div v-else>
                    <img
                            src="/assets/radio.gif"
187
                            :alt="$t('loading')"
Richard Blechinger's avatar
Richard Blechinger committed
188
189
190
191
192
193
194
                    >
                </div>
            </template>
        </b-modal>

        <b-modal
                ref="modalEmissionManagerDeleteTimeslots"
195
                :title="$t('scheduleEditor.delete.timeslotsTitle')"
Richard Blechinger's avatar
Richard Blechinger committed
196
197
198
199
200
                size="lg"
                centered
                hide-footer
                no-close-on-esc
                no-close-on-backdrop
201
        >
Richard Blechinger's avatar
Richard Blechinger committed
202
203
204
            <div>
                <img
                        src="/assets/radio.gif"
205
                        :alt="$t('loading')"
Richard Blechinger's avatar
Richard Blechinger committed
206
207
208
209
210
211
212
213
214
215
                >
                <b-progress
                        :value="deletion.count"
                        :max="deletion.amount"
                        variant="info"
                        animated
                />
            </div>
        </b-modal>
    </div>
216
217
218
</template>

<script>
Richard Blechinger's avatar
Richard Blechinger committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    import {mapGetters} from 'vuex'
    import prettyDate from '../../mixins/prettyDate'
    import rrules from '../../mixins/rrules'

    export default {
        mixins: [prettyDate, rrules],

        data() {
            return {
                currentPage: 1,
                perPage: 5,
                timeslot: null,
                deletion: {
                    amount: 0,
                    count: 0,
                },
                useSameTime: true,
                onlyBusinessDays: false,
                addNoOfDays: 1,
                repetitionRule: 1,
239
            }
Richard Blechinger's avatar
Richard Blechinger committed
240
241
242
243
244
245
246
247
248
249
250
        },

        computed: {
            loaded() {
                return {
                    shows: this.$store.state.shows.loaded.shows,
                    schedule: this.$store.state.shows.loaded.schedule,
                    scheduleTimeslots: this.$store.state.shows.loaded.scheduleTimeslots,
                }
            },

251
252
            repetitionOptions() {
                return [
253
254
255
                    {value: 1, text: this.$t("scheduleEditor.repetition.followingDay")},
                    {value: 2, text: this.$t("scheduleEditor.repetition.followingBusinessDay")},
                    {value: 3, text: this.$t("scheduleEditor.repetition.numberOfDaysLater")},
256
257
258
                ];
            },

Richard Blechinger's avatar
Richard Blechinger committed
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
            ...mapGetters({
                selectedShow: 'shows/selectedShow',
                schedule: 'shows/schedule',
                scheduleTimeslots: 'shows/scheduleTimeslots',
            })
        },

        methods: {
            trClass(item, type) {
                if (!item || type !== 'row') {
                    return '';
                }

                return item.id === this.timeslot.id
                    ? 'table-info'
                    : ''
            },

            createRepetitionSchedule() {
                const {onlyBusinessDays, addNoOfDays} = this.getRepetitionParameters();
                const {dstart, tstart, tend, rrule, until, fallback_id, automation_id, byweekday} = this.schedule;

                const newSchedule = {
                    schedule: {
                        dstart,
                        tstart,
                        tend,
                        rrule,
                        until,
                        fallback_id,
                        automation_id,
                        byweekday,
                        is_repetition: true,
                        add_business_days_only: onlyBusinessDays,
293
                        add_days_no: parseInt(addNoOfDays, 10),
Richard Blechinger's avatar
Richard Blechinger committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
                    }
                };

                this.$store.dispatch('shows/submitSchedule', {
                    showId: this.selectedShow.id,
                    schedule: newSchedule,
                    callback: (response) => {
                        console.log(this.$parent);
                        if (response.data.projected === undefined) {
                            this.$parent.renderView(null)
                        } else {
                            this.$log.debug('Timeslot conflict. Switching to resolve mode.');
                            this.$parent.resolve(response.data)
                        }
                        this.$refs.modalEmissionManagerEdit.hide()
                    },
                });
            },

            deleteFullSchedule(id) {
                this.$store.dispatch('shows/deleteSchedule', {
                    show: this.selectedShow.id,
                    schedule: id,
                    callback: () => {
                        this.$refs.modalEmissionManagerEdit.hide();
                        this.$parent.renderView(null)
                    }
                })
            },

            deleteSingleTimeslot(scheduleId, timeslotId) {
                this.$store.dispatch('shows/deleteTimeslot', {
                    show: this.selectedShow.id,
                    schedule: scheduleId,
                    timeslot: timeslotId,
                    callback: () => {
                        this.$refs.modalEmissionManagerEdit.hide()
                        this.$parent.renderView(null)
                    }
                })
            },

            deleteAllFutureTimeslots(scheduleId) {
                let startDate = new Date(this.timeslot.start)
                let toDelete = []
                for (let slot of this.scheduleTimeslots) {
                    if (new Date(slot.start) >= startDate) {
                        toDelete.push(slot.id)
                    }
                }

                if (toDelete.length === this.scheduleTimeslots.length) {
                    this.$log.debug('deleting full schedule')
                    this.deleteFullSchedule(scheduleId)
                } else {
                    this.deletion.amount = toDelete.length
                    this.deletion.count = 0
                    this.$refs.modalEmissionManagerDeleteTimeslots.show()

                    for (let i in toDelete) {
                        this.$log.debug('Deleting timeslot', toDelete[i])
                        this.$store.dispatch('shows/deleteTimeslot', {
                            show: this.selectedShow.id,
                            schedule: scheduleId,
                            timeslot: toDelete[i],
                            callback: () => {
                                this.deletion.count++
                                this.$log.debug('deleted ' + this.deletion.count + ' timeslots')
                                if (this.deletion.count === this.deletion.amount) {
                                    this.$parent.renderView(null)
                                    this.$refs.modalEmissionManagerDeleteTimeslots.hide()
                                    this.$refs.modalEmissionManagerEdit.hide()
                                }
                            }
                        })
                    }
                }
            },

            loadSchedule(scheduleId) {
                this.$store.dispatch('shows/fetchSchedule', {
                    show: this.selectedShow.id,
                    schedule: scheduleId,
                    callback: () => {
                        this.loadScheduleTimeslots(scheduleId)
                    }
                })
            },

            loadScheduleTimeslots(scheduleId) {
                this.$store.dispatch('shows/fetchTimeslots', {
                    id: this.selectedShow.id,
                    schedule: scheduleId,
                    start: this.schedule.dstart,
                    end: this.schedule.until,
                })
            },

            getRepetitionParameters() {
                if (this.repetitionRule == 1) {
                    return {
                        onlyBusinessDays: false,
                        addNoOfDays: 1,
                    }
                }

                if (this.repetitionRule == 2) {
                    return {
                        onlyBusinessDays: true,
                        addNoOfDays: 1,
                    }
                }

                return {
                    onlyBusinessDays: this.onlyBusinessDays,
                    addNoOfDays: this.addNoOfDays,
                }
            },

            // initialise a new schedule and open the modal
            open(timeslot) {
415
416
417
418
419
420
421
422
423
424
                const timeslotIndex = this.scheduleTimeslots.findIndex(item => item.id === timeslot.id);
                let timeslotPage = Math.ceil(timeslotIndex / this.perPage);
                const timeslotIsFirstEntryOnPage = timeslotIndex === ((timeslotPage) * this.perPage);

                // The math above is slightly off for every first entry on the page
                // So we add 1 to adjust the page to the correct one
                if (timeslotIsFirstEntryOnPage) {
                    timeslotPage += 1;
                }

Richard Blechinger's avatar
Richard Blechinger committed
425
                this.timeslot = timeslot
426
                this.currentPage = timeslotPage;
Richard Blechinger's avatar
Richard Blechinger committed
427
428
429
                this.$refs.modalEmissionManagerEdit.show()
                this.loadSchedule(timeslot.schedule)
            },
430
431

        }
Richard Blechinger's avatar
Richard Blechinger committed
432
    }
433
434
435
436
</script>

<style scoped>
</style>