QueueSongs.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. <button class='button is-primary' @click='edit(song, index)'>Edit</button>
  29. <button class='button is-success' @click='add(song)'>Add</button>
  30. <button class='button is-danger' @click='remove(song._id, index)'>Remove</button>
  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. playerReady: false
  56. },
  57. timeout: 0
  58. }
  59. },
  60. methods: {
  61. settings: function (type) {
  62. let _this = this;
  63. switch(type) {
  64. case 'stop':
  65. _this.video.player.stopVideo();
  66. _this.video.paused = true;
  67. break;
  68. case 'pause':
  69. _this.video.player.pauseVideo();
  70. _this.video.paused = true;
  71. break;
  72. case 'play':
  73. _this.video.player.playVideo();
  74. _this.video.paused = false;
  75. break;
  76. case 'skipToLast10Secs':
  77. _this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
  78. break;
  79. }
  80. },
  81. changeVolume: function() {
  82. let local = this;
  83. let volume = $("#volumeSlider").val();
  84. localStorage.setItem("volume", volume);
  85. local.video.player.setVolume(volume);
  86. if (volume > 0) local.video.player.unMute();
  87. },
  88. toggleModal: function () {
  89. this.isEditActive = !this.isEditActive;
  90. this.settings('stop');
  91. },
  92. addTag: function (type) {
  93. if (type == 'genres') {
  94. let genre = $('#new-genre').val().toLowerCase().trim();
  95. if (this.editing.song.genres.indexOf(genre) !== -1) return Toast.methods.addToast('Genre already exists', 3000);
  96. if (genre) {
  97. this.editing.song.genres.push(genre);
  98. $('#new-genre').val('');
  99. } else Toast.methods.addToast('Genre cannot be empty', 3000);
  100. } else if (type == 'artists') {
  101. let artist = $('#new-artist').val();
  102. if (this.editing.song.artists.indexOf(artist) !== -1) return Toast.methods.addToast('Artist already exists', 3000);
  103. if ($('#new-artist').val() !== '') {
  104. this.editing.song.artists.push(artist);
  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. if (this.video.player) {
  115. this.video.player.loadVideoById(song._id, this.editing.song.skipDuration);
  116. let songCopy = {};
  117. for (let n in song) {
  118. songCopy[n] = song[n];
  119. }
  120. this.editing = { index, song: songCopy };
  121. this.isEditActive = true;
  122. }
  123. },
  124. save: function (song, close) {
  125. let _this = this;
  126. this.socket.emit('queueSongs.update', song._id, song, function (res) {
  127. Toast.methods.addToast(res.message, 4000);
  128. if (res.status === 'success') {
  129. _this.songs.forEach((lSong) => {
  130. if (song._id === lSong._id) {
  131. for (let n in song) {
  132. lSong[n] = song[n];
  133. }
  134. }
  135. });
  136. }
  137. if (close) {
  138. _this.toggleModal();
  139. }
  140. });
  141. },
  142. add: function (song) {
  143. this.socket.emit('songs.add', song, res => {
  144. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  145. });
  146. },
  147. remove: function (id, index) {
  148. this.socket.emit('queueSongs.remove', id, res => {
  149. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  150. });
  151. },
  152. init: function() {
  153. let _this = this;
  154. _this.socket.emit('queueSongs.index', data => {
  155. _this.songs = data;
  156. });
  157. _this.socket.emit('apis.joinAdminRoom', 'queue', data => {});
  158. }
  159. },
  160. ready: function () {
  161. let _this = this;
  162. io.getSocket((socket) => {
  163. _this.socket = socket;
  164. if (_this.socket.connected) {
  165. _this.init();
  166. _this.socket.on('event:admin.queueSong.added', queueSong => {
  167. _this.songs.push(queueSong);
  168. });
  169. _this.socket.on('event:admin.queueSong.removed', songId => {
  170. _this.songs = _this.songs.filter(function(song) {
  171. return song._id !== songId;
  172. });
  173. });
  174. }
  175. io.onConnect(() => {
  176. _this.init();
  177. });
  178. });
  179. setInterval(() => {
  180. if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
  181. _this.video.paused = false;
  182. _this.video.player.stopVideo();
  183. }
  184. }, 200);
  185. this.video.player = new YT.Player('player', {
  186. height: 315,
  187. width: 560,
  188. videoId: this.editing.song._id,
  189. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  190. startSeconds: _this.editing.song.skipDuration,
  191. events: {
  192. 'onReady': () => {
  193. let volume = parseInt(localStorage.getItem("volume"));
  194. volume = (typeof volume === "number") ? volume : 20;
  195. _this.video.player.seekTo(_this.editing.song.skipDuration);
  196. _this.video.player.setVolume(volume);
  197. if (volume > 0) _this.video.player.unMute();
  198. _this.playerReady = true;
  199. },
  200. 'onStateChange': event => {
  201. if (event.data === 1) {
  202. _this.video.paused = false;
  203. let youtubeDuration = _this.video.player.getDuration();
  204. youtubeDuration -= _this.editing.song.skipDuration;
  205. if (_this.editing.song.duration > youtubeDuration) {
  206. this.video.player.stopVideo();
  207. _this.video.paused = true;
  208. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  209. } else if (_this.editing.song.duration <= 0) {
  210. this.video.player.stopVideo();
  211. _this.video.paused = true;
  212. Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
  213. }
  214. if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
  215. _this.video.player.seekTo(10);
  216. }
  217. } else if (event.data === 2) {
  218. this.video.paused = true;
  219. }
  220. }
  221. }
  222. });
  223. let volume = parseInt(localStorage.getItem("volume"));
  224. volume = (typeof volume === "number") ? volume : 20;
  225. $("#volumeSlider").val(volume);
  226. }
  227. }
  228. </script>
  229. <style lang='scss' scoped>
  230. .song-thumbnail {
  231. display: block;
  232. max-width: 50px;
  233. margin: 0 auto;
  234. }
  235. td { vertical-align: middle; }
  236. .is-primary:focus { background-color: #029ce3 !important; }
  237. </style>