QueueSongs.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <div v-scroll="handleScroll">
  3. <metadata title="Admin | Queue songs" />
  4. <div class="container">
  5. <p>
  6. <span>Sets loaded: {{ setsLoaded }} / {{ maxSets }}</span>
  7. <br />
  8. <span>Loaded songs: {{ this.songs.length }}</span>
  9. </p>
  10. <input
  11. v-model="searchQuery"
  12. type="text"
  13. class="input"
  14. placeholder="Search for Songs"
  15. />
  16. <button
  17. v-if="!loadAllSongs"
  18. class="button is-primary"
  19. @click="loadAll()"
  20. >
  21. Load all
  22. </button>
  23. <br />
  24. <br />
  25. <table class="table is-striped">
  26. <thead>
  27. <tr>
  28. <td>Thumbnail</td>
  29. <td>Title</td>
  30. <td>Artists</td>
  31. <td>Genres</td>
  32. <td>ID / YouTube ID</td>
  33. <td>Requested By</td>
  34. <td>Options</td>
  35. </tr>
  36. </thead>
  37. <tbody>
  38. <tr
  39. v-for="(song, index) in filteredSongs"
  40. :key="index"
  41. tabindex="0"
  42. v-on:keydown.prevent.38
  43. v-on:keydown.prevent.40
  44. v-on:keyup.38="selectPrevious($event)"
  45. v-on:keyup.40="selectNext($event)"
  46. v-on:keyup.69="edit(song, index)"
  47. v-on:keyup.65="add(song)"
  48. v-on:keyup.88="remove(song._id, index)"
  49. >
  50. <td>
  51. <img
  52. class="song-thumbnail"
  53. :src="song.thumbnail"
  54. onerror="this.src='/assets/notes-transparent.png'"
  55. />
  56. </td>
  57. <td>
  58. <strong>{{ song.title }}</strong>
  59. </td>
  60. <td>{{ song.artists.join(", ") }}</td>
  61. <td>{{ song.genres.join(", ") }}</td>
  62. <td>
  63. {{ song._id }}
  64. <br />
  65. <a
  66. :href="
  67. 'https://www.youtube.com/watch?v=' +
  68. `${song.songId}`
  69. "
  70. target="_blank"
  71. >
  72. {{ song.songId }}</a
  73. >
  74. </td>
  75. <td>
  76. <user-id-to-username
  77. :userId="song.requestedBy"
  78. :link="true"
  79. />
  80. </td>
  81. <td class="optionsColumn">
  82. <button
  83. class="button is-primary"
  84. @click="edit(song, index)"
  85. >
  86. <i class="material-icons">edit</i>
  87. </button>
  88. <button
  89. class="button is-success"
  90. @click="add(song)"
  91. >
  92. <i class="material-icons">add</i>
  93. </button>
  94. <button
  95. class="button is-danger"
  96. @click="remove(song._id, index)"
  97. >
  98. <i class="material-icons">cancel</i>
  99. </button>
  100. </td>
  101. </tr>
  102. </tbody>
  103. </table>
  104. </div>
  105. <edit-song v-if="modals.editSong" />
  106. </div>
  107. </template>
  108. <script>
  109. import { mapState, mapActions } from "vuex";
  110. import Vue from "vue";
  111. import Toast from "toasters";
  112. import EditSong from "../Modals/EditSong.vue";
  113. import UserIdToUsername from "../UserIdToUsername.vue";
  114. import io from "../../io";
  115. export default {
  116. components: { EditSong, UserIdToUsername },
  117. data() {
  118. return {
  119. position: 1,
  120. maxPosition: 1,
  121. searchQuery: "",
  122. songs: [],
  123. gettingSet: false,
  124. loadAllSongs: false
  125. };
  126. },
  127. computed: {
  128. filteredSongs() {
  129. return this.songs.filter(
  130. song =>
  131. JSON.stringify(Object.values(song)).indexOf(
  132. this.searchQuery
  133. ) !== -1
  134. );
  135. },
  136. setsLoaded() {
  137. return this.position - 1;
  138. },
  139. maxSets() {
  140. return this.maxPosition - 1;
  141. },
  142. ...mapState("modals", {
  143. modals: state => state.modals.admin
  144. })
  145. },
  146. watch: {
  147. // eslint-disable-next-line func-names
  148. "modals.editSong": function(value) {
  149. if (value === false) this.stopVideo();
  150. }
  151. },
  152. methods: {
  153. edit(song, index) {
  154. const newSong = {};
  155. Object.keys(song).forEach(n => {
  156. newSong[n] = song[n];
  157. });
  158. this.editSong({ index, song: newSong, type: "queueSongs" });
  159. this.openModal({ sector: "admin", modal: "editSong" });
  160. },
  161. add(song) {
  162. this.socket.emit("songs.add", song, res => {
  163. if (res.status === "success")
  164. new Toast({ content: res.message, timeout: 2000 });
  165. else new Toast({ content: res.message, timeout: 4000 });
  166. });
  167. },
  168. remove(id) {
  169. this.socket.emit("queueSongs.remove", id, res => {
  170. if (res.status === "success")
  171. new Toast({ content: res.message, timeout: 2000 });
  172. else new Toast({ content: res.message, timeout: 4000 });
  173. });
  174. },
  175. getSet() {
  176. if (this.gettingSet) return;
  177. if (this.position >= this.maxPosition) return;
  178. this.gettingSet = true;
  179. this.socket.emit("queueSongs.getSet", this.position, data => {
  180. data.forEach(song => {
  181. this.songs.push(song);
  182. });
  183. this.position += 1;
  184. this.gettingSet = false;
  185. if (this.loadAllSongs && this.maxPosition > this.position - 1)
  186. setTimeout(() => {
  187. this.getSet();
  188. }, 500);
  189. });
  190. },
  191. handleScroll() {
  192. const scrollPosition = document.body.clientHeight + window.scrollY;
  193. const bottomPosition = document.body.scrollHeight;
  194. if (this.loadAllSongs) return false;
  195. if (scrollPosition + 50 >= bottomPosition) this.getSet();
  196. return this.maxPosition === this.position;
  197. },
  198. loadAll() {
  199. this.loadAllSongs = true;
  200. this.getSet();
  201. },
  202. selectPrevious(event) {
  203. event.preventDefault();
  204. if (event.srcElement.previousElementSibling)
  205. event.srcElement.previousElementSibling.focus();
  206. },
  207. selectNext(event) {
  208. event.preventDefault();
  209. if (event.srcElement.nextElementSibling)
  210. event.srcElement.nextElementSibling.focus();
  211. },
  212. init() {
  213. if (this.songs.length > 0)
  214. this.position = Math.ceil(this.songs.length / 15) + 1;
  215. this.socket.emit("queueSongs.length", length => {
  216. this.maxPosition = Math.ceil(length / 15) + 1;
  217. this.getSet();
  218. });
  219. this.socket.emit("apis.joinAdminRoom", "queue", () => {});
  220. },
  221. ...mapActions("admin/songs", ["stopVideo", "editSong"]),
  222. ...mapActions("modals", ["openModal"])
  223. },
  224. mounted() {
  225. io.getSocket(socket => {
  226. this.socket = socket;
  227. this.socket.on("event:admin.queueSong.added", queueSong => {
  228. this.songs.push(queueSong);
  229. });
  230. this.socket.on("event:admin.queueSong.removed", songId => {
  231. this.songs = this.songs.filter(song => {
  232. return song._id !== songId;
  233. });
  234. });
  235. this.socket.on("event:admin.queueSong.updated", updatedSong => {
  236. for (let i = 0; i < this.songs.length; i += 1) {
  237. const song = this.songs[i];
  238. if (song._id === updatedSong._id) {
  239. Vue.set(this.songs, i, updatedSong);
  240. }
  241. }
  242. });
  243. if (this.socket.connected) {
  244. this.init();
  245. }
  246. io.onConnect(() => {
  247. this.init();
  248. });
  249. });
  250. }
  251. };
  252. </script>
  253. <style lang="scss" scoped>
  254. @import "styles/global.scss";
  255. .night-mode {
  256. .table {
  257. color: #ddd;
  258. background-color: #222;
  259. thead tr {
  260. background: $night-mode-secondary;
  261. td {
  262. color: #fff;
  263. }
  264. }
  265. tbody tr:hover {
  266. background-color: #111 !important;
  267. }
  268. tbody tr:nth-child(even) {
  269. background-color: #444;
  270. }
  271. strong {
  272. color: #ddd;
  273. }
  274. }
  275. }
  276. .optionsColumn {
  277. width: 140px;
  278. button {
  279. width: 35px;
  280. }
  281. }
  282. .song-thumbnail {
  283. display: block;
  284. max-width: 50px;
  285. margin: 0 auto;
  286. }
  287. td {
  288. vertical-align: middle;
  289. }
  290. .is-primary:focus {
  291. background-color: $primary-color !important;
  292. }
  293. </style>