Queue.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <script setup lang="ts">
  2. import { defineAsyncComponent, ref, computed, onUpdated } from "vue";
  3. import Toast from "toasters";
  4. import { DraggableList } from "vue-draggable-list";
  5. import { storeToRefs } from "pinia";
  6. import { useWebsocketsStore } from "@/stores/websockets";
  7. import { useStationStore } from "@/stores/station";
  8. import { useManageStationStore } from "@/stores/manageStation";
  9. import { useUserAuthStore } from "@/stores/userAuth";
  10. const SongItem = defineAsyncComponent(
  11. () => import("@/components/SongItem.vue")
  12. );
  13. const QuickConfirm = defineAsyncComponent(
  14. () => import("@/components/QuickConfirm.vue")
  15. );
  16. const props = defineProps({
  17. modalUuid: { type: String, default: null },
  18. sector: { type: String, default: "station" }
  19. });
  20. const userAuthStore = useUserAuthStore();
  21. const { loggedIn } = storeToRefs(userAuthStore);
  22. const { socket } = useWebsocketsStore();
  23. const stationStore = useStationStore();
  24. const manageStationStore = useManageStationStore({
  25. modalUuid: props.modalUuid
  26. });
  27. const actionableButtonVisible = ref(false);
  28. const drag = ref(false);
  29. const songItems = ref([]);
  30. const station = computed({
  31. get: () => {
  32. if (props.sector === "manageStation") return manageStationStore.station;
  33. return stationStore.station;
  34. },
  35. set: value => {
  36. if (props.sector === "manageStation")
  37. manageStationStore.updateStation(value);
  38. else stationStore.updateStation(value);
  39. }
  40. });
  41. const queue = computed({
  42. get: () => {
  43. if (props.sector === "manageStation")
  44. return manageStationStore.songsList;
  45. return stationStore.songsList;
  46. },
  47. set: value => {
  48. if (props.sector === "manageStation")
  49. manageStationStore.updateSongsList(value);
  50. else stationStore.updateSongsList(value);
  51. }
  52. });
  53. const hasPermission = permission =>
  54. props.sector === "manageStation"
  55. ? manageStationStore.hasPermission(permission)
  56. : stationStore.hasPermission(permission);
  57. const canRequest = () =>
  58. station.value &&
  59. loggedIn.value &&
  60. station.value.requests &&
  61. station.value.requests.enabled &&
  62. (station.value.requests.access === "user" ||
  63. (station.value.requests.access === "owner" &&
  64. hasPermission("stations.request")));
  65. const removeFromQueue = youtubeId => {
  66. socket.dispatch(
  67. "stations.removeFromQueue",
  68. station.value._id,
  69. youtubeId,
  70. res => {
  71. if (res.status === "success")
  72. new Toast("Successfully removed song from the queue.");
  73. else new Toast(res.message);
  74. }
  75. );
  76. };
  77. const repositionSongInQueue = ({ moved }) => {
  78. const { oldIndex, newIndex } = moved;
  79. if (oldIndex === newIndex) return; // we only need to update when song is moved
  80. const song = queue.value[newIndex];
  81. socket.dispatch(
  82. "stations.repositionSongInQueue",
  83. station.value._id,
  84. {
  85. ...song,
  86. oldIndex,
  87. newIndex
  88. },
  89. res => {
  90. new Toast({ content: res.message, timeout: 4000 });
  91. if (res.status !== "success")
  92. queue.value.splice(
  93. oldIndex,
  94. 0,
  95. queue.value.splice(newIndex, 1)[0]
  96. );
  97. }
  98. );
  99. };
  100. const moveSongToTop = index => {
  101. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  102. queue.value.splice(0, 0, queue.value.splice(index, 1)[0]);
  103. repositionSongInQueue({
  104. moved: {
  105. oldIndex: index,
  106. newIndex: 0
  107. }
  108. });
  109. };
  110. const moveSongToBottom = index => {
  111. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  112. queue.value.splice(
  113. queue.value.length - 1,
  114. 0,
  115. queue.value.splice(index, 1)[0]
  116. );
  117. repositionSongInQueue({
  118. moved: {
  119. oldIndex: index,
  120. newIndex: queue.value.length - 1
  121. }
  122. });
  123. };
  124. onUpdated(() => {
  125. // check if actionable button is visible, if not: set max-height of queue items to 100%
  126. actionableButtonVisible.value =
  127. document
  128. .getElementById("queue")
  129. .querySelectorAll(".tab-actionable-button").length > 0;
  130. });
  131. defineEmits(["onChangeTab"]);
  132. </script>
  133. <template>
  134. <div id="queue">
  135. <div class="inner-queue">
  136. <div
  137. v-if="queue.length > 0"
  138. :class="{
  139. 'actionable-button-hidden': !actionableButtonVisible,
  140. 'scrollable-list': true
  141. }"
  142. >
  143. <draggable-list
  144. v-model:list="queue"
  145. item-key="youtubeId"
  146. @start="drag = true"
  147. @end="drag = false"
  148. @update="repositionSongInQueue"
  149. :disabled="!hasPermission('stations.queue.reposition')"
  150. >
  151. <template #item="{ element, index }">
  152. <song-item
  153. :song="element"
  154. :requested-by="true"
  155. :requested-type="true"
  156. :disabled-actions="[]"
  157. :ref="el => (songItems[`song-item-${index}`] = el)"
  158. :key="`queue-song-item-${element.youtubeId}`"
  159. >
  160. <template
  161. v-if="
  162. hasPermission('stations.queue.reposition')
  163. "
  164. #tippyActions
  165. >
  166. <quick-confirm
  167. v-if="
  168. hasPermission('stations.queue.remove')
  169. "
  170. placement="left"
  171. @confirm="
  172. removeFromQueue(element.youtubeId)
  173. "
  174. >
  175. <i
  176. class="material-icons delete-icon"
  177. content="Remove Song from Queue"
  178. v-tippy
  179. >delete_forever</i
  180. >
  181. </quick-confirm>
  182. <i
  183. class="material-icons"
  184. v-if="index > 0"
  185. @click="moveSongToTop(index)"
  186. content="Move to top of Queue"
  187. v-tippy
  188. >vertical_align_top</i
  189. >
  190. <i
  191. v-if="queue.length - 1 !== index"
  192. @click="moveSongToBottom(index)"
  193. class="material-icons"
  194. content="Move to bottom of Queue"
  195. v-tippy
  196. >vertical_align_bottom</i
  197. >
  198. </template>
  199. </song-item>
  200. </template>
  201. </draggable-list>
  202. </div>
  203. <p class="nothing-here-text has-text-centered" v-else>
  204. There are no songs currently queued
  205. </p>
  206. <button
  207. v-if="canRequest() && sector === 'station'"
  208. class="floating button is-primary"
  209. @click="$emit('onChangeTab', 'request')"
  210. >
  211. <i class="material-icons icon-with-button">playlist_play</i>
  212. <span>Add song to queue</span>
  213. </button>
  214. </div>
  215. </div>
  216. </template>
  217. <style lang="less" scoped>
  218. .night-mode {
  219. #queue {
  220. background-color: var(--dark-grey-3) !important;
  221. border: 0 !important;
  222. }
  223. }
  224. #queue {
  225. background-color: var(--white);
  226. border-radius: 0 0 @border-radius @border-radius;
  227. user-select: none;
  228. .inner-queue {
  229. position: relative;
  230. height: 100%;
  231. width: 100%;
  232. .actionable-button-hidden {
  233. max-height: 100%;
  234. }
  235. #queue-locked {
  236. display: flex;
  237. justify-content: center;
  238. }
  239. button.disabled {
  240. filter: grayscale(0.4);
  241. }
  242. > .scrollable-list:last-of-type:not(:last-child) {
  243. padding-bottom: 50px;
  244. }
  245. .button.floating {
  246. position: sticky;
  247. z-index: 10;
  248. bottom: 8px;
  249. right: 8px;
  250. left: 8px;
  251. width: calc(100% - 16px);
  252. margin-top: 50px;
  253. }
  254. }
  255. }
  256. </style>