index.vue 15 KB

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