Playlists.vue 14 KB

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