FileManager.vue 47.3 KB
Newer Older
1
<template>
2
  <b-container>
3
4
    <!-- This first row is so far only used to provide a dropdown for
    choosing one of the loaded shows (which the user has access to) -->
5
6
7
8
9
    <b-row>
      <b-col>
        <h3>Dateien und Playlists</h3>
      </b-col>
      <b-col align="right">
10
11
12
13
14
15
        <b-dropdown
          id="ddshows"
          text="Sendereihe auswählen"
          variant="info"
        >
          <b-dropdown-item
16
            v-for="(show, index) in shows"
17
18
19
20
21
            :key="show.id"
            @click="switchShow(index)"
          >
            {{ show.name }}
          </b-dropdown-item>
22
23
24
        </b-dropdown>
      </b-col>
    </b-row>
25
    <hr>
26
27
28
29

    <!-- The jumbotron is used to display the name and short description of the
    currently selected show and let the user choose between editing files and
    playlists -->
30
31
32
33
34
35
36
37
38
39
    <b-jumbotron>
      <template slot="header">
        <span v-if="loaded.shows">
          {{ shows[currentShow].name }}
        </span>
        <span v-else>Shows are being loaded</span>
      </template>
      <template slot="lead">
        <span v-if="loaded.shows">{{ shows[currentShow].short_description }}</span>
      </template>
40
      <hr>
41
42
      <div align="center">
        <b-button-group>
43
44
45
46
47
48
49
50
51
52
53
54
55
56
          <b-button
            size="lg"
            :variant="button.files"
            @click="switchMode('files')"
          >
            Files
          </b-button>
          <b-button
            size="lg"
            :variant="button.playlists"
            @click="switchMode('playlists')"
          >
            Playlists
          </b-button>
57
58
59
60
        </b-button-group>
      </div>
    </b-jumbotron>

61
62
    <!-- All the UI for uploading and editing files is only shown if the user
    choose to edit files in the jumbotron above -->
63
    <div v-if="mode === 'files'">
64
      <!-- Only display a spinner if the files are not loaded yet -->
65
66
67
      <div v-if="!loaded.files">
        <b-row>
          <b-col align="center">
68
69
70
71
            <img
              src="../assets/radio.gif"
              alt="loading data"
            >
72
73
74
          </b-col>
        </b-row>
      </div>
75
76
77
78

      <!-- If all file data is loaded we can present an upload button and
      a table of files (if there are already any or if there are any already
      being uploaded/imported )-->
79
      <div v-else>
80
81
82
83
        <!-- Modal: Add new file
        This is the modal that is used when the user clicks on the
        "Upload or add a file" button. When the user hits the OK button,
        the addFile method will be called. -->
84
85
86
87
88
        <b-modal
          id="modal-add-file"
          title="Add new file"
          @ok="addFile"
        >
89
90
          <!-- If the checkbox below is checked we only need a simple input
          for entering an URI, instead of a complete file upload dialogue -->
91
92
93
94
95
96
          <div v-if="addNewFileURI">
            <b-row>
              <b-col md="2">
                <b>Link:</b>
              </b-col>
              <b-col>
97
98
99
100
101
                <b-form-input
                  v-model="uploadSourceURI"
                  type="url"
                  placeholder="Insert a HTTP(S) link here"
                />
102
103
104
              </b-col>
            </b-row>
          </div>
105
106
          <!-- If the checkbox is not checked, we provide a file selection
          dialogue instead of a simple input for an URI -->
107
          <div v-else>
108
109
110
111
112
113
            <b-form-file
              v-model="uploadSourceFile"
              accept="audio/*"
              placeholder="Choose a file..."
              drop-placeholder="Drop file here..."
            />
114
          </div>
115

116
          <hr>
117
118
119

          <!-- This checkbox determines whether there is just a simple input
          or a file selection dialogue (see above) -->
120
          <div align="center">
121
122
123
124
125
            <b-form-checkbox
              v-model="addNewFileURI"
              value="true"
              unchecked_value="false"
            >
126
127
128
129
              Download from remote source instead of uploading a file
            </b-form-checkbox>
          </div>
        </b-modal>
130
131
132
133
134

        <!-- Modal: Edit meta information
        This modal lets the user change a file's meta information (artist,
        album, title). When the user hits OK, the saveFile method is called.
        -->
