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