index.vue 16 KB

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