Songs.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. this.editing = { index, song };
  113. this.video.player.loadVideoById(song._id);
  114. this.isEditActive = true;
  115. },
  116. save: function (song) {
  117. let _this = this;
  118. this.socket.emit('songs.update', song._id, song, function (res) {
  119. if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
  120. _this.toggleModal();
  121. });
  122. },
  123. remove: function (id, index) {
  124. this.socket.emit('songs.remove', id, res => {
  125. if (res.status == 'success') Toast.methods.addToast(res.message, 4000);
  126. else Toast.methods.addToast(res.message, 8000);
  127. });
  128. },
  129. init: function() {
  130. let _this = this;
  131. _this.socket.emit('songs.index', data => {
  132. _this.songs = data;
  133. });
  134. _this.socket.emit('apis.joinAdminRoom', 'songs', data => {});
  135. }
  136. },
  137. ready: function () {
  138. let _this = this;
  139. io.getSocket((socket) => {
  140. _this.socket = socket;
  141. if (_this.socket.connected) {
  142. _this.init();
  143. _this.socket.on('event:admin.song.added', song => {
  144. _this.songs.push(song);
  145. });
  146. _this.socket.on('event:admin.song.removed', songId => {
  147. _this.songs = _this.songs.filter(function(song) {
  148. return song._id !== songId;
  149. });
  150. });
  151. }
  152. io.onConnect(() => {
  153. _this.init();
  154. });
  155. });
  156. this.video.player = new YT.Player('player', {
  157. height: 315,
  158. width: 560,
  159. videoId: this.editing.song._id,
  160. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  161. events: {
  162. 'onReady': () => {
  163. let volume = parseInt(localStorage.getItem("volume"));
  164. volume = (typeof volume === "number") ? volume : 20;
  165. _this.video.player.setVolume(volume);
  166. if (volume > 0) _this.video.player.unMute();
  167. },
  168. 'onStateChange': event => {
  169. if (event.data == 1) {
  170. let youtubeDuration = _this.video.player.getDuration();
  171. youtubeDuration -= _this.editing.song.skipDuration;
  172. if (_this.editing.song.duration > youtubeDuration) this.stopVideo();
  173. }
  174. }
  175. }
  176. });
  177. let volume = parseInt(localStorage.getItem("volume"));
  178. volume = (typeof volume === "number") ? volume : 20;
  179. $("#volumeSlider").val(volume);
  180. }
  181. }
  182. </script>
  183. <style lang='scss' scoped>
  184. body { font-family: 'Roboto', sans-serif; }
  185. .song-thumbnail {
  186. display: block;
  187. max-width: 50px;
  188. margin: 0 auto;
  189. }
  190. td { vertical-align: middle; }
  191. .is-primary:focus { background-color: #029ce3 !important; }
  192. </style>