index.vue 17 KB

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