index.vue 16 KB

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