index.vue 16 KB

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