Songs.vue 5.8 KB

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