EmissionManager.vue 15.4 KB
Newer Older
1
<template>
2
3
4
  <b-container>
    <b-row v-if="loaded.shows">
      <b-col>
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
        <b-alert
          :show="showChangedAlert.countdown"
          @dismiss-count-down="showChangedAlertCountdown"
        >
          Selected new show: <b>{{ shows[currentShow].name }}</b>
          <b-progress
            variant="info"
            animated
            :max="showChangedAlert.seconds"
            :value="showChangedAlert.countdown"
            height="3px"
          />
        </b-alert>
        <b-alert
          :show="!showChangedAlert.countdown"
          variant="light"
        >
          Currently selected show: <b>{{ shows[currentShow].name }}</b>
        </b-alert>
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
      </b-col>
      <b-col align="right">
        <b-dropdown
          id="ddshows"
          text="Sendereihe auswählen"
          variant="outline-info"
        >
          <b-dropdown-item
            v-for="(show, index) in shows"
            :key="show.id"
            @click="switchShow(index)"
          >
            {{ show.name }}
          </b-dropdown-item>
        </b-dropdown>
      </b-col>
    </b-row>
    <b-row v-else>
      <b-col cols="12">
        <div align="center">
          ... loading show data ...
        </div>
      </b-col>
    </b-row>
48

49
50
    <hr>

51
52
53
54
    <b-alert
      variant="danger"
      :show="conflictMode"
    >
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
      <div
        v-if="conflictMode"
        align="center"
      >
        <h4>Conflict Resolution</h4>
        <p>for new schedule</p>
        <p>
          from <b>{{ resolveData.schedule.dstart }}, {{ resolveData.schedule.tstart }}</b>
          to <b>{{ resolveData.schedule.tend }}</b>.
        </p>
        <p v-if="resolveData.schedule.rrule !== 1">
          This is a recurring event: <b>{{ rruleRender(resolveData.schedule.rrule) }}</b>,
          until: <b>{{ resolveData.schedule.until }}</b>.
        </p>
        <p v-if="conflictCount > 0">
          Conflicts to resolve: {{ conflictCount }}
        </p>
        <p v-else>
          <b-button
            variant="success"
            @click="resolveSubmit"
          >
            0 conflicts left! Submit this solution.
          </b-button>
        </p>
      </div>
81
82
    </b-alert>

83
84
85
86
87
88
    <full-calendar
      ref="calendar"
      default-view="agendaWeek"
      :events="calendarSlots"
      :config="calendarConfig"
      @view-render="renderView"
89
90
91
92
93
94
95
96
      @event-selected="eventSelected"
      @event-drop="eventDrop"
      @event-resize="eventResize"
      @event-created="eventCreated"
    />

    <app-modalEmissionManagerCreate
      ref="appModalEmissionManagerCreate"
97
    />
98
99
100
    <app-modalEmissionManagerResolve
      ref="appModalEmissionManagerResolve"
    />
101
102
103
    <app-modalEmissionManagerEdit
      ref="appModalEmissionManagerEdit"
    />
104
  </b-container>
105
106
107
108
</template>

<script>
import axios from 'axios'
109
import { FullCalendar } from 'vue-full-calendar'
110
import 'fullcalendar/dist/fullcalendar.css'
111
import modalEmissionManagerCreate from './EmissionManagerModalCreate.vue'
112
import modalEmissionManagerResolve from './EmissionManagerModalResolve.vue'
113
import modalEmissionManagerEdit from './EmissionManagerModalEdit.vue'
114
import rrules from '../mixins/rrules'
115
116
117
118

