EditPlaylistModal.vue 10.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
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
293
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
<template>
  <b-modal
    id="modal-edit-playlist"
    size="lg"
    :title="playlistEditor.mode === 'edit' ? 'Edit playlist' : 'Add new playlist'"
    @ok="storePlaylist"
  >
    <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>

    <hr>

    <p>Playlist entries:</p>

    <!-- If no entries are here (i.e. we add a new playlist), only show
    a hint that there's nothing here yet. -->
    <div
      v-if="playlistEditor.entries.length === 0"
      align="center"
    >
      No entries yet. Add some.
    </div>
    <!-- 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 -->
    <div v-else>
      <b-table
        ref="playlistEditTable"
        striped
        :items="playlistEditor.entries"
        :fields="playlistEditTableFields"
      >
        <!-- 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
        -->
        <template v-slot:cell(id)="data">
          {{ 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
        -->
        <template v-slot:cell(type)="data">
          <b-badge
            v-if="data.item.file"
            variant="success"
          >
            File
          </b-badge>
          <b-badge
            v-else-if="data.item.uri.startsWith('line://')"
            variant="info"
          >
            Line-in
          </b-badge>
          <b-badge
            v-else-if="data.item.uri.startsWith('http://') || data.item.uri.startsWith('https://')"
            variant="light"
          >
            Stream
          </b-badge>
          <b-badge
            v-else
            variant="dark"
          >
            Other
          </b-badge>
        </template>

        <!-- Column: Source
             Here we display where this playlist entry is coming from
        -->
        <template v-slot:cell(source)="data">
          <span v-if="data.item.file">
            {{ getFileTitleForPlaylist(data.item.file.show, data.item.file.id) }}
            <small><i>( file://{{ data.item.file.show }}/{{ data.item.file.id }} )</i></small>
          </span>
          <span v-else>
            {{ data.item.uri }}
          </span>
        </template>

        <!-- Column: Actions
             Finally some buttons to reorder or delete playlist entries
        -->
        <template v-slot:cell(actions)="data">
          <b-button-group size="sm">
            <b-button
              :disabled="data.index === 0"
              @click="movePlaylistItemUp(data.index)"
            >
              <b class="upDownArrows">&uarr;</b>
            </b-button>
            <b-button
              :disabled="data.index === playlistEditor.entries.length - 1"
              @click="movePlaylistItemDown(data.index)"
            >
              <b class="upDownArrows">&darr;</b>
            </b-button>
            <b-button
              variant="danger"
              @click="deletePlaylistItem(data.index)"
            >
              Delete
            </b-button>
          </b-button-group>
        </template>
      </b-table>
    </div>

    <hr>

    <!-- 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>
      <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>
      Add:
      <b-button-group>
        <b-dropdown text="File">
          <b-dropdown-item
            v-for="(file, index) in files"
            :key="index"
            @click="addPlaylistItemFile(file.show, file.id)"
          >
            {{ 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">
          <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>
        </b-dropdown>
        <b-button
          @click="addPlaylistItemStream('openModal')"
        >
          Stream
        </b-button>
      </b-button-group>
    </div>
  </b-modal>
</template>

<script>
import { mapGetters } from 'vuex'
import prettyDate from '../../mixins/prettyDate'
import filesize from 'filesize'

export default {
  mixins: [ prettyDate ],

  // include the modal for displaying file import logs
  data () {
    return {
      // for adding and editing the playlists we need some temporary storage
      playlistEditor: {
        id: null,
        mode: 'add',  // should be either 'add' or 'edit'
        description: '',
        entries: [],
        newStreamURL: null
      },

      // configuration of the playlists entries table
      playlistEditTableFields: [
        { key: 'id', label: 'Index' },
        { key: 'type', label: 'Type' },
        { key: 'source', label: 'Source' },
        { key: 'actions', label: 'Actions', class: 'text-right' },
      ]
    }
  },

  computed: {
    loaded () {
      return {
        playlists: this.$store.state.playlists.loaded.playlists,
      }
    },

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

  methods: {
    // 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.
    prettyFileSize: function (s) {
      return filesize(s)
    },

    // return a string representing a file entry for the playlist editor
    getFileTitleForPlaylist (show, id) {
      // TODO: change structure of files array, so we can access all shows
      // the user has access to.
      let file = this.getFileById(id)
      if (file && file.metadata.title) {
        return show + ": " + file.metadata.title
      } else {
        return ""
      }
    },

    movePlaylistItemUp (index) {
      if (index > 0 && index < this.playlistEditor.entries.length) {
        let temp = this.playlistEditor.entries[index - 1]
        this.playlistEditor.entries[index - 1] = this.playlistEditor.entries[index]
        this.playlistEditor.entries[index] = temp
        this.$refs.playlistEditTable.refresh()
      }
    },

    movePlaylistItemDown (index) {
      if (index < this.playlistEditor.entries.length - 1 && index >= 0) {
        let temp = this.playlistEditor.entries[index + 1]
        this.playlistEditor.entries[index + 1] = this.playlistEditor.entries[index]
        this.playlistEditor.entries[index] = temp
        this.$refs.playlistEditTable.refresh()
      }
    },

    deletePlaylistItem (index) {
      if (index >= 0 && index < this.playlistEditor.entries.length) {
        this.playlistEditor.entries.splice(index, 1)
        this.$refs.playlistEditTable.refresh()
      }
    },

    // add a file from the file manager to the playlist that is being edited
    addPlaylistItemFile (show, id) {
      let item = {}
      item.file = {show: show, id: id}
      this.playlistEditor.entries.push(item)
    },

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

    // controls sub-modal to add a new URI to the playlist editor
    addPlaylistItemStream (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
      }
    },

    addPlaylist () {
      this.playlistEditor.mode = 'add'
      this.playlistEditor.id = null
      this.playlistEditor.description = ''
      this.playlistEditor.entries = []
      this.$bvModal.show('modal-edit-playlist')
    },

    editPlaylist (id) {
      this.playlistEditor.mode = 'edit'
      this.playlistEditor.entries = []
      this.playlistEditor.id = id
      let playlist = this.getPlaylistById(id)
      this.playlistEditor.description = playlist.description
      for (let i in playlist.entries) {
        let entry = {}
        if (playlist.entries[i].file) {
          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
        }
        this.playlistEditor.entries.push(entry)
      }
      this.$bvModal.show('modal-edit-playlist')
    },

    storePlaylist () {
      // TODO: add some spinner or other visualisation while the playlist is added/updated
      let playlist = {
        description: this.playlistEditor.description,
        entries: this.playlistEditor.entries
      }
      if (this.playlistEditor.mode === 'add') {
        this.$store.dispatch('playlists/add', {
          slug: this.selectedShow.slug,
          playlist: playlist
        })
      } else if (this.playlistEditor.mode === 'edit') {
        this.$store.dispatch('playlists/update', {
          slug: this.selectedShow.slug,
          id: this.playlistEditor.id,
          playlist: playlist,
          callback: () => {
            this.$root.$emit('bv::refresh::table', 'playlistsTable')
          }
        })
      }
    },

  },
}
</script>