Songs.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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.songId }}</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='modals.editSong'></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. position: 1,
  48. maxPosition: 1,
  49. songs: [],
  50. searchQuery: '',
  51. modals: { editSong: false },
  52. editing: {
  53. index: 0,
  54. song: {}
  55. },
  56. video: {
  57. player: null,
  58. paused: false,
  59. playerReady: false
  60. }
  61. }
  62. },
  63. computed: {
  64. filteredSongs: function () {
  65. return this.$eval('songs | filterBy searchQuery');
  66. }
  67. },
  68. watch: {
  69. 'modals.editSong': function (value) {
  70. if (!value) this.$broadcast('stopVideo');
  71. }
  72. },
  73. methods: {
  74. toggleModal: function () {
  75. this.modals.editSong = !this.modals.editSong;
  76. },
  77. edit: function (song, index) {
  78. this.$broadcast('editSong', song, index, 'songs');
  79. },
  80. remove: function (id) {
  81. this.socket.emit('songs.remove', id, res => {
  82. if (res.status == 'success') Toast.methods.addToast(res.message, 4000);
  83. else Toast.methods.addToast(res.message, 8000);
  84. });
  85. },
  86. getSet: function () {
  87. let _this = this;
  88. _this.socket.emit('songs.getSet', _this.position, data => {
  89. data.forEach(song => {
  90. _this.songs.push(song);
  91. });
  92. _this.position = _this.position + 1;
  93. if (_this.maxPosition > _this.position - 1) _this.getSet();
  94. });
  95. },
  96. init: function () {
  97. let _this = this;
  98. _this.songs = [];
  99. _this.socket.emit('songs.length', length => {
  100. _this.maxPosition = Math.round(length / 15);
  101. _this.getSet();
  102. });
  103. _this.socket.emit('apis.joinAdminRoom', 'songs', () => {});
  104. }
  105. },
  106. ready: function () {
  107. let _this = this;
  108. io.getSocket((socket) => {
  109. _this.socket = socket;
  110. if (_this.socket.connected) {
  111. _this.init();
  112. _this.socket.on('event:admin.song.added', song => {
  113. _this.songs.push(song);
  114. });
  115. _this.socket.on('event:admin.song.removed', songId => {
  116. _this.songs = _this.songs.filter(function(song) {
  117. return song._id !== songId;
  118. });
  119. });
  120. _this.socket.on('event:admin.song.updated', updatedSong => {
  121. for (let i = 0; i < _this.songs.length; i++) {
  122. let song = _this.songs[i];
  123. if (song._id === updatedSong._id) {
  124. _this.songs.$set(i, updatedSong);
  125. }
  126. }
  127. });
  128. }
  129. io.onConnect(() => {
  130. _this.init();
  131. });
  132. });
  133. }
  134. }
  135. </script>
  136. <style lang='scss' scoped>
  137. body { font-family: 'Roboto', sans-serif; }
  138. .song-thumbnail {
  139. display: block;
  140. max-width: 50px;
  141. margin: 0 auto;
  142. }
  143. td { vertical-align: middle; }
  144. .is-primary:focus { background-color: #029ce3 !important; }
  145. </style>