AddToPlaylistDropdown.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <div id="nav-dropdown">
  3. <div class="nav-dropdown-items" v-if="playlistsArr.length > 0">
  4. <!-- <a class="nav-item" id="nightmode-toggle">
  5. <span>Nightmode</span>
  6. <label class="switch">
  7. <input type="checkbox" checked />
  8. <span class="slider round"></span>
  9. </label>
  10. </a> -->
  11. <button
  12. class="nav-item"
  13. href="#"
  14. v-for="(playlist, index) in playlistsArr"
  15. :key="index"
  16. @click.prevent="toggleSongInPlaylist(playlist._id)"
  17. :title="playlist.displayName"
  18. >
  19. <p class="control is-expanded checkbox-control">
  20. <input
  21. type="checkbox"
  22. :id="index"
  23. v-model="playlists[playlist._id].hasSong"
  24. />
  25. <label :for="index">
  26. <span></span>
  27. <p>{{ playlist.displayName }}</p>
  28. </label>
  29. </p>
  30. </button>
  31. </div>
  32. <p class="nav-dropdown-items" id="no-playlists" v-else>
  33. You haven't created any playlists.
  34. </p>
  35. </div>
  36. </template>
  37. <script>
  38. import { mapState } from "vuex";
  39. import Toast from "toasters";
  40. import io from "../../../io";
  41. export default {
  42. data() {
  43. return {
  44. playlists: {},
  45. playlistsArr: [],
  46. songId: null,
  47. song: null
  48. };
  49. },
  50. computed: {
  51. ...mapState("station", {
  52. currentSong: state => state.currentSong
  53. })
  54. },
  55. mounted() {
  56. this.songId = this.currentSong.songId;
  57. this.song = this.currentSong;
  58. io.getSocket(socket => {
  59. this.socket = socket;
  60. this.socket.emit("playlists.indexMyPlaylists", false, res => {
  61. if (res.status === "success") {
  62. res.data.forEach(playlist => {
  63. this.playlists[playlist._id] = playlist;
  64. });
  65. this.recalculatePlaylists();
  66. }
  67. });
  68. });
  69. },
  70. methods: {
  71. toggleSongInPlaylist(playlistId) {
  72. if (!this.playlists[playlistId].hasSong) {
  73. this.socket.emit(
  74. "playlists.addSongToPlaylist",
  75. false,
  76. this.currentSong.songId,
  77. playlistId,
  78. res => {
  79. new Toast({ content: res.message, timeout: 4000 });
  80. if (res.status === "success") {
  81. this.playlists[playlistId].songs.push(this.song);
  82. }
  83. this.recalculatePlaylists();
  84. }
  85. );
  86. } else {
  87. this.socket.emit(
  88. "playlists.removeSongFromPlaylist",
  89. this.songId,
  90. playlistId,
  91. res => {
  92. new Toast({ content: res.message, timeout: 4000 });
  93. if (res.status === "success") {
  94. this.playlists[playlistId].songs.forEach(
  95. (song, index) => {
  96. if (song.songId === this.songId)
  97. this.playlists[playlistId].songs.splice(
  98. index,
  99. 1
  100. );
  101. }
  102. );
  103. }
  104. this.recalculatePlaylists();
  105. }
  106. );
  107. }
  108. },
  109. recalculatePlaylists() {
  110. this.playlistsArr = Object.values(this.playlists).map(playlist => {
  111. let hasSong = false;
  112. for (let i = 0; i < playlist.songs.length; i += 1) {
  113. if (playlist.songs[i].songId === this.songId) {
  114. hasSong = true;
  115. }
  116. }
  117. playlist.hasSong = hasSong; // eslint-disable-line no-param-reassign
  118. this.playlists[playlist._id] = playlist;
  119. return playlist;
  120. });
  121. }
  122. }
  123. };
  124. </script>
  125. <style lang="scss" scoped>
  126. @import "../../../styles/global.scss";
  127. .night-mode {
  128. .nav-dropdown-items {
  129. background-color: $dark-grey-2;
  130. .nav-item {
  131. background-color: $dark-grey;
  132. &:focus {
  133. outline-color: $dark-grey;
  134. }
  135. p {
  136. color: #fff;
  137. }
  138. }
  139. }
  140. }
  141. #nav-dropdown {
  142. position: absolute;
  143. margin-left: 4px;
  144. z-index: 1;
  145. margin-bottom: 36px;
  146. }
  147. #nav-dropdown-triangle {
  148. border-style: solid;
  149. border-width: 15px 15px 0 15px;
  150. border-color: $dark-grey-2 transparent transparent transparent;
  151. }
  152. .nav-dropdown-items {
  153. border: 1px solid $light-grey-2;
  154. border-bottom: 0;
  155. background-color: #fff;
  156. padding: 5px;
  157. border-radius: 5px;
  158. .nav-item {
  159. width: 100%;
  160. justify-content: flex-start;
  161. border: 0;
  162. padding: 10px;
  163. font-size: 15.5px;
  164. height: 36px;
  165. background: #eee;
  166. border-radius: 5px;
  167. cursor: pointer;
  168. .checkbox-control {
  169. display: flex;
  170. align-items: center;
  171. margin-bottom: 0 !important;
  172. input {
  173. margin-right: 5px;
  174. }
  175. input[type="checkbox"] {
  176. opacity: 0;
  177. position: absolute;
  178. }
  179. label {
  180. display: flex;
  181. flex-direction: row;
  182. align-items: center;
  183. span {
  184. cursor: pointer;
  185. width: 24px;
  186. height: 24px;
  187. background-color: $white;
  188. display: inline-block;
  189. border: 1px solid $dark-grey-2;
  190. position: relative;
  191. border-radius: 3px;
  192. }
  193. p {
  194. margin-left: 10px;
  195. cursor: pointer;
  196. color: #000;
  197. }
  198. }
  199. input[type="checkbox"]:checked + label span::after {
  200. content: "";
  201. width: 18px;
  202. height: 18px;
  203. left: 2px;
  204. top: 2px;
  205. border-radius: 3px;
  206. background-color: var(--station-theme);
  207. position: absolute;
  208. }
  209. }
  210. &:focus {
  211. outline-color: $light-grey-2;
  212. }
  213. &:not(:last-of-type) {
  214. margin-bottom: 5px;
  215. }
  216. }
  217. }
  218. #nightmode-toggle {
  219. display: flex;
  220. justify-content: space-evenly;
  221. }
  222. /* CSS - Toggle Switch */
  223. .switch {
  224. position: relative;
  225. display: inline-block;
  226. width: 50px;
  227. height: 24px;
  228. input {
  229. opacity: 0;
  230. width: 0;
  231. height: 0;
  232. }
  233. }
  234. .slider {
  235. position: absolute;
  236. cursor: pointer;
  237. top: 0;
  238. left: 0;
  239. right: 0;
  240. bottom: 0;
  241. background-color: #ccc;
  242. transition: 0.4s;
  243. border-radius: 34px;
  244. &:before {
  245. position: absolute;
  246. content: "";
  247. height: 16px;
  248. width: 16px;
  249. left: 4px;
  250. bottom: 4px;
  251. background-color: white;
  252. transition: 0.4s;
  253. border-radius: 50%;
  254. }
  255. }
  256. input:checked + .slider {
  257. background-color: $primary-color;
  258. }
  259. input:focus + .slider {
  260. box-shadow: 0 0 1px $primary-color;
  261. }
  262. input:checked + .slider:before {
  263. transform: translateX(26px);
  264. }
  265. #no-playlists {
  266. padding: 10px;
  267. }
  268. </style>