Songs.vue 11 KB

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