135
136
137
138
139
140
        <b-modal
          id="modal-edit-file"
          title="Edit meta information"
          size="lg"
          @ok="saveFile"
        >
141
          <!-- Input element: Artist -->
142
          <b-row>
143
144
145
146
147
148
149
150
151
            <b-col md="2">
              Artist:
            </b-col>
            <b-col>
              <b-form-input
                v-model="temp.artist"
                type="text"
              />
            </b-col>
152
          </b-row>
153
154

          <!-- Input element: Album -->
155
          <b-row>
156
157
158
159
160
161
162
163
164
            <b-col md="2">
              Album:
            </b-col>
            <b-col>
              <b-form-input
                v-model="temp.album"
                type="text"
              />
            </b-col>
165
          </b-row>
166
167

          <!-- Input element: Title -->
168
          <b-row>
169
170
171
172
173
174
175
176
177
            <b-col md="2">
              Title:
            </b-col>
            <b-col>
              <b-form-input
                v-model="temp.title"
                type="text"
              />
            </b-col>
178
          </b-row>
179

180
          <hr>
181
182
183
184
185

          <!-- As additional orientation we provide the source this file was
          uploaded/imported from. This might help the user to manage all files
          and set the meta information accordingly.
          -->
186
          <b-row>
187
188
189
            <b-col md="2">
              Sourced from:
            </b-col>
190
191
192
            <b-col>{{ temp.uri }}</b-col>
          </b-row>
        </b-modal>
193
194
195

        <!-- Now for the files. If there are none yet, just show an alert and
        a button for uploading/importing a new file -->
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
        <div
          v-if="files.length === 0"
          align="center"
        >
          <b-alert
            show
            variant="warning"
          >
            There are no files for this show yet.
          </b-alert>
          <b-button
            v-b-modal.modal-add-file
            variant="success"
          >
            Upload or add a file
          </b-button>
212
        </div>
213
214
215

        <!-- If there already are files for this show we'll show the button
        for uploading/importing a new file and then a table with all files -->
216
        <div v-else>
217
          <!-- This is the button -->
218
219
220
221
222
223
224
225
226
227
          <div
            align="center"
            style="padding-bottom: 1.5em;"
          >
            <b-button
              v-b-modal.modal-add-file
              variant="success"
            >
              Upload or add a file
            </b-button>
228
          </div>
229

230
231
232
          <!-- We also import the modal for showing file import logs here -->
          <app-modalFileManagerLog ref="appModalFileManagerLog" />

233
          <!-- And here comes the table -->
234
235
236
237
238
239
          <b-table
            ref="filesTable"
            striped
            :fields="filesTableFields"
            :items="files"
          >
240
241
242
243
244
245
246
247
248
249
            <!-- The first two columns in the table (Index & Artist) are filled
            in automatically, because we do not use these fields for displaying
            upload/import progress information.
            -->

            <!-- Column: Album
            This column displays either the album meta information or, in case
            the file is just being uploaded/imported, a spinner visualising an
            ongoing import.
            -->
250
            <template v-slot:cell(metadata.album)="data">
251
252
              <span v-if="data.item.source.import.state === 'aborted'"><b-badge variant="danger">Error:</b-badge></span>
              <span v-else-if="data.item.source.import.state !== 'done'"><img
253
254
255
256
                src="../assets/radio.gif"
                width="24"
                alt="loading data"
              ></span>
257
258
              <span v-else>{{ data.value }}</span>
            </template>
259
260
261
262
263
264

            <!-- Column: Title
            This column displays either the title meta information or, in case
            the file is just being uploaded/imported, the current phase
            of the ongoing import (fetching or normalizing).
            -->
265
            <template v-slot:cell(metadata.title)="data">
266
              <span v-if="data.item.source.import.state === 'done'">{{ data.value }}</span>
267
              <span v-else-if="data.item.source.import.state === 'aborted'">import was aborted</span>
268
              <span v-else-if="data.item.source.import.progress !== undefined">{{ data.item.source.import.progress.step }} :</span>
269
            </template>
270
271
272
273
274

            <!-- Column: Duration
            This column displays either the duration of the audio file, or, if
            the file is just being uploaded/imported, the current progress.
            -->
