Playlists.vue 8.7 KB

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