QueueSongs.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class='columns is-mobile'>
  3. <div class='column is-8-desktop is-offset-2-desktop is-12-mobile'>
  4. <table class='table is-striped'>
  5. <thead>
  6. <tr>
  7. <td>Thumbnail</td>
  8. <td>Title</td>
  9. <td>YouTube ID</td>
  10. <td>Artists</td>
  11. <td>Genres</td>
  12. <td>Requested By</td>
  13. <td>Options</td>
  14. </tr>
  15. </thead>
  16. <tbody>
  17. <tr v-for='(index, song) in songs' track-by='$index'>
  18. <td>
  19. <img class='song-thumbnail' :src='song.thumbnail' onerror="this.src='/assets/notes.png'">
  20. </td>
  21. <td>
  22. <strong>{{ song.title }}</strong>
  23. </td>
  24. <td>{{ song._id }}</td>
  25. <td>{{ song.artists.join(', ') }}</td>
  26. <td>{{ song.genres.join(', ') }}</td>
  27. <td>{{ song.requestedBy }}</td>
  28. <td>
  29. <a class='button is-primary' @click='edit(song, index)'>Edit</a>
  30. <a class='button is-success' @click='add(song)'>Add</a>
  31. <a class='button is-danger' @click='remove(song._id, index)'>Remove</a>
  32. </td>
  33. </tr>
  34. </tbody>
  35. </table>
  36. </div>
  37. </div>
  38. <edit-song v-show='isEditActive'></edit-song>
  39. </template>
  40. <script>
  41. import { Toast } from 'vue-roaster';
  42. import EditSong from '../Modals/EditSong.vue';
  43. export default {
  44. components: { EditSong },
  45. data() {
  46. return {
  47. songs: [],
  48. isEditActive: false,
  49. editing: {
  50. index: 0,
  51. song: {}
  52. },
  53. video: {
  54. player: null,
  55. paused: false,
  56. settings: function (type) {
  57. switch(type) {
  58. case 'stop':
  59. this.player.stopVideo();
  60. this.paused = true;
  61. break;
  62. case 'pause':
  63. this.player.pauseVideo();
  64. this.paused = true;
  65. break;
  66. case 'play':
  67. this.player.playVideo();
  68. this.paused = false;
  69. break;
  70. case 'skipToLast10Secs':
  71. this.player.seekTo(this.player.getDuration() - 10);
  72. break;
  73. }
  74. }
  75. }
  76. }
  77. },
  78. methods: {
  79. changeVolume: function() {
  80. let local = this;
  81. let volume = $("#volumeSlider").val();
  82. localStorage.setItem("volume", volume);
  83. local.video.player.setVolume(volume);
  84. if (volume > 0) local.video.player.unMute();
  85. },
  86. toggleModal: function () {
  87. this.isEditActive = !this.isEditActive;
  88. this.video.settings('stop');
  89. },
  90. addTag: function (type) {
  91. if (type == 'genres') {
  92. for (let z = 0; z < this.editing.song.genres.length; z++) {
  93. if (this.editing.song.genres[z] == $('#new-genre').val()) return Toast.methods.addToast('Genre already exists', 3000);
  94. }
  95. if ($('#new-genre').val() !== '') {
  96. this.editing.song.genres.push($('#new-genre').val());
  97. $('#new-genre').val('');
  98. } else Toast.methods.addToast('Genre cannot be empty', 3000);
  99. } else if (type == 'artists') {
  100. for (let z = 0; z < this.editing.song.artists.length; z++) {
  101. if (this.editing.song.artists[z] == $('#new-artist').val()) return Toast.methods.addToast('Artist already exists', 3000);
  102. }
  103. if ($('#new-artist').val() !== '') {
  104. this.editing.song.artists.push($('#new-artist').val());
  105. $('#new-artist').val('');
  106. } else Toast.methods.addToast('Artist cannot be empty', 3000);
  107. }
  108. },
  109. removeTag: function (type, index) {
  110. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  111. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  112. },
  113. edit: function (song, index) {
  114. this.editing = { index, song };
  115. this.video.player.loadVideoById(song._id);
  116. this.isEditActive = true;
  117. },
  118. save: function (song) {
  119. let _this = this;
  120. this.socket.emit('queueSongs.update', song._id, song, function (res) {
  121. if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
  122. _this.toggleModal();
  123. });
  124. },
  125. add: function (song) {
  126. this.socket.emit('songs.add', song, res => {
  127. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  128. });
  129. },
  130. remove: function (id, index) {
  131. this.songs.splice(index, 1);
  132. this.socket.emit('queueSongs.remove', id, res => {
  133. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  134. });
  135. }
  136. },
  137. ready: function () {
  138. let _this = this;
  139. let socketInterval = setInterval(() => {
  140. if (!!_this.$parent.$parent.socket) {
  141. _this.socket = _this.$parent.$parent.socket;
  142. _this.socket.emit('queueSongs.index', data => {
  143. _this.songs = data;
  144. });
  145. clearInterval(socketInterval);
  146. }
  147. }, 100);
  148. this.video.player = new YT.Player('player', {
  149. height: 315,
  150. width: 560,
  151. videoId: this.editing.song._id,
  152. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  153. events: {
  154. 'onReady': () => {
  155. let volume = parseInt(localStorage.getItem("volume"));
  156. volume = (typeof volume === "number") ? volume : 20;
  157. _this.video.player.setVolume(volume);
  158. if (volume > 0) _this.video.player.unMute();
  159. },
  160. 'onStateChange': event => {
  161. if (event.data == 1) {
  162. let youtubeDuration = _this.video.player.getDuration();
  163. youtubeDuration -= _this.editing.song.skipDuration;
  164. if (_this.editing.song.duration > youtubeDuration) this.stopVideo();
  165. }
  166. }
  167. }
  168. });
  169. let volume = parseInt(localStorage.getItem("volume"));
  170. volume = (typeof volume === "number") ? volume : 20;
  171. $("#volumeSlider").val(volume);
  172. }
  173. }
  174. </script>
  175. <style lang='scss' scoped>
  176. .song-thumbnail {
  177. display: block;
  178. max-width: 50px;
  179. margin: 0 auto;
  180. }
  181. td { vertical-align: middle; }
  182. </style>