index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. :size="isEditable() ? 'wide' : null"
  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. <quick-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. </quick-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. <quick-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. </quick-confirm>
  213. <quick-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. </quick-confirm>
  221. <quick-confirm
  222. v-if="
  223. isEditable() &&
  224. !(
  225. playlist.type === 'user-liked' ||
  226. playlist.type === 'user-disliked'
  227. )
  228. "
  229. @confirm="removePlaylist()"
  230. >
  231. <a class="button is-danger"> Remove Playlist </a>
  232. </quick-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 QuickConfirm from "@/components/QuickConfirm.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. QuickConfirm,
  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. shuffle() {
  428. this.socket.dispatch(
  429. "playlists.shuffle",
  430. this.playlist._id,
  431. res => {
  432. new Toast(res.message);
  433. if (res.status === "success") {
  434. this.updatePlaylistSongs(
  435. res.data.playlist.songs.sort(
  436. (a, b) => a.position - b.position
  437. )
  438. );
  439. }
  440. }
  441. );
  442. },
  443. removeSongFromPlaylist(id) {
  444. return this.socket.dispatch(
  445. "playlists.removeSongFromPlaylist",
  446. id,
  447. this.playlist._id,
  448. res => {
  449. new Toast(res.message);
  450. }
  451. );
  452. },
  453. removePlaylist() {
  454. if (this.isOwner()) {
  455. this.socket.dispatch(
  456. "playlists.remove",
  457. this.playlist._id,
  458. res => {
  459. new Toast(res.message);
  460. if (res.status === "success")
  461. this.closeModal("editPlaylist");
  462. }
  463. );
  464. } else if (this.isAdmin()) {
  465. this.socket.dispatch(
  466. "playlists.removeAdmin",
  467. this.playlist._id,
  468. res => {
  469. new Toast(res.message);
  470. if (res.status === "success")
  471. this.closeModal("editPlaylist");
  472. }
  473. );
  474. }
  475. },
  476. async downloadPlaylist() {
  477. if (this.apiDomain === "")
  478. this.apiDomain = await lofig.get("backend.apiDomain");
  479. fetch(`${this.apiDomain}/export/playlist/${this.playlist._id}`, {
  480. credentials: "include"
  481. })
  482. .then(res => res.blob())
  483. .then(blob => {
  484. const url = window.URL.createObjectURL(blob);
  485. const a = document.createElement("a");
  486. a.style.display = "none";
  487. a.href = url;
  488. a.download = `musare-playlist-${
  489. this.playlist._id
  490. }-${new Date().toISOString()}.json`;
  491. document.body.appendChild(a);
  492. a.click();
  493. window.URL.revokeObjectURL(url);
  494. new Toast("Successfully downloaded playlist.");
  495. })
  496. .catch(
  497. () => new Toast("Failed to export and download playlist.")
  498. );
  499. },
  500. addSongToQueue(youtubeId) {
  501. this.socket.dispatch(
  502. "stations.addToQueue",
  503. this.station._id,
  504. youtubeId,
  505. data => {
  506. if (data.status !== "success")
  507. new Toast({
  508. content: `Error: ${data.message}`,
  509. timeout: 8000
  510. });
  511. else new Toast({ content: data.message, timeout: 4000 });
  512. }
  513. );
  514. },
  515. clearAndRefillStationPlaylist() {
  516. this.socket.dispatch(
  517. "playlists.clearAndRefillStationPlaylist",
  518. this.playlist._id,
  519. data => {
  520. if (data.status !== "success")
  521. new Toast({
  522. content: `Error: ${data.message}`,
  523. timeout: 8000
  524. });
  525. else new Toast({ content: data.message, timeout: 4000 });
  526. }
  527. );
  528. },
  529. clearAndRefillGenrePlaylist() {
  530. this.socket.dispatch(
  531. "playlists.clearAndRefillGenrePlaylist",
  532. this.playlist._id,
  533. data => {
  534. if (data.status !== "success")
  535. new Toast({
  536. content: `Error: ${data.message}`,
  537. timeout: 8000
  538. });
  539. else new Toast({ content: data.message, timeout: 4000 });
  540. }
  541. );
  542. },
  543. ...mapActions({
  544. showTab(dispatch, payload) {
  545. this.$refs[`${payload}-tab`].scrollIntoView({
  546. block: "nearest"
  547. });
  548. return dispatch("modals/editPlaylist/showTab", payload);
  549. }
  550. }),
  551. ...mapActions("modals/editPlaylist", [
  552. "setPlaylist",
  553. "clearPlaylist",
  554. "addSong",
  555. "removeSong",
  556. "repositionedSong"
  557. ]),
  558. ...mapActions("modalVisibility", ["openModal", "closeModal"])
  559. }
  560. };
  561. </script>
  562. <style lang="scss" scoped>
  563. .night-mode {
  564. .label,
  565. p,
  566. strong {
  567. color: var(--light-grey-2);
  568. }
  569. .edit-playlist-modal.modal .modal-card-body {
  570. .left-section {
  571. #playlist-info-section {
  572. background-color: var(--dark-grey-3) !important;
  573. border: 0;
  574. }
  575. .tabs-container {
  576. background-color: transparent !important;
  577. .tab-selection .button {
  578. background: var(--dark-grey);
  579. color: var(--white);
  580. }
  581. .tab {
  582. background-color: var(--dark-grey-3) !important;
  583. border: 0 !important;
  584. }
  585. }
  586. }
  587. .right-section .section {
  588. border-radius: 5px;
  589. }
  590. }
  591. }
  592. .menu-list li {
  593. display: flex;
  594. justify-content: space-between;
  595. &:not(:last-of-type) {
  596. margin-bottom: 10px;
  597. }
  598. a {
  599. display: flex;
  600. }
  601. }
  602. .controls {
  603. display: flex;
  604. a {
  605. display: flex;
  606. align-items: center;
  607. }
  608. }
  609. .tabs-container {
  610. .tab-selection {
  611. display: flex;
  612. margin: 24px 10px 0 10px;
  613. max-width: 100%;
  614. .button {
  615. border-radius: 5px 5px 0 0;
  616. border: 0;
  617. text-transform: uppercase;
  618. font-size: 14px;
  619. color: var(--dark-grey-3);
  620. background-color: var(--light-grey-2);
  621. flex-grow: 1;
  622. height: 32px;
  623. &:not(:first-of-type) {
  624. margin-left: 5px;
  625. }
  626. }
  627. .selected {
  628. background-color: var(--primary-color) !important;
  629. color: var(--white) !important;
  630. font-weight: 600;
  631. }
  632. }
  633. .tab {
  634. border: 1px solid var(--light-grey-3);
  635. border-radius: 0 0 5px 5px;
  636. }
  637. }
  638. .edit-playlist-modal {
  639. &.view-only {
  640. height: auto !important;
  641. .left-section {
  642. flex-basis: 100% !important;
  643. }
  644. .right-section {
  645. max-height: unset !important;
  646. }
  647. /deep/ .section {
  648. max-width: 100% !important;
  649. }
  650. }
  651. .nothing-here-text {
  652. display: flex;
  653. align-items: center;
  654. justify-content: center;
  655. }
  656. .label {
  657. font-size: 1rem;
  658. font-weight: normal;
  659. }
  660. .input-with-button .button {
  661. width: 150px;
  662. }
  663. .left-section {
  664. #playlist-info-section {
  665. border: 1px solid var(--light-grey-3);
  666. border-radius: 3px;
  667. padding: 15px !important;
  668. h3 {
  669. font-weight: 600;
  670. font-size: 30px;
  671. }
  672. h5 {
  673. font-size: 18px;
  674. }
  675. h3,
  676. h5 {
  677. margin: 0;
  678. }
  679. }
  680. }
  681. .right-section {
  682. #rearrange-songs-section {
  683. .scrollable-list:not(:last-of-type) {
  684. margin-bottom: 10px;
  685. }
  686. }
  687. }
  688. }
  689. </style>