EmissionManager.vue 18.3 KB
Newer Older
1
<template>
2
  <b-container>
3
    <show-selector
4
      ref="showSelector"
5
6
7
      title="Emissions"
      :callback="showHasSwitched"
    />
8
9
    <hr>

10
11
12
13
    <b-alert
      variant="danger"
      :show="conflictMode"
    >
14
15
16
17
18
19
      <div
        v-if="conflictMode"
        align="center"
      >
        <h4>Conflict Resolution</h4>
        <p>
20
          for new schedule
21
22
23
24
25
26
27
          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>
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

        <div v-if="submitting">
          <b-row>
            <b-col align="center">
              <img
                src="../assets/radio.gif"
                alt="submitting resolve data"
              >
            </b-col>
          </b-row>
        </div>
        <div v-else>
          <p v-if="conflictCount > 0">
            Conflicts left to resolve:
            <b-badge variant="danger">
              {{ conflictCount }}
            </b-badge>
            &nbsp;
            <b-button
              variant="danger"
              size="sm"
              @click="resolveCancel"
            >
              Cancel
            </b-button>
          </p>
          <p v-else>
            <b-button
              variant="success"
              @click="resolveSubmit"
            >
              0 conflicts left! Submit this solution.
            </b-button>
            &nbsp;
            <b-button
              variant="danger"
              @click="resolveCancel"
            >
              Cancel
            </b-button>
          </p>
        </div>
70
      </div>
71
72
    </b-alert>

73
74
75
76
77
78
    <full-calendar
      ref="calendar"
      default-view="agendaWeek"
      :events="calendarSlots"
      :config="calendarConfig"
      @view-render="renderView"
79
80
81
82
83
84
85
86
      @event-selected="eventSelected"
      @event-drop="eventDrop"
      @event-resize="eventResize"
      @event-created="eventCreated"
    />

    <app-modalEmissionManagerCreate
      ref="appModalEmissionManagerCreate"
87
    />
88
89
90
    <app-modalEmissionManagerResolve
      ref="appModalEmissionManagerResolve"
    />
91
92
93
    <app-modalEmissionManagerEdit
      ref="appModalEmissionManagerEdit"
    />
94
  </b-container>
95
96
97
</template>

<script>
98
import { mapGetters } from 'vuex'
99
import { FullCalendar } from 'vue-full-calendar'
100
import 'fullcalendar/dist/fullcalendar.css'
101
import showSelector from './ShowSelector.vue'
102
103
104
import modalEmissionManagerCreate from './emissions/ModalCreate.vue'
import modalEmissionManagerResolve from './emissions/ModalResolve.vue'
import modalEmissionManagerEdit from './emissions/ModalEdit.vue'
105
import rrules from '../mixins/rrules'
106
107
108
109

export default {
  components: {
    FullCalendar,
110
    'show-selector': showSelector,
111
    'app-modalEmissionManagerCreate': modalEmissionManagerCreate,
112
    'app-modalEmissionManagerResolve': modalEmissionManagerResolve,
113
    'app-modalEmissionManagerEdit': modalEmissionManagerEdit,
114
  },
115

116
117
  mixins: [ rrules ],

118
119
120
  data () {
    return {
      calendarSlots: [],
121

122
      // flag for when submitting resolve data
123
      submitting: false,
124
125
126

      // this flag signifies if we are in conflict resolution mode
      conflictMode: false,
127
128
129
130
131
      // 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: [],
132

133
      // this is the whole configuration for our schedule calendar, including
134
      // simple event handlers that do not need the whole components scope
135
136
137
138
139
140
141
142
143
144
145
146
147
148
      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,
149
            editable: false,
150
151
          },
        },
152
153
154
155
156
        // 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);
        },
157
158
159
160
      },
    }
  },

161
  computed: {
162
163
164
165
166
167
    loaded () {
      return {
        shows: this.$store.state.shows.loaded['shows'],
        timeslots: this.$store.state.shows.loaded['timeslots'],
      }
    },
168
169
170
171
172
173
174
175
176

    ...mapGetters({
      shows: 'shows/shows',
      selectedShow: 'shows/selectedShow',
      timeslots: 'shows/timeslots',
      getPlaylistById: 'playlists/getPlaylistById',
      files: 'files/files',
      getFileById: 'files/getFileById',
    })
177
178
  },

