Songs.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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-danger' @click='remove(song._id, index)'>Remove</button>
  32. </td>
  33. </tr>
  34. </tbody>
  35. </table>
  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. playerReady: false
  57. }
  58. }
  59. },
  60. computed: {
  61. filteredSongs: function () {
  62. return this.$eval('songs | filterBy searchQuery');
  63. }
  64. },
  65. methods: {
  66. settings: function (type) {
  67. let _this = this;
  68. switch(type) {
  69. case 'stop':
  70. _this.video.player.stopVideo();
  71. _this.video.paused = true;
  72. break;
  73. case 'pause':
  74. _this.video.player.pauseVideo();
  75. _this.video.paused = true;
  76. break;
  77. case 'play':
  78. _this.video.player.playVideo();
  79. _this.video.paused = false;
  80. break;
  81. case 'skipToLast10Secs':
  82. _this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
  83. break;
  84. }
  85. },
  86. changeVolume: function() {
  87. let local = this;
  88. let volume = $("#volumeSlider").val();
  89. localStorage.setItem("volume", volume);
  90. local.video.player.setVolume(volume);
  91. if (volume > 0) local.video.player.unMute();
  92. },
  93. toggleModal: function () {
  94. this.isEditActive = !this.isEditActive;
  95. this.settings('stop');
  96. },
  97. addTag: function (type) {
  98. if (type == 'genres') {
  99. let genre = $('#new-genre').val().toLowerCase().trim();
  100. if (this.editing.song.genres.indexOf(genre) !== -1) return Toast.methods.addToast('Genre already exists', 3000);
  101. if (genre) {
  102. this.editing.song.genres.push(genre);
  103. $('#new-genre').val('');
  104. } else Toast.methods.addToast('Genre cannot be empty', 3000);
  105. } else if (type == 'artists') {
  106. let artist = $('#new-artist').val();
  107. if (this.editing.song.artists.indexOf(artist) !== -1) return Toast.methods.addToast('Artist already exists', 3000);
  108. if ($('#new-artist').val() !== '') {
  109. this.editing.song.artists.push(artist);
  110. $('#new-artist').val('');
  111. } else Toast.methods.addToast('Artist cannot be empty', 3000);
  112. }
  113. },
  114. removeTag: function (type, index) {
  115. if (type == 'genres') this.editing.song.genres.splice(index, 1);
  116. else if (type == 'artists') this.editing.song.artists.splice(index, 1);
  117. },
  118. edit: function (song, index) {
  119. if (this.video.player) {
  120. this.video.player.loadVideoById(song._id, this.editing.song.skipDuration);
  121. let songCopy = {};
  122. for (let n in song) {
  123. songCopy[n] = song[n];
  124. }
  125. this.editing = { index, song: songCopy };
  126. this.isEditActive = true;
  127. }
  128. },
  129. save: function (song, close) {
  130. let _this = this;
  131. this.socket.emit('songs.update', song._id, song, function (res) {
  132. Toast.methods.addToast(res.message, 4000);
  133. if (res.status === 'success') {
  134. _this.songs.forEach((lSong) => {
  135. if (song._id === lSong._id) {
  136. for (let n in song) {
  137. lSong[n] = song[n];
  138. }
  139. }
  140. });
  141. }
  142. if (close) _this.toggleModal();
  143. });
  144. },
  145. remove: function (id, index) {
  146. this.socket.emit('songs.remove', id, res => {
  147. if (res.status == 'success') Toast.methods.addToast(res.message, 4000);
  148. else Toast.methods.addToast(res.message, 8000);
  149. });
  150. },
  151. init: function() {
  152. let _this = this;
  153. _this.socket.emit('songs.index', data => {
  154. _this.songs = data;
  155. });
  156. _this.socket.emit('apis.joinAdminRoom', 'songs', data => {});
  157. }
  158. },
  159. ready: function () {
  160. let _this = this;
  161. io.getSocket((socket) => {
  162. _this.socket = socket;
  163. if (_this.socket.connected) {
  164. _this.init();
  165. _this.socket.on('event:admin.song.added', song => {
  166. _this.songs.push(song);
  167. });
  168. _this.socket.on('event:admin.song.removed', songId => {
  169. _this.songs = _this.songs.filter(function(song) {
  170. return song._id !== songId;
  171. });
  172. });
  173. }
  174. io.onConnect(() => {
  175. _this.init();
  176. });
  177. });
  178. setInterval(() => {
  179. if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
  180. _this.video.paused = false;
  181. _this.video.player.stopVideo();
  182. }
  183. }, 200);
  184. this.video.player = new YT.Player('player', {
  185. height: 315,
  186. width: 560,
  187. videoId: this.editing.song._id,
  188. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  189. startSeconds: _this.editing.song.skipDuration,
  190. events: {
  191. 'onReady': () => {
  192. let volume = parseInt(localStorage.getItem("volume"));
  193. volume = (typeof volume === "number") ? volume : 20;
  194. _this.video.player.seekTo(_this.editing.song.skipDuration);
  195. _this.video.player.setVolume(volume);
  196. if (volume > 0) _this.video.player.unMute();
  197. _this.playerReady = true;
  198. },
  199. 'onStateChange': event => {
  200. if (event.data === 1) {
  201. _this.video.paused = false;
  202. let youtubeDuration = _this.video.player.getDuration();
  203. youtubeDuration -= _this.editing.song.skipDuration;
  204. if (_this.editing.song.duration > youtubeDuration) {
  205. this.video.player.stopVideo();
  206. _this.video.paused = true;
  207. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  208. } else if (_this.editing.song.duration <= 0) {
  209. this.video.player.stopVideo();
  210. _this.video.paused = true;
  211. Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
  212. }
  213. if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
  214. _this.video.player.seekTo(10);
  215. }
  216. } else if (event.data === 2) {
  217. this.video.paused = true;
  218. }
  219. }
  220. }
  221. });
  222. let volume = parseInt(localStorage.getItem("volume"));
  223. volume = (typeof volume === "number") ? volume : 20;
  224. $("#volumeSlider").val(volume);
  225. }
  226. }
  227. </script>
  228. <style lang='scss' scoped>
  229. body { font-family: 'Roboto', sans-serif; }
  230. .song-thumbnail {
  231. display: block;
  232. max-width: 50px;
  233. margin: 0 auto;
  234. }
  235. td { vertical-align: middle; }
  236. .is-primary:focus { background-color: #029ce3 !important; }
  237. </style>