Songs.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. <template>
  2. <div class="songs">
  3. <div class="tabs-container">
  4. <div class="tab-selection">
  5. <button
  6. class="button is-default"
  7. :class="{ selected: tab === 'search' }"
  8. v-if="isAllowedToParty()"
  9. @click="showTab('search')"
  10. >
  11. Search
  12. </button>
  13. <button
  14. class="button is-default"
  15. :class="{ selected: tab === 'included' }"
  16. v-if="isOwnerOrAdmin() && isPlaylistMode()"
  17. @click="showTab('included')"
  18. >
  19. Included
  20. </button>
  21. <button
  22. class="button is-default"
  23. :class="{ selected: tab === 'excluded' }"
  24. v-if="isOwnerOrAdmin()"
  25. @click="showTab('excluded')"
  26. >
  27. Excluded
  28. </button>
  29. </div>
  30. <div
  31. class="tab"
  32. v-show="tab === 'search'"
  33. v-if="
  34. station.type === 'community' &&
  35. station.partyMode &&
  36. (isOwnerOrAdmin() || !station.locked)
  37. "
  38. >
  39. <div class="musare-songs">
  40. <label class="label"> Search for a song on Musare </label>
  41. <div class="control is-grouped input-with-button">
  42. <p class="control is-expanded">
  43. <input
  44. class="input"
  45. type="text"
  46. placeholder="Enter your song query here..."
  47. v-model="musareSearch.query"
  48. @keyup.enter="searchForMusareSongs(1)"
  49. />
  50. </p>
  51. <p class="control">
  52. <a
  53. class="button is-info"
  54. @click="searchForMusareSongs(1)"
  55. ><i class="material-icons icon-with-button"
  56. >search</i
  57. >Search</a
  58. >
  59. </p>
  60. </div>
  61. <div v-if="musareSearch.results.length > 0">
  62. <song-item
  63. v-for="song in musareSearch.results"
  64. :key="song._id"
  65. :song="song"
  66. >
  67. <template #actions>
  68. <i
  69. class="material-icons add-to-queue-icon"
  70. v-if="station.partyMode && !station.locked"
  71. @click="addSongToQueue(song.youtubeId)"
  72. content="Add Song to Queue"
  73. v-tippy
  74. >queue</i
  75. >
  76. </template>
  77. </song-item>
  78. <button
  79. v-if="resultsLeftCount > 0"
  80. class="button is-primary load-more-button"
  81. @click="searchForMusareSongs(musareSearch.page + 1)"
  82. >
  83. Load {{ nextPageResultsCount }} more results
  84. </button>
  85. </div>
  86. </div>
  87. <div class="youtube-search">
  88. <label class="label"> Search for a song on YouTube </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 YouTube query here..."
  95. v-model="search.songs.query"
  96. autofocus
  97. @keyup.enter="searchForSongs()"
  98. />
  99. </p>
  100. <p class="control">
  101. <a
  102. class="button is-info"
  103. @click.prevent="searchForSongs()"
  104. ><i class="material-icons icon-with-button"
  105. >search</i
  106. >Search</a
  107. >
  108. </p>
  109. </div>
  110. <div
  111. v-if="search.songs.results.length > 0"
  112. id="song-query-results"
  113. >
  114. <search-query-item
  115. v-for="(result, index) in search.songs.results"
  116. :key="result.id"
  117. :result="result"
  118. >
  119. <template #actions>
  120. <transition
  121. name="search-query-actions"
  122. mode="out-in"
  123. >
  124. <a
  125. class="button is-success"
  126. v-if="result.isAddedToQueue"
  127. key="added-to-queue"
  128. >
  129. <i
  130. class="
  131. material-icons
  132. icon-with-button
  133. "
  134. >done</i
  135. >
  136. Added to queue
  137. </a>
  138. <a
  139. class="button is-dark"
  140. v-else
  141. @click.prevent="
  142. addSongToQueue(result.id, index)
  143. "
  144. key="add-to-queue"
  145. >
  146. <i
  147. class="
  148. material-icons
  149. icon-with-button
  150. "
  151. >add</i
  152. >
  153. Add to queue
  154. </a>
  155. </transition>
  156. </template>
  157. </search-query-item>
  158. <a
  159. class="button is-primary load-more-button"
  160. @click.prevent="loadMoreSongs()"
  161. >
  162. Load more...
  163. </a>
  164. </div>
  165. </div>
  166. </div>
  167. <div
  168. class="tab"
  169. v-show="tab === 'included'"
  170. v-if="
  171. isOwnerOrAdmin() &&
  172. !(station.type === 'community' && station.partyMode)
  173. "
  174. >
  175. <div v-if="stationPlaylist.songs.length > 0">
  176. <div id="playlist-info-section">
  177. <h5>Song Count: {{ stationPlaylist.songs.length }}</h5>
  178. <h5>Duration: {{ totalLength(stationPlaylist) }}</h5>
  179. </div>
  180. <song-item
  181. v-for="song in stationPlaylist.songs"
  182. :key="song._id"
  183. :song="song"
  184. >
  185. </song-item>
  186. </div>
  187. <p v-else class="has-text-centered scrollable-list">
  188. No songs currently included. To include songs, include a
  189. playlist.
  190. </p>
  191. </div>
  192. <div
  193. class="tab"
  194. v-show="tab === 'excluded'"
  195. v-if="isOwnerOrAdmin()"
  196. >
  197. <div v-if="excludedSongs.length > 0">
  198. <div id="playlist-info-section" class="section">
  199. <h5>Song Count: {{ excludedSongs.length }}</h5>
  200. </div>
  201. <song-item
  202. v-for="song in excludedSongs"
  203. :key="song._id"
  204. :song="song"
  205. >
  206. </song-item>
  207. </div>
  208. <p v-else class="has-text-centered scrollable-list">
  209. No songs currently excluded. To excluded songs, exclude a
  210. playlist.
  211. </p>
  212. </div>
  213. </div>
  214. </div>
  215. </template>
  216. <script>
  217. import { mapState, mapGetters } from "vuex";
  218. import Toast from "toasters";
  219. import SearchYoutube from "@/mixins/SearchYoutube.vue";
  220. import SongItem from "@/components/SongItem.vue";
  221. import SearchQueryItem from "../../../SearchQueryItem.vue";
  222. import utils from "../../../../../js/utils";
  223. export default {
  224. components: {
  225. SongItem,
  226. SearchQueryItem
  227. },
  228. mixins: [SearchYoutube],
  229. data() {
  230. return {
  231. utils,
  232. tab: "search",
  233. musareSearch: {
  234. query: "",
  235. searchedQuery: "",
  236. page: 0,
  237. count: 0,
  238. resultsLeft: 0,
  239. results: []
  240. }
  241. };
  242. },
  243. computed: {
  244. resultsLeftCount() {
  245. return this.musareSearch.count - this.musareSearch.results.length;
  246. },
  247. nextPageResultsCount() {
  248. return Math.min(this.musareSearch.pageSize, this.resultsLeftCount);
  249. },
  250. excludedSongs() {
  251. return this.excludedPlaylists
  252. .map(playlist => playlist.songs)
  253. .flat()
  254. .filter((song, index, self) => self.indexOf(song) === index);
  255. },
  256. excludedSongIds() {
  257. return this.excludedSongs.map(excludedSong => excludedSong._id);
  258. },
  259. ...mapState({
  260. loggedIn: state => state.user.auth.loggedIn,
  261. userId: state => state.user.auth.userId,
  262. role: state => state.user.auth.role
  263. }),
  264. ...mapState("modals/manageStation", {
  265. parentTab: state => state.tab,
  266. station: state => state.station,
  267. originalStation: state => state.originalStation,
  268. excludedPlaylists: state => state.excludedPlaylists,
  269. stationPlaylist: state => state.stationPlaylist
  270. }),
  271. ...mapGetters({
  272. socket: "websockets/getSocket"
  273. })
  274. },
  275. watch: {
  276. // eslint-disable-next-line func-names
  277. parentTab(value) {
  278. if (value === "songs") {
  279. if (this.tab === "search" && this.isPlaylistMode()) {
  280. this.showTab("included");
  281. } else if (this.tab === "included" && this.isPartyMode()) {
  282. this.showTab("search");
  283. }
  284. }
  285. }
  286. },
  287. methods: {
  288. showTab(tab) {
  289. this.tab = tab;
  290. },
  291. isOwner() {
  292. return (
  293. this.loggedIn &&
  294. this.station &&
  295. this.userId === this.station.owner
  296. );
  297. },
  298. isAdmin() {
  299. return this.loggedIn && this.role === "admin";
  300. },
  301. isOwnerOrAdmin() {
  302. return this.isOwner() || this.isAdmin();
  303. },
  304. isPartyMode() {
  305. return (
  306. this.station &&
  307. this.station.type === "community" &&
  308. this.station.partyMode
  309. );
  310. },
  311. isAllowedToParty() {
  312. return (
  313. this.station &&
  314. this.isPartyMode() &&
  315. (!this.station.locked || this.isOwnerOrAdmin()) &&
  316. this.loggedIn
  317. );
  318. },
  319. isPlaylistMode() {
  320. return this.station && !this.isPartyMode();
  321. },
  322. totalLength(playlist) {
  323. let length = 0;
  324. playlist.songs.forEach(song => {
  325. length += song.duration;
  326. });
  327. return this.utils.formatTimeLong(length);
  328. },
  329. addSongToQueue(youtubeId, index) {
  330. if (this.station.type === "community") {
  331. this.socket.dispatch(
  332. "stations.addToQueue",
  333. this.station._id,
  334. youtubeId,
  335. res => {
  336. if (res.status !== "success")
  337. new Toast(`Error: ${res.message}`);
  338. else {
  339. if (index)
  340. this.search.songs.results[
  341. index
  342. ].isAddedToQueue = true;
  343. new Toast(res.message);
  344. }
  345. }
  346. );
  347. } else {
  348. this.socket.dispatch("songs.request", youtubeId, res => {
  349. if (res.status !== "success")
  350. new Toast(`Error: ${res.message}`);
  351. else {
  352. this.search.songs.results[index].isAddedToQueue = true;
  353. new Toast(res.message);
  354. }
  355. });
  356. }
  357. },
  358. searchForMusareSongs(page) {
  359. if (
  360. this.musareSearch.page >= page ||
  361. this.musareSearch.searchedQuery !== this.musareSearch.query
  362. ) {
  363. this.musareSearch.results = [];
  364. this.musareSearch.page = 0;
  365. this.musareSearch.count = 0;
  366. this.musareSearch.resultsLeft = 0;
  367. this.musareSearch.pageSize = 0;
  368. }
  369. this.musareSearch.searchedQuery = this.musareSearch.query;
  370. this.socket.dispatch(
  371. "songs.searchOfficial",
  372. this.musareSearch.query,
  373. page,
  374. res => {
  375. const { data } = res;
  376. const { count, pageSize, songs } = data;
  377. if (res.status === "success") {
  378. this.musareSearch.results = [
  379. ...this.musareSearch.results,
  380. ...songs
  381. ];
  382. this.musareSearch.page = page;
  383. this.musareSearch.count = count;
  384. this.musareSearch.resultsLeft =
  385. count - this.musareSearch.results.length;
  386. this.musareSearch.pageSize = pageSize;
  387. } else if (res.status === "error") {
  388. this.musareSearch.results = [];
  389. this.musareSearch.page = 0;
  390. this.musareSearch.count = 0;
  391. this.musareSearch.resultsLeft = 0;
  392. this.musareSearch.pageSize = 0;
  393. new Toast(res.message);
  394. }
  395. }
  396. );
  397. }
  398. }
  399. };
  400. </script>
  401. <style lang="scss" scoped>
  402. .songs {
  403. .tabs-container {
  404. .tab-selection {
  405. display: flex;
  406. overflow-x: auto;
  407. .button {
  408. border-radius: 0;
  409. border: 0;
  410. text-transform: uppercase;
  411. font-size: 14px;
  412. color: var(--dark-grey-3);
  413. background-color: var(--light-grey-2);
  414. flex-grow: 1;
  415. height: 32px;
  416. &:not(:first-of-type) {
  417. margin-left: 5px;
  418. }
  419. }
  420. .selected {
  421. background-color: var(--primary-color) !important;
  422. color: var(--white) !important;
  423. font-weight: 600;
  424. }
  425. }
  426. .tab {
  427. padding: 15px 0;
  428. border-radius: 0;
  429. .playlist-item:not(:last-of-type),
  430. .item.item-draggable:not(:last-of-type) {
  431. margin-bottom: 10px;
  432. }
  433. .load-more-button {
  434. width: 100%;
  435. margin-top: 10px;
  436. }
  437. }
  438. }
  439. .musare-songs,
  440. .universal-item:not(:last-of-type) {
  441. margin-bottom: 10px;
  442. }
  443. .load-more-button {
  444. width: 100%;
  445. margin-top: 10px;
  446. }
  447. #playlist-info-section {
  448. border: 1px solid var(--light-grey-3);
  449. border-radius: 3px;
  450. padding: 15px !important;
  451. margin-bottom: 16px;
  452. h3 {
  453. font-weight: 600;
  454. font-size: 30px;
  455. }
  456. h5 {
  457. font-size: 18px;
  458. }
  459. h3,
  460. h5 {
  461. margin: 0;
  462. }
  463. }
  464. }
  465. </style>