Playlists.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  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 ws from "@/ws";
  321. import PlaylistItem from "@/components/PlaylistItem.vue";
  322. import Confirm from "@/components/Confirm.vue";
  323. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  324. export default {
  325. components: {
  326. PlaylistItem,
  327. Confirm
  328. },
  329. mixins: [SortablePlaylists],
  330. data() {
  331. return {
  332. tab: "current",
  333. search: {
  334. query: "",
  335. searchedQuery: "",
  336. page: 0,
  337. count: 0,
  338. resultsLeft: 0,
  339. results: []
  340. }
  341. };
  342. },
  343. computed: {
  344. currentPlaylists() {
  345. if (this.station.type === "community" && this.station.partyMode) {
  346. return this.partyPlaylists;
  347. }
  348. return this.includedPlaylists;
  349. },
  350. resultsLeftCount() {
  351. return this.search.count - this.search.results.length;
  352. },
  353. nextPageResultsCount() {
  354. return Math.min(this.search.pageSize, this.resultsLeftCount);
  355. },
  356. ...mapState({
  357. loggedIn: state => state.user.auth.loggedIn,
  358. role: state => state.user.auth.role,
  359. userId: state => state.user.auth.userId,
  360. partyPlaylists: state => state.station.partyPlaylists
  361. }),
  362. ...mapState("modals/manageStation", {
  363. originalStation: state => state.originalStation,
  364. station: state => state.station,
  365. includedPlaylists: state => state.includedPlaylists,
  366. excludedPlaylists: state => state.excludedPlaylists,
  367. songsList: state => state.songsList
  368. }),
  369. ...mapGetters({
  370. socket: "websockets/getSocket"
  371. })
  372. },
  373. mounted() {
  374. if (this.station.type === "community" && this.station.partyMode)
  375. this.showTab("search");
  376. ws.onConnect(this.init);
  377. },
  378. methods: {
  379. init() {
  380. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  381. if (res.status === "success")
  382. this.setPlaylists(res.data.playlists);
  383. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  384. });
  385. this.socket.dispatch(
  386. `stations.getStationIncludedPlaylistsById`,
  387. this.station._id,
  388. res => {
  389. if (res.status === "success") {
  390. this.station.includedPlaylists = res.data.playlists;
  391. this.originalStation.includedPlaylists =
  392. res.data.playlists;
  393. }
  394. }
  395. );
  396. this.socket.dispatch(
  397. `stations.getStationExcludedPlaylistsById`,
  398. this.station._id,
  399. res => {
  400. if (res.status === "success") {
  401. this.station.excludedPlaylists = res.data.playlists;
  402. this.originalStation.excludedPlaylists =
  403. res.data.playlists;
  404. }
  405. }
  406. );
  407. },
  408. showTab(tab) {
  409. this.$refs[`${tab}-tab`].scrollIntoView({ block: "nearest" });
  410. this.tab = tab;
  411. },
  412. isOwner() {
  413. return this.loggedIn && this.userId === this.station.owner;
  414. },
  415. isAdmin() {
  416. return this.loggedIn && this.role === "admin";
  417. },
  418. isOwnerOrAdmin() {
  419. return this.isOwner() || this.isAdmin();
  420. },
  421. showPlaylist(playlistId) {
  422. this.editPlaylist(playlistId);
  423. this.openModal("editPlaylist");
  424. },
  425. selectPlaylist(playlist) {
  426. if (this.station.type === "community" && this.station.partyMode) {
  427. if (!this.isSelected(playlist.id)) {
  428. this.partyPlaylists.push(playlist);
  429. this.addPartyPlaylistSongToQueue();
  430. new Toast(
  431. "Successfully selected playlist to auto request songs."
  432. );
  433. } else {
  434. new Toast("Error: Playlist already selected.");
  435. }
  436. } else {
  437. this.socket.dispatch(
  438. "stations.includePlaylist",
  439. this.station._id,
  440. playlist._id,
  441. res => {
  442. new Toast(res.message);
  443. }
  444. );
  445. }
  446. },
  447. deselectPlaylist(id) {
  448. return new Promise(resolve => {
  449. if (
  450. this.station.type === "community" &&
  451. this.station.partyMode
  452. ) {
  453. let selected = false;
  454. this.currentPlaylists.forEach((playlist, index) => {
  455. if (playlist._id === id) {
  456. selected = true;
  457. this.partyPlaylists.splice(index, 1);
  458. }
  459. });
  460. if (selected) {
  461. new Toast("Successfully deselected playlist.");
  462. resolve();
  463. } else {
  464. new Toast("Playlist not selected.");
  465. resolve();
  466. }
  467. } else {
  468. this.socket.dispatch(
  469. "stations.removeIncludedPlaylist",
  470. this.station._id,
  471. id,
  472. res => {
  473. new Toast(res.message);
  474. resolve();
  475. }
  476. );
  477. }
  478. });
  479. },
  480. isSelected(id) {
  481. let selected = false;
  482. this.currentPlaylists.forEach(playlist => {
  483. if (playlist._id === id) selected = true;
  484. });
  485. return selected;
  486. },
  487. isExcluded(id) {
  488. let selected = false;
  489. this.excludedPlaylists.forEach(playlist => {
  490. if (playlist._id === id) selected = true;
  491. });
  492. return selected;
  493. },
  494. searchForPlaylists(page) {
  495. if (
  496. this.search.page >= page ||
  497. this.search.searchedQuery !== this.search.query
  498. ) {
  499. this.search.results = [];
  500. this.search.page = 0;
  501. this.search.count = 0;
  502. this.search.resultsLeft = 0;
  503. this.search.pageSize = 0;
  504. }
  505. const { query } = this.search;
  506. const action =
  507. this.station.type === "official"
  508. ? "playlists.searchOfficial"
  509. : "playlists.searchCommunity";
  510. this.search.searchedQuery = this.search.query;
  511. this.socket.dispatch(action, query, page, res => {
  512. const { data } = res;
  513. if (res.status === "success") {
  514. const { count, pageSize, playlists } = data;
  515. this.search.results = [
  516. ...this.search.results,
  517. ...playlists
  518. ];
  519. this.search.page = page;
  520. this.search.count = count;
  521. this.search.resultsLeft =
  522. count - this.search.results.length;
  523. this.search.pageSize = pageSize;
  524. } else if (res.status === "error") {
  525. this.search.results = [];
  526. this.search.page = 0;
  527. this.search.count = 0;
  528. this.search.resultsLeft = 0;
  529. this.search.pageSize = 0;
  530. new Toast(res.message);
  531. }
  532. });
  533. },
  534. async blacklistPlaylist(id) {
  535. if (this.isSelected(id)) await this.deselectPlaylist(id);
  536. this.socket.dispatch(
  537. "stations.excludePlaylist",
  538. this.station._id,
  539. id,
  540. res => {
  541. new Toast(res.message);
  542. }
  543. );
  544. },
  545. addPartyPlaylistSongToQueue() {
  546. if (
  547. this.station.type === "community" &&
  548. this.station.partyMode === true &&
  549. this.songsList.length < 50 &&
  550. this.songsList.filter(
  551. queueSong => queueSong.requestedBy === this.userId
  552. ).length < 3 &&
  553. this.partyPlaylists
  554. ) {
  555. const selectedPlaylist =
  556. this.partyPlaylists[
  557. Math.floor(Math.random() * this.partyPlaylists.length)
  558. ];
  559. if (selectedPlaylist._id && selectedPlaylist.songs.length > 0) {
  560. const selectedSong =
  561. selectedPlaylist.songs[
  562. Math.floor(
  563. Math.random() * selectedPlaylist.songs.length
  564. )
  565. ];
  566. if (selectedSong.youtubeId) {
  567. this.socket.dispatch(
  568. "stations.addToQueue",
  569. this.station._id,
  570. selectedSong.youtubeId,
  571. data => {
  572. if (data.status !== "success")
  573. this.addPartyPlaylistSongToQueue();
  574. }
  575. );
  576. }
  577. }
  578. }
  579. },
  580. ...mapActions("station", ["updatePartyPlaylists"]),
  581. ...mapActions("modalVisibility", ["openModal"]),
  582. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  583. }
  584. };
  585. </script>
  586. <style lang="scss" scoped>
  587. .night-mode {
  588. .tabs-container .tab-selection .button {
  589. background: var(--dark-grey) !important;
  590. color: var(--white) !important;
  591. }
  592. }
  593. .station-playlists {
  594. .tabs-container {
  595. .tab-selection {
  596. display: flex;
  597. overflow-x: auto;
  598. .button {
  599. border-radius: 0;
  600. border: 0;
  601. text-transform: uppercase;
  602. font-size: 14px;
  603. color: var(--dark-grey-3);
  604. background-color: var(--light-grey-2);
  605. flex-grow: 1;
  606. height: 32px;
  607. &:not(:first-of-type) {
  608. margin-left: 5px;
  609. }
  610. }
  611. .selected {
  612. background-color: var(--primary-color) !important;
  613. color: var(--white) !important;
  614. font-weight: 600;
  615. }
  616. }
  617. .tab {
  618. padding: 15px 0;
  619. border-radius: 0;
  620. .playlist-item:not(:last-of-type),
  621. .item.item-draggable:not(:last-of-type) {
  622. margin-bottom: 10px;
  623. }
  624. .load-more-button {
  625. width: 100%;
  626. margin-top: 10px;
  627. }
  628. }
  629. }
  630. }
  631. .draggable-list-transition-move {
  632. transition: transform 0.5s;
  633. }
  634. .draggable-list-ghost {
  635. opacity: 0.5;
  636. filter: brightness(95%);
  637. }
  638. </style>