Songs.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.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' @click='edit(song, index)'>Edit</a>
  30. <a class='button is-danger' @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. 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. settings: function (type) {
  56. switch(type) {
  57. case 'stop':
  58. this.player.stopVideo();
  59. this.paused = true;
  60. break;
  61. case 'pause':
  62. this.player.pauseVideo();
  63. this.paused = true;
  64. break;
  65. case 'play':
  66. this.player.playVideo();
  67. this.paused = false;
  68. break;
  69. case 'skipToLast10Secs':
  70. this.player.seekTo(this.player.getDuration() - 10);
  71. break;
  72. }
  73. }
  74. }
  75. }
  76. },
  77. methods: {
  78. changeVolume: function() {
  79. let local = this;
  80. let volume = $("#volumeSlider").val();
  81. localStorage.setItem("volume", volume);
  82. local.video.player.setVolume(volume);
  83. if (volume > 0) local.video.player.unMute();
  84. },
  85. toggleModal: function () {
  86. this.isEditActive = !this.isEditActive;
  87. this.video.settings('stop');
  88. },
  89. addTag: function (type) {
  90. if (type == 'genres') {
  91. for (let z = 0; z < this.editing.song.genres.length; z++) {
  92. if (this.editing.song.genres[z] == $('#new-genre').val()) return Toast.methods.addToast('Genre already exists', 3000);
  93. }
  94. if ($('#new-genre').val() !== '') this.editing.song.genres.push($('#new-genre').val());
  95. else Toast.methods.addToast('Genre cannot be empty', 3000);
  96. } else if (type == 'artists') {
  97. for (let z = 0; z < this.editing.song.artists.length; z++) {
  98. if (this.editing.song.artists[z] == $('#new-artist').val()) return Toast.methods.addToast('Artist already exists', 3000);
  99. }
  100. if ($('#new-artist').val() !== '') this.editing.song.artists.push($('#new-artist').val());
  101. else Toast.methods.addToast('Artist cannot be empty', 3000);
  102. }
  103. },
  104. removeTag: function (type, index) {
  105. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  106. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  107. },
  108. edit: function (song, index) {
  109. this.editing = { index, song };
  110. this.video.player.loadVideoById(song._id);
  111. this.isEditActive = true;
  112. },
  113. save: function (song) {
  114. let _this = this;
  115. this.socket.emit('songs.update', song._id, song, function (res) {
  116. if (res.status == 'success' || res.status == 'error') Toast.methods.addToast(res.message, 2000);
  117. _this.toggleModal();
  118. });
  119. },
  120. remove: function (id, index) {
  121. this.songs.splice(index, 1);
  122. this.socket.emit('songs.remove', id, res => {
  123. if (res.status == 'success') Toast.methods.addToast(res.message, 2000);
  124. });
  125. }
  126. },
  127. ready: function () {
  128. let _this = this;
  129. let socketInterval = setInterval(() => {
  130. if (!!_this.$parent.$parent.socket) {
  131. _this.socket = _this.$parent.$parent.socket;
  132. _this.socket.emit('songs.index', data => {
  133. _this.songs = data;
  134. });
  135. clearInterval(socketInterval);
  136. }
  137. }, 100);
  138. this.video.player = new YT.Player('player', {
  139. height: 315,
  140. width: 560,
  141. videoId: this.editing.song._id,
  142. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  143. events: {
  144. 'onReady': () => {
  145. let volume = parseInt(localStorage.getItem("volume"));
  146. volume = (typeof volume === "number") ? volume : 20;
  147. _this.video.player.setVolume(volume);
  148. if (volume > 0) _this.video.player.unMute();
  149. },
  150. 'onStateChange': event => {
  151. if (event.data == 1) {
  152. let youtubeDuration = _this.video.player.getDuration();
  153. youtubeDuration -= _this.editing.song.skipDuration;
  154. if (_this.editing.song.duration > youtubeDuration) this.stopVideo();
  155. }
  156. }
  157. }
  158. });
  159. let volume = parseInt(localStorage.getItem("volume"));
  160. volume = (typeof volume === "number") ? volume : 20;
  161. $("#volumeSlider").val(volume);
  162. }
  163. }
  164. </script>
  165. <style lang='scss' scoped>
  166. body { font-family: 'Roboto', sans-serif; }
  167. .song-thumbnail {
  168. display: block;
  169. max-width: 50px;
  170. margin: 0 auto;
  171. }
  172. td { vertical-align: middle; }
  173. </style>