EditPlaylist.vue 11 KB

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