Playlists.vue 9.1 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. <i
  20. v-if="isExcluded(element._id)"
  21. class="material-icons stop-icon"
  22. content="This playlist is blacklisted in this station"
  23. v-tippy="{ theme: 'info' }"
  24. >play_disabled</i
  25. >
  26. <i
  27. v-if="
  28. station.type === 'community' &&
  29. (isOwnerOrAdmin() || station.partyMode) &&
  30. !isSelected(element._id) &&
  31. !isExcluded(element._id)
  32. "
  33. @click="selectPlaylist(element)"
  34. class="material-icons play-icon"
  35. :content="
  36. station.partyMode
  37. ? 'Request songs from this playlist'
  38. : 'Play songs from this playlist'
  39. "
  40. v-tippy
  41. >play_arrow</i
  42. >
  43. <confirm
  44. v-if="
  45. station.type === 'community' &&
  46. (isOwnerOrAdmin() || station.partyMode) &&
  47. isSelected(element._id)
  48. "
  49. @confirm="deselectPlaylist(element._id)"
  50. >
  51. <i
  52. class="material-icons stop-icon"
  53. :content="
  54. station.partyMode
  55. ? 'Stop requesting songs from this playlist'
  56. : 'Stop playing songs from this playlist'
  57. "
  58. v-tippy
  59. >stop</i
  60. >
  61. </confirm>
  62. <confirm
  63. v-if="
  64. isOwnerOrAdmin() && !isExcluded(element._id)
  65. "
  66. @confirm="blacklistPlaylist(element._id)"
  67. >
  68. <i
  69. class="material-icons stop-icon"
  70. content="Blacklist Playlist"
  71. v-tippy
  72. >block</i
  73. >
  74. </confirm>
  75. <i
  76. @click="edit(element._id)"
  77. class="material-icons edit-icon"
  78. content="Edit Playlist"
  79. v-tippy
  80. >edit</i
  81. >
  82. </template>
  83. </playlist-item>
  84. </template>
  85. </draggable>
  86. </div>
  87. <p v-else class="nothing-here-text scrollable-list">
  88. No Playlists found
  89. </p>
  90. <a
  91. class="button create-playlist tab-actionable-button"
  92. href="#"
  93. @click="openModal('createPlaylist')"
  94. >
  95. <i class="material-icons icon-with-button">create</i>
  96. <span> Create Playlist </span>
  97. </a>
  98. </div>
  99. </template>
  100. <script>
  101. import { mapState, mapActions, mapGetters } from "vuex";
  102. import Toast from "toasters";
  103. import ws from "@/ws";
  104. import PlaylistItem from "@/components/PlaylistItem.vue";
  105. import SortablePlaylists from "@/mixins/SortablePlaylists.vue";
  106. import Confirm from "@/components/Confirm.vue";
  107. export default {
  108. components: { PlaylistItem, Confirm },
  109. mixins: [SortablePlaylists],
  110. computed: {
  111. currentPlaylists() {
  112. if (this.station.type === "community" && this.station.partyMode)
  113. return this.partyPlaylists;
  114. return this.includedPlaylists;
  115. },
  116. ...mapState({
  117. role: state => state.user.auth.role,
  118. userId: state => state.user.auth.userId,
  119. loggedIn: state => state.user.auth.loggedIn
  120. }),
  121. ...mapState("station", {
  122. partyPlaylists: state => state.partyPlaylists,
  123. includedPlaylists: state => state.includedPlaylists,
  124. excludedPlaylists: state => state.excludedPlaylists,
  125. songsList: state => state.songsList
  126. }),
  127. ...mapGetters({
  128. socket: "websockets/getSocket"
  129. })
  130. },
  131. mounted() {
  132. ws.onConnect(this.init);
  133. this.socket.on("event:station.includedPlaylist", res => {
  134. const { playlist } = res.data;
  135. const playlistIndex = this.includedPlaylists
  136. .map(includedPlaylist => includedPlaylist._id)
  137. .indexOf(playlist._id);
  138. if (playlistIndex === -1) this.includedPlaylists.push(playlist);
  139. });
  140. this.socket.on("event:station.excludedPlaylist", res => {
  141. const { playlist } = res.data;
  142. const playlistIndex = this.excludedPlaylists
  143. .map(excludedPlaylist => excludedPlaylist._id)
  144. .indexOf(playlist._id);
  145. if (playlistIndex === -1) this.excludedPlaylists.push(playlist);
  146. });
  147. this.socket.on("event:station.removedIncludedPlaylist", res => {
  148. const { playlistId } = res.data;
  149. const playlistIndex = this.includedPlaylists
  150. .map(playlist => playlist._id)
  151. .indexOf(playlistId);
  152. if (playlistIndex >= 0)
  153. this.includedPlaylists.splice(playlistIndex, 1);
  154. });
  155. this.socket.on("event:station.removedExcludedPlaylist", res => {
  156. const { playlistId } = res.data;
  157. const playlistIndex = this.excludedPlaylists
  158. .map(playlist => playlist._id)
  159. .indexOf(playlistId);
  160. if (playlistIndex >= 0)
  161. this.excludedPlaylists.splice(playlistIndex, 1);
  162. });
  163. },
  164. methods: {
  165. init() {
  166. /** Get playlists for user */
  167. this.socket.dispatch("playlists.indexMyPlaylists", true, res => {
  168. if (res.status === "success")
  169. this.setPlaylists(res.data.playlists);
  170. this.orderOfPlaylists = this.calculatePlaylistOrder(); // order in regards to the database
  171. });
  172. },
  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 =
  277. this.partyPlaylists[
  278. Math.floor(
  279. Math.random() * this.partyPlaylists.length
  280. )
  281. ];
  282. if (
  283. selectedPlaylist._id &&
  284. selectedPlaylist.songs.length > 0
  285. ) {
  286. const selectedSong =
  287. selectedPlaylist.songs[
  288. Math.floor(
  289. Math.random() *
  290. selectedPlaylist.songs.length
  291. )
  292. ];
  293. if (selectedSong.youtubeId) {
  294. this.socket.dispatch(
  295. "stations.addToQueue",
  296. this.station._id,
  297. selectedSong.youtubeId,
  298. data => {
  299. if (data.status !== "success")
  300. new Toast("Error auto queueing song");
  301. }
  302. );
  303. }
  304. }
  305. }
  306. }
  307. },
  308. ...mapActions("station", ["updatePartyPlaylists"]),
  309. ...mapActions("modalVisibility", ["openModal"]),
  310. ...mapActions("user/playlists", ["editPlaylist", "setPlaylists"])
  311. }
  312. };
  313. </script>
  314. <style lang="scss" scoped>
  315. #my-playlists {
  316. background-color: var(--white);
  317. margin-bottom: 20px;
  318. border-radius: 0 0 5px 5px;
  319. max-height: 100%;
  320. }
  321. .night-mode {
  322. #my-playlists {
  323. background-color: var(--dark-grey-3) !important;
  324. border: 0 !important;
  325. }
  326. .draggable-list-ghost {
  327. filter: brightness(95%);
  328. }
  329. }
  330. .nothing-here-text {
  331. margin-bottom: 10px;
  332. }
  333. .icons-group {
  334. .edit-icon {
  335. color: var(--primary-color);
  336. }
  337. }
  338. .menu-list .playlist-item:not(:last-of-type) {
  339. margin-bottom: 10px;
  340. }
  341. .create-playlist {
  342. width: 100%;
  343. height: 40px;
  344. border-radius: 5px;
  345. border: 0;
  346. &:active,
  347. &:focus {
  348. border: 0;
  349. }
  350. }
  351. .draggable-list-transition-move {
  352. transition: transform 0.5s;
  353. }
  354. .draggable-list-ghost {
  355. opacity: 0.5;
  356. filter: brightness(95%);
  357. }
  358. </style>