Edit.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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">
  8. Total Length
  9. </p>
  10. <p class="title">
  11. {{ totalLength() }}
  12. </p>
  13. </div>
  14. </div>
  15. </nav>
  16. <hr />
  17. <aside class="menu">
  18. <ul class="menu-list">
  19. <li v-for="(song, index) in playlist.songs" :key="index">
  20. <a href="#" target="_blank">{{ song.title }}</a>
  21. <div class="controls">
  22. <a href="#" v-on:click="promoteSong(song.songId)">
  23. <i class="material-icons" v-if="index > 0"
  24. >keyboard_arrow_up</i
  25. >
  26. <i
  27. v-else
  28. class="material-icons"
  29. style="opacity: 0"
  30. >error</i
  31. >
  32. </a>
  33. <a href="#" v-on:click="demoteSong(song.songId)">
  34. <i
  35. v-if="playlist.songs.length - 1 !== index"
  36. class="material-icons"
  37. >keyboard_arrow_down</i
  38. >
  39. <i
  40. v-else
  41. class="material-icons"
  42. style="opacity: 0"
  43. >error</i
  44. >
  45. </a>
  46. <a
  47. href="#"
  48. v-on:click="removeSongFromPlaylist(song.songId)"
  49. >
  50. <i class="material-icons">delete</i>
  51. </a>
  52. </div>
  53. </li>
  54. </ul>
  55. <br />
  56. </aside>
  57. <div class="control is-grouped">
  58. <p class="control is-expanded">
  59. <input
  60. v-model="songQuery"
  61. class="input"
  62. type="text"
  63. placeholder="Search for Song to add"
  64. autofocus
  65. @keyup.enter="searchForSongs()"
  66. />
  67. </p>
  68. <p class="control">
  69. <a class="button is-info" @click="searchForSongs()" href="#"
  70. >Search</a
  71. >
  72. </p>
  73. </div>
  74. <table v-if="songQueryResults.length > 0" class="table">
  75. <tbody>
  76. <tr
  77. v-for="(result, index) in songQueryResults"
  78. :key="index"
  79. >
  80. <td>
  81. <img :src="result.thumbnail" />
  82. </td>
  83. <td>{{ result.title }}</td>
  84. <td>
  85. <a
  86. class="button is-success"
  87. href="#"
  88. @click="addSongToPlaylist(result.id)"
  89. >Add</a
  90. >
  91. </td>
  92. </tr>
  93. </tbody>
  94. </table>
  95. <div class="control is-grouped">
  96. <p class="control is-expanded">
  97. <input
  98. v-model="importQuery"
  99. class="input"
  100. type="text"
  101. placeholder="YouTube Playlist URL"
  102. @keyup.enter="importPlaylist()"
  103. />
  104. </p>
  105. <p class="control">
  106. <a
  107. class="button is-info"
  108. @click="importPlaylist(true)"
  109. href="#"
  110. >Import music</a
  111. >
  112. </p>
  113. <p class="control">
  114. <a
  115. class="button is-info"
  116. @click="importPlaylist(false)"
  117. href="#"
  118. >Import all</a
  119. >
  120. </p>
  121. </div>
  122. <h5>Edit playlist details:</h5>
  123. <div class="control is-grouped">
  124. <p class="control is-expanded">
  125. <input
  126. v-model="playlist.displayName"
  127. class="input"
  128. type="text"
  129. placeholder="Playlist Display Name"
  130. @keyup.enter="renamePlaylist()"
  131. />
  132. </p>
  133. <p class="control">
  134. <a class="button is-info" @click="renamePlaylist()" href="#"
  135. >Rename</a
  136. >
  137. </p>
  138. </div>
  139. </div>
  140. <div slot="footer">
  141. <a class="button is-danger" v-on:click="removePlaylist()" href="#"
  142. >Remove Playlist</a
  143. >
  144. </div>
  145. </modal>
  146. </template>
  147. <script>
  148. import { mapState, mapActions } from "vuex";
  149. import Toast from "toasters";
  150. import Modal from "../Modal.vue";
  151. import io from "../../../io";
  152. import validation from "../../../validation";
  153. export default {
  154. components: { Modal },
  155. data() {
  156. return {
  157. playlist: { songs: [] },
  158. songQueryResults: [],
  159. songQuery: "",
  160. importQuery: ""
  161. };
  162. },
  163. computed: mapState("user/playlists", {
  164. editing: state => state.editing
  165. }),
  166. mounted() {
  167. io.getSocket(socket => {
  168. this.socket = socket;
  169. this.socket.emit("playlists.getPlaylist", this.editing, res => {
  170. if (res.status === "success") this.playlist = res.data;
  171. this.playlist.oldId = res.data._id;
  172. });
  173. this.socket.on("event:playlist.addSong", data => {
  174. if (this.playlist._id === data.playlistId)
  175. this.playlist.songs.push(data.song);
  176. });
  177. this.socket.on("event:playlist.removeSong", data => {
  178. if (this.playlist._id === data.playlistId) {
  179. this.playlist.songs.forEach((song, index) => {
  180. if (song.songId === data.songId)
  181. this.playlist.songs.splice(index, 1);
  182. });
  183. }
  184. });
  185. this.socket.on("event:playlist.updateDisplayName", data => {
  186. if (this.playlist._id === data.playlistId)
  187. this.playlist.displayName = data.displayName;
  188. });
  189. this.socket.on("event:playlist.moveSongToBottom", data => {
  190. if (this.playlist._id === data.playlistId) {
  191. let songIndex;
  192. this.playlist.songs.forEach((song, index) => {
  193. if (song.songId === data.songId) songIndex = index;
  194. });
  195. const song = this.playlist.songs.splice(songIndex, 1)[0];
  196. this.playlist.songs.push(song);
  197. }
  198. });
  199. this.socket.on("event:playlist.moveSongToTop", data => {
  200. if (this.playlist._id === data.playlistId) {
  201. let songIndex;
  202. this.playlist.songs.forEach((song, index) => {
  203. if (song.songId === data.songId) songIndex = index;
  204. });
  205. const song = this.playlist.songs.splice(songIndex, 1)[0];
  206. this.playlist.songs.unshift(song);
  207. }
  208. });
  209. });
  210. },
  211. methods: {
  212. formatTime(duration) {
  213. if (duration <= 0) return "0 seconds";
  214. const hours = Math.floor(duration / (60 * 60));
  215. const formatHours = () => {
  216. if (hours > 0) {
  217. if (hours > 1) {
  218. if (hours < 10) return `0${hours} hours `;
  219. return `${hours} hours `;
  220. }
  221. return `0${hours} hour `;
  222. }
  223. return "";
  224. };
  225. const minutes = Math.floor((duration - hours * 60 * 60) / 60);
  226. const formatMinutes = () => {
  227. if (minutes > 0) {
  228. if (minutes > 1) {
  229. if (minutes < 10) return `0${minutes} minutes `;
  230. return `${minutes} minutes `;
  231. }
  232. return `0${minutes} minute `;
  233. }
  234. return "";
  235. };
  236. const seconds = Math.floor(
  237. duration - hours * 60 * 60 - minutes * 60
  238. );
  239. const formatSeconds = () => {
  240. if (seconds > 0) {
  241. if (seconds > 1) {
  242. if (seconds < 10) return `0${seconds} seconds `;
  243. return `${seconds} seconds `;
  244. }
  245. return `0${seconds} second `;
  246. }
  247. return "";
  248. };
  249. return formatHours() + formatMinutes() + formatSeconds();
  250. },
  251. totalLength() {
  252. let length = 0;
  253. this.playlist.songs.forEach(song => {
  254. length += song.duration;
  255. });
  256. return this.formatTime(length);
  257. },
  258. searchForSongs() {
  259. let query = this.songQuery;
  260. if (query.indexOf("&index=") !== -1) {
  261. query = query.split("&index=");
  262. query.pop();
  263. query = query.join("");
  264. }
  265. if (query.indexOf("&list=") !== -1) {
  266. query = query.split("&list=");
  267. query.pop();
  268. query = query.join("");
  269. }
  270. this.socket.emit("apis.searchYoutube", query, res => {
  271. if (res.status === "success") {
  272. this.songQueryResults = [];
  273. for (let i = 0; i < res.data.items.length; i += 1) {
  274. this.songQueryResults.push({
  275. id: res.data.items[i].id.videoId,
  276. url: `https://www.youtube.com/watch?v=${this.id}`,
  277. title: res.data.items[i].snippet.title,
  278. thumbnail:
  279. res.data.items[i].snippet.thumbnails.default.url
  280. });
  281. }
  282. } else if (res.status === "error")
  283. new Toast({ content: res.message, timeout: 3000 });
  284. });
  285. },
  286. addSongToPlaylist(id) {
  287. this.socket.emit(
  288. "playlists.addSongToPlaylist",
  289. id,
  290. this.playlist._id,
  291. res => {
  292. new Toast({ content: res.message, timeout: 4000 });
  293. }
  294. );
  295. },
  296. importPlaylist(musicOnly) {
  297. new Toast({
  298. content:
  299. "Starting to import your playlist. This can take some time to do.",
  300. timeout: 4000
  301. });
  302. this.socket.emit(
  303. "playlists.addSetToPlaylist",
  304. this.importQuery,
  305. this.playlist._id,
  306. musicOnly,
  307. res => {
  308. new Toast({ content: res.message, timeout: 4000 });
  309. if (res.status === "success") {
  310. new Toast({
  311. content: `Successfully added ${res.stats.songsAddedSuccessfully} songs. Failed to add ${res.stats.songsFailedToAdd} songs.`,
  312. timeout: 4000
  313. });
  314. if (musicOnly) {
  315. new Toast({
  316. content: `${res.stats.songsInPlaylistTotal} of the ${res.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
  317. timeout: 4000
  318. });
  319. }
  320. }
  321. }
  322. );
  323. },
  324. removeSongFromPlaylist(id) {
  325. this.socket.emit(
  326. "playlists.removeSongFromPlaylist",
  327. id,
  328. this.playlist._id,
  329. res => {
  330. new Toast({ content: res.message, timeout: 4000 });
  331. }
  332. );
  333. },
  334. renamePlaylist() {
  335. const { displayName } = this.playlist;
  336. if (!validation.isLength(displayName, 2, 32))
  337. return new Toast({
  338. content:
  339. "Display name must have between 2 and 32 characters.",
  340. timeout: 8000
  341. });
  342. if (!validation.regex.ascii.test(displayName))
  343. return new Toast({
  344. content:
  345. "Invalid display name format. Only ASCII characters are allowed.",
  346. timeout: 8000
  347. });
  348. return this.socket.emit(
  349. "playlists.updateDisplayName",
  350. this.playlist._id,
  351. this.playlist.displayName,
  352. res => {
  353. new Toast({ content: res.message, timeout: 4000 });
  354. }
  355. );
  356. },
  357. removePlaylist() {
  358. this.socket.emit("playlists.remove", this.playlist._id, res => {
  359. new Toast({ content: res.message, timeout: 3000 });
  360. if (res.status === "success") {
  361. this.closeModal({
  362. sector: "station",
  363. modal: "editPlaylist"
  364. });
  365. }
  366. });
  367. },
  368. promoteSong(songId) {
  369. this.socket.emit(
  370. "playlists.moveSongToTop",
  371. this.playlist._id,
  372. songId,
  373. res => {
  374. new Toast({ content: res.message, timeout: 4000 });
  375. }
  376. );
  377. },
  378. demoteSong(songId) {
  379. this.socket.emit(
  380. "playlists.moveSongToBottom",
  381. this.playlist._id,
  382. songId,
  383. res => {
  384. new Toast({ content: res.message, timeout: 4000 });
  385. }
  386. );
  387. },
  388. ...mapActions("modals", ["closeModal"])
  389. }
  390. };
  391. </script>
  392. <style lang="scss" scoped>
  393. @import "styles/global.scss";
  394. .menu {
  395. padding: 0 20px;
  396. }
  397. .menu-list li {
  398. display: flex;
  399. justify-content: space-between;
  400. }
  401. .menu-list a:hover {
  402. color: $black !important;
  403. }
  404. li a {
  405. display: flex;
  406. align-items: center;
  407. }
  408. .controls {
  409. display: flex;
  410. a {
  411. display: flex;
  412. align-items: center;
  413. }
  414. }
  415. .table {
  416. margin-bottom: 0;
  417. }
  418. h5 {
  419. padding: 20px 0;
  420. }
  421. </style>