Songs.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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-transparent.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' href='#' @click='edit(song, index)'>Edit</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. </div>
  37. <edit-song v-show='isEditActive'></edit-song>
  38. </template>
  39. <script>
  40. import { Toast } from 'vue-roaster';
  41. import EditSong from '../Modals/EditSong.vue';
  42. import io from '../../io';
  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. let genre = $('#new-genre').val().toLowerCase().trim();
  93. if (this.editing.song.genres.indexOf(genre) !== -1) return Toast.methods.addToast('Genre already exists', 3000);
  94. if (genre) {
  95. this.editing.song.genres.push(genre);
  96. $('#new-genre').val('');
  97. } else Toast.methods.addToast('Genre cannot be empty', 3000);
  98. } else if (type == 'artists') {
  99. let artist = $('#new-artist').val();
  100. if (this.editing.song.artists.indexOf(artist) !== -1) return Toast.methods.addToast('Artist already exists', 3000);
  101. if ($('#new-artist').val() !== '') {
  102. this.editing.song.artists.push(artist);
  103. $('#new-artist').val('');
  104. } else Toast.methods.addToast('Artist cannot be empty', 3000);
  105. }
  106. },
  107. removeTag: function (type, index) {
  108. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  109. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  110. },
  111. edit: function (song, index) {
  112. if (this.video.player) {
  113. this.video.player.loadVideoById(song._id);
  114. let songCopy = {};
  115. for (let n in song) {
  116. songCopy[n] = song[n];
  117. }
  118. this.editing = { index, song: songCopy };
  119. this.isEditActive = true;
  120. }
  121. },
  122. save: function (song) {
  123. let _this = this;
  124. this.socket.emit('songs.update', song._id, song, function (res) {
  125. if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
  126. _this.toggleModal();
  127. });
  128. },
  129. remove: function (id, index) {
  130. this.socket.emit('songs.remove', id, res => {
  131. if (res.status == 'success') Toast.methods.addToast(res.message, 4000);
  132. else Toast.methods.addToast(res.message, 8000);
  133. });
  134. },
  135. init: function() {
  136. let _this = this;
  137. _this.socket.emit('songs.index', data => {
  138. _this.songs = data;
  139. });
  140. _this.socket.emit('apis.joinAdminRoom', 'songs', data => {});
  141. }
  142. },
  143. ready: function () {
  144. let _this = this;
  145. io.getSocket((socket) => {
  146. _this.socket = socket;
  147. if (_this.socket.connected) {
  148. _this.init();
  149. _this.socket.on('event:admin.song.added', song => {
  150. _this.songs.push(song);
  151. });
  152. _this.socket.on('event:admin.song.removed', songId => {
  153. _this.songs = _this.songs.filter(function(song) {
  154. return song._id !== songId;
  155. });
  156. });
  157. }
  158. io.onConnect(() => {
  159. _this.init();
  160. });
  161. });
  162. this.video.player = new YT.Player('player', {
  163. height: 315,
  164. width: 560,
  165. videoId: this.editing.song._id,
  166. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  167. events: {
  168. 'onReady': () => {
  169. let volume = parseInt(localStorage.getItem("volume"));
  170. volume = (typeof volume === "number") ? volume : 20;
  171. _this.video.player.setVolume(volume);
  172. if (volume > 0) _this.video.player.unMute();
  173. },
  174. 'onStateChange': event => {
  175. if (event.data == 1) {
  176. let youtubeDuration = _this.video.player.getDuration();
  177. youtubeDuration -= _this.editing.song.skipDuration;
  178. if (_this.editing.song.duration > youtubeDuration) {
  179. this.video.player.stopVideo();
  180. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  181. }
  182. }
  183. }
  184. }
  185. });
  186. let volume = parseInt(localStorage.getItem("volume"));
  187. volume = (typeof volume === "number") ? volume : 20;
  188. $("#volumeSlider").val(volume);
  189. }
  190. }
  191. </script>
  192. <style lang='scss' scoped>
  193. body { font-family: 'Roboto', sans-serif; }
  194. .song-thumbnail {
  195. display: block;
  196. max-width: 50px;
  197. margin: 0 auto;
  198. }
  199. td { vertical-align: middle; }
  200. .is-primary:focus { background-color: #029ce3 !important; }
  201. </style>