Songs.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. <button class='button is-primary' @click='edit(song, index)'>Edit</button>
  29. <button class='button is-danger' @click='remove(song._id, index)'>Remove</button>
  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. playerReady: false
  55. }
  56. }
  57. },
  58. methods: {
  59. settings: function (type) {
  60. let _this = this;
  61. switch(type) {
  62. case 'stop':
  63. _this.video.player.stopVideo();
  64. _this.video.paused = true;
  65. break;
  66. case 'pause':
  67. _this.video.player.pauseVideo();
  68. _this.video.paused = true;
  69. break;
  70. case 'play':
  71. _this.video.player.playVideo();
  72. _this.video.paused = false;
  73. break;
  74. case 'skipToLast10Secs':
  75. _this.video.player.seekTo((_this.editing.song.duration - 10) + _this.editing.song.skipDuration);
  76. break;
  77. }
  78. },
  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.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, this.editing.song.skipDuration);
  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, close) {
  123. let _this = this;
  124. this.socket.emit('songs.update', song._id, song, function (res) {
  125. Toast.methods.addToast(res.message, 4000);
  126. if (res.status === 'success') {
  127. _this.songs.forEach((lSong) => {
  128. if (song._id === lSong._id) {
  129. for (let n in song) {
  130. lSong[n] = song[n];
  131. }
  132. }
  133. });
  134. }
  135. if (close) {
  136. _this.toggleModal();
  137. }
  138. });
  139. },
  140. remove: function (id, index) {
  141. this.socket.emit('songs.remove', id, res => {
  142. if (res.status == 'success') Toast.methods.addToast(res.message, 4000);
  143. else Toast.methods.addToast(res.message, 8000);
  144. });
  145. },
  146. init: function() {
  147. let _this = this;
  148. _this.socket.emit('songs.index', data => {
  149. _this.songs = data;
  150. });
  151. _this.socket.emit('apis.joinAdminRoom', 'songs', data => {});
  152. }
  153. },
  154. ready: function () {
  155. let _this = this;
  156. io.getSocket((socket) => {
  157. _this.socket = socket;
  158. if (_this.socket.connected) {
  159. _this.init();
  160. _this.socket.on('event:admin.song.added', song => {
  161. _this.songs.push(song);
  162. });
  163. _this.socket.on('event:admin.song.removed', songId => {
  164. _this.songs = _this.songs.filter(function(song) {
  165. return song._id !== songId;
  166. });
  167. });
  168. }
  169. io.onConnect(() => {
  170. _this.init();
  171. });
  172. });
  173. setInterval(() => {
  174. if (_this.video.paused === false && _this.playerReady && _this.video.player.getCurrentTime() - _this.editing.song.skipDuration > _this.editing.song.duration) {
  175. _this.video.paused = false;
  176. _this.video.player.stopVideo();
  177. }
  178. }, 200);
  179. this.video.player = new YT.Player('player', {
  180. height: 315,
  181. width: 560,
  182. videoId: this.editing.song._id,
  183. playerVars: { controls: 0, iv_load_policy: 3, rel: 0, showinfo: 0 },
  184. startSeconds: _this.editing.song.skipDuration,
  185. events: {
  186. 'onReady': () => {
  187. let volume = parseInt(localStorage.getItem("volume"));
  188. volume = (typeof volume === "number") ? volume : 20;
  189. _this.video.player.seekTo(_this.editing.song.skipDuration);
  190. _this.video.player.setVolume(volume);
  191. if (volume > 0) _this.video.player.unMute();
  192. _this.playerReady = true;
  193. },
  194. 'onStateChange': event => {
  195. if (event.data === 1) {
  196. _this.video.paused = false;
  197. let youtubeDuration = _this.video.player.getDuration();
  198. youtubeDuration -= _this.editing.song.skipDuration;
  199. if (_this.editing.song.duration > youtubeDuration) {
  200. this.video.player.stopVideo();
  201. _this.video.paused = true;
  202. Toast.methods.addToast("Video can't play. Specified duration is bigger than the YouTube song duration.", 4000);
  203. } else if (_this.editing.song.duration <= 0) {
  204. this.video.player.stopVideo();
  205. _this.video.paused = true;
  206. Toast.methods.addToast("Video can't play. Specified duration has to be more than 0 seconds.", 4000);
  207. }
  208. if (_this.video.player.getCurrentTime() < _this.editing.song.skipDuration) {
  209. _this.video.player.seekTo(10);
  210. }
  211. } else if (event.data === 2) {
  212. this.video.paused = true;
  213. }
  214. }
  215. }
  216. });
  217. let volume = parseInt(localStorage.getItem("volume"));
  218. volume = (typeof volume === "number") ? volume : 20;
  219. $("#volumeSlider").val(volume);
  220. }
  221. }
  222. </script>
  223. <style lang='scss' scoped>
  224. body { font-family: 'Roboto', sans-serif; }
  225. .song-thumbnail {
  226. display: block;
  227. max-width: 50px;
  228. margin: 0 auto;
  229. }
  230. td { vertical-align: middle; }
  231. .is-primary:focus { background-color: #029ce3 !important; }
  232. </style>