index.vue 16 KB

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