275
            <template v-slot:cell(duration)="data">
276
              <!-- In case the import is already done just print a pretty duration -->
277
278
279
              <div v-if="data.item.source.import.state === 'done'">
                {{ prettyNanoseconds(data.value) }}
              </div>
280
281
282
283
              <!-- If the import was aborted show some error info -->
              <div v-else-if="data.item.source.import.state === 'aborted'">
                {{ data.item.source.import.error }}
              </div>
284
285
286
287
              <!-- Or print the progress for ongoing imports. We use the variant
              prop of the b-progress to display the bar in different colours -
              either the info variant for the fetching phase or the success
              variant for the normalizing phase -->
288
              <div v-else-if="data.item.source.import.progress !== undefined">
289
290
291
292
293
294
295
                <b-progress
                  :value="data.item.source.import.progress.progress"
                  :max="1"
                  show-progress
                  :variant="data.item.source.import.progress.step === 'fetching' ? 'info' : 'success'"
                  animated
                />
296
              </div>
297
            </template>
298
299
300
301
302

            <!-- Column: Size
            This column displays the size of the audio file, if the file is
            already fully imported. Otherwise we'll just leave it empty.
            -->
303
            <template v-slot:cell(size)="data">
304
              <span v-if="data.item.source.import.state === 'done'">{{ prettyFileSize(data.value) }}</span>
305
            </template>
306
307
308
309
310

            <!-- Column: Actions
            This column displays the available button for actions  the user can
            take on this file (e.g. editing and deleting).
            -->
311
            <template v-slot:cell(actions)="data">
312
              <b-button-group size="sm">
313
314
315
                <b-button @click="editFile(data.item.id)">
                  Edit
                </b-button>
316
317
318
319
320
321
322
                <b-button
                  v-b-tooltip.hover
                  title="Show import log"
                  @click="$refs.appModalFileManagerLog.show(data.item.show, data.item.id)"
                >
                  &#128220;
                </b-button>
323
324
325
326
327
328
                <b-button
                  variant="danger"
                  @click="deleteFile(data.item.id)"
                >
                  Delete
                </b-button>
329
330
331
              </b-button-group>
            </template>
          </b-table>
332
        </div>
333
        <!-- End of files table -->
334
335
336
      </div>
    </div>

337
338
    <!-- All the UI for creating and editing playlists is only shown if the user
    choose to edit playlists in the jumbotron above -->
339
    <div v-if="mode === 'playlists'">
340
341
342
343
344
345
346
347
348
349
      <!-- Modal: Add new file
      This is the modal that is used when the user clicks on the
      "Upload or add a file" button. When the user hits the OK button,
      the addFile method will be called. -->
      <b-modal
        id="modal-edit-playlist"
        size="lg"
        :title="playlistEditor.mode === 'edit' ? 'Edit playlist' : 'Add new playlist'"
        @ok="storePlaylist"
      >
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
        <b-row
          v-if="playlistEditor.mode === 'edit'"
          style="padding-bottom: 1em;"
        >
          <b-col
            cols="3"
          >
            Playlist index:
          </b-col>
          <b-col>{{ playlistEditor.id }}</b-col>
        </b-row>

        <b-row>
          <b-col cols="3">
            Description:
          </b-col>
          <b-col>
            <b-form-input
              v-model="playlistEditor.description"
              type="text"
            />
          </b-col>
        </b-row>
373

374
        <hr>
375

376
377
        <p>Playlist entries:</p>

378
379
        <!-- If no entries are here (i.e. we add a new playlist), only show
        a hint that there's nothing here yet. -->
380
381
382
383
384
385
        <div
          v-if="playlistEditor.entries.length === 0"
          align="center"
        >
          No entries yet. Add some.
        </div>
386
387
        <!-- As soon as we have at least one entry in our temporary playlist
        we can display a table with all the info and action buttons -->
388
        <div v-else>
389
390
391
392
393
          <b-table
            ref="playlistEditTable"
            striped
            :items="playlistEditor.entries"
            :fields="playlistEditTableFields"
394
          >
395
396
397
398
            <!-- Column: Index
                 Here we just use the array index, because the playlist entries
                 are ordered as an array, without the need for an extra id field
            -->
