Playlists.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. <template>
  2. <div class="station-playlists">
  3. <div class="tabs-container">
  4. <div class="tab-selection">
  5. <button
  6. class="button is-default"
  7. ref="current-tab"
  8. :class="{ selected: tab === 'current' }"
  9. @click="showTab('current')"
  10. >
  11. Current
  12. </button>
  13. <button
  14. class="button is-default"
  15. ref="search-tab"
  16. :class="{ selected: tab === 'search' }"
  17. @click="showTab('search')"
  18. >
  19. Search
  20. </button>
  21. <button
  22. v-if="station.type === 'community'"
  23. class="button is-default"
  24. ref="my-playlists-tab"
  25. :class="{ selected: tab === 'my-playlists' }"
  26. @click="showTab('my-playlists')"
  27. >
  28. My Playlists
  29. </button>
  30. </div>
  31. <div class="tab" v-show="tab === 'current'">
  32. <div v-if="currentPlaylists.length > 0">
  33. <playlist-item
  34. v-for="playlist in currentPlaylists"
  35. :key="`key-${playlist._id}`"
  36. :playlist="playlist"
  37. :show-owner="true"
  38. >
  39. <template #actions>
  40. <confirm
  41. v-if="isOwnerOrAdmin()"
  42. @confirm="deselectPlaylist(playlist._id)"
  43. >
  44. <i
  45. class="material-icons stop-icon"
  46. content="Stop playing songs from this playlist"
  47. v-tippy
  48. >
  49. stop
  50. </i>
  51. </confirm>
  52. <confirm
  53. v-if="isOwnerOrAdmin()"
  54. @confirm="blacklistPlaylist(playlist._id)"
  55. >
  56. <i
  57. class="material-icons stop-icon"
  58. content="Blacklist Playlist"
  59. v-tippy
  60. >block</i
  61. >
  62. </confirm>
  63. <i
  64. v-if="playlist.createdBy === myUserId"
  65. @click="showPlaylist(playlist._id)"
  66. class="material-icons edit-icon"
  67. content="Edit Playlist"
  68. v-tippy
  69. >edit</i
  70. >
  71. <i
  72. v-if="
  73. playlist.createdBy !== myUserId &&
  74. (playlist.privacy === 'public' || isAdmin())
  75. "
  76. @click="showPlaylist(playlist._id)"
  77. class="material-icons edit-icon"
  78. content="View Playlist"
  79. v-tippy
  80. >visibility</i
  81. >
  82. </template>
  83. </playlist-item>
  84. </div>
  85. <p v-else class="has-text-centered scrollable-list">
  86. No playlists currently selected.
  87. </p>
  88. </div>
  89. <div class="tab" v-show="tab === 'search'">
  90. <label class="label"> Search for a public playlist </label>
  91. <div class="control is-grouped input-with-button">
  92. <p class="control is-expanded">
  93. <input
  94. class="input"
  95. type="text"
  96. placeholder="Enter your playlist query here..."
  97. v-model="search.query"
  98. @keyup.enter="searchForPlaylists(1)"
  99. />
  100. </p>
  101. <p class="control">
  102. <a class="button is-info" @click="searchForPlaylists(1)"
  103. ><i class="material-icons icon-with-button"
  104. >search</i
  105. >Search</a
  106. >
  107. </p>
  108. </div>
  109. <div v-if="search.results.length > 0">
  110. <playlist-item
  111. v-for="playlist in search.results"
  112. :key="`searchKey-${playlist._id}`"
  113. :playlist="playlist"
  114. :show-owner="true"
  115. >
  116. <template #actions>
  117. <i
  118. v-if="isExcluded(playlist._id)"
  119. class="material-icons stop-icon"
  120. content="This playlist is blacklisted in this station"
  121. v-tippy="{ theme: 'info' }"
  122. >play_disabled</i
  123. >
  124. <confirm
  125. v-if="
  126. (isOwnerOrAdmin() ||
  127. (station.type === 'community' &&
  128. station.partyMode)) &&
  129. isSelected(playlist._id)
  130. "
  131. @confirm="deselectPlaylist(playlist._id)"
  132. >
  133. <i
  134. class="material-icons stop-icon"
  135. content="Stop playing songs from this playlist"
  136. v-tippy
  137. >
  138. stop
  139. </i>
  140. </confirm>
  141. <i
  142. v-if="
  143. (isOwnerOrAdmin() ||
  144. (station.type === 'community' &&
  145. station.partyMode)) &&
  146. !isSelected(playlist._id) &&
  147. !isExcluded(playlist._id)
  148. "
  149. @click="selectPlaylist(playlist)"
  150. class="material-icons play-icon"
  151. :content="
  152. station.partyMode
  153. ? 'Request songs from this playlist'
  154. : 'Play songs from this playlist'
  155. "
  156. v-tippy
  157. >play_arrow</i
  158. >
  159. <confirm
  160. v-if="
  161. isOwnerOrAdmin() &&
  162. !isExcluded(playlist._id)
  163. "
  164. @confirm="blacklistPlaylist(playlist._id)"
  165. >
  166. <i
  167. class="material-icons stop-icon"
  168. content="Blacklist Playlist"
  169. v-tippy
  170. >block</i
  171. >
  172. </confirm>
  173. <i
  174. v-if="playlist.createdBy === myUserId"
  175. @click="showPlaylist(playlist._id)"
  176. class="material-icons edit-icon"
  177. content="Edit Playlist"
  178. v-tippy
  179. >edit</i
  180. >
  181. <i
  182. v-if="
  183. playlist.createdBy !== myUserId &&
  184. (playlist.privacy === 'public' || isAdmin())
  185. "
  186. @click="showPlaylist(playlist._id)"
  187. class="material-icons edit-icon"
  188. content="View Playlist"
  189. v-tippy
  190. >visibility</i
  191. >
  192. </template>
  193. </playlist-item>
  194. <button
  195. v-if="resultsLeftCount > 0"
  196. class="button is-primary load-more-button"
  197. @click="searchForPlaylists(search.page + 1)"
  198. >
  199. Load {{ nextPageResultsCount }} more results
  200. </button>
  201. </div>
  202. </div>
  203. <div
  204. v-if="station.type === 'community'"
  205. class="tab"
  206. v-show="tab === 'my-playlists'"
  207. >
  208. <button
  209. class="button is-primary"
  210. id="create-new-playlist-button"
  211. @click="openModal('createPlaylist')"
  212. >
  213. Create new playlist
  214. </button>
  215. <div
  216. class="menu-list scrollable-list"
  217. v-if="playlists.length > 0"
  218. >
  219. <draggable
  220. tag="transition-group"
  221. :component-data="{
  222. name: !drag ? 'draggable-list-transition' : null
  223. }"
  224. item-key="_id"
  225. v-model="playlists"
  226. v-bind="dragOptions"
  227. @start="drag = true"
  228. @end="drag = false"
  229. @change="savePlaylistOrder"
  230. >
  231. <template #item="{ element }">
  232. <playlist-item
  233. class="item-draggable"
  234. :playlist="element"
  235. >
  236. <template #actions>
  237. <i
  238. v-if="isExcluded(element._id)"
  239. class="material-icons stop-icon"
  240. content="This playlist is blacklisted in this station"
  241. v-tippy="{ theme: 'info' }"
  242. >play_disabled</i
  243. >
  244. <i
  245. v-if="
  246. station.type === 'community' &&
  247. (isOwnerOrAdmin() ||
  248. station.partyMode) &&
  249. !isSelected(element._id) &&
  250. !isExcluded(element._id)
  251. "
  252. @click="selectPlaylist(element)"
  253. class="material-icons play-icon"
  254. :content="
  255. station.partyMode
  256. ? 'Request songs from this playlist'
  257. : 'Play songs from this playlist'
  258. "
  259. v-tippy
  260. >play_arrow</i
  261. >
  262. <confirm
  263. v-if="
  264. station.type === 'community' &&
  265. (isOwnerOrAdmin() ||
  266. station.partyMode) &&
  267. isSelected(element._id)
  268. "
  269. @confirm="deselectPlaylist(element._id)"
  270. >
  271. <i
  272. class="material-icons stop-icon"
  273. :content="
  274. station.partyMode
  275. ? 'Stop requesting songs from this playlist'
  276. : 'Stop playing songs from this playlist'
  277. "
  278. v-tippy
  279. >stop</i
  280. >
  281. </confirm>
  282. <confirm
  283. v-if="
  284. isOwnerOrAdmin() &&
  285. !isExcluded(element._id)
  286. "
  287. @confirm="
  288. blacklistPlaylist(element._id)
  289. "
  290. >
  291. <i
  292. class="material-icons stop-icon"
  293. content="Blacklist Playlist"
  294. v-tippy
  295. >block</i
  296. >
  297. </confirm>
  298. <i
  299. @click="showPlaylist(element._id)"
  300. class="material-icons edit-icon"
  301. content="Edit Playlist"
  302. v-tippy
  303. >edit</i
  304. >
  305. </template>
  306. </playlist-item>
  307. </template>
  308. </draggable>
  309. </div>
  310. <p v-else class="has-text-centered scrollable-list">
  311. You don't have any playlists!
  312. </p>
  313. </div>
  314. </div>
  315. </div>
  316. </template>
  317. <script>
  318. import { mapActions, mapState, mapGetters } from "vuex";
  319. import Toast from "toasters";
  320. import PlaylistItem from "@/components/PlaylistItem.vue";
  321. import Confirm from "@/components/Confirm.vue";
  322. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  323. export default {
  324. components: {
  325. PlaylistItem,
  326. Confirm
  327. },
  328. mixins: [SortablePlaylists],
  329. data() {
  330. return {
  331. tab: "current",
  332. search: {
  333. query: "",
  334. searchedQuery: "",
  335. page: 0,
  336. count: 0,
  337. resultsLeft: 0,
  338. results: []
  339. }
  340. };
  341. },
  342. computed: {
  343. currentPlaylists() {
  344. if (this.station.type === "community" && this.station.partyMode) {
  345. return this.partyPlaylists;
  346. }
  347. return this.includedPlaylists;
  348. },
  349. resultsLeftCount() {
  350. return this.search.count - this.search.results.length;
  351. },
  352. nextPageResultsCount() {
  353. return Math.min(this.search.pageSize, this.resultsLeftCount);
  354. },
  355. ...mapState({
  356. loggedIn: state => state.user.auth.loggedIn,
  357. role: state => state.user.auth.role,
  358. userId: state => state.user.auth.userId,
  359. partyPlaylists: state => state.station.partyPlaylists
  360. }),
  361. ...mapState("modals/manageStation", {
  362. originalStation: state => state.originalStation,
  363. station: state => state.station,
  364. includedPlaylists: state => state.includedPlaylists,
  365. excludedPlaylists: state => state.excludedPlaylists,
  366. songsList: state => state.songsList
  367. }),
  368. ...mapGetters({
  369. socket: "websockets/getSocket"
  370. })
  371. },
  372. mounted() {
  373. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  374. if (res.status === "success") this.setPlaylists(res.data.playlists);
  375. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  376. });
  377. this.socket.dispatch(
  378. `stations.getStationIncludedPlaylistsById`,
  379. this.station._id,
  380. res => {
  381. if (res.status === "success") {
  382. this.station.includedPlaylists = res.data.playlists;
  383. this.originalStation.includedPlaylists = res.data.playlists;
  384. }
  385. }
  386. );
  387. this.socket.dispatch(
  388. `stations.getStationExcludedPlaylistsById`,
  389. this.station._id,
  390. res => {
  391. if (res.status === "success") {
  392. this.station.excludedPlaylists = res.data.playlists;
  393. this.originalStation.excludedPlaylists = res.data.playlists;
  394. }
  395. }
  396. );
  397. },
  398. methods: {
  399. showTab(tab) {
  400. this.$refs[`${tab}-tab`].scrollIntoView();
  401. this.tab = tab;
  402. },
  403. isOwner() {
  404. return this.loggedIn && this.userId === this.station.owner;
  405. },
  406. isAdmin() {
  407. return this.loggedIn && this.role === "admin";
  408. },
  409. isOwnerOrAdmin() {
  410. return this.isOwner() || this.isAdmin();
  411. },
  412. showPlaylist(playlistId) {
  413. this.editPlaylist(playlistId);
  414. this.openModal("editPlaylist");
  415. },
  416. selectPlaylist(playlist) {
  417. if (this.station.type === "community" && this.station.partyMode) {
  418. if (!this.isSelected(playlist.id)) {
  419. this.partyPlaylists.push(playlist);
  420. this.addPartyPlaylistSongToQueue();
  421. new Toast(
  422. "Successfully selected playlist to auto request songs."
  423. );
  424. } else {
  425. new Toast("Error: Playlist already selected.");
  426. }
  427. } else {
  428. this.socket.dispatch(
  429. "stations.includePlaylist",
  430. this.station._id,
  431. playlist._id,
  432. res => {
  433. new Toast(res.message);
  434. }
  435. );
  436. }
  437. },
  438. deselectPlaylist(id) {
  439. return new Promise(resolve => {
  440. if (
  441. this.station.type === "community" &&
  442. this.station.partyMode
  443. ) {
  444. let selected = false;
  445. this.currentPlaylists.forEach((playlist, index) => {
  446. if (playlist._id === id) {
  447. selected = true;
  448. this.partyPlaylists.splice(index, 1);
  449. }
  450. });
  451. if (selected) {
  452. new Toast("Successfully deselected playlist.");
  453. resolve();
  454. } else {
  455. new Toast("Playlist not selected.");
  456. resolve();
  457. }
  458. } else {
  459. this.socket.dispatch(
  460. "stations.removeIncludedPlaylist",
  461. this.station._id,
  462. id,
  463. res => {
  464. new Toast(res.message);
  465. resolve();
  466. }
  467. );
  468. }
  469. });
  470. },
  471. isSelected(id) {
  472. let selected = false;
  473. this.currentPlaylists.forEach(playlist => {
  474. if (playlist._id === id) selected = true;
  475. });
  476. return selected;
  477. },
  478. isExcluded(id) {
  479. let selected = false;
  480. this.excludedPlaylists.forEach(playlist => {
  481. if (playlist._id === id) selected = true;
  482. });
  483. return selected;
  484. },
  485. searchForPlaylists(page) {
  486. if (
  487. this.search.page >= page ||
  488. this.search.searchedQuery !== this.search.query
  489. ) {
  490. this.search.results = [];
  491. this.search.page = 0;
  492. this.search.count = 0;
  493. this.search.resultsLeft = 0;
  494. this.search.pageSize = 0;
  495. }
  496. const { query } = this.search;
  497. const action =
  498. this.station.type === "official"
  499. ? "playlists.searchOfficial"
  500. : "playlists.searchCommunity";
  501. this.search.searchedQuery = this.search.query;
  502. this.socket.dispatch(action, query, page, res => {
  503. const { data } = res;
  504. const { count, pageSize, playlists } = data;
  505. if (res.status === "success") {
  506. this.search.results = [
  507. ...this.search.results,
  508. ...playlists
  509. ];
  510. this.search.page = page;
  511. this.search.count = count;
  512. this.search.resultsLeft =
  513. count - this.search.results.length;
  514. this.search.pageSize = pageSize;
  515. } else if (res.status === "error") {
  516. this.search.results = [];
  517. this.search.page = 0;
  518. this.search.count = 0;
  519. this.search.resultsLeft = 0;
  520. this.search.pageSize = 0;
  521. new Toast(res.message);
  522. }
  523. });
  524. },
  525. async blacklistPlaylist(id) {
  526. if (this.isSelected(id)) await this.deselectPlaylist(id);
  527. this.socket.dispatch(
  528. "stations.excludePlaylist",
  529. this.station._id,
  530. id,
  531. res => {
  532. new Toast(res.message);
  533. }
  534. );
  535. },
  536. addPartyPlaylistSongToQueue() {
  537. let isInQueue = false;
  538. if (
  539. this.station.type === "community" &&
  540. this.station.partyMode === true
  541. ) {
  542. this.songsList.forEach(queueSong => {
  543. if (queueSong.requestedBy === this.userId) isInQueue = true;
  544. });
  545. if (!isInQueue && this.partyPlaylists) {
  546. const selectedPlaylist =
  547. this.partyPlaylists[
  548. Math.floor(
  549. Math.random() * this.partyPlaylists.length
  550. )
  551. ];
  552. if (
  553. selectedPlaylist._id &&
  554. selectedPlaylist.songs.length > 0
  555. ) {
  556. const selectedSong =
  557. selectedPlaylist.songs[
  558. Math.floor(
  559. Math.random() *
  560. selectedPlaylist.songs.length
  561. )
  562. ];
  563. if (selectedSong.youtubeId) {
  564. this.socket.dispatch(
  565. "stations.addToQueue",
  566. this.station._id,
  567. selectedSong.youtubeId,
  568. data => {
  569. if (data.status !== "success")
  570. new Toast("Error auto queueing song");
  571. }
  572. );
  573. }
  574. }
  575. }
  576. }
  577. },
  578. ...mapActions("station", ["updatePartyPlaylists"]),
  579. ...mapActions("modalVisibility", ["openModal"]),
  580. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  581. }
  582. };
  583. </script>
  584. <style lang="scss" scoped>
  585. .night-mode {
  586. .tabs-container .tab-selection .button {
  587. background: var(--dark-grey) !important;
  588. color: var(--white) !important;
  589. }
  590. }
  591. .station-playlists {
  592. .tabs-container {
  593. .tab-selection {
  594. display: flex;
  595. overflow-x: auto;
  596. .button {
  597. border-radius: 0;
  598. border: 0;
  599. text-transform: uppercase;
  600. font-size: 14px;
  601. color: var(--dark-grey-3);
  602. background-color: var(--light-grey-2);
  603. flex-grow: 1;
  604. height: 32px;
  605. &:not(:first-of-type) {
  606. margin-left: 5px;
  607. }
  608. }
  609. .selected {
  610. background-color: var(--primary-color) !important;
  611. color: var(--white) !important;
  612. font-weight: 600;
  613. }
  614. }
  615. .tab {
  616. padding: 15px 0;
  617. border-radius: 0;
  618. .playlist-item:not(:last-of-type),
  619. .item.item-draggable:not(:last-of-type) {
  620. margin-bottom: 10px;
  621. }
  622. .load-more-button {
  623. width: 100%;
  624. margin-top: 10px;
  625. }
  626. }
  627. }
  628. }
  629. .draggable-list-transition-move {
  630. transition: transform 0.5s;
  631. }
  632. .draggable-list-ghost {
  633. opacity: 0.5;
  634. filter: brightness(95%);
  635. }
  636. </style>