index.vue 15 KB

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