179
  created () {
180
181
182
    this.$store.dispatch('shows/fetchShows', {
      callback: () => {
        this.showHasSwitched()
183
        this.$refs.showSelector.updateInputSelector()
184
185
      }
    })
186
187
188
  },

  methods: {
189
    switchShow () {
190
      this.loadCalendarSlots()
191
192
    },

193
194
195
196
197
198
199
    // This is the callback function that is activated by the show-selector
    // component, whenever the user switches to a different show
    showHasSwitched () {
      this.$log.debug('show has switched to', this.selectedShow.name)
      let start = this.$refs.calendar.fireMethod('getView').start.format()
      let end = this.$refs.calendar.fireMethod('getView').end.format()
      this.loadTimeslots(start, end)
200
201
202
203
204
205
206
207
208
209
210
    },

    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'
      }
    },

211
212
213
214
215
216
217
218
219
220
    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
      }
    },

221
222
    // this handler will be called whenever the user clicks on one of the
    // displayed timeslots
223
    eventSelected (event) {
224
225
226
      // 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
227
228
229
230
231
232
233
234
235
      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)
        }
      }
236
237
238
239
      // 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)
240
        if (timeslot.show !== this.selectedShow.id) {
241
242
          this.switchShow(this.getShowIndexById(timeslot.show))
        } else {
243
          this.$refs.appModalEmissionManagerEdit.open(timeslot)
244
        }
245
      }
246
247
    },

248
249
250
    // 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
251
252
    eventDrop (event) {
      this.$log.debug('eventDrop', event)
253
      this.notYetImplemented()
254
255
    },

256
257
258
    // 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
259
260
    eventResize (event) {
      this.$log.debug('eventResize', event)
261
      this.notYetImplemented()
262
263
    },

264
    // this handler is called when the user creates a new timeslot
265
    eventCreated (event) {
jackie / Andrea Ida Malkah Klaura's avatar
FIX #31    
jackie / Andrea Ida Malkah Klaura committed
266
267
268
      if (!this.conflictMode) {
        this.$refs.appModalEmissionManagerCreate.open(event.start, event.end)
      }
269
270
    },

271
272
273
274
    // 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) {
275
276
277
278
279
280
281
282
283
284
285
        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()
        }
286
287
        // we only load new timeslots, if we are not in conflict mode
        if (!this.conflictMode) {
288
289
          this.loadTimeslots(start, end)
        }
290
291
292
      }
    },

293
    resolve (data) {
294
      this.resolveData = data
295
      this.conflictMode = true
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
      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)
      }
339
340
    },

341
    resolveEvent (toResolve, mode) {
342
343
      let calendarSlot = this.calendarSlots.find(s => s.id === toResolve.id)
      let originalSlot = this.resolveData.projected.find(s => s.hash === toResolve.hash)
344
345
      // we only need the conflicting slot specifically for theirs-both mode, where there should be only one collision
      let conflictingSlot = this.calendarSlots.find(s => s.id === toResolve.collisions[0].id)
346
347
348
349
350
351
352
353
354
355
356
357
358
359

      // we only reduce the conflict count, if there was no other solution set already
      if (this.conflictSolutions[toResolve.hash] === '') {
        this.conflictCount -= toResolve.collisions.length
      }

      // if there already was a resolution chosen before, that added a second timeslot
      // because either the "ours" or "theirs" was split, we have to clean it up before
      // in the calendar before setting a new resolution
      let oldResolutionSlot = this.calendarSlots.findIndex(s => s.id === calendarSlot.id * 10)
      if (oldResolutionSlot > -1) {
        this.calendarSlots.splice(oldResolutionSlot, 1)
      }

360
361
362
363
364
      // and in case their was a ours-* choice before, we have to reset the (one)
      // conflicting slot to its original start and end time
      conflictingSlot.start = toResolve.collisions[0].start
      conflictingSlot.end = toResolve.collisions[0].end

365
366
      // for a detailed description of the resolution modes, see conflict-resolution.md
      // and conflict-resolution.pdf at https://gitlab.servus.at/autoradio/meta
367
368
      switch (mode) {
        case 'theirs':
369
          this.conflictSolutions[toResolve.hash] = mode
370
371
372
373
          calendarSlot.className = 'timeslot-discarded'
          calendarSlot.title = 'new'
          calendarSlot.start = originalSlot.start
          calendarSlot.end = originalSlot.end
374
          for (let theirs of toResolve.collisions) {
375
376
377
378
379
380
381
            this.calendarSlots.find(s => s.id === theirs.id).className = 'timeslot-accepted'
          }
          this.renderView(null)
          break

        case 'ours':
          this.conflictSolutions[toResolve.hash] = mode
382
383
384
385
          calendarSlot.className = 'timeslot-accepted'
          calendarSlot.title = 'new'
          calendarSlot.start = originalSlot.start
          calendarSlot.end = originalSlot.end
386
387
388
389
390
391
392
393
394
          for (let theirs of toResolve.collisions) {
            this.calendarSlots.find(s => s.id === theirs.id).className = 'timeslot-discarded'
          }
          this.renderView(null)
          break

        case 'theirs-start':
        case 'theirs-end':
        case 'theirs-both':
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
          this.conflictSolutions[toResolve.hash] = mode
          calendarSlot.className = 'timeslot-partly'
          calendarSlot.title = 'new [' + mode + ']'
          if (mode === 'theirs-start') {
            calendarSlot.start = toResolve.collisions[0].end
            calendarSlot.end = originalSlot.end
          } else if (mode === 'theirs-end') {
            calendarSlot.start = originalSlot.start
            calendarSlot.end = toResolve.collisions[0].start
          } else {
            calendarSlot.start = originalSlot.start
            calendarSlot.end = toResolve.collisions[0].start
            this.calendarSlots.push({
              id: calendarSlot.id * 10,
              start: toResolve.collisions[0].end,
              end: originalSlot.end,
              title: 'new [theirs-both]',
              className: 'timeslot-partly',
              editable: false,
            })
          }
          for (let theirs of toResolve.collisions) {
            this.calendarSlots.find(s => s.id === theirs.id).className = 'timeslot-accepted'
          }
          this.renderView(null)
          break

422
423
424
425
        case 'ours-start':
        case 'ours-end':
        case 'ours-both':
          this.conflictSolutions[toResolve.hash] = mode
426
          calendarSlot.className = 'timeslot-accepted'
427
          calendarSlot.title = 'new [' + mode + ']'
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
          if (mode === 'ours-start') {
            conflictingSlot.start = toResolve.collisions[0].start
            conflictingSlot.end = originalSlot.start
          } else if (mode === 'ours-end') {
            conflictingSlot.start = originalSlot.end
            conflictingSlot.end = toResolve.collisions[0].end
          } else {
            conflictingSlot.start = toResolve.collisions[0].start
            conflictingSlot.end = originalSlot.start
            this.calendarSlots.push({
              id: calendarSlot.id * 10,
              start: originalSlot.end,
              end: toResolve.collisions[0].end,
              title: conflictingSlot.title,
              className: 'timeslot-partly',
              editable: false,
            })
          }
446
          for (let theirs of toResolve.collisions) {
447
            this.calendarSlots.find(s => s.id === theirs.id).className = 'timeslot-partly'
448
          }
449
          this.renderView(null)
450
451
452
453
454
455
456
457
458
459
460
          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
      }
    },

