123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <template>
- <div>
- <metadata title="Admin | Queue songs" />
- <div class="container" v-scroll="handleScroll">
- <p>
- <span>Sets loaded: {{ position - 1 }} / {{ maxPosition }}</span>
- <br />
- <span>Loaded songs: {{ this.songs.length }}</span>
- </p>
- <input
- v-model="searchQuery"
- type="text"
- class="input"
- placeholder="Search for Songs"
- />
- <button
- v-if="!getSetsAutomatically"
- class="button is-primary"
- @click="loadSongsAutomatically()"
- >
- Load songs automatically
- </button>
- <br />
- <br />
- <table class="table is-striped">
- <thead>
- <tr>
- <td>Thumbnail</td>
- <td>Title</td>
- <td>Artists</td>
- <td>Genres</td>
- <td>ID / YouTube ID</td>
- <td>Requested By</td>
- <td>Options</td>
- </tr>
- </thead>
- <tbody>
- <tr v-for="(song, index) in filteredSongs" :key="index">
- <td>
- <img
- class="song-thumbnail"
- :src="song.thumbnail"
- onerror="this.src='/assets/notes-transparent.png'"
- />
- </td>
- <td>
- <strong>{{ song.title }}</strong>
- </td>
- <td>{{ song.artists.join(", ") }}</td>
- <td>{{ song.genres.join(", ") }}</td>
- <td>
- {{ song._id }}
- <br />
- <a
- :href="
- 'https://www.youtube.com/watch?v=' +
- `${song.songId}`
- "
- target="_blank"
- >
- {{ song.songId }}</a
- >
- </td>
- <td>
- <user-id-to-username
- :userId="song.requestedBy"
- :link="true"
- />
- </td>
- <td class="optionsColumn">
- <button
- class="button is-primary"
- @click="edit(song, index)"
- >
- <i class="material-icons">edit</i>
- </button>
- <button
- class="button is-success"
- @click="add(song)"
- >
- <i class="material-icons">add</i>
- </button>
- <button
- class="button is-danger"
- @click="remove(song._id, index)"
- >
- <i class="material-icons">cancel</i>
- </button>
- </td>
- </tr>
- </tbody>
- </table>
- </div>
- <edit-song v-if="modals.editSong" />
- </div>
- </template>
- <script>
- import { mapState, mapActions } from "vuex";
- import Toast from "toasters";
- import EditSong from "../Modals/EditSong.vue";
- import UserIdToUsername from "../UserIdToUsername.vue";
- import io from "../../io";
- export default {
- components: { EditSong, UserIdToUsername },
- data() {
- return {
- position: 1,
- maxPosition: 1,
- searchQuery: "",
- songs: [],
- gettingSet: false,
- getSetsAutomatically: false
- };
- },
- computed: {
- filteredSongs() {
- return this.songs.filter(
- song =>
- JSON.stringify(Object.values(song)).indexOf(
- this.searchQuery
- ) !== -1
- );
- },
- ...mapState("modals", {
- modals: state => state.modals.admin
- })
- },
- // watch: {
- // "modals.editSong": function(value) {
- // console.log(value);
- // if (value === false) this.stopVideo();
- // }
- // },
- methods: {
- edit(song, index) {
- const newSong = {};
- Object.keys(song).forEach(n => {
- newSong[n] = song[n];
- });
- this.editSong({ index, song: newSong, type: "queueSongs" });
- this.openModal({ sector: "admin", modal: "editSong" });
- },
- add(song) {
- this.socket.emit("songs.add", song, res => {
- if (res.status === "success")
- new Toast({ content: res.message, timeout: 2000 });
- else new Toast({ content: res.message, timeout: 4000 });
- });
- },
- remove(id) {
- this.socket.emit("queueSongs.remove", id, res => {
- if (res.status === "success")
- new Toast({ content: res.message, timeout: 2000 });
- else new Toast({ content: res.message, timeout: 4000 });
- });
- },
- getSet() {
- if (this.gettingSet) return;
- if (this.position > this.maxPosition) return;
- this.gettingSet = true;
- this.socket.emit("queueSongs.getSet", this.position, data => {
- data.forEach(song => {
- this.songs.push(song);
- });
- this.position += 1;
- this.gettingSet = false;
- if (
- this.getSetsAutomatically &&
- this.maxPosition > this.position - 1
- )
- setTimeout(() => {
- this.getSet();
- }, 500);
- });
- },
- handleScroll() {
- if (this.getSetsAutomatically) return false;
- if (window.scrollY + 50 >= window.scrollMaxY) this.getSet();
- return this.maxPosition === this.position;
- },
- loadSongsAutomatically() {
- this.getSetsAutomatically = true;
- this.getSet();
- },
- init() {
- if (this.songs.length > 0)
- this.position = Math.ceil(this.songs.length / 15) + 1;
- this.socket.emit("queueSongs.length", length => {
- this.maxPosition = Math.ceil(length / 15);
- this.getSet();
- });
- this.socket.emit("apis.joinAdminRoom", "queue", () => {});
- },
- ...mapActions("admin/songs", ["stopVideo", "editSong"]),
- ...mapActions("modals", ["openModal"])
- },
- mounted() {
- io.getSocket(socket => {
- this.socket = socket;
- this.socket.on("event:admin.queueSong.added", queueSong => {
- this.songs.push(queueSong);
- });
- this.socket.on("event:admin.queueSong.removed", songId => {
- this.songs = this.songs.filter(song => {
- return song._id !== songId;
- });
- });
- this.socket.on("event:admin.queueSong.updated", updatedSong => {
- for (let i = 0; i < this.songs.length; i += 1) {
- const song = this.songs[i];
- if (song._id === updatedSong._id) {
- this.songs.$set(i, updatedSong);
- }
- }
- });
- if (this.socket.connected) {
- this.init();
- }
- io.onConnect(() => {
- this.init();
- });
- });
- }
- };
- </script>
- <style lang="scss" scoped>
- @import "styles/global.scss";
- .optionsColumn {
- width: 140px;
- button {
- width: 35px;
- }
- }
- .song-thumbnail {
- display: block;
- max-width: 50px;
- margin: 0 auto;
- }
- td {
- vertical-align: middle;
- }
- .is-primary:focus {
- background-color: $primary-color !important;
- }
- </style>
|