index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. <template>
  2. <modal
  3. :title="
  4. userId === playlist.createdBy ? 'Edit Playlist' : 'View Playlist'
  5. "
  6. class="edit-playlist-modal"
  7. >
  8. <div
  9. slot="body"
  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>Duration: {{ totalLength() }}</h5>
  19. </div>
  20. <div id="playlist-settings-section" class="section">
  21. <div v-if="isEditable()">
  22. <h4 class="section-title">Edit Details</h4>
  23. <p class="section-description">
  24. Change the display name and privacy of the playlist.
  25. </p>
  26. <hr class="section-horizontal-rule" />
  27. <label class="label"> Change display name </label>
  28. <div class="control is-grouped input-with-button">
  29. <p class="control is-expanded">
  30. <input
  31. v-model="playlist.displayName"
  32. class="input"
  33. type="text"
  34. placeholder="Playlist Display Name"
  35. @keyup.enter="renamePlaylist()"
  36. />
  37. </p>
  38. <p class="control">
  39. <a
  40. class="button is-info"
  41. @click.prevent="renamePlaylist()"
  42. href="#"
  43. >Rename</a
  44. >
  45. </p>
  46. </div>
  47. </div>
  48. <div v-if="userId === playlist.createdBy">
  49. <label class="label"> Change privacy </label>
  50. <div class="control is-grouped input-with-button">
  51. <div class="control is-expanded select">
  52. <select v-model="playlist.privacy">
  53. <option value="private">Private</option>
  54. <option value="public">Public</option>
  55. </select>
  56. </div>
  57. <p class="control">
  58. <a
  59. class="button is-info"
  60. @click.prevent="updatePrivacy()"
  61. href="#"
  62. >Update Privacy</a
  63. >
  64. </p>
  65. </div>
  66. </div>
  67. </div>
  68. <div
  69. id="import-from-youtube-section"
  70. class="section"
  71. v-if="isEditable()"
  72. >
  73. <h4 class="section-title">Import from YouTube</h4>
  74. <p class="section-description">
  75. Import a playlist or song by searching or using a link
  76. from YouTube.
  77. </p>
  78. <hr class="section-horizontal-rule" />
  79. <label class="label">
  80. Search for a playlist from YouTube
  81. </label>
  82. <div class="control is-grouped input-with-button">
  83. <p class="control is-expanded">
  84. <input
  85. class="input"
  86. type="text"
  87. placeholder="Enter YouTube Playlist URL here..."
  88. v-model="search.playlist.query"
  89. @keyup.enter="importPlaylist()"
  90. />
  91. </p>
  92. <p class="control has-addons">
  93. <span class="select" id="playlist-import-type">
  94. <select
  95. v-model="
  96. search.playlist.isImportingOnlyMusic
  97. "
  98. >
  99. <option :value="false">Import all</option>
  100. <option :value="true">
  101. Import only music
  102. </option>
  103. </select>
  104. </span>
  105. <a
  106. class="button is-info"
  107. @click.prevent="importPlaylist()"
  108. href="#"
  109. ><i class="material-icons icon-with-button"
  110. >publish</i
  111. >Import</a
  112. >
  113. </p>
  114. </div>
  115. <label class="label">
  116. Search for a song from YouTube
  117. </label>
  118. <div class="control is-grouped input-with-button">
  119. <p class="control is-expanded">
  120. <input
  121. class="input"
  122. type="text"
  123. placeholder="Enter your YouTube query here..."
  124. v-model="search.songs.query"
  125. autofocus
  126. @keyup.enter="searchForSongs()"
  127. />
  128. </p>
  129. <p class="control">
  130. <a
  131. class="button is-info"
  132. @click.prevent="searchForSongs()"
  133. href="#"
  134. ><i class="material-icons icon-with-button"
  135. >search</i
  136. >Search</a
  137. >
  138. </p>
  139. </div>
  140. <div
  141. v-if="search.songs.results.length > 0"
  142. id="song-query-results"
  143. >
  144. <search-query-item
  145. v-for="(result, index) in search.songs.results"
  146. :key="index"
  147. :result="result"
  148. >
  149. <div slot="actions">
  150. <transition
  151. name="search-query-actions"
  152. mode="out-in"
  153. >
  154. <a
  155. class="button is-success"
  156. v-if="result.isAddedToQueue"
  157. href="#"
  158. key="added-to-playlist"
  159. >
  160. <i
  161. class="material-icons icon-with-button"
  162. >done</i
  163. >
  164. Added to playlist
  165. </a>
  166. <a
  167. class="button is-dark"
  168. v-else
  169. @click.prevent="
  170. addSongToPlaylist(result.id, index)
  171. "
  172. href="#"
  173. key="add-to-playlist"
  174. >
  175. <i
  176. class="material-icons icon-with-button"
  177. >add</i
  178. >
  179. Add to playlist
  180. </a>
  181. </transition>
  182. </div>
  183. </search-query-item>
  184. <a
  185. class="button is-default load-more-button"
  186. @click.prevent="loadMoreSongs()"
  187. href="#"
  188. >
  189. Load more...
  190. </a>
  191. </div>
  192. </div>
  193. </div>
  194. <div id="second-column">
  195. <div id="rearrange-songs-section" class="section">
  196. <div v-if="isEditable()">
  197. <h4 class="section-title">Rearrange Songs</h4>
  198. <p class="section-description">
  199. Drag and drop songs to change their order
  200. </p>
  201. <hr class="section-horizontal-rule" />
  202. </div>
  203. <aside class="menu">
  204. <draggable
  205. class="menu-list scrollable-list"
  206. tag="ul"
  207. v-if="playlist.songs.length > 0"
  208. v-model="playlist.songs"
  209. v-bind="dragOptions"
  210. @start="drag = true"
  211. @end="drag = false"
  212. @change="updateSongPositioning"
  213. >
  214. <transition-group
  215. type="transition"
  216. :name="
  217. !drag ? 'draggable-list-transition' : null
  218. "
  219. >
  220. <li
  221. v-for="(song, index) in playlist.songs"
  222. :key="'key-' + index"
  223. >
  224. <playlist-song-item
  225. :song="song"
  226. :class="{
  227. 'item-draggable': isEditable()
  228. }"
  229. >
  230. <div slot="actions">
  231. <i
  232. class="material-icons"
  233. v-if="isEditable() && index > 0"
  234. @click="moveSongToTop(index)"
  235. >vertical_align_top</i
  236. >
  237. <i
  238. v-else
  239. class="material-icons"
  240. style="opacity: 0"
  241. >error</i
  242. >
  243. <i
  244. v-if="
  245. isEditable() &&
  246. playlist.songs.length -
  247. 1 !==
  248. index
  249. "
  250. @click="moveSongToBottom(index)"
  251. class="material-icons"
  252. >vertical_align_bottom</i
  253. >
  254. <i
  255. v-else
  256. class="material-icons"
  257. style="opacity: 0"
  258. >error</i
  259. >
  260. <i
  261. v-if="
  262. userId ===
  263. playlist.createdBy
  264. "
  265. @click="
  266. removeSongFromPlaylist(
  267. song.songId
  268. )
  269. "
  270. class="material-icons delete-icon"
  271. >delete</i
  272. >
  273. </div>
  274. </playlist-song-item>
  275. </li>
  276. </transition-group>
  277. </draggable>
  278. <p v-else class="nothing-here-text">
  279. This playlist doesn't have any songs.
  280. </p>
  281. </aside>
  282. </div>
  283. </div>
  284. <!--
  285. <button
  286. class="button is-info"
  287. @click="shuffle()"
  288. v-if="playlist.isUserModifiable"
  289. >
  290. Shuffle
  291. </button>
  292. <h5>Edit playlist details:</h5>
  293. -->
  294. </div>
  295. <div slot="footer">
  296. <a
  297. class="button is-default"
  298. v-if="
  299. this.userId === this.playlist.createdBy ||
  300. this.playlist.privacy === 'public'
  301. "
  302. @click="downloadPlaylist()"
  303. href="#"
  304. >
  305. Download Playlist
  306. </a>
  307. <a
  308. class="button is-danger"
  309. @click="removePlaylist()"
  310. href="#"
  311. v-if="isEditable()"
  312. >
  313. Remove Playlist
  314. </a>
  315. </div>
  316. </modal>
  317. </template>
  318. <script>
  319. import { mapState, mapActions } from "vuex";
  320. import draggable from "vuedraggable";
  321. import Toast from "toasters";
  322. import SearchYoutube from "../../../mixins/SearchYoutube.vue";
  323. import Modal from "../../Modal.vue";
  324. import SearchQueryItem from "../../ui/SearchQueryItem.vue";
  325. import PlaylistSongItem from "./components/PlaylistSongItem.vue";
  326. import io from "../../../io";
  327. import validation from "../../../validation";
  328. import utils from "../../../../js/utils";
  329. export default {
  330. components: { Modal, draggable, SearchQueryItem, PlaylistSongItem },
  331. mixins: [SearchYoutube],
  332. data() {
  333. return {
  334. utils,
  335. drag: false,
  336. serverDomain: "",
  337. playlist: { songs: [] }
  338. };
  339. },
  340. computed: {
  341. ...mapState("user/playlists", {
  342. editing: state => state.editing
  343. }),
  344. ...mapState({ userId: state => state.user.auth.userId }),
  345. dragOptions() {
  346. return {
  347. animation: 200,
  348. group: "description",
  349. disabled: !this.isEditable(),
  350. ghostClass: "draggable-list-ghost"
  351. };
  352. }
  353. },
  354. watch: {
  355. "search.songs.results": function checkIfSongInPlaylist(songs) {
  356. songs.forEach((searchItem, index) =>
  357. this.playlist.songs.find(song => {
  358. if (song.songId === searchItem.id)
  359. this.search.songs.results[index].isAddedToQueue = true;
  360. return song.songId === searchItem.id;
  361. })
  362. );
  363. }
  364. },
  365. mounted() {
  366. io.getSocket(socket => {
  367. this.socket = socket;
  368. this.socket.emit("playlists.getPlaylist", this.editing, res => {
  369. if (res.status === "success") {
  370. this.playlist = res.data;
  371. this.playlist.songs.sort((a, b) => a.position - b.position);
  372. }
  373. this.playlist.oldId = res.data._id;
  374. });
  375. this.socket.on("event:playlist.addSong", data => {
  376. if (this.playlist._id === data.playlistId)
  377. this.playlist.songs.push(data.song);
  378. });
  379. this.socket.on("event:playlist.removeSong", data => {
  380. if (this.playlist._id === data.playlistId) {
  381. // remove song from array of playlists
  382. this.playlist.songs.forEach((song, index) => {
  383. if (song.songId === data.songId)
  384. this.playlist.songs.splice(index, 1);
  385. });
  386. // if this song is in search results, mark it available to add to the playlist again
  387. this.search.songs.results.forEach((searchItem, index) => {
  388. if (data.songId === searchItem.id) {
  389. this.search.songs.results[
  390. index
  391. ].isAddedToQueue = false;
  392. }
  393. });
  394. }
  395. });
  396. this.socket.on("event:playlist.updateDisplayName", data => {
  397. if (this.playlist._id === data.playlistId)
  398. this.playlist.displayName = data.displayName;
  399. });
  400. this.socket.on("event:playlist.repositionSongs", data => {
  401. if (this.playlist._id === data.playlistId) {
  402. // for each song that has a new position
  403. data.songsBeingChanged.forEach(changedSong => {
  404. this.playlist.songs.forEach((song, index) => {
  405. // find song locally
  406. if (song.songId === changedSong.songId) {
  407. // change song position attribute
  408. this.playlist.songs[index].position =
  409. changedSong.position;
  410. // reposition in array if needed
  411. if (index !== changedSong.position - 1)
  412. this.playlist.songs.splice(
  413. changedSong.position - 1,
  414. 0,
  415. this.playlist.songs.splice(index, 1)[0]
  416. );
  417. }
  418. });
  419. });
  420. }
  421. });
  422. });
  423. },
  424. methods: {
  425. importPlaylist() {
  426. let isImportingPlaylist = true;
  427. // import query is blank
  428. if (!this.search.playlist.query)
  429. return new Toast({
  430. content: "Please enter a YouTube playlist URL.",
  431. timeout: 4000
  432. });
  433. // don't give starting import message instantly in case of instant error
  434. setTimeout(() => {
  435. if (isImportingPlaylist) {
  436. new Toast({
  437. content:
  438. "Starting to import your playlist. This can take some time to do.",
  439. timeout: 4000
  440. });
  441. }
  442. }, 750);
  443. return this.socket.emit(
  444. "playlists.addSetToPlaylist",
  445. this.search.playlist.query,
  446. this.playlist._id,
  447. this.search.playlist.isImportingOnlyMusic,
  448. res => {
  449. new Toast({ content: res.message, timeout: 20000 });
  450. if (res.status === "success") {
  451. isImportingPlaylist = false;
  452. if (this.search.playlist.isImportingOnlyMusic) {
  453. new Toast({
  454. content: `${res.stats.songsInPlaylistTotal} of the ${res.stats.videosInPlaylistTotal} videos in the playlist were songs.`,
  455. timeout: 20000
  456. });
  457. }
  458. }
  459. }
  460. );
  461. },
  462. isEditable() {
  463. return (
  464. this.playlist.isUserModifiable &&
  465. this.userId === this.playlist.createdBy
  466. );
  467. },
  468. updateSongPositioning({ moved }) {
  469. if (!moved) return; // we only need to update when song is moved
  470. const songsBeingChanged = [];
  471. this.playlist.songs.forEach((song, index) => {
  472. if (song.position !== index + 1)
  473. songsBeingChanged.push({
  474. songId: song.songId,
  475. position: index + 1
  476. });
  477. });
  478. this.socket.emit(
  479. "playlists.repositionSongs",
  480. this.playlist._id,
  481. songsBeingChanged,
  482. res => {
  483. new Toast({ content: res.message, timeout: 4000 });
  484. }
  485. );
  486. },
  487. totalLength() {
  488. let length = 0;
  489. this.playlist.songs.forEach(song => {
  490. length += song.duration;
  491. });
  492. return this.utils.formatTimeLong(length);
  493. },
  494. shuffle() {
  495. this.socket.emit("playlists.shuffle", this.playlist._id, res => {
  496. new Toast({ content: res.message, timeout: 4000 });
  497. if (res.status === "success") {
  498. this.playlist.songs = res.data.songs.sort(
  499. (a, b) => a.position - b.position
  500. );
  501. }
  502. });
  503. },
  504. addSongToPlaylist(id, index) {
  505. this.socket.emit(
  506. "playlists.addSongToPlaylist",
  507. false,
  508. id,
  509. this.playlist._id,
  510. res => {
  511. new Toast({ content: res.message, timeout: 4000 });
  512. if (res.status === "success")
  513. this.search.songs.results[index].isAddedToQueue = true;
  514. }
  515. );
  516. },
  517. removeSongFromPlaylist(id) {
  518. if (this.playlist.displayName === "Liked Songs") {
  519. this.socket.emit("songs.unlike", id, res => {
  520. new Toast({ content: res.message, timeout: 4000 });
  521. });
  522. }
  523. if (this.playlist.displayName === "Disliked Songs") {
  524. this.socket.emit("songs.undislike", id, res => {
  525. new Toast({ content: res.message, timeout: 4000 });
  526. });
  527. } else {
  528. this.socket.emit(
  529. "playlists.removeSongFromPlaylist",
  530. id,
  531. this.playlist._id,
  532. res => {
  533. new Toast({ content: res.message, timeout: 4000 });
  534. }
  535. );
  536. }
  537. },
  538. renamePlaylist() {
  539. const { displayName } = this.playlist;
  540. if (!validation.isLength(displayName, 2, 32))
  541. return new Toast({
  542. content:
  543. "Display name must have between 2 and 32 characters.",
  544. timeout: 8000
  545. });
  546. if (!validation.regex.ascii.test(displayName))
  547. return new Toast({
  548. content:
  549. "Invalid display name format. Only ASCII characters are allowed.",
  550. timeout: 8000
  551. });
  552. return this.socket.emit(
  553. "playlists.updateDisplayName",
  554. this.playlist._id,
  555. this.playlist.displayName,
  556. res => {
  557. new Toast({ content: res.message, timeout: 4000 });
  558. }
  559. );
  560. },
  561. removePlaylist() {
  562. this.socket.emit("playlists.remove", this.playlist._id, res => {
  563. new Toast({ content: res.message, timeout: 3000 });
  564. if (res.status === "success") {
  565. this.closeModal({
  566. sector: "station",
  567. modal: "editPlaylist"
  568. });
  569. }
  570. });
  571. },
  572. async downloadPlaylist() {
  573. if (this.serverDomain === "")
  574. this.serverDomain = await lofig.get("serverDomain");
  575. fetch(
  576. `${this.serverDomain}/export/privatePlaylist/${this.playlist._id}`,
  577. { credentials: "include" }
  578. )
  579. .then(res => res.blob())
  580. .then(blob => {
  581. const url = window.URL.createObjectURL(blob);
  582. const a = document.createElement("a");
  583. a.style.display = "none";
  584. a.href = url;
  585. a.download = `musare-privateplaylist-${
  586. this.playlist._id
  587. }-${new Date().toISOString()}.json`;
  588. document.body.appendChild(a);
  589. a.click();
  590. window.URL.revokeObjectURL(url);
  591. new Toast({
  592. content: "Successfully downloaded playlist.",
  593. timeout: 3000
  594. });
  595. })
  596. .catch(
  597. () =>
  598. new Toast({
  599. content: "Failed to export and download playlist.",
  600. timeout: 3000
  601. })
  602. );
  603. },
  604. moveSongToTop(index) {
  605. this.playlist.songs.splice(
  606. 0,
  607. 0,
  608. this.playlist.songs.splice(index, 1)[0]
  609. );
  610. this.updateSongPositioning({ moved: {} });
  611. },
  612. moveSongToBottom(index) {
  613. this.playlist.songs.splice(
  614. this.playlist.songs.length,
  615. 0,
  616. this.playlist.songs.splice(index, 1)[0]
  617. );
  618. this.updateSongPositioning({ moved: {} });
  619. },
  620. updatePrivacy() {
  621. const { privacy } = this.playlist;
  622. if (privacy === "public" || privacy === "private") {
  623. this.socket.emit(
  624. "playlists.updatePrivacy",
  625. this.playlist._id,
  626. privacy,
  627. res => {
  628. new Toast({ content: res.message, timeout: 4000 });
  629. }
  630. );
  631. }
  632. },
  633. ...mapActions("modalVisibility", ["closeModal"])
  634. }
  635. };
  636. </script>
  637. <style lang="scss">
  638. .edit-playlist-modal {
  639. .modal-card {
  640. width: 1300px;
  641. .modal-card-body {
  642. padding: 16px;
  643. }
  644. }
  645. .modal-card-foot {
  646. justify-content: flex-end;
  647. }
  648. }
  649. </style>
  650. <style lang="scss" scoped>
  651. @import "../../../styles/global.scss";
  652. .night-mode {
  653. .section {
  654. background-color: $night-mode-bg-secondary !important;
  655. }
  656. .label,
  657. p,
  658. strong {
  659. color: $night-mode-text;
  660. }
  661. }
  662. .menu-list li {
  663. display: flex;
  664. justify-content: space-between;
  665. &:not(:last-of-type) {
  666. margin-bottom: 10px;
  667. }
  668. a {
  669. display: flex;
  670. }
  671. }
  672. .controls {
  673. display: flex;
  674. a {
  675. display: flex;
  676. align-items: center;
  677. }
  678. }
  679. @media screen and (max-width: 1300px) {
  680. .edit-playlist-modal .edit-playlist-modal-inner-container {
  681. height: auto !important;
  682. #import-from-youtube-section #song-query-results,
  683. .section {
  684. max-width: 100% !important;
  685. }
  686. }
  687. }
  688. .edit-playlist-modal {
  689. .edit-playlist-modal-inner-container {
  690. display: flex;
  691. flex-wrap: wrap;
  692. height: 100%;
  693. &.view-only {
  694. height: auto !important;
  695. #first-column {
  696. break-after: always;
  697. }
  698. .section {
  699. max-width: 100% !important;
  700. }
  701. }
  702. }
  703. .nothing-here-text {
  704. display: flex;
  705. align-items: center;
  706. justify-content: center;
  707. }
  708. .section {
  709. padding: 15px !important;
  710. margin: 0 10px;
  711. max-width: 600px;
  712. display: flex;
  713. flex-direction: column;
  714. flex-grow: 1;
  715. }
  716. .label {
  717. font-size: 1rem;
  718. font-weight: normal;
  719. }
  720. .input-with-button .button {
  721. width: 150px;
  722. }
  723. #first-column {
  724. max-width: 100%;
  725. height: 100%;
  726. overflow-y: auto;
  727. flex-grow: 1;
  728. .section {
  729. width: auto;
  730. }
  731. #playlist-info-section {
  732. border: 1px solid $light-grey-2;
  733. border-radius: 3px;
  734. padding: 15px !important;
  735. h3 {
  736. font-weight: 600;
  737. font-size: 30px;
  738. }
  739. h5 {
  740. font-size: 18px;
  741. }
  742. h3,
  743. h5 {
  744. margin: 0;
  745. }
  746. }
  747. #import-from-youtube-section {
  748. #playlist-import-type select {
  749. border-radius: 0;
  750. }
  751. #song-query-results {
  752. padding: 10px;
  753. margin-top: 10px;
  754. border: 1px solid $light-grey-2;
  755. border-radius: 3px;
  756. max-width: 565px;
  757. .search-query-item:not(:last-of-type) {
  758. margin-bottom: 10px;
  759. }
  760. }
  761. .load-more-button {
  762. width: 100%;
  763. margin-top: 10px;
  764. }
  765. }
  766. }
  767. #second-column {
  768. max-width: 100%;
  769. height: 100%;
  770. overflow-y: auto;
  771. flex-grow: 1;
  772. }
  773. }
  774. </style>