index.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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. 'edit-playlist-modal-inner-container': true
  13. }"
  14. >
  15. <div id="first-column">
  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 id="tabs-container">
  22. <div id="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 id="second-column">
  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 #actions>
  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 v-else class="nothing-here-text">
  194. This playlist doesn't have any songs.
  195. </p>
  196. </aside>
  197. </div>
  198. </div>
  199. <!--
  200. <button
  201. class="button is-info"
  202. @click="shuffle()"
  203. v-if="playlist.isUserModifiable"
  204. >
  205. Shuffle
  206. </button>
  207. <h5>Edit playlist details:</h5>
  208. -->
  209. </div>
  210. </template>
  211. <template #footer>
  212. <a
  213. class="button is-default"
  214. v-if="
  215. userId === playlist.createdBy ||
  216. isEditable() ||
  217. playlist.privacy === 'public'
  218. "
  219. @click="downloadPlaylist()"
  220. href="#"
  221. >
  222. Download Playlist
  223. </a>
  224. <div class="right">
  225. <confirm
  226. v-if="playlist.type === 'station'"
  227. @confirm="clearAndRefillStationPlaylist()"
  228. >
  229. <a class="button is-danger">
  230. Clear and refill station playlist
  231. </a>
  232. </confirm>
  233. <confirm
  234. v-if="playlist.type === 'genre'"
  235. @confirm="clearAndRefillGenrePlaylist()"
  236. >
  237. <a class="button is-danger">
  238. Clear and refill genre playlist
  239. </a>
  240. </confirm>
  241. <confirm v-if="isEditable()" @confirm="removePlaylist()">
  242. <a class="button is-danger"> Remove Playlist </a>
  243. </confirm>
  244. </div>
  245. </template>
  246. </modal>
  247. </template>
  248. <script>
  249. import { mapState, mapGetters, mapActions } from "vuex";
  250. import draggable from "vuedraggable";
  251. import Toast from "toasters";
  252. import Confirm from "@/components/Confirm.vue";
  253. import Modal from "../../Modal.vue";
  254. import SongItem from "../../SongItem.vue";
  255. import Settings from "./Tabs/Settings.vue";
  256. import AddSongs from "./Tabs/AddSongs.vue";
  257. import ImportPlaylists from "./Tabs/ImportPlaylists.vue";
  258. import utils from "../../../../js/utils";
  259. export default {
  260. components: {
  261. Modal,
  262. draggable,
  263. Confirm,
  264. SongItem,
  265. Settings,
  266. AddSongs,
  267. ImportPlaylists
  268. },
  269. data() {
  270. return {
  271. utils,
  272. drag: false,
  273. apiDomain: ""
  274. };
  275. },
  276. computed: {
  277. ...mapState("station", {
  278. station: state => state.station
  279. }),
  280. ...mapState("user/playlists", {
  281. editing: state => state.editing
  282. }),
  283. ...mapState("modals/editPlaylist", {
  284. tab: state => state.tab,
  285. playlist: state => state.playlist
  286. }),
  287. playlistSongs: {
  288. get() {
  289. return this.$store.state.modals.editPlaylist.playlist.songs;
  290. },
  291. set(value) {
  292. this.$store.commit(
  293. "modals/editPlaylist/updatePlaylistSongs",
  294. value
  295. );
  296. }
  297. },
  298. ...mapState({
  299. userId: state => state.user.auth.userId,
  300. userRole: state => state.user.auth.role
  301. }),
  302. dragOptions() {
  303. return {
  304. animation: 200,
  305. group: "songs",
  306. disabled: !this.isEditable(),
  307. ghostClass: "draggable-list-ghost"
  308. };
  309. },
  310. ...mapGetters({
  311. socket: "websockets/getSocket"
  312. })
  313. },
  314. mounted() {
  315. this.socket.dispatch("playlists.getPlaylist", this.editing, res => {
  316. if (res.status === "success") {
  317. // this.playlist = res.data.playlist;
  318. // this.playlist.songs.sort((a, b) => a.position - b.position);
  319. this.setPlaylist(res.data.playlist);
  320. } else new Toast(res.message);
  321. });
  322. this.socket.on(
  323. "event:playlist.song.added",
  324. res => {
  325. if (this.playlist._id === res.data.playlistId)
  326. this.addSong(res.data.song);
  327. },
  328. { modal: "editPlaylist" }
  329. );
  330. this.socket.on(
  331. "event:playlist.song.removed",
  332. res => {
  333. if (this.playlist._id === res.data.playlistId) {
  334. // remove song from array of playlists
  335. this.removeSong(res.data.youtubeId);
  336. // // if this song is in search results, mark it available to add to the playlist again
  337. // this.search.songs.results.forEach((searchItem, index) => {
  338. // if (res.data.youtubeId === searchItem.id) {
  339. // this.search.songs.results[
  340. // index
  341. // ].isAddedToQueue = false;
  342. // }
  343. // });
  344. }
  345. },
  346. { modal: "editPlaylist" }
  347. );
  348. this.socket.on(
  349. "event:playlist.displayName.updated",
  350. res => {
  351. if (this.playlist._id === res.data.playlistId) {
  352. const playlist = {
  353. displayName: res.data.displayName,
  354. ...this.playlist
  355. };
  356. this.setPlaylist(playlist);
  357. }
  358. },
  359. { modal: "editPlaylist" }
  360. );
  361. this.socket.on(
  362. "event:playlist.song.repositioned",
  363. res => {
  364. if (this.playlist._id === res.data.playlistId) {
  365. const { song, playlistId } = res.data;
  366. if (this.playlist._id === playlistId) {
  367. this.repositionedSong(song);
  368. }
  369. }
  370. },
  371. { modal: "editPlaylist" }
  372. );
  373. },
  374. methods: {
  375. isEditable() {
  376. return (
  377. this.playlist.isUserModifiable &&
  378. (this.userId === this.playlist.createdBy ||
  379. this.userRole === "admin")
  380. );
  381. },
  382. isAdmin() {
  383. return this.userRole === "admin";
  384. },
  385. repositionSong({ moved }) {
  386. if (!moved) return; // we only need to update when song is moved
  387. this.socket.dispatch(
  388. "playlists.repositionSong",
  389. this.playlist._id,
  390. {
  391. ...moved.element,
  392. oldIndex: moved.oldIndex,
  393. newIndex: moved.newIndex
  394. },
  395. res => {
  396. if (res.status !== "success")
  397. this.repositionedSong({
  398. ...moved.element,
  399. newIndex: moved.oldIndex,
  400. oldIndex: moved.newIndex
  401. });
  402. }
  403. );
  404. },
  405. moveSongToTop(song, index) {
  406. this.$refs[`song-item-${index}`].$refs.songActions.tippy.hide();
  407. this.repositionSong({
  408. moved: {
  409. element: song,
  410. oldIndex: index,
  411. newIndex: 0
  412. }
  413. });
  414. },
  415. moveSongToBottom(song, index) {
  416. this.$refs[`song-item-${index}`].$refs.songActions.tippy.hide();
  417. this.repositionSong({
  418. moved: {
  419. element: song,
  420. oldIndex: index,
  421. newIndex: this.playlistSongs.length
  422. }
  423. });
  424. },
  425. totalLength() {
  426. let length = 0;
  427. this.playlist.songs.forEach(song => {
  428. length += song.duration;
  429. });
  430. return this.utils.formatTimeLong(length);
  431. },
  432. shuffle() {
  433. this.socket.dispatch(
  434. "playlists.shuffle",
  435. this.playlist._id,
  436. res => {
  437. new Toast(res.message);
  438. if (res.status === "success") {
  439. this.updatePlaylistSongs(
  440. res.data.playlist.songs.sort(
  441. (a, b) => a.position - b.position
  442. )
  443. );
  444. }
  445. }
  446. );
  447. },
  448. removeSongFromPlaylist(id) {
  449. if (this.playlist.displayName === "Liked Songs")
  450. return this.socket.dispatch("songs.unlike", id, res => {
  451. new Toast(res.message);
  452. });
  453. if (this.playlist.displayName === "Disliked Songs")
  454. return this.socket.dispatch("songs.undislike", id, res => {
  455. new Toast(res.message);
  456. });
  457. return this.socket.dispatch(
  458. "playlists.removeSongFromPlaylist",
  459. id,
  460. this.playlist._id,
  461. res => {
  462. new Toast(res.message);
  463. }
  464. );
  465. },
  466. removePlaylist() {
  467. this.socket.dispatch("playlists.remove", this.playlist._id, res => {
  468. new Toast(res.message);
  469. if (res.status === "success") this.closeModal("editPlaylist");
  470. });
  471. },
  472. async downloadPlaylist() {
  473. if (this.apiDomain === "")
  474. this.apiDomain = await lofig.get("apiDomain");
  475. fetch(
  476. `${this.apiDomain}/export/privatePlaylist/${this.playlist._id}`,
  477. { credentials: "include" }
  478. )
  479. .then(res => res.blob())
  480. .then(blob => {
  481. const url = window.URL.createObjectURL(blob);
  482. const a = document.createElement("a");
  483. a.style.display = "none";
  484. a.href = url;
  485. a.download = `musare-privateplaylist-${
  486. this.playlist._id
  487. }-${new Date().toISOString()}.json`;
  488. document.body.appendChild(a);
  489. a.click();
  490. window.URL.revokeObjectURL(url);
  491. new Toast("Successfully downloaded playlist.");
  492. })
  493. .catch(
  494. () => new Toast("Failed to export and download playlist.")
  495. );
  496. },
  497. addSongToQueue(youtubeId) {
  498. this.socket.dispatch(
  499. "stations.addToQueue",
  500. this.station._id,
  501. youtubeId,
  502. data => {
  503. if (data.status !== "success")
  504. new Toast({
  505. content: `Error: ${data.message}`,
  506. timeout: 8000
  507. });
  508. else new Toast({ content: data.message, timeout: 4000 });
  509. }
  510. );
  511. },
  512. clearAndRefillStationPlaylist() {
  513. this.socket.dispatch(
  514. "playlists.clearAndRefillStationPlaylist",
  515. this.playlist._id,
  516. data => {
  517. console.log(data.message);
  518. if (data.status !== "success")
  519. new Toast({
  520. content: `Error: ${data.message}`,
  521. timeout: 8000
  522. });
  523. else new Toast({ content: data.message, timeout: 4000 });
  524. }
  525. );
  526. },
  527. clearAndRefillGenrePlaylist() {
  528. this.socket.dispatch(
  529. "playlists.clearAndRefillGenrePlaylist",
  530. this.playlist._id,
  531. data => {
  532. if (data.status !== "success")
  533. new Toast({
  534. content: `Error: ${data.message}`,
  535. timeout: 8000
  536. });
  537. else new Toast({ content: data.message, timeout: 4000 });
  538. }
  539. );
  540. },
  541. ...mapActions({
  542. showTab(dispatch, payload) {
  543. this.$refs[`${payload}-tab`].scrollIntoView();
  544. return dispatch("modals/editPlaylist/showTab", payload);
  545. }
  546. }),
  547. ...mapActions("modals/editPlaylist", [
  548. "setPlaylist",
  549. "addSong",
  550. "removeSong",
  551. "repositionedSong"
  552. ]),
  553. ...mapActions("modalVisibility", ["openModal", "closeModal"])
  554. }
  555. };
  556. </script>
  557. <style lang="scss">
  558. .edit-playlist-modal {
  559. .modal-card {
  560. width: 1300px;
  561. .modal-card-body {
  562. padding: 16px;
  563. }
  564. }
  565. }
  566. </style>
  567. <style lang="scss" scoped>
  568. .night-mode {
  569. .label,
  570. p,
  571. strong {
  572. color: var(--light-grey-2);
  573. }
  574. }
  575. .menu-list li {
  576. display: flex;
  577. justify-content: space-between;
  578. &:not(:last-of-type) {
  579. margin-bottom: 10px;
  580. }
  581. a {
  582. display: flex;
  583. }
  584. }
  585. .controls {
  586. display: flex;
  587. a {
  588. display: flex;
  589. align-items: center;
  590. }
  591. }
  592. #tabs-container {
  593. // padding: 16px;
  594. #tab-selection {
  595. display: flex;
  596. // overflow-x: auto;
  597. margin: 24px 10px 0 10px;
  598. max-width: 100%;
  599. .button {
  600. border-radius: 5px 5px 0 0;
  601. border: 0;
  602. text-transform: uppercase;
  603. font-size: 14px;
  604. color: var(--dark-grey-3);
  605. background-color: var(--light-grey-2);
  606. flex-grow: 1;
  607. height: 32px;
  608. &:not(:first-of-type) {
  609. margin-left: 5px;
  610. }
  611. }
  612. .selected {
  613. background-color: var(--primary-color) !important;
  614. color: var(--white) !important;
  615. font-weight: 600;
  616. }
  617. }
  618. .tab {
  619. border: 1px solid var(--light-grey-3);
  620. // padding: 15px;
  621. border-radius: 0 0 5px 5px;
  622. }
  623. }
  624. .edit-playlist-modal {
  625. .edit-playlist-modal-inner-container {
  626. display: flex;
  627. flex-wrap: wrap;
  628. height: 100%;
  629. row-gap: 24px;
  630. &.view-only {
  631. height: auto !important;
  632. #first-column {
  633. flex-basis: 100%;
  634. }
  635. /deep/ .section {
  636. max-width: 100% !important;
  637. }
  638. }
  639. }
  640. .nothing-here-text {
  641. display: flex;
  642. align-items: center;
  643. justify-content: center;
  644. }
  645. /deep/ .section {
  646. padding: 15px !important;
  647. margin: 0 10px;
  648. max-width: 100%;
  649. display: flex;
  650. flex-direction: column;
  651. flex-grow: 1;
  652. }
  653. .label {
  654. font-size: 1rem;
  655. font-weight: normal;
  656. }
  657. .input-with-button .button {
  658. width: 150px;
  659. }
  660. #first-column {
  661. flex-basis: 550px;
  662. height: 100%;
  663. overflow-y: auto;
  664. flex-grow: 1;
  665. /deep/ .section {
  666. width: auto;
  667. }
  668. #playlist-info-section {
  669. border: 1px solid var(--light-grey-3);
  670. border-radius: 3px;
  671. padding: 15px !important;
  672. h3 {
  673. font-weight: 600;
  674. font-size: 30px;
  675. }
  676. h5 {
  677. font-size: 18px;
  678. }
  679. h3,
  680. h5 {
  681. margin: 0;
  682. }
  683. }
  684. }
  685. #second-column {
  686. flex-basis: 650px;
  687. height: 100%;
  688. overflow-y: auto;
  689. flex-grow: 1;
  690. #rearrange-songs-section {
  691. .scrollable-list:not(:last-of-type) {
  692. margin-bottom: 10px;
  693. }
  694. }
  695. }
  696. }
  697. </style>