Edit.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <modal title='Edit Playlist'>
  3. <div slot='body'>
  4. <aside class='menu' v-if='playlist.songs && playlist.songs.length > 0'>
  5. <ul class='menu-list'>
  6. <li v-for='song in playlist.songs' track-by='$index'>
  7. <a :href='' target='_blank'>{{ song.title }}</a>
  8. <div class='controls'>
  9. <a href='#' @click='promoteSong(song.songId)'>
  10. <i class='material-icons' v-if='$index > 0'>keyboard_arrow_up</i>
  11. <i class='material-icons' style='opacity: 0' v-else>error</i>
  12. </a>
  13. <a href='#' @click='demoteSong(song.songId)'>
  14. <i class='material-icons' v-if='playlist.songs.length - 1 !== $index'>keyboard_arrow_down</i>
  15. <i class='material-icons' style='opacity: 0' v-else>error</i>
  16. </a>
  17. <a href='#' @click='removeSongFromPlaylist(song.songId)'><i class='material-icons'>delete</i></a>
  18. </div>
  19. </li>
  20. </ul>
  21. <br />
  22. </aside>
  23. <div class='control is-grouped'>
  24. <p class='control is-expanded'>
  25. <input class='input' type='text' placeholder='Search for Song to add' v-model='songQuery' autofocus @keyup.enter='searchForSongs()'>
  26. </p>
  27. <p class='control'>
  28. <a class='button is-info' @click='searchForSongs()' href="#">Search</a>
  29. </p>
  30. </div>
  31. <table class='table' v-if='songQueryResults.length > 0'>
  32. <tbody>
  33. <tr v-for='result in songQueryResults'>
  34. <td>
  35. <img :src='result.thumbnail' />
  36. </td>
  37. <td>{{ result.title }}</td>
  38. <td>
  39. <a class='button is-success' @click='addSongToPlaylist(result.id)' href='#'>
  40. Add
  41. </a>
  42. </td>
  43. </tr>
  44. </tbody>
  45. </table>
  46. <div class='control is-grouped'>
  47. <p class='control is-expanded'>
  48. <input class='input' type='text' placeholder='YouTube Playlist URL' v-model='importQuery' @keyup.enter="importPlaylist()">
  49. </p>
  50. <p class='control'>
  51. <a class='button is-info' @click='importPlaylist()' href="#">Import</a>
  52. </p>
  53. </div>
  54. <h5>Edit playlist details:</h5>
  55. <div class='control is-grouped'>
  56. <p class='control is-expanded'>
  57. <input class='input' type='text' placeholder='Playlist Display Name' v-model='playlist.displayName' @keyup.enter="renamePlaylist()">
  58. </p>
  59. <p class='control'>
  60. <a class='button is-info' @click='renamePlaylist()' href="#">Rename</a>
  61. </p>
  62. </div>
  63. </div>
  64. <div slot='footer'>
  65. <a class='button is-danger' @click='removePlaylist()' href="#">Remove Playlist</a>
  66. </div>
  67. </modal>
  68. </template>
  69. <script>
  70. import { Toast } from 'vue-roaster';
  71. import Modal from '../Modal.vue';
  72. import io from '../../../io';
  73. export default {
  74. components: { Modal },
  75. data() {
  76. return {
  77. playlist: {},
  78. songQueryResults: [],
  79. songQuery: '',
  80. importQuery: ''
  81. }
  82. },
  83. methods: {
  84. searchForSongs: function () {
  85. let _this = this;
  86. let query = _this.songQuery;
  87. if (query.indexOf('&index=') !== -1) {
  88. query = query.split('&index=');
  89. query.pop();
  90. query = query.join('');
  91. }
  92. if (query.indexOf('&list=') !== -1) {
  93. query = query.split('&list=');
  94. query.pop();
  95. query = query.join('');
  96. }
  97. _this.socket.emit('apis.searchYoutube', query, res => {
  98. if (res.status == 'success') {
  99. _this.songQueryResults = [];
  100. for (let i = 0; i < res.data.items.length; i++) {
  101. _this.songQueryResults.push({
  102. id: res.data.items[i].id.videoId,
  103. url: `https://www.youtube.com/watch?v=${this.id}`,
  104. title: res.data.items[i].snippet.title,
  105. thumbnail: res.data.items[i].snippet.thumbnails.default.url
  106. });
  107. }
  108. } else if (res.status === 'error') Toast.methods.addToast(res.message, 3000);
  109. });
  110. },
  111. addSongToPlaylist: function (id) {
  112. let _this = this;
  113. _this.socket.emit('playlists.addSongToPlaylist', id, _this.playlist._id, res => {
  114. Toast.methods.addToast(res.message, 4000);
  115. });
  116. },
  117. importPlaylist: function () {
  118. let _this = this;
  119. Toast.methods.addToast('Starting to import your playlist. This can take some time to do.', 4000);
  120. this.socket.emit('playlists.addSetToPlaylist', _this.importQuery, _this.playlist._id, res => {
  121. if (res.status === 'success') _this.playlist.songs = res.data;
  122. Toast.methods.addToast(res.message, 4000);
  123. });
  124. },
  125. removeSongFromPlaylist: function (id) {
  126. let _this = this;
  127. this.socket.emit('playlists.removeSongFromPlaylist', id, _this.playlist._id, res => {
  128. Toast.methods.addToast(res.message, 4000);
  129. });
  130. },
  131. renamePlaylist: function () {
  132. this.socket.emit('playlists.updateDisplayName', this.playlist._id, this.playlist.displayName, res => {
  133. Toast.methods.addToast(res.message, 4000);
  134. });
  135. },
  136. removePlaylist: function () {
  137. let _this = this;
  138. _this.socket.emit('playlists.remove', _this.playlist._id, res => {
  139. Toast.methods.addToast(res.message, 3000);
  140. if (res.status === 'success') {
  141. _this.$parent.modals.editPlaylist = !_this.$parent.modals.editPlaylist;
  142. }
  143. });
  144. },
  145. promoteSong: function (songId) {
  146. let _this = this;
  147. _this.socket.emit('playlists.moveSongToTop', _this.playlist._id, songId, res => {
  148. Toast.methods.addToast(res.message, 4000);
  149. });
  150. },
  151. demoteSong: function (songId) {
  152. let _this = this;
  153. _this.socket.emit('playlists.moveSongToBottom', _this.playlist._id, songId, res => {
  154. Toast.methods.addToast(res.message, 4000);
  155. });
  156. }
  157. },
  158. ready: function () {
  159. let _this = this;
  160. io.getSocket((socket) => {
  161. _this.socket = socket;
  162. _this.socket.emit('playlists.getPlaylist', _this.$parent.playlistBeingEdited, res => {
  163. if (res.status == 'success') _this.playlist = res.data; _this.playlist.oldId = res.data._id;
  164. });
  165. _this.socket.on('event:playlist.addSong', (data) => {
  166. if (_this.playlist._id === data.playlistId) _this.playlist.songs.push(data.song);
  167. });
  168. _this.socket.on('event:playlist.removeSong', (data) => {
  169. if (_this.playlist._id === data.playlistId) {
  170. _this.playlist.songs.forEach((song, index) => {
  171. if (song.songId === data.songId) _this.playlist.songs.splice(index, 1);
  172. });
  173. }
  174. });
  175. _this.socket.on('event:playlist.updateDisplayName', (data) => {
  176. if (_this.playlist._id === data.playlistId) _this.playlist.displayName = data.displayName;
  177. });
  178. _this.socket.on('event:playlist.moveSongToBottom', (data) => {
  179. if (_this.playlist._id === data.playlistId) {
  180. let songIndex;
  181. _this.playlist.songs.forEach((song, index) => {
  182. if (song.songId === data.songId) songIndex = index;
  183. });
  184. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  185. _this.playlist.songs.push(song);
  186. }
  187. });
  188. _this.socket.on('event:playlist.moveSongToTop', (data) => {
  189. if (_this.playlist._id === data.playlistId) {
  190. let songIndex;
  191. _this.playlist.songs.forEach((song, index) => {
  192. if (song.songId === data.songId) songIndex = index;
  193. });
  194. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  195. _this.playlist.songs.unshift(song);
  196. }
  197. });
  198. });
  199. },
  200. events: {
  201. closeModal: function() {
  202. this.$parent.modals.editPlaylist = !this.$parent.modals.editPlaylist;
  203. }
  204. }
  205. }
  206. </script>
  207. <style type='scss' scoped>
  208. .menu { padding: 0 20px; }
  209. .menu-list li {
  210. display: flex;
  211. justify-content: space-between;
  212. }
  213. .menu-list a:hover { color: #000 !important; }
  214. li a {
  215. display: flex;
  216. align-items: center;
  217. }
  218. .controls {
  219. display: flex;
  220. a {
  221. display: flex;
  222. align-items: center;
  223. }
  224. }
  225. .table {
  226. margin-bottom: 0;
  227. }
  228. h5 { padding: 20px 0; }
  229. </style>