1
0

index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. <script setup lang="ts">
  2. import { useStore } from "vuex";
  3. import {
  4. defineAsyncComponent,
  5. ref,
  6. computed,
  7. onMounted,
  8. onBeforeUnmount
  9. } from "vue";
  10. import { Sortable } from "sortablejs-vue3";
  11. import Toast from "toasters";
  12. import { useModalState, useModalActions } from "@/vuex_helpers";
  13. import ws from "@/ws";
  14. import utils from "@/utils";
  15. const SongItem = defineAsyncComponent(
  16. () => import("@/components/SongItem.vue")
  17. );
  18. const Settings = defineAsyncComponent(() => import("./Tabs/Settings.vue"));
  19. const AddSongs = defineAsyncComponent(() => import("./Tabs/AddSongs.vue"));
  20. const ImportPlaylists = defineAsyncComponent(
  21. () => import("./Tabs/ImportPlaylists.vue")
  22. );
  23. const props = defineProps({
  24. modalUuid: { type: String, default: "" }
  25. });
  26. const store = useStore();
  27. const station = computed(() => store.state.station);
  28. const loggedIn = computed(() => store.state.user.auth.loggedIn);
  29. const userId = computed(() => store.state.user.auth.userId);
  30. const userRole = computed(() => store.state.user.auth.role);
  31. const { socket } = store.state.websockets;
  32. const drag = ref(false);
  33. const apiDomain = ref("");
  34. const gettingSongs = ref(false);
  35. const tabs = ref([]);
  36. const songItems = ref([]);
  37. const playlistSongs = computed({
  38. get: () => store.state.modals.editPlaylist[props.modalUuid].playlist.songs,
  39. set: value => {
  40. store.commit(
  41. `modals/editPlaylist/${props.modalUuid}/updatePlaylistSongs`,
  42. value
  43. );
  44. }
  45. });
  46. const modalState = useModalState("modals/editPlaylist/MODAL_UUID", {
  47. modalUuid: props.modalUuid
  48. });
  49. const playlistId = computed(() => modalState.playlistId);
  50. const tab = computed(() => modalState.tab);
  51. const playlist = computed(() => modalState.playlist);
  52. const { setPlaylist, clearPlaylist, addSong, removeSong, repositionedSong } =
  53. useModalActions(
  54. "modals/editPlaylist/MODAL_UUID",
  55. [
  56. "setPlaylist",
  57. "clearPlaylist",
  58. "addSong",
  59. "removeSong",
  60. "repositionedSong"
  61. ],
  62. {
  63. modalUuid: props.modalUuid
  64. }
  65. );
  66. const closeCurrentModal = () =>
  67. store.dispatch("modalVisibility/closeCurrentModal");
  68. const showTab = payload => {
  69. tabs.value[`${payload}-tab`].scrollIntoView({ block: "nearest" });
  70. store.dispatch(`modals/editPlaylist/${props.modalUuid}/showTab`, payload);
  71. };
  72. const isEditable = () =>
  73. (playlist.value.type === "user" ||
  74. playlist.value.type === "user-liked" ||
  75. playlist.value.type === "user-disliked") &&
  76. (userId.value === playlist.value.createdBy || userRole.value === "admin");
  77. const dragOptions = computed(() => ({
  78. animation: 200,
  79. group: "songs",
  80. disabled: !isEditable(),
  81. ghostClass: "draggable-list-ghost"
  82. }));
  83. const init = () => {
  84. gettingSongs.value = true;
  85. socket.dispatch("playlists.getPlaylist", playlistId.value, res => {
  86. if (res.status === "success") {
  87. setPlaylist(res.data.playlist);
  88. } else new Toast(res.message);
  89. gettingSongs.value = false;
  90. });
  91. };
  92. const isAdmin = () => userRole.value === "admin";
  93. const isOwner = () =>
  94. loggedIn.value && userId.value === playlist.value.createdBy;
  95. const repositionSong = ({ oldIndex, newIndex }) => {
  96. if (oldIndex === newIndex) return; // we only need to update when song is moved
  97. const song = playlistSongs.value[oldIndex];
  98. socket.dispatch(
  99. "playlists.repositionSong",
  100. playlist.value._id,
  101. {
  102. ...song,
  103. oldIndex,
  104. newIndex
  105. },
  106. res => {
  107. if (res.status !== "success")
  108. repositionedSong({
  109. ...song,
  110. newIndex: oldIndex,
  111. oldIndex: newIndex
  112. });
  113. }
  114. );
  115. };
  116. const moveSongToTop = (song, index) => {
  117. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  118. repositionSong({
  119. oldIndex: index,
  120. newIndex: 0
  121. });
  122. };
  123. const moveSongToBottom = (song, index) => {
  124. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  125. repositionSong({
  126. oldIndex: index,
  127. newIndex: playlistSongs.value.length
  128. });
  129. };
  130. const totalLength = () => {
  131. let length = 0;
  132. playlist.value.songs.forEach(song => {
  133. length += song.duration;
  134. });
  135. return utils.formatTimeLong(length);
  136. };
  137. // const shuffle = () => {
  138. // socket.dispatch("playlists.shuffle", playlist.value._id, res => {
  139. // new Toast(res.message);
  140. // if (res.status === "success") {
  141. // updatePlaylistSongs(
  142. // res.data.playlist.songs.sort((a, b) => a.position - b.position)
  143. // );
  144. // }
  145. // });
  146. // };
  147. const removeSongFromPlaylist = id =>
  148. socket.dispatch(
  149. "playlists.removeSongFromPlaylist",
  150. id,
  151. playlist.value._id,
  152. res => {
  153. new Toast(res.message);
  154. }
  155. );
  156. const removePlaylist = () => {
  157. if (isOwner()) {
  158. socket.dispatch("playlists.remove", playlist.value._id, res => {
  159. new Toast(res.message);
  160. if (res.status === "success") closeCurrentModal();
  161. });
  162. } else if (isAdmin()) {
  163. socket.dispatch("playlists.removeAdmin", playlist.value._id, res => {
  164. new Toast(res.message);
  165. if (res.status === "success") closeCurrentModal();
  166. });
  167. }
  168. };
  169. const downloadPlaylist = async () => {
  170. if (apiDomain.value === "")
  171. apiDomain.value = await lofig.get("backend.apiDomain");
  172. fetch(`${apiDomain.value}/export/playlist/${playlist.value._id}`, {
  173. credentials: "include"
  174. })
  175. .then(res => res.blob())
  176. .then(blob => {
  177. const url = window.URL.createObjectURL(blob);
  178. const a = document.createElement("a");
  179. a.style.display = "none";
  180. a.href = url;
  181. a.download = `musare-playlist-${
  182. playlist.value._id
  183. }-${new Date().toISOString()}.json`;
  184. document.body.appendChild(a);
  185. a.click();
  186. window.URL.revokeObjectURL(url);
  187. new Toast("Successfully downloaded playlist.");
  188. })
  189. .catch(() => new Toast("Failed to export and download playlist."));
  190. };
  191. const addSongToQueue = youtubeId => {
  192. socket.dispatch(
  193. "stations.addToQueue",
  194. station.value._id,
  195. youtubeId,
  196. data => {
  197. if (data.status !== "success")
  198. new Toast({
  199. content: `Error: ${data.message}`,
  200. timeout: 8000
  201. });
  202. else new Toast({ content: data.message, timeout: 4000 });
  203. }
  204. );
  205. };
  206. const clearAndRefillStationPlaylist = () => {
  207. socket.dispatch(
  208. "playlists.clearAndRefillStationPlaylist",
  209. playlist.value._id,
  210. data => {
  211. if (data.status !== "success")
  212. new Toast({
  213. content: `Error: ${data.message}`,
  214. timeout: 8000
  215. });
  216. else new Toast({ content: data.message, timeout: 4000 });
  217. }
  218. );
  219. };
  220. const clearAndRefillGenrePlaylist = () => {
  221. socket.dispatch(
  222. "playlists.clearAndRefillGenrePlaylist",
  223. playlist.value._id,
  224. data => {
  225. if (data.status !== "success")
  226. new Toast({
  227. content: `Error: ${data.message}`,
  228. timeout: 8000
  229. });
  230. else new Toast({ content: data.message, timeout: 4000 });
  231. }
  232. );
  233. };
  234. onMounted(() => {
  235. ws.onConnect(init);
  236. socket.on(
  237. "event:playlist.song.added",
  238. res => {
  239. if (playlist.value._id === res.data.playlistId)
  240. addSong(res.data.song);
  241. },
  242. { modalUuid: props.modalUuid }
  243. );
  244. socket.on(
  245. "event:playlist.song.removed",
  246. res => {
  247. if (playlist.value._id === res.data.playlistId) {
  248. // remove song from array of playlists
  249. removeSong(res.data.youtubeId);
  250. }
  251. },
  252. { modalUuid: props.modalUuid }
  253. );
  254. socket.on(
  255. "event:playlist.displayName.updated",
  256. res => {
  257. if (playlist.value._id === res.data.playlistId) {
  258. setPlaylist({
  259. displayName: res.data.displayName,
  260. ...playlist.value
  261. });
  262. }
  263. },
  264. { modalUuid: props.modalUuid }
  265. );
  266. socket.on(
  267. "event:playlist.song.repositioned",
  268. res => {
  269. if (playlist.value._id === res.data.playlistId) {
  270. const { song, playlistId } = res.data;
  271. if (playlist.value._id === playlistId) {
  272. repositionedSong(song);
  273. }
  274. }
  275. },
  276. { modalUuid: props.modalUuid }
  277. );
  278. });
  279. onBeforeUnmount(() => {
  280. clearPlaylist();
  281. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  282. store.unregisterModule(["modals", "editPlaylist", props.modalUuid]);
  283. });
  284. </script>
  285. <template>
  286. <modal
  287. :title="
  288. userId === playlist.createdBy ? 'Edit Playlist' : 'View Playlist'
  289. "
  290. :class="{
  291. 'edit-playlist-modal': true,
  292. 'view-only': !isEditable()
  293. }"
  294. :size="isEditable() ? 'wide' : null"
  295. :split="true"
  296. >
  297. <template #body>
  298. <div class="left-section">
  299. <div id="playlist-info-section" class="section">
  300. <h3>{{ playlist.displayName }}</h3>
  301. <h5>Song Count: {{ playlist.songs.length }}</h5>
  302. <h5>Duration: {{ totalLength() }}</h5>
  303. </div>
  304. <div class="tabs-container">
  305. <div class="tab-selection">
  306. <button
  307. class="button is-default"
  308. :class="{ selected: tab === 'settings' }"
  309. :ref="el => (tabs['settings-tab'] = el)"
  310. @click="showTab('settings')"
  311. v-if="
  312. userId === playlist.createdBy ||
  313. isEditable() ||
  314. (playlist.type === 'genre' && isAdmin())
  315. "
  316. >
  317. Settings
  318. </button>
  319. <button
  320. class="button is-default"
  321. :class="{ selected: tab === 'add-songs' }"
  322. :ref="el => (tabs['add-songs-tab'] = el)"
  323. @click="showTab('add-songs')"
  324. v-if="isEditable()"
  325. >
  326. Add Songs
  327. </button>
  328. <button
  329. class="button is-default"
  330. :class="{
  331. selected: tab === 'import-playlists'
  332. }"
  333. :ref="el => (tabs['import-playlists-tab'] = el)"
  334. @click="showTab('import-playlists')"
  335. v-if="isEditable()"
  336. >
  337. Import Playlists
  338. </button>
  339. </div>
  340. <settings
  341. class="tab"
  342. v-show="tab === 'settings'"
  343. v-if="
  344. userId === playlist.createdBy ||
  345. isEditable() ||
  346. (playlist.type === 'genre' && isAdmin())
  347. "
  348. :modal-uuid="modalUuid"
  349. />
  350. <add-songs
  351. class="tab"
  352. v-show="tab === 'add-songs'"
  353. v-if="isEditable()"
  354. :modal-uuid="modalUuid"
  355. />
  356. <import-playlists
  357. class="tab"
  358. v-show="tab === 'import-playlists'"
  359. v-if="isEditable()"
  360. :modal-uuid="modalUuid"
  361. />
  362. </div>
  363. </div>
  364. <div class="right-section">
  365. <div id="rearrange-songs-section" class="section">
  366. <div v-if="isEditable()">
  367. <h4 class="section-title">Rearrange Songs</h4>
  368. <p class="section-description">
  369. Drag and drop songs to change their order
  370. </p>
  371. <hr class="section-horizontal-rule" />
  372. </div>
  373. <aside class="menu">
  374. <sortable
  375. :component-data="{
  376. name: !drag ? 'draggable-list-transition' : null
  377. }"
  378. v-if="playlistSongs.length > 0"
  379. :list="playlistSongs"
  380. item-key="_id"
  381. :options="dragOptions"
  382. @start="drag = true"
  383. @end="drag = false"
  384. @update="repositionSong"
  385. >
  386. <template #item="{ element, index }">
  387. <div class="menu-list scrollable-list">
  388. <song-item
  389. :song="element"
  390. :class="{
  391. 'item-draggable': isEditable()
  392. }"
  393. :ref="
  394. el =>
  395. (songItems[
  396. `song-item-${index}`
  397. ] = el)
  398. "
  399. >
  400. <template #tippyActions>
  401. <i
  402. class="material-icons add-to-queue-icon"
  403. v-if="
  404. station &&
  405. station.requests &&
  406. station.requests.enabled &&
  407. (station.requests.access ===
  408. 'user' ||
  409. (station.requests
  410. .access ===
  411. 'owner' &&
  412. (userRole ===
  413. 'admin' ||
  414. station.owner ===
  415. userId)))
  416. "
  417. @click="
  418. addSongToQueue(
  419. element.youtubeId
  420. )
  421. "
  422. content="Add Song to Queue"
  423. v-tippy
  424. >queue</i
  425. >
  426. <quick-confirm
  427. v-if="
  428. userId ===
  429. playlist.createdBy ||
  430. isEditable()
  431. "
  432. placement="left"
  433. @confirm="
  434. removeSongFromPlaylist(
  435. element.youtubeId
  436. )
  437. "
  438. >
  439. <i
  440. class="material-icons delete-icon"
  441. content="Remove Song from Playlist"
  442. v-tippy
  443. >delete_forever</i
  444. >
  445. </quick-confirm>
  446. <i
  447. class="material-icons"
  448. v-if="isEditable() && index > 0"
  449. @click="
  450. moveSongToTop(
  451. element,
  452. index
  453. )
  454. "
  455. content="Move to top of Playlist"
  456. v-tippy
  457. >vertical_align_top</i
  458. >
  459. <i
  460. v-if="
  461. isEditable() &&
  462. playlistSongs.length - 1 !==
  463. index
  464. "
  465. @click="
  466. moveSongToBottom(
  467. element,
  468. index
  469. )
  470. "
  471. class="material-icons"
  472. content="Move to bottom of Playlist"
  473. v-tippy
  474. >vertical_align_bottom</i
  475. >
  476. </template>
  477. </song-item>
  478. </div>
  479. </template>
  480. </sortable>
  481. <p v-else-if="gettingSongs" class="nothing-here-text">
  482. Loading songs...
  483. </p>
  484. <p v-else class="nothing-here-text">
  485. This playlist doesn't have any songs.
  486. </p>
  487. </aside>
  488. </div>
  489. </div>
  490. </template>
  491. <template #footer>
  492. <button
  493. class="button is-default"
  494. v-if="isOwner() || isAdmin() || playlist.privacy === 'public'"
  495. @click="downloadPlaylist()"
  496. >
  497. Download Playlist
  498. </button>
  499. <div class="right">
  500. <quick-confirm
  501. v-if="playlist.type === 'station'"
  502. @confirm="clearAndRefillStationPlaylist()"
  503. >
  504. <a class="button is-danger">
  505. Clear and refill station playlist
  506. </a>
  507. </quick-confirm>
  508. <quick-confirm
  509. v-if="playlist.type === 'genre'"
  510. @confirm="clearAndRefillGenrePlaylist()"
  511. >
  512. <a class="button is-danger">
  513. Clear and refill genre playlist
  514. </a>
  515. </quick-confirm>
  516. <quick-confirm
  517. v-if="
  518. isEditable() &&
  519. !(
  520. playlist.type === 'user-liked' ||
  521. playlist.type === 'user-disliked'
  522. )
  523. "
  524. @confirm="removePlaylist()"
  525. >
  526. <a class="button is-danger"> Remove Playlist </a>
  527. </quick-confirm>
  528. </div>
  529. </template>
  530. </modal>
  531. </template>
  532. <style lang="less" scoped>
  533. .night-mode {
  534. .label,
  535. p,
  536. strong {
  537. color: var(--light-grey-2);
  538. }
  539. .edit-playlist-modal.modal .modal-card-body {
  540. .left-section {
  541. #playlist-info-section {
  542. background-color: var(--dark-grey-3) !important;
  543. border: 0;
  544. }
  545. .tabs-container {
  546. background-color: transparent !important;
  547. .tab-selection .button {
  548. background: var(--dark-grey);
  549. color: var(--white);
  550. }
  551. .tab {
  552. background-color: var(--dark-grey-3) !important;
  553. border: 0 !important;
  554. }
  555. }
  556. }
  557. .right-section .section {
  558. border-radius: @border-radius;
  559. }
  560. }
  561. }
  562. .menu-list li {
  563. display: flex;
  564. justify-content: space-between;
  565. &:not(:last-of-type) {
  566. margin-bottom: 10px;
  567. }
  568. a {
  569. display: flex;
  570. }
  571. }
  572. .controls {
  573. display: flex;
  574. a {
  575. display: flex;
  576. align-items: center;
  577. }
  578. }
  579. .tabs-container {
  580. .tab-selection {
  581. display: flex;
  582. margin: 24px 10px 0 10px;
  583. max-width: 100%;
  584. .button {
  585. border-radius: @border-radius @border-radius 0 0;
  586. border: 0;
  587. text-transform: uppercase;
  588. font-size: 14px;
  589. color: var(--dark-grey-3);
  590. background-color: var(--light-grey-2);
  591. flex-grow: 1;
  592. height: 32px;
  593. &:not(:first-of-type) {
  594. margin-left: 5px;
  595. }
  596. }
  597. .selected {
  598. background-color: var(--primary-color) !important;
  599. color: var(--white) !important;
  600. font-weight: 600;
  601. }
  602. }
  603. .tab {
  604. border: 1px solid var(--light-grey-3);
  605. border-radius: 0 0 @border-radius @border-radius;
  606. }
  607. }
  608. .edit-playlist-modal {
  609. &.view-only {
  610. height: auto !important;
  611. .left-section {
  612. flex-basis: 100% !important;
  613. }
  614. .right-section {
  615. max-height: unset !important;
  616. }
  617. :deep(.section) {
  618. max-width: 100% !important;
  619. }
  620. }
  621. .nothing-here-text {
  622. display: flex;
  623. align-items: center;
  624. justify-content: center;
  625. }
  626. .label {
  627. font-size: 1rem;
  628. font-weight: normal;
  629. }
  630. .input-with-button .button {
  631. width: 150px;
  632. }
  633. .left-section {
  634. #playlist-info-section {
  635. border: 1px solid var(--light-grey-3);
  636. border-radius: @border-radius;
  637. padding: 15px !important;
  638. h3 {
  639. font-weight: 600;
  640. font-size: 30px;
  641. }
  642. h5 {
  643. font-size: 18px;
  644. }
  645. h3,
  646. h5 {
  647. margin: 0;
  648. }
  649. }
  650. }
  651. .right-section {
  652. #rearrange-songs-section {
  653. .scrollable-list:not(:last-of-type) {
  654. margin-bottom: 10px;
  655. }
  656. }
  657. }
  658. }
  659. </style>