index.vue 16 KB

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