399
            <template v-slot:cell(id)="data">
400
401
402
403
404
405
406
              {{ data.index + 1 }}.
            </template>

            <!-- Column: Type
                 Based on the entry content (either file or uri), we display
                 a small badge indicating which type of source this is
            -->
407
            <template v-slot:cell(type)="data">
408
              <b-badge
409
                v-if="data.item.file"
410
411
412
413
414
                variant="success"
              >
                File
              </b-badge>
              <b-badge
415
                v-else-if="data.item.uri.startsWith('line://')"
416
417
418
419
420
                variant="info"
              >
                Line-in
              </b-badge>
              <b-badge
421
                v-else-if="data.item.uri.startsWith('http://') || data.item.uri.startsWith('https://')"
422
423
424
425
426
427
428
429
430
431
                variant="light"
              >
                Stream
              </b-badge>
              <b-badge
                v-else
                variant="dark"
              >
                Other
              </b-badge>
432
433
434
435
436
            </template>

            <!-- Column: Source
                 Here we display where this playlist entry is coming from
            -->
437
            <template v-slot:cell(source)="data">
438
              <span v-if="data.item.file">
439
440
                {{ getFileTitleForPlaylist(data.item.file.show, data.item.file.id) }}
                <small><i>( file://{{ data.item.file.show }}/{{ data.item.file.id }} )</i></small>
441
442
              </span>
              <span v-else>
443
                {{ data.item.uri }}
444
              </span>
445
446
447
448
449
            </template>

            <!-- Column: Actions
                 Finally some buttons to reorder or delete playlist entries
            -->
450
            <template v-slot:cell(actions)="data">
451
452
              <b-button-group size="sm">
                <b-button
453
454
                  :disabled="data.index === 0"
                  @click="movePlaylistItemUp(data.index)"
455
                >
456
                  <b class="upDownArrows">&uarr;</b>
457
458
                </b-button>
                <b-button
459
460
                  :disabled="data.index === playlistEditor.entries.length - 1"
                  @click="movePlaylistItemDown(data.index)"
461
                >
462
                  <b class="upDownArrows">&darr;</b>
463
464
465
                </b-button>
                <b-button
                  variant="danger"
466
                  @click="deletePlaylistItem(data.index)"
467
468
469
470
                >
                  Delete
                </b-button>
              </b-button-group>
471
472
            </template>
          </b-table>
473
        </div>
474

475
        <hr>
476
477
478
479
480
481
482
483
484

        <!-- Below the table with the playlists entry we display buttons to
        add new entries to the table - these can either be files from our
        uploaded/imported files, or one of the preconfigured inputs, or a
        stream.
        TODO: should we disable choosing files that are still being imported?
        TODO: make the inputs configurable
        -->
        <div>
485
486
487
488
489
490
491
492
493
494
495
496
          <b-modal
            id="modal-edit-playlist-add-stream"
            title="Add stream to the playlist"
            @ok="addPlaylistItemStream('save')"
          >
            <b-input
              v-model="playlistEditor.newStreamURL"
              type="url"
            >
              ...
            </b-input>
          </b-modal>
497
498
499
500
501
502
          Add:
          <b-button-group>
            <b-dropdown text="File">
              <b-dropdown-item
                v-for="(file, index) in files"
                :key="index"
503
                @click="addPlaylistItemFile(file.show, file.id)"
504
505
506
507
508
              >
                {{ file.id }}: {{ file.metadata.title ? file.metadata.title : "" }} ({{ prettyNanoseconds(file.duration) }}, {{ prettyFileSize(file.size) }}, {{ file.source.uri }})
              </b-dropdown-item>
            </b-dropdown>
            <b-dropdown text="Line-in">
509
510
511
512
513
514
515
516
517
              <b-dropdown-item @click="addPlaylistItemLine('0')">
                Studio 1
              </b-dropdown-item>
              <b-dropdown-item @click="addPlaylistItemLine('1')">
                Preprod
              </b-dropdown-item>
              <b-dropdown-item @click="addPlaylistItemLine('2')">
                Line 2
              </b-dropdown-item>
518
            </b-dropdown>
519
520
521
522
523
            <b-button
              @click="addPlaylistItemStream('openModal')"
            >
              Stream
            </b-button>
524
525
          </b-button-group>
        </div>
526
527
      </b-modal>

528
      <!-- Only display a spinner if the playlists are not loaded yet -->
529
530
531
      <div v-if="!loaded.playlists">
        <b-row>
          <b-col align="center">
532
533
534
535
            <img
              src="../assets/radio.gif"
              alt="loading data"
            >
536
537
538
          </b-col>
        </b-row>
      </div>
539

540
      <!-- If all playlist data is loaded we can present a create button and
541
      a table of playlists (if there are already any)-->
542
      <div v-else>
543
        <!-- If no playlists are available, only show an alert and a create button -->
544
545
546
547
548
549
550
551
552
553
554
555
        <div
          v-if="playlists.length === 0"
          align="center"
        >
          <b-alert
            show
            variant="warning"
          >
            There are no playlists for this show yet.
          </b-alert>
          <b-button
            variant="success"
556
            @click="addPlaylist"
557
558
559
          >
            Create a playlist
          </b-button>
560
        </div>
561
        <!-- In case there are playlists, show the button and then the table -->
562
        <div v-else>
563
564
565
566
567
568
          <div
            align="center"
            style="padding-bottom: 1.5em;"
          >
            <b-button
              variant="success"
569
              @click="addPlaylist"
570
571
572
573
            >
              Create a playlist
            </b-button>
          </div>
574
          <b-table
575
            ref="playlistsTable"
576
            striped
577
578
579
580
581
582
            :fields="playlistsTableFields"
            :items="playlists"
          >
            <!-- Column: Entries
            This column displays the number of entries of the playlist.
            -->
583
            <template v-slot:cell(entries)="data">
584
585
586
587
588
589
590
591
592
593
594
595
596
              {{ data.value.length }} items
              <b-button
                v-b-tooltip.html="playlistToolTip(data.value)"
                variant="outline-success"
                size="sm"
              >
                show entries
              </b-button>
            </template>

            <!-- Column: Last edit
            This column lists the last date this playlist was changed.
            -->
597
            <template v-slot:cell(updated)="data">
598
599
600
601
602
603
604
              {{ prettyDateTime(data.value) }}
            </template>

            <!-- Column: Actions
            This column displays the available buttons for actions the user can
            take on this playlist (e.g. editing and deleting).
            -->
605
            <template v-slot:cell(actions)="data">
606
607
608
609
610
611
612
613
614
615
616
617
618
              <b-button-group size="sm">
                <b-button @click="editPlaylist(data.item.id)">
                  Edit
                </b-button>
                <b-button
                  variant="danger"
                  @click="deletePlaylist(data.item.id)"
                >
                  Delete
                </b-button>
              </b-button-group>
            </template>
          </b-table>
619
        </div>
620
        <!-- End of playlists table -->
621
      </div>
622
    </div>
623
  </b-container>
624
625
626
</template>

<script>
627
import axios from 'axios'
628
629
import filesize from 'filesize'
import prettyDate from '../mixins/prettyDate'
630
import modalFileManagerLog from './FileManagerModalLog.vue'
631
632

export default {
633
634
635
636
637
  // include the modal for displaying file import logs
  components: {
    'app-modalFileManagerLog': modalFileManagerLog
  },

638
  // generic functions that we want to use from our mixins folder
639
  mixins: [ prettyDate ],
640
641
642

  // the data this component will be handling: mostly flags and local versions
  // of the data fetched from the AuRa tank API
643
644
645
646
647
  data () {
    return {
      shows: [],        // an array of objects describing our shows (empty at load, will be populated on created())
      currentShow: 0,   // index of the currently selected show in our shows array
      currentShowID: 0, // actual id of the currently selected show
648
649
      files: [],
      playlists: [],
650
      mode: 'files',
651
      uploadInterval: null,
652
653

      // some flags to signal if API data is already fully loaded
654
655
656
657
658
      loaded: {
        shows: false,
        files: false,
        playlists: false
      },
659
660
661
662
663
664

      // variables used by file upload/import modal
      addNewFileURI: false,
      uploadSourceURI: '',
      uploadSourceFile: null,

665
666
667
668
      // and for adding and editing the playlists we need some temporary stuff
      playlistEditor: {
        id: null,
        mode: 'add',  // should be either 'add' or 'edit'
669
        description: '',
670
671
        entries: [],
        newStreamURL: null
672
673
      },

674
      // we need this for the modal to edit a file's meta information
675
676
677
678
679
680
      temp: {
        id: null,
        artist: '',
        album: '',
        title: ''
      },
681
682

      // for formatting the buttons - this way we could customize it later
683
684
685
      button: {
        files: 'info',
        playlists: 'outline-info'
686
      },
687
688

      // configuration of the files table, which will use the files array as data
689
690
691
692
693
694
695
      filesTableFields: [
        { key: 'id', label: 'Index' },
        { key: 'metadata.artist', label: 'Artist' },
        { key: 'metadata.album', label: 'Album' },
        { key: 'metadata.title', label: 'Title' },
        { key: 'duration', label: 'Duration' },
        { key: 'size', label: 'Size' },
696
697
        { key: 'actions', label: 'Actions', class: 'text-right' },
      ],
698

699
700
701
      // configuration of the playlists table, which will use the playlists array as data
      playlistsTableFields: [
        { key: 'id', label: 'Index' },
702
        { key: 'description', label: 'Description' },
703
704
705
        { key: 'entries', label: 'Entries' },
        { key: 'updated', label: 'Last edit' },
        { key: 'actions', label: 'Actions', class: 'text-right' },
706
707
708
709
710
711
712
      ],

      playlistEditTableFields: [
        { key: 'id', label: 'Index' },
        { key: 'type', label: 'Type' },
        { key: 'source', label: 'Source' },
        { key: 'actions', label: 'Actions', class: 'text-right' },
713
      ]
714
715
    }
  },
716
717
718
719
720

  // Right after this component is set up, we want to fetch all available shows
  // from the AuRa tank module. This works quite similar to the ShowManager.
  // We also want to load the files and playlists as soon as the shows are
  // loaded.
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
  created () {
    // when we enter this module, we want to load all shows of the current user
    // before we search for corresponding shows in the tank
    var uri = process.env.VUE_APP_API_STEERING_SHOWS
    // only the superuser should see all shows and therefore files and playlists
    // normal users should only see their own shows
    if (!this.$parent.user.steeringUser.is_superuser) {
      uri += '?owner=' + this.$parent.user.steeringUser.id
    }
    // now make the API call to fetch the shows
    axios.get(uri, {
      withCredentials: true,
      headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
    }).then(response => {
      if (response.data.length === 0) {
736
        alert('There are no shows associated with your account!')
737
738
739
740
741
742
743
744
        return
      }
      this.shows = response.data
      this.currentShowID = this.shows[0].id
      this.currentShow = 0
      this.loaded.shows = true
      this.switchShow(this.currentShow)
    }).catch(error => {
745
746
      this.$log.error(error.response.status + ' ' + error.response.statusText)
      this.$log.error(error.response)
747
      alert('Error: could not fetch shows from steering. See console for details.')
748
749
    })
  },
750
751
752

  // Now for our hotchpotch of methods, mostly for fetching data from and
  // posting/updating data to the AuRa tank API
753
  methods: {
754
755
756

    // Just a placeholder function we can use in the UI, to signal if something
    // is not yet implemented
757
758
759
    notYetImplemented: function () {
      alert('By the mighty witchcraftry of the mother of time!\n\nThis feature is not implemented yet.')
    },
760
761
762

    // We want to have a nice format for the file size, given a size in Bytes.
    // For that we're useing the filesize npm module.
763
764
765
    prettyFileSize: function (s) {
      return filesize(s)
    },
766
767

    // Returns a file object from the files array, given a file ID
768
769
770
771
772
773
774
775
    getFileById: function (id) {
      for (var i in this.files) {
        if (this.files[i].id === id) {
          return this.files[i]
        }
      }
      return null
    },
776

777
778
779
780
    // Returns a playlist object from the playlists array, given a playlist ID.
    // If the mode parameter is used and set to 'index', then the playlists
    // index in the playlist array is returned instead of the object
    getPlaylistById: function (id, mode) {
781
782
      for (var i in this.playlists) {
        if (this.playlists[i].id === id) {
783
784
          if (mode === 'index') { return i }
          else { return this.playlists[i] }
785
786
787
788
789
        }
      }
      return null
    },

790
791
792
793
794
795
796
797
798
799
800
801
    // return a string representing a file entry for the playlist editor
    getFileTitleForPlaylist: function (show, id) {
      // TODO: change structure of files array, so we can access all shows
      // the user has access to.
      var file = this.getFileById(id)
      if (file && file.metadata.title) {
        return show + ": " + file.metadata.title
      } else {
        return ""
      }
    },

802
803
804
805
806
807
808
809
810
    playlistToolTip: function (entries) {
      var text = '<div style="white-space: nowrap;" align="left">'
      for (var i in entries) {
        text += i + ': ' + entries[i].uri + '<br>'
      }
      text += '</div>'
      return text
    },

811
    movePlaylistItemUp: function (index) {
812
813
814
815
816
817
      if (index > 0 && index < this.playlistEditor.entries.length) {
        var temp = this.playlistEditor.entries[index - 1]
        this.playlistEditor.entries[index - 1] = this.playlistEditor.entries[index]
        this.playlistEditor.entries[index] = temp
        this.$refs.playlistEditTable.refresh()
      }
818
819
820
    },

    movePlaylistItemDown: function (index) {
821
822
823
824
825
826
      if (index < this.playlistEditor.entries.length - 1 && index >= 0) {
        var temp = this.playlistEditor.entries[index + 1]
        this.playlistEditor.entries[index + 1] = this.playlistEditor.entries[index]
        this.playlistEditor.entries[index] = temp
        this.$refs.playlistEditTable.refresh()
      }
827
828
829
    },

    deletePlaylistItem: function (index) {
830
831
832
833
      if (index >= 0 && index < this.playlistEditor.entries.length) {
        this.playlistEditor.entries.splice(index, 1)
        this.$refs.playlistEditTable.refresh()
      }
834
835
    },

836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
    // add a file from the file manager to the playlist that is being edited
    addPlaylistItemFile: function (show, id) {
      var item = {}
      item.file = {show: show, id: id}
      this.playlistEditor.entries.push(item)
    },

    // add a line input to the playlist that is being edited
    addPlaylistItemLine: function (line) {
      this.playlistEditor.entries.push({uri: 'line://' + line})
    },

    // controls sub-modal to add a new URI to the playlist editor
    addPlaylistItemStream: function (action) {
      if (action === 'openModal') {
        // the function gets called with the action 'openModal' when the users
        // clicks on the add new stream button. then we clear all temp data and
        // open the modal
        this.playlistEditor.newStreamURL = ''
        this.$bvModal.show('modal-edit-playlist-add-stream')
      } else {
        // when the user hits ok to add the new uri, this function gets called
        // with the action 'save' and the modal closes automagically. so we
        // just have to push a new entry to the playlist in the editor
        if (this.playlistEditor.newStreamURL.trim().length > 0) {
          this.playlistEditor.entries.push({uri: this.playlistEditor.newStreamURL})
        }
        // if an empty string was provided, we just do nothing
      }
    },

867
    deletePlaylist: function (id) {
868
869
870
871
872
873
874
875
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/playlists/' + id
      // TODO: add mechanism to indicate the running delete request in the files table
      axios.delete(uri, {
        withCredentials: true,
        headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token },
      }).then(
        this.fetchPlaylists(this.shows[this.currentShow].slug)
      ).catch(error => {
876
877
        this.$log.error(error.response.status + ' ' + error.response.statusText)
        this.$log.error(error.response)
878
        alert('Error: could not delete playlist. See console for details.')
879
      })
880
881
    },

882
883
884
    addPlaylist: function () {
      this.playlistEditor.mode = 'add'
      this.playlistEditor.id = null
885
886
      this.playlistEditor.description = ''
      this.playlistEditor.entries = []
887
888
889
      this.$bvModal.show('modal-edit-playlist')
    },

890
    editPlaylist: function (id) {
891
892
893
894
      this.playlistEditor.mode = 'edit'
      this.playlistEditor.entries = []
      this.playlistEditor.id = id
      var playlist = this.getPlaylistById(id)
895
      this.playlistEditor.description = playlist.description
896
897
898
      for (var i in playlist.entries) {
        var entry = {}
        if (playlist.entries[i].file) {
899
900
901
902
903
          entry.file = {}
          entry.file.show = playlist.entries[i].file.show
          entry.file.id = playlist.entries[i].file.id
        } else {
          entry.uri = playlist.entries[i].uri
904
905
906
907
908
909
        }
        this.playlistEditor.entries.push(entry)
      }
      this.$bvModal.show('modal-edit-playlist')
    },

910
911
    // Adds a new or updates an existing playlist by sending a POST/PUT
    // request to the AURA tank API
912
    storePlaylist: function () {
913
914
915
916
      var data = {
        description: this.playlistEditor.description,
        entries: this.playlistEditor.entries
      }
917
918
919
920
921
922
923
924
925
926
927
      var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/playlists'

      // TODO: add some spinner or other visualisation while the playlist is added/updated

      if (this.playlistEditor.mode === 'add') {
        axios.post(uri, data, {
          withCredentials: true,
          headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
        }).then(response => {
          this.playlists.push(response.data)
        }).catch(error => {
928
929
          this.$log.error(error.response.status + ' ' + error.response.statusText)
          this.$log.error(error.response)
930
          alert('Error: could not add new playlist. See console for details.')
931
932
933
934
935
        })

      // when updating an existing playlist we use a PUT request and add the id
      // of the playlist to the uri
      } else if (this.playlistEditor.mode === 'edit') {
936
937
        var id = this.playlistEditor.id
        uri += '/' + id
938
939
940
941
        axios.put(uri, data, {
          withCredentials: true,
          headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
        }).then(response => {
942
943
          this.playlists[this.getPlaylistById(id, 'index')] = response.data
          this.$refs.playlistsTable.refresh()
944
        }).catch(error => {
945
946
          this.$log.error(error.response.status + ' ' + error.response.statusText)
          this.$log.error(error.response)
947
          alert('Error: could not update playlist. See console for details.')
948
949
        })
      }
950
951
    },

952
953
954
    // To start modifying the meta information for a file we have to set our
    // temporary data (which will be used to check if anything changed) and
    // then open the editing modal
955
    editFile: function (id) {
956
957
958
959
960
961
962
963
      var file = this.getFileById(id)
      this.temp.id = file.id
      this.temp.artist = file.metadata.artist
      this.temp.album = file.metadata.album
      this.temp.title = file.metadata.title
      this.temp.uri = file.source.uri
      this.$bvModal.show('modal-edit-file')
    },
964
965
966
967

    // Once the OK button is hit in the file editing modal, we have to check
    // if anything changed and then send an appropriate metadata obecjt to
    // the AuRa tank API to update it
968
969
    saveFile: function (){
      var file = this.getFileById(this.temp.id)
970
      // we only want to send a PATCH request if some metadata actually changed
971
      if (this.temp.artist !== file.metadata.artist || this.temp.album !== file.metadata.album || this.temp.title !== file.metadata.title ) {
972
973
974
        // if a metadata property was in use before and now shall be emptied
        // we cannot just omit the property, but have to explicitly send null
        var metadata = {
975
          artist: this.temp.artist ? this.temp.artist : null,
976
977
978
          album: this.temp.album ? this.temp.album : null,
          title: this.temp.title ? this.temp.title : null
        }
979
        var uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + file.id
980
        // TODO: add mechanism to indicate the running patch request in the files table
981
982
983
984
985
986
        axios.patch(uri, metadata, {
          withCredentials: true,
          headers: { 'Authorization': 'Bearer ' + this.$parent.user.access_token }
        }).then(response => {
          this.getFileById(this.temp.id).metadata = response.data.metadata
        }).catch(error => {
987
988
          this.$log.error(error.response.status + ' ' + error.response.statusText)
          this.$log.error(error.response)
989
          alert('Error: could not save metadata info to file. See console for details.')
990
991
        })
      }
992
    },
993

994
    // Deletes a file with a specific ID calling the AuRa tank API and afterwards
995
    // fetching a fresh list of files from it.
996
    deleteFile: function (id) {
997
      let uri = process.env.VUE_APP_API_TANK + 'shows/' + this.shows[this.currentShow].slug + '/files/' + id
998
      // TODO: add mechanism to indicate the running delete request in the files table
999
1000
      axios.delete(uri, {
        withCredentials: true,