AddSongToPlaylist.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <modal title='Add Song To Playlist'>
  3. <div slot='body'>
  4. <h4 class="songTitle">{{ $parent.currentSong.title }}</h4>
  5. <h5 class="songArtist">{{ $parent.currentSong.artists }}</h5>
  6. <aside class="menu">
  7. <p class="menu-label">
  8. Playlists
  9. </p>
  10. <ul class="menu-list">
  11. <li v-for='playlist in playlistsArr'>
  12. <div class='playlist'>
  13. <span class='icon is-small' @click='removeSongFromPlaylist(playlist._id)' v-if='playlists[playlist._id].hasSong'>
  14. <i class="material-icons">playlist_add_check</i>
  15. </span>
  16. <span class='icon' @click='addSongToPlaylist(playlist._id)' v-else>
  17. <i class="material-icons">playlist_add</i>
  18. </span>
  19. {{ playlist.displayName }}
  20. </div>
  21. </li>
  22. </ul>
  23. </aside>
  24. </div>
  25. </modal>
  26. </template>
  27. <script>
  28. import { Toast } from 'vue-roaster';
  29. import Modal from './Modal.vue';
  30. import io from '../../io';
  31. import auth from '../../auth';
  32. export default {
  33. data() {
  34. return {
  35. playlists: {},
  36. playlistsArr: [],
  37. songId: null,
  38. song: null
  39. }
  40. },
  41. methods: {
  42. addSongToPlaylist: function (playlistId) {
  43. let _this = this;
  44. this.socket.emit('playlists.addSongToPlaylist', this.$parent.currentSong.songId, playlistId, res => {
  45. Toast.methods.addToast(res.message, 4000);
  46. if (res.status === 'success') {
  47. _this.playlists[playlistId].songs.push(_this.song);
  48. }
  49. _this.recalculatePlaylists();
  50. //this.$parent.modals.addSongToPlaylist = false;
  51. });
  52. },
  53. removeSongFromPlaylist: function (playlistId) {
  54. let _this = this;
  55. this.socket.emit('playlists.removeSongFromPlaylist', _this.songId, playlistId, res => {
  56. Toast.methods.addToast(res.message, 4000);
  57. if (res.status === 'success') {
  58. _this.playlists[playlistId].songs.forEach((song, index) => {
  59. if (song.songId === _this.songId) _this.playlists[playlistId].songs.splice(index, 1);
  60. });
  61. }
  62. _this.recalculatePlaylists();
  63. //this.$parent.modals.addSongToPlaylist = false;
  64. });
  65. },
  66. recalculatePlaylists: function() {
  67. let _this = this;
  68. _this.playlistsArr = Object.values(_this.playlists).map((playlist) => {
  69. let hasSong = false;
  70. for (let i = 0; i < playlist.songs.length; i++) {
  71. if (playlist.songs[i].songId === _this.songId) {
  72. hasSong = true;
  73. }
  74. }
  75. playlist.hasSong = hasSong;
  76. _this.playlists[playlist._id] = playlist;
  77. return playlist;
  78. });
  79. }
  80. },
  81. ready: function () {
  82. let _this = this;
  83. this.songId = this.$parent.currentSong.songId;
  84. this.song = this.$parent.currentSong;
  85. io.getSocket((socket) => {
  86. _this.socket = socket;
  87. _this.socket.emit('playlists.indexForUser', res => {
  88. if (res.status === 'success') {
  89. res.data.forEach((playlist) => {
  90. _this.playlists[playlist._id] = playlist;
  91. });
  92. _this.recalculatePlaylists();
  93. }
  94. });
  95. });
  96. },
  97. events: {
  98. closeModal: function () {
  99. this.$parent.modals.addSongToPlaylist = !this.$parent.modals.addSongToPlaylist;
  100. }
  101. },
  102. components: { Modal }
  103. }
  104. </script>
  105. <style type='scss' scoped>
  106. .icon.is-small {
  107. margin-right: 10px !important;
  108. }
  109. .songTitle {
  110. font-size: 22px;
  111. padding: 0 10px;
  112. }
  113. .songArtist {
  114. font-size: 19px;
  115. font-weight: 200;
  116. padding: 0 10px;
  117. }
  118. .menu-label {
  119. font-size: 16px;
  120. }
  121. </style>