export default {
  components: {
    FullCalendar,
119
    'app-modalEmissionManagerCreate': modalEmissionManagerCreate,
120
    'app-modalEmissionManagerResolve': modalEmissionManagerResolve,
121
    'app-modalEmissionManagerEdit': modalEmissionManagerEdit,
122
  },
123

124
125
  mixins: [ rrules ],

126
127
  data () {
    return {
128
      currentShow: 0,
129
130
131
      shows: [],
      timeslots: [],
      calendarSlots: [],
132

133
134
135
136
137
      showChangedAlert: {
        countdown: 0,
        seconds: 3
      },

138
139
140
141
142
143
144
145
146
      // flags for loading data
      loaded: {
        shows: false,
        timeslots: false,
        calendarSlots: false,
      },

      // this flag signifies if we are in conflict resolution mode
      conflictMode: false,
147
148
149
150
151
      // when conflict mode is activated, this should hold the steering response
      // from schedule creation, with all the conflicts and solutions
      resolveData: null,
      conflictCount: 0,
      conflictSolutions: [],
152

153
      // this is the whole configuration for our schedule calendar, including
154
      // simple event handlers that do not need the whole components scope
155
156
157
158
159
160
161
162
163
164
165
166
167
168
      calendarConfig: {
        height: 600,
        firstDay: 1,
        header: {
          left: 'title',
          center: '',
          right: 'today prev,next'
        },
        views: {
          agendaWeek: {
            columnHeaderFormat: 'ddd D.M.',
            timeFormat: 'k:mm',
            slotLabelFormat: 'k:mm',
            allDaySlot: false,
169
            editable: false,
170
171
          },
        },
172
173
174
175
176
        // here we add a simple tooltip to every event, so that the full title
        // of a show can be viewed
        eventRender: function(event, element) {
          element.attr('title', event.title);
        },
177
178
179
180
      },
    }
  },

181
182
183
184
185
186
187
  created () {
    if (this.$route.query.show) {
      this.currentShow = this.$route.query.show
    } else {
      this.currentShow = 0
    }
    this.loadShows()
188
189
190
  },

  methods: {
191
192
193
    switchShow (index) {
      this.currentShow = index
      this.loadCalendarSlots()
194
195
196
197
198
      this.showChangedAlert.countdown = this.showChangedAlert.seconds
    },

    showChangedAlertCountdown(countdown) {
      this.showChangedAlert.countdown = countdown
199
200
201
202
203
204
205
206
207
208
209
    },

    getShowTitleById (id) {
      let i = this.shows.findIndex(show => show.id === id)
      if (i >= 0) {
        return this.shows[i].name
      } else {
        return 'Error: no show found for this timeslot'
      }
    },

210
211
212
213
214
215
216
217
218
219
    getShowIndexById (id) {
      let i = this.shows.findIndex(show => show.id === id)
      if (i >= 0) {
        return i
      } else {
        this.$log.error('No show found for id ' + id)
        return 0
      }
    },

220
221
    // this handler will be called whenever the user clicks on one of the
    // displayed timeslots
222
    eventSelected (event) {
223
224
225
      // in conflict resolution mode only the newly generated events are
      // clickable. if there is no conflict for an event, only a modal
      // with a short notice should be opend, otherwise the resolution modal
226
227
228
229
230
231
232
233
234
      if (this.conflictMode) {
        if (event.hash === undefined) {
          return
        } else if (this.conflictSolutions[event.hash] === undefined) {
          this.$refs.appModalEmissionManagerResolve.openNotNeeded()
        } else {
          this.$refs.appModalEmissionManagerResolve.open(event)
        }
      }
235
236
237
238
      // standard mode only those events are clickable that belong to the
      // currently selected show.
      else {
        let timeslot = this.timeslots.find(slot => slot.id === event.id)
239
240
241
242
243
        if (timeslot.show !== this.shows[this.currentShow].id) {
          this.switchShow(this.getShowIndexById(timeslot.show))
        } else {
          this.$refs.appModalEmissionManagerEdit.open(timeslot, this.shows[this.currentShow])
        }
244
      }
245
246
    },

247
248
249
    // currently this will not be called, as our events are not editable
    // if editable is set to true in the calendar config, this handler will
    // be called if a timeslot was dragged somewhere else
250
251
    eventDrop (event) {
      this.$log.debug('eventDrop', event)
252
      this.notYetImplemented()
253
254
    },

255
256
257
    // currently this will not be called, as our events are not editable
    // if editable is set to true in the calendar config, this handler will
    // be called if a timeslot was resized
258
259
    eventResize (event) {
      this.$log.debug('eventResize', event)
260
      this.notYetImplemented()
261
262
    },

263
    // this handler is called when the user creates a new timeslot
264
265
266
267
    eventCreated (event) {
      this.$refs.appModalEmissionManagerCreate.open(event.start, event.end)
    },

268
269
270
271
    // this is called when the user changes the calendar view, so we just
    // refetch the timeslots with the updated visible date range
    renderView (view) {
      if (this.loaded.shows) {
272
273
274
275
276
277
278
279
280
281
282
        let start = null
        let end = null
        // in case it gets called from a modal, we use the current view
        // otherwise we use the new dates from the view received by the renderView event
        if (view === null) {
          start = this.$refs.calendar.fireMethod('getView').start.format()
          end = this.$refs.calendar.fireMethod('getView').end.format()
        } else {
          start = view.start.format()
          end = view.end.format()
        }
283
284
        // we only load new timeslots, if we are not in conflict mode
        if (!this.conflictMode) {
285
286
          this.loadTimeslots(start, end)
        }
287
288
289
      }
    },

290
291
    resolve (data) {
      this.$log.debug('resolve', data)
292
      this.resolveData = data
293
      this.conflictMode = true
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
      this.conflictCount = 0
      this.conflictSolutions = data.solutions
      this.calendarSlots = []
      try {
        for (let i in data.projected) {
          let newSlot = {
            // we need a numeric ID for the event for later selection by the user.
            // with converting the hash to a number (in this case a float), we
            // do not risk using a number that is already used by a timeslot id
            // of a conflicting timeslot
            id: Number(data.projected[i].hash),
            // the hash is needed to compare against solutions and conflicts
            hash: data.projected[i].hash,
            start: data.projected[i].start,
            end: data.projected[i].end,
            title: 'new',
            collisions: [],
            solutionChoices: [],
            className: 'noconflict',
            editable: false,
          }
          if (data.projected[i].collisions.length > 0) {
            newSlot.className = 'conflict'
            newSlot.solutionChoices = data.projected[i].solution_choices
            for (let col of data.projected[i].collisions) {
              let conflictingSlot = {
                id: col.id,
                start: col.start,
                end: col.end,
                title: col.show_name,
                className: 'otherShow',
                editable: false,
              }
              this.calendarSlots.push(conflictingSlot)
              this.conflictCount++
              newSlot.collisions.push(col)
            }
          }
          this.calendarSlots.push(newSlot)
        }
      } catch (err) {
        this.$log.error(err)
      }
337
338
    },

339
340
    resolveEvent (toResolve, mode) {
      this.conflictCount -= toResolve.collisions.length
341
      let slotIndex = this.calendarSlots.findIndex(s => s.id === toResolve.id)
342
343
344
      switch (mode) {
        case 'theirs':
          this.conflictSolutions[toResolve.hash] = 'theirs'
345
346
          this.calendarSlots[slotIndex].className = 'ours-discarded'
          this.renderView(null)
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
          break

        default:
          this.$log.error('EmissionManager.resolveEvent')
          this.$log.error('toResolve:', toResolve)
          this.$log.error('mode:', mode)
          alert('Error: an undefined conflict resolution mode was chosen. See console for details')
          break
      }
    },

    // submit a conflict-resolved schedule to steering
    resolveSubmit () {
      // TODO: check why steering retourns undefined and null values here
      if (this.resolveData.schedule.add_business_days_only === undefined) { this.resolveData.schedule.add_business_days_only = false }
      if (this.resolveData.schedule.add_days_no === null) { this.resolveData.schedule.add_days_no = 0 }
      if (this.resolveData.schedule.is_repetition === undefined) { this.resolveData.schedule.is_repetition = false }
      if (this.resolveData.schedule.fallback_id === null) { this.resolveData.schedule.fallback_id = 0 }
      if (this.resolveData.schedule.automation_id === null) { this.resolveData.schedule.automation_id = 0 }
      if (this.resolveData.schedule.byweekday === undefined) { this.resolveData.schedule.byweekday = 0 }
      // create the resolved schedule object including solutions
      let resolvedSchedule = {
        schedule: this.resolveData.schedule,
        solutions: this.resolveData.solutions,
      }
      this.$log.debug('resolveSubmit: schedule:', resolvedSchedule)
      // now generate the URL and POST it to steering
      let uri = process.env.VUE_APP_API_STEERING_SHOWS + this.shows[this.currentShow].id + '/schedules/'
      axios.post(uri, resolvedSchedule, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
      }).then(response => {
        this.$log.debug('resolveSubmit: response:', response)
        // if for some reason a new conflict arose, e.g. because in the meantime
        // someone else inserted a conflicting schedule, we have to resolve.
        if (response.data.projected === undefined) {
          this.conflictMode = false
          this.renderView(null)
        } else {
          this.resolve(response.data)
        }
      }).catch(error => {
        this.$log.error(error.response.status + ' ' + error.response.statusText)
        this.$log.error(error.response)
        alert('Error: could not submit final schedule. See console for details.')
        // and we leave the modal open, so no call to its .hide function here
      })
    },

396
397
398
399
400
401
402
403
404
    loadCalendarSlots () {
      this.loaded.calendarSlots = false
      this.calendarSlots = []
      for (let i in this.timeslots) {
        let highlighting = 'otherShow'
        if (this.timeslots[i].show === this.shows[this.currentShow].id) {
          highlighting = 'currentShow'
        }
        this.calendarSlots.push({
405
          id: this.timeslots[i].id,
406
407
408
409
410
411
412
413
414
          start: this.timeslots[i].start,
          end: this.timeslots[i].end,
          title: this.getShowTitleById(this.timeslots[i].show),
          className: highlighting
        })
      }
      this.loaded.calendarSlots = true
    },

415
    loadTimeslots (start, end) {
416
      this.$log.debug('loadTimeslots: currentShow = '+this.currentShow)
417
418
419
420
      this.loaded.timeslots = false
      let uri = process.env.VUE_APP_API_STEERING + 'timeslots?start=' + start + '&end=' + end
      axios.get(uri, {
        withCredentials: true,
421
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
422
      }).then(response => {
423
        this.timeslots = response.data
424
        this.loaded.timeslots = true
425
        this.loadCalendarSlots()
426
427
428
429
430
431
432
433
434
435
436
437
      }).catch(error => {
        this.$log.error(error.response.status + ' ' + error.response.statusText)
        this.$log.error(error.response)
        alert('Error: could not load timeslots. See console for details.')
      })
    },

    loadShows () {
      this.loaded.shows = false
      let uri = process.env.VUE_APP_API_STEERING + 'shows'
      axios.get(uri, {
        withCredentials: true,
438
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
439
440
441
442
443
444
445
446
447
448
449
450
      }).then(response => {
        this.shows = response.data
        this.loaded.shows = true
        let start = this.$refs.calendar.fireMethod('getView').start.format()
        let end = this.$refs.calendar.fireMethod('getView').end.format()
        this.loadTimeslots(start, end)
      }).catch(error => {
        this.$log.error(error.response.status + ' ' + error.response.statusText)
        this.$log.error(error.response)
        alert('Error: could not load shows. See console for details.')
      })
    },
451
452
453
454

    notYetImplemented: function () {
      alert('By the mighty witchcraftry of the mother of time!\n\nThis feature is not implemented yet.')
    },
455
456
457
458
459
460
461
462
463
464
465
  },
}
</script>

<style>
.otherShow {
  background-color: #eee;
}
a.currentShow {
  background-color: #17a2b8;
}
466
467
468
469
470
471
.conflict {
  background-color: #b00;
}
.noconflict {
  background-color: #17a2b8;
}
472
473
474
475
.ours-discarded {
  background-color: #b00;
  opacity: 0.5;
  text-decoration: line-through !important;
476
}
477
</style>