index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. <template>
  2. <modal
  3. v-if="station"
  4. :title="
  5. sector === 'home' && !isOwnerOrAdmin()
  6. ? 'View Queue'
  7. : !isOwnerOrAdmin()
  8. ? 'Add Song to Queue'
  9. : 'Manage Station'
  10. "
  11. :style="`--primary-color: var(--${station.theme})`"
  12. class="manage-station-modal"
  13. :size="isOwnerOrAdmin() || sector !== 'home' ? 'wide' : null"
  14. :split="isOwnerOrAdmin() || sector !== 'home'"
  15. :intercept-close="true"
  16. @close="onCloseModal"
  17. >
  18. <template #body v-if="station && station._id">
  19. <div class="left-section">
  20. <div class="section">
  21. <div class="station-info-box-wrapper">
  22. <station-info-box
  23. :station="station"
  24. :station-paused="stationPaused"
  25. :show-go-to-station="sector !== 'station'"
  26. />
  27. </div>
  28. <div v-if="isOwnerOrAdmin() || sector !== 'home'">
  29. <div class="tab-selection">
  30. <button
  31. v-if="isOwnerOrAdmin()"
  32. class="button is-default"
  33. :class="{ selected: tab === 'settings' }"
  34. ref="settings-tab"
  35. @click="showTab('settings')"
  36. >
  37. Settings
  38. </button>
  39. <button
  40. v-if="canRequest()"
  41. class="button is-default"
  42. :class="{ selected: tab === 'request' }"
  43. ref="request-tab"
  44. @click="showTab('request')"
  45. >
  46. Request
  47. </button>
  48. <button
  49. v-if="
  50. isOwnerOrAdmin() && station.autofill.enabled
  51. "
  52. class="button is-default"
  53. :class="{ selected: tab === 'autofill' }"
  54. ref="autofill-tab"
  55. @click="showTab('autofill')"
  56. >
  57. Autofill
  58. </button>
  59. <button
  60. v-if="isOwnerOrAdmin()"
  61. class="button is-default"
  62. :class="{ selected: tab === 'blacklist' }"
  63. ref="blacklist-tab"
  64. @click="showTab('blacklist')"
  65. >
  66. Blacklist
  67. </button>
  68. </div>
  69. <settings
  70. v-if="isOwnerOrAdmin()"
  71. class="tab"
  72. v-show="tab === 'settings'"
  73. :modal-uuid="modalUuid"
  74. ref="settingsTabComponent"
  75. />
  76. <request
  77. v-if="canRequest()"
  78. class="tab"
  79. v-show="tab === 'request'"
  80. :sector="'manageStation'"
  81. :disable-auto-request="sector !== 'station'"
  82. :modal-uuid="modalUuid"
  83. />
  84. <playlist-tab-base
  85. v-if="isOwnerOrAdmin() && station.autofill.enabled"
  86. class="tab"
  87. v-show="tab === 'autofill'"
  88. :type="'autofill'"
  89. :modal-uuid="modalUuid"
  90. >
  91. <template #info>
  92. <p>
  93. Select playlists to automatically add songs
  94. within to the queue
  95. </p>
  96. </template>
  97. </playlist-tab-base>
  98. <playlist-tab-base
  99. v-if="isOwnerOrAdmin()"
  100. class="tab"
  101. v-show="tab === 'blacklist'"
  102. :type="'blacklist'"
  103. :modal-uuid="modalUuid"
  104. >
  105. <template #info>
  106. <p>
  107. Blacklist a playlist to prevent all songs
  108. within from playing in this station
  109. </p>
  110. </template>
  111. </playlist-tab-base>
  112. </div>
  113. </div>
  114. </div>
  115. <div class="right-section">
  116. <div class="section">
  117. <div class="queue-title">
  118. <h4 class="section-title">Queue</h4>
  119. </div>
  120. <hr class="section-horizontal-rule" />
  121. <song-item
  122. v-if="currentSong._id"
  123. :song="currentSong"
  124. :requested-by="true"
  125. header="Currently Playing.."
  126. class="currently-playing"
  127. />
  128. <queue :modal-uuid="modalUuid" sector="manageStation" />
  129. </div>
  130. </div>
  131. </template>
  132. <template #footer>
  133. <div v-if="isOwnerOrAdmin()" class="right">
  134. <quick-confirm @confirm="resetQueue()">
  135. <a class="button is-danger">Reset queue</a>
  136. </quick-confirm>
  137. <quick-confirm @confirm="removeStation()">
  138. <button class="button is-danger">Delete station</button>
  139. </quick-confirm>
  140. </div>
  141. </template>
  142. </modal>
  143. </template>
  144. <script>
  145. import { mapState, mapGetters, mapActions } from "vuex";
  146. import Toast from "toasters";
  147. import { mapModalState, mapModalActions } from "@/vuex_helpers";
  148. import Queue from "@/components/Queue.vue";
  149. import SongItem from "@/components/SongItem.vue";
  150. import StationInfoBox from "@/components/StationInfoBox.vue";
  151. import Settings from "./Settings.vue";
  152. import PlaylistTabBase from "@/components/PlaylistTabBase.vue";
  153. import Request from "@/components/Request.vue";
  154. export default {
  155. components: {
  156. Queue,
  157. SongItem,
  158. StationInfoBox,
  159. Settings,
  160. PlaylistTabBase,
  161. Request
  162. },
  163. props: {
  164. modalUuid: { type: String, default: "" }
  165. },
  166. computed: {
  167. ...mapState({
  168. loggedIn: state => state.user.auth.loggedIn,
  169. userId: state => state.user.auth.userId,
  170. role: state => state.user.auth.role
  171. }),
  172. ...mapModalState("modals/manageStation/MODAL_UUID", {
  173. stationId: state => state.stationId,
  174. sector: state => state.sector,
  175. tab: state => state.tab,
  176. station: state => state.station,
  177. songsList: state => state.songsList,
  178. stationPlaylist: state => state.stationPlaylist,
  179. autofill: state => state.autofill,
  180. blacklist: state => state.blacklist,
  181. stationPaused: state => state.stationPaused,
  182. currentSong: state => state.currentSong
  183. }),
  184. ...mapGetters({
  185. socket: "websockets/getSocket"
  186. })
  187. },
  188. watch: {
  189. // eslint-disable-next-line
  190. "station.requests": function (requests) {
  191. if (this.tab === "request" && !this.canRequest()) {
  192. if (this.isOwnerOrAdmin()) this.showTab("settings");
  193. else if (!(this.sector === "home" && !this.isOwnerOrAdmin()))
  194. this.closeModal("manageStation");
  195. }
  196. },
  197. // eslint-disable-next-line
  198. "station.autofill": function (autofill) {
  199. if (this.tab === "autofill" && autofill && !autofill.enabled)
  200. this.showTab("settings");
  201. }
  202. },
  203. mounted() {
  204. this.socket.dispatch(`stations.getStationById`, this.stationId, res => {
  205. if (res.status === "success") {
  206. const { station } = res.data;
  207. this.editStation(station);
  208. if (!this.isOwnerOrAdmin()) this.showTab("request");
  209. const currentSong = res.data.station.currentSong
  210. ? res.data.station.currentSong
  211. : {};
  212. this.updateCurrentSong(currentSong);
  213. this.updateStationPaused(res.data.station.paused);
  214. this.socket.dispatch(
  215. "stations.getStationAutofillPlaylistsById",
  216. this.stationId,
  217. res => {
  218. if (res.status === "success")
  219. this.setAutofillPlaylists(res.data.playlists);
  220. }
  221. );
  222. this.socket.dispatch(
  223. "stations.getStationBlacklistById",
  224. this.stationId,
  225. res => {
  226. if (res.status === "success")
  227. this.setBlacklist(res.data.playlists);
  228. }
  229. );
  230. if (this.isOwnerOrAdmin()) {
  231. this.socket.dispatch(
  232. "playlists.getPlaylistForStation",
  233. this.station._id,
  234. true,
  235. res => {
  236. if (res.status === "success") {
  237. this.updateStationPlaylist(res.data.playlist);
  238. }
  239. }
  240. );
  241. }
  242. this.socket.dispatch(
  243. "stations.getQueue",
  244. this.stationId,
  245. res => {
  246. if (res.status === "success")
  247. this.updateSongsList(res.data.queue);
  248. }
  249. );
  250. this.socket.dispatch(
  251. "apis.joinRoom",
  252. `manage-station.${this.stationId}`
  253. );
  254. this.socket.on(
  255. "event:station.updated",
  256. res => {
  257. this.updateStation(res.data.station);
  258. },
  259. { modalUuid: this.modalUuid }
  260. );
  261. this.socket.on(
  262. "event:station.autofillPlaylist",
  263. res => {
  264. const { playlist } = res.data;
  265. const playlistIndex = this.autofill
  266. .map(autofillPlaylist => autofillPlaylist._id)
  267. .indexOf(playlist._id);
  268. if (playlistIndex === -1) this.autofill.push(playlist);
  269. },
  270. { modalUuid: this.modalUuid }
  271. );
  272. this.socket.on(
  273. "event:station.blacklistedPlaylist",
  274. res => {
  275. const { playlist } = res.data;
  276. const playlistIndex = this.blacklist
  277. .map(blacklistedPlaylist => blacklistedPlaylist._id)
  278. .indexOf(playlist._id);
  279. if (playlistIndex === -1) this.blacklist.push(playlist);
  280. },
  281. { modalUuid: this.modalUuid }
  282. );
  283. this.socket.on(
  284. "event:station.removedAutofillPlaylist",
  285. res => {
  286. const { playlistId } = res.data;
  287. const playlistIndex = this.autofill
  288. .map(playlist => playlist._id)
  289. .indexOf(playlistId);
  290. if (playlistIndex >= 0)
  291. this.autofill.splice(playlistIndex, 1);
  292. },
  293. { modalUuid: this.modalUuid }
  294. );
  295. this.socket.on(
  296. "event:station.removedBlacklistedPlaylist",
  297. res => {
  298. const { playlistId } = res.data;
  299. const playlistIndex = this.blacklist
  300. .map(playlist => playlist._id)
  301. .indexOf(playlistId);
  302. if (playlistIndex >= 0)
  303. this.blacklist.splice(playlistIndex, 1);
  304. },
  305. { modalUuid: this.modalUuid }
  306. );
  307. this.socket.on(
  308. "event:station.deleted",
  309. () => {
  310. new Toast(`The station you were editing was deleted.`);
  311. this.closeModal("manageStation");
  312. },
  313. { modalUuid: this.modalUuid }
  314. );
  315. this.socket.on(
  316. "event:user.station.favorited",
  317. res => {
  318. if (res.data.stationId === this.stationId)
  319. this.updateIsFavorited(true);
  320. },
  321. { modalUuid: this.modalUuid }
  322. );
  323. this.socket.on(
  324. "event:user.station.unfavorited",
  325. res => {
  326. if (res.data.stationId === this.stationId)
  327. this.updateIsFavorited(false);
  328. },
  329. { modalUuid: this.modalUuid }
  330. );
  331. } else {
  332. new Toast(`Station with that ID not found`);
  333. this.closeModal("manageStation");
  334. }
  335. });
  336. this.socket.on(
  337. "event:manageStation.queue.updated",
  338. res => {
  339. if (res.data.stationId === this.station._id)
  340. this.updateSongsList(res.data.queue);
  341. },
  342. { modalUuid: this.modalUuid }
  343. );
  344. this.socket.on(
  345. "event:manageStation.queue.song.repositioned",
  346. res => {
  347. if (res.data.stationId === this.station._id)
  348. this.repositionSongInList(res.data.song);
  349. },
  350. { modalUuid: this.modalUuid }
  351. );
  352. this.socket.on(
  353. "event:station.pause",
  354. res => {
  355. if (res.data.stationId === this.station._id)
  356. this.updateStationPaused(true);
  357. },
  358. { modalUuid: this.modalUuid }
  359. );
  360. this.socket.on(
  361. "event:station.resume",
  362. res => {
  363. if (res.data.stationId === this.station._id)
  364. this.updateStationPaused(false);
  365. },
  366. { modalUuid: this.modalUuid }
  367. );
  368. this.socket.on(
  369. "event:station.nextSong",
  370. res => {
  371. if (res.data.stationId === this.station._id)
  372. this.updateCurrentSong(res.data.currentSong || {});
  373. },
  374. { modalUuid: this.modalUuid }
  375. );
  376. if (this.isOwnerOrAdmin()) {
  377. this.socket.on(
  378. "event:playlist.song.added",
  379. res => {
  380. if (this.stationPlaylist._id === res.data.playlistId)
  381. this.stationPlaylist.songs.push(res.data.song);
  382. },
  383. {
  384. modalUuid: this.modalUuid
  385. }
  386. );
  387. this.socket.on(
  388. "event:playlist.song.removed",
  389. res => {
  390. if (this.stationPlaylist._id === res.data.playlistId) {
  391. // remove song from array of playlists
  392. this.stationPlaylist.songs.forEach((song, index) => {
  393. if (song.youtubeId === res.data.youtubeId)
  394. this.stationPlaylist.songs.splice(index, 1);
  395. });
  396. }
  397. },
  398. {
  399. modalUuid: this.modalUuid
  400. }
  401. );
  402. this.socket.on(
  403. "event:playlist.songs.repositioned",
  404. res => {
  405. if (this.stationPlaylist._id === res.data.playlistId) {
  406. // for each song that has a new position
  407. res.data.songsBeingChanged.forEach(changedSong => {
  408. this.stationPlaylist.songs.forEach(
  409. (song, index) => {
  410. // find song locally
  411. if (
  412. song.youtubeId === changedSong.youtubeId
  413. ) {
  414. // change song position attribute
  415. this.stationPlaylist.songs[
  416. index
  417. ].position = changedSong.position;
  418. // reposition in array if needed
  419. if (index !== changedSong.position - 1)
  420. this.stationPlaylist.songs.splice(
  421. changedSong.position - 1,
  422. 0,
  423. this.stationPlaylist.songs.splice(
  424. index,
  425. 1
  426. )[0]
  427. );
  428. }
  429. }
  430. );
  431. });
  432. }
  433. },
  434. {
  435. modalUuid: this.modalUuid
  436. }
  437. );
  438. }
  439. },
  440. beforeUnmount() {
  441. this.socket.dispatch(
  442. "apis.leaveRoom",
  443. `manage-station.${this.stationId}`,
  444. () => {}
  445. );
  446. if (this.isOwnerOrAdmin()) this.showTab("settings");
  447. this.clearStation();
  448. // Delete the VueX module that was created for this modal, after all other cleanup tasks are performed
  449. this.$store.unregisterModule([
  450. "modals",
  451. "manageStation",
  452. this.modalUuid
  453. ]);
  454. },
  455. methods: {
  456. isOwner() {
  457. return (
  458. this.loggedIn &&
  459. this.station &&
  460. this.userId === this.station.owner
  461. );
  462. },
  463. isAdmin() {
  464. return this.loggedIn && this.role === "admin";
  465. },
  466. isOwnerOrAdmin() {
  467. return this.isOwner() || this.isAdmin();
  468. },
  469. canRequest() {
  470. return (
  471. this.station &&
  472. this.loggedIn &&
  473. this.station.requests &&
  474. this.station.requests.enabled &&
  475. (this.station.requests.access === "user" ||
  476. (this.station.requests.access === "owner" &&
  477. this.isOwnerOrAdmin()))
  478. );
  479. },
  480. removeStation() {
  481. this.socket.dispatch("stations.remove", this.station._id, res => {
  482. new Toast(res.message);
  483. });
  484. },
  485. resumeStation() {
  486. this.socket.dispatch("stations.resume", this.station._id, res => {
  487. if (res.status !== "success")
  488. new Toast(`Error: ${res.message}`);
  489. else new Toast("Successfully resumed the station.");
  490. });
  491. },
  492. pauseStation() {
  493. this.socket.dispatch("stations.pause", this.station._id, res => {
  494. if (res.status !== "success")
  495. new Toast(`Error: ${res.message}`);
  496. else new Toast("Successfully paused the station.");
  497. });
  498. },
  499. skipStation() {
  500. this.socket.dispatch(
  501. "stations.forceSkip",
  502. this.station._id,
  503. res => {
  504. if (res.status !== "success")
  505. new Toast(`Error: ${res.message}`);
  506. else
  507. new Toast(
  508. "Successfully skipped the station's current song."
  509. );
  510. }
  511. );
  512. },
  513. resetQueue() {
  514. this.socket.dispatch(
  515. "stations.resetQueue",
  516. this.station._id,
  517. res => {
  518. if (res.status !== "success")
  519. new Toast({
  520. content: `Error: ${res.message}`,
  521. timeout: 8000
  522. });
  523. else new Toast({ content: res.message, timeout: 4000 });
  524. }
  525. );
  526. },
  527. onCloseModal() {
  528. if (this.isOwnerOrAdmin() || this.sector !== "home")
  529. this.$refs.settingsTabComponent.onCloseModal();
  530. else this.closeModal("manageStation");
  531. },
  532. ...mapModalActions("modals/manageStation/MODAL_UUID", [
  533. "editStation",
  534. "setAutofillPlaylists",
  535. "setBlacklist",
  536. "clearStation",
  537. "updateSongsList",
  538. "updateStationPlaylist",
  539. "repositionSongInList",
  540. "updateStationPaused",
  541. "updateCurrentSong",
  542. "updateStation",
  543. "updateIsFavorited"
  544. ]),
  545. ...mapActions({
  546. showTab(dispatch, payload) {
  547. if (this.$refs[`${payload}-tab`])
  548. this.$refs[`${payload}-tab`].scrollIntoView({
  549. block: "nearest"
  550. }); // Only works if the ref exists, which it doesn't always
  551. return dispatch(
  552. `modals/manageStation/${this.modalUuid}/showTab`,
  553. payload
  554. );
  555. }
  556. }),
  557. ...mapActions("modalVisibility", ["openModal", "closeModal"])
  558. }
  559. };
  560. </script>
  561. <style lang="less">
  562. .manage-station-modal.modal .modal-card {
  563. .tab > button {
  564. width: 100%;
  565. margin-bottom: 10px;
  566. }
  567. .currently-playing.song-item {
  568. .thumbnail {
  569. min-width: 130px;
  570. width: 130px;
  571. height: 130px;
  572. }
  573. }
  574. }
  575. </style>
  576. <style lang="less" scoped>
  577. .night-mode {
  578. .manage-station-modal.modal .modal-card-body {
  579. .left-section {
  580. .station-info-box-wrapper {
  581. border: 0;
  582. }
  583. .section {
  584. background-color: transparent !important;
  585. }
  586. .tab-selection .button {
  587. background: var(--dark-grey);
  588. color: var(--white);
  589. }
  590. .tab {
  591. background-color: var(--dark-grey-3);
  592. border: 0;
  593. }
  594. }
  595. .right-section .section,
  596. #queue {
  597. border-radius: @border-radius;
  598. background-color: transparent !important;
  599. }
  600. }
  601. }
  602. .manage-station-modal.modal .modal-card-body {
  603. display: flex;
  604. flex-wrap: wrap;
  605. height: 100%;
  606. .left-section {
  607. .station-info-box-wrapper {
  608. border-radius: @border-radius;
  609. border: 1px solid var(--light-grey-3);
  610. overflow: hidden;
  611. margin-bottom: 20px;
  612. }
  613. .tab-selection {
  614. display: flex;
  615. overflow-x: auto;
  616. .button {
  617. border-radius: @border-radius @border-radius 0 0;
  618. border: 0;
  619. text-transform: uppercase;
  620. font-size: 14px;
  621. color: var(--dark-grey-3);
  622. background-color: var(--light-grey-2);
  623. flex-grow: 1;
  624. height: 32px;
  625. &:not(:first-of-type) {
  626. margin-left: 5px;
  627. }
  628. }
  629. .selected {
  630. background-color: var(--primary-color) !important;
  631. color: var(--white) !important;
  632. font-weight: 600;
  633. }
  634. }
  635. .tab {
  636. border: 1px solid var(--light-grey-3);
  637. padding: 15px 10px;
  638. border-radius: 0 0 @border-radius @border-radius;
  639. }
  640. }
  641. .right-section {
  642. .section {
  643. .queue-title {
  644. display: flex;
  645. line-height: 30px;
  646. .material-icons {
  647. margin-left: 5px;
  648. margin-bottom: 5px;
  649. font-size: 28px;
  650. cursor: pointer;
  651. &:first-of-type {
  652. margin-left: auto;
  653. }
  654. &.skip-station {
  655. color: var(--dark-red);
  656. }
  657. &.resume-station,
  658. &.pause-station {
  659. color: var(--primary-color);
  660. }
  661. }
  662. }
  663. .currently-playing {
  664. margin-bottom: 10px;
  665. }
  666. }
  667. }
  668. &.modal-wide .left-section .section:first-child {
  669. padding: 0 15px 15px !important;
  670. }
  671. }
  672. </style>