QueueSongs.vue 7.4 KB

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