Edit.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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="searchSongQuery"
  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="directSongQuery"
  99. class="input"
  100. type="text"
  101. placeholder="Enter a YouTube id or URL directly"
  102. autofocus
  103. @keyup.enter="addSong()"
  104. />
  105. </p>
  106. <p class="control">
  107. <a class="button is-info" @click="addSong()" href="#"
  108. >Add</a
  109. >
  110. </p>
  111. </div>
  112. <div class="control is-grouped">
  113. <p class="control is-expanded">
  114. <input
  115. v-model="importQuery"
  116. class="input"
  117. type="text"
  118. placeholder="YouTube Playlist URL"
  119. @keyup.enter="importPlaylist(false)"
  120. />
  121. </p>
  122. <p class="control">
  123. <a
  124. class="button is-info"
  125. @click="importPlaylist(true)"
  126. href="#"
  127. >Import music</a
  128. >
  129. </p>
  130. <p class="control">
  131. <a
  132. class="button is-info"
  133. @click="importPlaylist(false)"
  134. href="#"
  135. >Import all</a
  136. >
  137. </p>
  138. </div>
  139. <h5>Edit playlist details:</h5>
  140. <div class="control is-grouped">
  141. <p class="control is-expanded">
  142. <input
  143. v-model="playlist.displayName"
  144. class="input"
  145. type="text"
  146. placeholder="Playlist Display Name"
  147. @keyup.enter="renamePlaylist()"
  148. />
  149. </p>
  150. <p class="control">
  151. <a class="button is-info" @click="renamePlaylist()" href="#"
  152. >Rename</a
  153. >
  154. </p>
  155. </div>
  156. </div>
  157. <div slot="footer">
  158. <a class="button is-danger" v-on:click="removePlaylist()" href="#"
  159. >Remove Playlist</a
  160. >
  161. </div>
  162. </modal>
  163. </template>
  164. <script>
  165. import { mapState, mapActions } from "vuex";
  166. import Toast from "toasters";
  167. import Modal from "../Modal.vue";
  168. import io from "../../../io";
  169. import validation from "../../../validation";
  170. export default {
  171. components: { Modal },
  172. data() {
  173. return {
  174. playlist: { songs: [] },
  175. songQueryResults: [],
  176. searchSongQuery: "",
  177. directSongQuery: "",
  178. importQuery: ""
  179. };
  180. },
  181. computed: mapState("user/playlists", {
  182. editing: state => state.editing
  183. }),
  184. mounted() {
  185. io.getSocket(socket => {
  186. this.socket = socket;
  187. this.socket.emit("playlists.getPlaylist", this.editing, res => {
  188. if (res.status === "success") this.playlist = res.data;
  189. this.playlist.oldId = res.data._id;
  190. });
  191. this.socket.on("event:playlist.addSong", data => {
  192. if (this.playlist._id === data.playlistId)
  193. this.playlist.songs.push(data.song);
  194. });
  195. this.socket.on("event:playlist.removeSong", data => {
  196. if (this.playlist._id === data.playlistId) {
  197. this.playlist.songs.forEach((song, index) => {
  198. if (song.songId === data.songId)
  199. this.playlist.songs.splice(index, 1);
  200. });
  201. }
  202. });
  203. this.socket.on("event:playlist.updateDisplayName", data => {
  204. if (this.playlist._id === data.playlistId)
  205. this.playlist.displayName = data.displayName;
  206. });
  207. this.socket.on("event:playlist.moveSongToBottom", data => {
  208. if (this.playlist._id === data.playlistId) {
  209. let songIndex;
  210. this.playlist.songs.forEach((song, index) => {
  211. if (song.songId === data.songId) songIndex = index;
  212. });
  213. const song = this.playlist.songs.splice(songIndex, 1)[0];
  214. this.playlist.songs.push(song);
  215. }
  216. });
  217. this.socket.on("event:playlist.moveSongToTop", data => {
  218. if (this.playlist._id === data.playlistId) {
  219. let songIndex;
  220. this.playlist.songs.forEach((song, index) => {
  221. if (song.songId === data.songId) songIndex = index;
  222. });
  223. const song = this.playlist.songs.splice(songIndex, 1)[0];
  224. this.playlist.songs.unshift(song);
  225. }
  226. });
  227. });
  228. },
  229. methods: {
  230. formatTime(duration) {
  231. if (duration <= 0) return "0 seconds";
  232. const hours = Math.floor(duration / (60 * 60));
  233. const formatHours = () => {
  234. if (hours > 0) {
  235. if (hours > 1) {
  236. if (hours < 10) return `0${hours} hours `;
  237. return `${hours} hours `;
  238. }
  239. return `0${hours} hour `;
  240. }
  241. return "";
  242. };
  243. const minutes = Math.floor((duration - hours * 60 * 60) / 60);
  244. const formatMinutes = () => {
  245. if (minutes > 0) {
  246. if (minutes > 1) {
  247. if (minutes < 10) return `0${minutes} minutes `;
  248. return `${minutes} minutes `;
  249. }
  250. return `0${minutes} minute `;
  251. }
  252. return "";
  253. };
  254. const seconds = Math.floor(
  255. duration - hours * 60 * 60 - minutes * 60
  256. );
  257. const formatSeconds = () => {
  258. if (seconds > 0) {
  259. if (seconds > 1) {
  260. if (seconds < 10) return `0${seconds} seconds `;
  261. return `${seconds} seconds `;
  262. }
  263. return `0${seconds} second `;
  264. }
  265. return "";
  266. };
  267. return formatHours() + formatMinutes() + formatSeconds();
  268. },
  269. totalLength() {
  270. let length = 0;
  271. this.playlist.songs.forEach(song => {
  272. length += song.duration;
  273. });
  274. return this.formatTime(length);
  275. },
  276. searchForSongs() {
  277. let query = this.songQuery;
  278. if (query.indexOf("&index=") !== -1) {
  279. query = query.split("&index=");
  280. query.pop();
  281. query = query.join("");
  282. }
  283. if (query.indexOf("&list=") !== -1) {
  284. query = query.split("&list=");
  285. query.pop();
  286. query = query.join("");
  287. }
  288. this.socket.emit("apis.searchYoutube", query, res => {
  289. if (res.status === "success") {
  290. this.songQueryResults = [];
  291. for (let i = 0; i < res.data.items.length; i += 1) {
  292. this.songQueryResults.push({
  293. id: res.data.items[i].id.videoId,
  294. url: `https://www.youtube.com/watch?v=${this.id}`,
  295. title: res.data.items[i].snippet.title,
  296. thumbnail:
  297. res.data.items[i].snippet.thumbnails.default.url
  298. });
  299. }
  300. } else if (res.status === "error")
  301. new Toast({ content: res.message, timeout: 3000 });
  302. });
  303. },
  304. addSongToPlaylist(id) {
  305. this.socket.emit(
  306. "playlists.addSongToPlaylist",
  307. id,
  308. this.playlist._id,
  309. res => {
  310. new Toast({ content: res.message, timeout: 4000 });
  311. }
  312. );
  313. },
  314. /* eslint-disable prefer-destructuring */
  315. addSong() {
  316. let id = "";
  317. if (this.directSongQuery.length === 11) id = this.directSongQuery;
  318. else {
  319. const match = this.directSongQuery.match("v=([0-9A-Za-z_-]+)");
  320. if (match.length > 0) id = match[1];
  321. }
  322. this.addSongToPlaylist(id);
  323. },
  324. /* eslint-enable prefer-destructuring */
  325. importPlaylist(musicOnly) {
  326. new Toast({
  327. content:
  328. "Starting to import your playlist. This can take some time to do.",
  329. timeout: 4000
  330. });
  331. this.socket.emit(
  332. "playlists.addSetToPlaylist",
  333. this.importQuery,
  334. this.playlist._id,
  335. musicOnly,
  336. res => {
  337. new Toast({ content: res.message, timeout: 4000 });
  338. if (res.status === "success") {
  339. new Toast({
  340. content: `Successfully added ${res.stats.songsAddedSuccessfully} songs. Failed to add ${res.stats.songsFailedToAdd} songs.`,
  341. timeout: 4000
  342. });
  343. if (musicOnly) {
  344. new Toast({
  345. content: `${res.stats.songsInPlaylistTotal} of the ${res.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
  346. timeout: 4000
  347. });
  348. }
  349. }
  350. }
  351. );
  352. },
  353. removeSongFromPlaylist(id) {
  354. this.socket.emit(
  355. "playlists.removeSongFromPlaylist",
  356. id,
  357. this.playlist._id,
  358. res => {
  359. new Toast({ content: res.message, timeout: 4000 });
  360. }
  361. );
  362. },
  363. renamePlaylist() {
  364. const { displayName } = this.playlist;
  365. if (!validation.isLength(displayName, 2, 32))
  366. return new Toast({
  367. content:
  368. "Display name must have between 2 and 32 characters.",
  369. timeout: 8000
  370. });
  371. if (!validation.regex.ascii.test(displayName))
  372. return new Toast({
  373. content:
  374. "Invalid display name format. Only ASCII characters are allowed.",
  375. timeout: 8000
  376. });
  377. return this.socket.emit(
  378. "playlists.updateDisplayName",
  379. this.playlist._id,
  380. this.playlist.displayName,
  381. res => {
  382. new Toast({ content: res.message, timeout: 4000 });
  383. }
  384. );
  385. },
  386. removePlaylist() {
  387. this.socket.emit("playlists.remove", this.playlist._id, res => {
  388. new Toast({ content: res.message, timeout: 3000 });
  389. if (res.status === "success") {
  390. this.closeModal({
  391. sector: "station",
  392. modal: "editPlaylist"
  393. });
  394. }
  395. });
  396. },
  397. promoteSong(songId) {
  398. this.socket.emit(
  399. "playlists.moveSongToTop",
  400. this.playlist._id,
  401. songId,
  402. res => {
  403. new Toast({ content: res.message, timeout: 4000 });
  404. }
  405. );
  406. },
  407. demoteSong(songId) {
  408. this.socket.emit(
  409. "playlists.moveSongToBottom",
  410. this.playlist._id,
  411. songId,
  412. res => {
  413. new Toast({ content: res.message, timeout: 4000 });
  414. }
  415. );
  416. },
  417. ...mapActions("modals", ["closeModal"])
  418. }
  419. };
  420. </script>
  421. <style lang="scss" scoped>
  422. @import "styles/global.scss";
  423. .menu {
  424. padding: 0 20px;
  425. }
  426. .menu-list li {
  427. display: flex;
  428. justify-content: space-between;
  429. }
  430. .menu-list a:hover {
  431. color: $black !important;
  432. }
  433. li a {
  434. display: flex;
  435. align-items: center;
  436. }
  437. .controls {
  438. display: flex;
  439. a {
  440. display: flex;
  441. align-items: center;
  442. }
  443. }
  444. .table {
  445. margin-bottom: 0;
  446. }
  447. h5 {
  448. padding: 20px 0;
  449. }
  450. </style>