461
462
463
464
465
    resolveCancel () {
      this.conflictMode = false
      this.renderView(null)
    },

466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
    // 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)
481
      this.submitting = true
482
      this.$store.dispatch('shows/submitSchedule', {
483
        showId: this.selectedShow.id,
484
485
486
487
488
489
490
491
492
493
494
495
        schedule: resolvedSchedule,
        callback: (response) => {
          this.submitting = false
          if (response.data.projected === undefined) {
            this.conflictMode = false
            this.renderView(null)
          } else {
            this.resolve(response.data)
          }
        },
        callbackCancel: () => {
          this.submitting = false
496
497
498
499
        }
      })
    },

500
501
502
503
    loadCalendarSlots () {
      this.calendarSlots = []
      for (let i in this.timeslots) {
        let highlighting = 'otherShow'
504
        if (this.timeslots[i].show === this.selectedShow.id) {
505
506
507
          highlighting = 'currentShow'
        }
        this.calendarSlots.push({
508
          id: this.timeslots[i].id,
509
510
511
512
513
514
515
516
          start: this.timeslots[i].start,
          end: this.timeslots[i].end,
          title: this.getShowTitleById(this.timeslots[i].show),
          className: highlighting
        })
      }
    },

517
    loadTimeslots (start, end) {
518
519
520
521
522
523
      this.$store.dispatch('shows/fetchTimeslots', {
        start: start,
        end: end,
        callback: () => {
          this.loadCalendarSlots()
        }
524
525
526
      })
    },

527
528
529
    notYetImplemented: function () {
      alert('By the mighty witchcraftry of the mother of time!\n\nThis feature is not implemented yet.')
    },
530
531
532
533
534
535
536
537
538
539
540
  },
}
</script>

<style>
.otherShow {
  background-color: #eee;
}
a.currentShow {
  background-color: #17a2b8;
}
541
542
543
544
545
546
.conflict {
  background-color: #b00;
}
.noconflict {
  background-color: #17a2b8;
}
547
.timeslot-discarded {
548
549
550
  background-color: #b00;
  opacity: 0.5;
  text-decoration: line-through !important;
551
}
552
553
554
555
556
557
558
559
.timeslot-accepted {
  background-color: #17a2b8;
}
.timeslot-partly {
  background-color: #17a2b8;
  opacity: 0.5;
  font-weight: bold;
}
560
</style>