QueueSongs.vue 6.0 KB

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