Edit.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. function getHours() {return Math.floor(duration.asHours());}
  97. if (length <= 0) return '0 seconds';
  98. else return ((getHours() > 0 ? (getHours() > 1 ? (getHours() < 10 ? ('0' + getHours() + ' hours ') : (getHours() + ' hours ')) : ('0' + getHours() + ' 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 ')) : ''));
  99. },
  100. totalLength: function() {
  101. let length = 0;
  102. this.playlist.songs.forEach((song) => {
  103. length += song.duration;
  104. });
  105. return this.formatTime(length);
  106. },
  107. searchForSongs: function () {
  108. let _this = this;
  109. let query = _this.songQuery;
  110. if (query.indexOf('&index=') !== -1) {
  111. query = query.split('&index=');
  112. query.pop();
  113. query = query.join('');
  114. }
  115. if (query.indexOf('&list=') !== -1) {
  116. query = query.split('&list=');
  117. query.pop();
  118. query = query.join('');
  119. }
  120. _this.socket.emit('apis.searchYoutube', query, res => {
  121. if (res.status == 'success') {
  122. _this.songQueryResults = [];
  123. for (let i = 0; i < res.data.items.length; i++) {
  124. _this.songQueryResults.push({
  125. id: res.data.items[i].id.videoId,
  126. url: `https://www.youtube.com/watch?v=${this.id}`,
  127. title: res.data.items[i].snippet.title,
  128. thumbnail: res.data.items[i].snippet.thumbnails.default.url
  129. });
  130. }
  131. } else if (res.status === 'error') Toast.methods.addToast(res.message, 3000);
  132. });
  133. },
  134. addSongToPlaylist: function (id) {
  135. let _this = this;
  136. _this.socket.emit('playlists.addSongToPlaylist', id, _this.playlist._id, res => {
  137. Toast.methods.addToast(res.message, 4000);
  138. });
  139. },
  140. importPlaylist: function () {
  141. let _this = this;
  142. Toast.methods.addToast('Starting to import your playlist. This can take some time to do.', 4000);
  143. this.socket.emit('playlists.addSetToPlaylist', _this.importQuery, _this.playlist._id, res => {
  144. if (res.status === 'success') _this.playlist.songs = res.data;
  145. Toast.methods.addToast(res.message, 4000);
  146. });
  147. },
  148. removeSongFromPlaylist: function (id) {
  149. let _this = this;
  150. this.socket.emit('playlists.removeSongFromPlaylist', id, _this.playlist._id, res => {
  151. Toast.methods.addToast(res.message, 4000);
  152. });
  153. },
  154. renamePlaylist: function () {
  155. const displayName = this.playlist.displayName;
  156. if (!validation.isLength(displayName, 2, 32)) return Toast.methods.addToast('Display name must have between 2 and 32 characters.', 8000);
  157. if (!validation.regex.azAZ09_.test(displayName)) return Toast.methods.addToast('Invalid display name format. Allowed characters: a-z, A-Z, 0-9 and _.', 8000);
  158. this.socket.emit('playlists.updateDisplayName', this.playlist._id, this.playlist.displayName, res => {
  159. Toast.methods.addToast(res.message, 4000);
  160. });
  161. },
  162. removePlaylist: function () {
  163. let _this = this;
  164. _this.socket.emit('playlists.remove', _this.playlist._id, res => {
  165. Toast.methods.addToast(res.message, 3000);
  166. if (res.status === 'success') {
  167. _this.$parent.modals.editPlaylist = !_this.$parent.modals.editPlaylist;
  168. }
  169. });
  170. },
  171. promoteSong: function (songId) {
  172. let _this = this;
  173. _this.socket.emit('playlists.moveSongToTop', _this.playlist._id, songId, res => {
  174. Toast.methods.addToast(res.message, 4000);
  175. });
  176. },
  177. demoteSong: function (songId) {
  178. let _this = this;
  179. _this.socket.emit('playlists.moveSongToBottom', _this.playlist._id, songId, res => {
  180. Toast.methods.addToast(res.message, 4000);
  181. });
  182. }
  183. },
  184. ready: function () {
  185. let _this = this;
  186. io.getSocket((socket) => {
  187. _this.socket = socket;
  188. _this.socket.emit('playlists.getPlaylist', _this.$parent.playlistBeingEdited, res => {
  189. if (res.status === 'success') _this.playlist = res.data; _this.playlist.oldId = res.data._id;
  190. });
  191. _this.socket.on('event:playlist.addSong', data => {
  192. if (_this.playlist._id === data.playlistId) _this.playlist.songs.push(data.song);
  193. });
  194. _this.socket.on('event:playlist.removeSong', data => {
  195. if (_this.playlist._id === data.playlistId) {
  196. _this.playlist.songs.forEach((song, index) => {
  197. if (song.songId === data.songId) _this.playlist.songs.splice(index, 1);
  198. });
  199. }
  200. });
  201. _this.socket.on('event:playlist.updateDisplayName', data => {
  202. if (_this.playlist._id === data.playlistId) _this.playlist.displayName = data.displayName;
  203. });
  204. _this.socket.on('event:playlist.moveSongToBottom', data => {
  205. if (_this.playlist._id === data.playlistId) {
  206. let songIndex;
  207. _this.playlist.songs.forEach((song, index) => {
  208. if (song.songId === data.songId) songIndex = index;
  209. });
  210. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  211. _this.playlist.songs.push(song);
  212. }
  213. });
  214. _this.socket.on('event:playlist.moveSongToTop', (data) => {
  215. if (_this.playlist._id === data.playlistId) {
  216. let songIndex;
  217. _this.playlist.songs.forEach((song, index) => {
  218. if (song.songId === data.songId) songIndex = index;
  219. });
  220. let song = _this.playlist.songs.splice(songIndex, 1)[0];
  221. _this.playlist.songs.unshift(song);
  222. }
  223. });
  224. });
  225. },
  226. events: {
  227. closeModal: function() {
  228. this.$parent.modals.editPlaylist = !this.$parent.modals.editPlaylist;
  229. }
  230. }
  231. }
  232. </script>
  233. <style type='scss' scoped>
  234. .menu { padding: 0 20px; }
  235. .menu-list li {
  236. display: flex;
  237. justify-content: space-between;
  238. }
  239. .menu-list a:hover { color: #000 !important; }
  240. li a {
  241. display: flex;
  242. align-items: center;
  243. }
  244. .controls {
  245. display: flex;
  246. a {
  247. display: flex;
  248. align-items: center;
  249. }
  250. }
  251. .table {
  252. margin-bottom: 0;
  253. }
  254. h5 { padding: 20px 0; }
  255. </style>