Edit.vue 9.0 KB

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