Playlists.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <template>
  2. <div id="my-playlists">
  3. <div class="menu-list scrollable-list" v-if="playlists.length > 0">
  4. <draggable
  5. tag="transition-group"
  6. :component-data="{
  7. name: !drag ? 'draggable-list-transition' : null
  8. }"
  9. v-model="playlists"
  10. item-key="_id"
  11. v-bind="dragOptions"
  12. @start="drag = true"
  13. @end="drag = false"
  14. @change="savePlaylistOrder"
  15. >
  16. <template #item="{element}">
  17. <playlist-item :playlist="element" class="item-draggable">
  18. <template #actions>
  19. <div class="icons-group">
  20. <i
  21. v-if="isExcluded(element._id)"
  22. class="material-icons stop-icon"
  23. content="This playlist is blacklisted in this station"
  24. v-tippy="{ theme: 'info' }"
  25. >play_disabled</i
  26. >
  27. <i
  28. v-if="
  29. station.type === 'community' &&
  30. (isOwnerOrAdmin() ||
  31. station.partyMode) &&
  32. !isSelected(element._id) &&
  33. !isExcluded(element._id)
  34. "
  35. @click="selectPlaylist(element)"
  36. class="material-icons play-icon"
  37. :content="
  38. station.partyMode
  39. ? 'Request songs from this playlist'
  40. : 'Play songs from this playlist'
  41. "
  42. v-tippy
  43. >play_arrow</i
  44. >
  45. <confirm
  46. v-if="
  47. station.type === 'community' &&
  48. (isOwnerOrAdmin() ||
  49. station.partyMode) &&
  50. isSelected(element._id)
  51. "
  52. @confirm="deselectPlaylist(element._id)"
  53. >
  54. <i
  55. class="material-icons stop-icon"
  56. :content="
  57. station.partyMode
  58. ? 'Stop requesting songs from this playlist'
  59. : 'Stop playing songs from this playlist'
  60. "
  61. v-tippy
  62. >stop</i
  63. >
  64. </confirm>
  65. <confirm
  66. v-if="
  67. isOwnerOrAdmin() &&
  68. !isExcluded(element._id)
  69. "
  70. @confirm="blacklistPlaylist(element._id)"
  71. >
  72. <i
  73. class="material-icons stop-icon"
  74. content="Blacklist Playlist"
  75. v-tippy
  76. >block</i
  77. >
  78. </confirm>
  79. <i
  80. @click="edit(element._id)"
  81. class="material-icons edit-icon"
  82. content="Edit Playlist"
  83. v-tippy
  84. >edit</i
  85. >
  86. </div>
  87. </template>
  88. </playlist-item>
  89. </template>
  90. </draggable>
  91. </div>
  92. <p v-else class="nothing-here-text scrollable-list">
  93. No Playlists found
  94. </p>
  95. <a
  96. class="button create-playlist tab-actionable-button"
  97. href="#"
  98. @click="openModal('createPlaylist')"
  99. >
  100. <i class="material-icons icon-with-button">create</i>
  101. <span> Create Playlist </span>
  102. </a>
  103. </div>
  104. </template>
  105. <script>
  106. import { mapState, mapActions, mapGetters } from "vuex";
  107. import Toast from "toasters";
  108. import PlaylistItem from "@/components/PlaylistItem.vue";
  109. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  110. import Confirm from "@/components/Confirm.vue";
  111. export default {
  112. components: { PlaylistItem, Confirm },
  113. mixins: [SortablePlaylists],
  114. computed: {
  115. currentPlaylists() {
  116. if (this.station.type === "community" && this.station.partyMode)
  117. return this.partyPlaylists;
  118. return this.includedPlaylists;
  119. },
  120. ...mapState({
  121. role: state => state.user.auth.role,
  122. userId: state => state.user.auth.userId,
  123. loggedIn: state => state.user.auth.loggedIn
  124. }),
  125. ...mapState("station", {
  126. partyPlaylists: state => state.partyPlaylists,
  127. includedPlaylists: state => state.includedPlaylists,
  128. excludedPlaylists: state => state.excludedPlaylists,
  129. songsList: state => state.songsList
  130. }),
  131. ...mapGetters({
  132. socket: "websockets/getSocket"
  133. })
  134. },
  135. mounted() {
  136. /** Get playlists for user */
  137. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  138. if (res.status === "success") this.setPlaylists(res.data.playlists);
  139. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  140. });
  141. this.socket.on("event:station.includedPlaylist", res => {
  142. const { playlist } = res.data;
  143. const playlistIndex = this.includedPlaylists
  144. .map(includedPlaylist => includedPlaylist._id)
  145. .indexOf(playlist._id);
  146. if (playlistIndex === -1) this.includedPlaylists.push(playlist);
  147. });
  148. this.socket.on("event:station.excludedPlaylist", res => {
  149. const { playlist } = res.data;
  150. const playlistIndex = this.excludedPlaylists
  151. .map(excludedPlaylist => excludedPlaylist._id)
  152. .indexOf(playlist._id);
  153. if (playlistIndex === -1) this.excludedPlaylists.push(playlist);
  154. });
  155. this.socket.on("event:station.removedIncludedPlaylist", res => {
  156. const { playlistId } = res.data;
  157. const playlistIndex = this.includedPlaylists
  158. .map(playlist => playlist._id)
  159. .indexOf(playlistId);
  160. if (playlistIndex >= 0)
  161. this.includedPlaylists.splice(playlistIndex, 1);
  162. });
  163. this.socket.on("event:station.removedExcludedPlaylist", res => {
  164. const { playlistId } = res.data;
  165. const playlistIndex = this.excludedPlaylists
  166. .map(playlist => playlist._id)
  167. .indexOf(playlistId);
  168. if (playlistIndex >= 0)
  169. this.excludedPlaylists.splice(playlistIndex, 1);
  170. });
  171. },
  172. methods: {
  173. isOwner() {
  174. return this.loggedIn && this.userId === this.station.owner;
  175. },
  176. isAdmin() {
  177. return this.loggedIn && this.role === "admin";
  178. },
  179. isOwnerOrAdmin() {
  180. return this.isOwner() || this.isAdmin();
  181. },
  182. edit(id) {
  183. this.editPlaylist(id);
  184. this.openModal("editPlaylist");
  185. },
  186. selectPlaylist(playlist) {
  187. if (this.station.type === "community" && this.station.partyMode) {
  188. if (!this.isSelected(playlist.id)) {
  189. this.partyPlaylists.push(playlist);
  190. this.addPartyPlaylistSongToQueue();
  191. new Toast(
  192. "Successfully selected playlist to auto request songs."
  193. );
  194. } else {
  195. new Toast("Error: Playlist already selected.");
  196. }
  197. } else {
  198. this.socket.dispatch(
  199. "stations.includePlaylist",
  200. this.station._id,
  201. playlist._id,
  202. res => {
  203. new Toast(res.message);
  204. }
  205. );
  206. }
  207. },
  208. deselectPlaylist(id) {
  209. return new Promise(resolve => {
  210. if (
  211. this.station.type === "community" &&
  212. this.station.partyMode
  213. ) {
  214. let selected = false;
  215. this.currentPlaylists.forEach((playlist, index) => {
  216. if (playlist._id === id) {
  217. selected = true;
  218. this.partyPlaylists.splice(index, 1);
  219. }
  220. });
  221. if (selected) {
  222. new Toast("Successfully deselected playlist.");
  223. resolve();
  224. } else {
  225. new Toast("Playlist not selected.");
  226. resolve();
  227. }
  228. } else {
  229. this.socket.dispatch(
  230. "stations.removeIncludedPlaylist",
  231. this.station._id,
  232. id,
  233. res => {
  234. new Toast(res.message);
  235. resolve();
  236. }
  237. );
  238. }
  239. });
  240. },
  241. isSelected(id) {
  242. let selected = false;
  243. this.currentPlaylists.forEach(playlist => {
  244. if (playlist._id === id) selected = true;
  245. });
  246. return selected;
  247. },
  248. isExcluded(id) {
  249. let selected = false;
  250. this.excludedPlaylists.forEach(playlist => {
  251. if (playlist._id === id) selected = true;
  252. });
  253. return selected;
  254. },
  255. async blacklistPlaylist(id) {
  256. if (this.isSelected(id)) await this.deselectPlaylist(id);
  257. this.socket.dispatch(
  258. "stations.excludePlaylist",
  259. this.station._id,
  260. id,
  261. res => {
  262. new Toast(res.message);
  263. }
  264. );
  265. },
  266. addPartyPlaylistSongToQueue() {
  267. let isInQueue = false;
  268. if (
  269. this.station.type === "community" &&
  270. this.station.partyMode === true
  271. ) {
  272. this.songsList.forEach(queueSong => {
  273. if (queueSong.requestedBy === this.userId) isInQueue = true;
  274. });
  275. if (!isInQueue && this.partyPlaylists) {
  276. const selectedPlaylist = this.partyPlaylists[
  277. Math.floor(Math.random() * this.partyPlaylists.length)
  278. ];
  279. if (
  280. selectedPlaylist._id &&
  281. selectedPlaylist.songs.length > 0
  282. ) {
  283. const selectedSong =
  284. selectedPlaylist.songs[
  285. Math.floor(
  286. Math.random() *
  287. selectedPlaylist.songs.length
  288. )
  289. ];
  290. if (selectedSong.youtubeId) {
  291. this.socket.dispatch(
  292. "stations.addToQueue",
  293. this.station._id,
  294. selectedSong.youtubeId,
  295. data => {
  296. if (data.status !== "success")
  297. new Toast("Error auto queueing song");
  298. }
  299. );
  300. }
  301. }
  302. }
  303. }
  304. },
  305. ...mapActions("station", ["updatePartyPlaylists"]),
  306. ...mapActions("modalVisibility", ["openModal"]),
  307. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  308. }
  309. };
  310. </script>
  311. <style lang="scss" scoped>
  312. #my-playlists {
  313. background-color: var(--white);
  314. margin-bottom: 20px;
  315. border-radius: 0 0 5px 5px;
  316. max-height: 100%;
  317. }
  318. .night-mode {
  319. #my-playlists {
  320. background-color: var(--dark-grey-3) !important;
  321. border: 0 !important;
  322. }
  323. .draggable-list-ghost {
  324. filter: brightness(95%);
  325. }
  326. }
  327. .nothing-here-text {
  328. margin-bottom: 10px;
  329. }
  330. .icons-group {
  331. display: flex;
  332. align-items: center;
  333. .edit-icon {
  334. color: var(--primary-color);
  335. }
  336. }
  337. .menu-list .playlist-item:not(:last-of-type) {
  338. margin-bottom: 10px;
  339. }
  340. .create-playlist {
  341. width: 100%;
  342. height: 40px;
  343. border-radius: 5px;
  344. border: 0;
  345. &:active,
  346. &:focus {
  347. border: 0;
  348. }
  349. }
  350. .draggable-list-transition-move {
  351. transition: transform 0.5s;
  352. }
  353. .draggable-list-ghost {
  354. opacity: 0.5;
  355. filter: brightness(95%);
  356. }
  357. </style>