Queue.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 MediaItem = defineAsyncComponent(
  11. () => import("@/components/MediaItem.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 = mediaSource => {
  66. socket.dispatch(
  67. "stations.removeFromQueue",
  68. station.value._id,
  69. mediaSource,
  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, song }) => {
  78. const { oldIndex, newIndex } = moved;
  79. if (oldIndex === newIndex) return; // we only need to update when song is moved
  80. const _song = 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. repositionSongInQueue({
  103. moved: {
  104. oldIndex: index,
  105. newIndex: 0
  106. },
  107. song: queue.value[index]
  108. });
  109. };
  110. const moveSongToBottom = index => {
  111. songItems.value[`song-item-${index}`].$refs.songActions.tippy.hide();
  112. repositionSongInQueue({
  113. moved: {
  114. oldIndex: index,
  115. newIndex: queue.value.length - 1
  116. },
  117. song: queue.value[index]
  118. });
  119. };
  120. onUpdated(() => {
  121. // check if actionable button is visible, if not: set max-height of queue items to 100%
  122. actionableButtonVisible.value =
  123. document
  124. .getElementById("queue")
  125. .querySelectorAll(".tab-actionable-button").length > 0;
  126. });
  127. defineEmits(["onChangeTab"]);
  128. </script>
  129. <template>
  130. <div id="queue">
  131. <div class="inner-queue">
  132. <div
  133. v-if="queue.length > 0"
  134. :class="{
  135. 'actionable-button-hidden': !actionableButtonVisible,
  136. 'scrollable-list': true
  137. }"
  138. >
  139. <draggable-list
  140. v-model:list="queue"
  141. item-key="mediaSource"
  142. @start="drag = true"
  143. @end="drag = false"
  144. @update="repositionSongInQueue"
  145. :disabled="!hasPermission('stations.queue.reposition')"
  146. >
  147. <template #item="{ element, index }">
  148. <media-item
  149. :song="element"
  150. :requested-by="true"
  151. :requested-type="true"
  152. :disabled-actions="[]"
  153. :ref="el => (songItems[`song-item-${index}`] = el)"
  154. :key="`queue-song-item-${element.mediaSource}`"
  155. >
  156. <template
  157. v-if="
  158. hasPermission('stations.queue.reposition')
  159. "
  160. #tippyActions
  161. >
  162. <quick-confirm
  163. v-if="
  164. hasPermission('stations.queue.remove')
  165. "
  166. placement="left"
  167. @confirm="
  168. removeFromQueue(element.mediaSource)
  169. "
  170. >
  171. <i
  172. class="material-icons delete-icon"
  173. content="Remove Song from Queue"
  174. v-tippy
  175. >delete_forever</i
  176. >
  177. </quick-confirm>
  178. <i
  179. class="material-icons"
  180. v-if="index > 0"
  181. @click="moveSongToTop(index)"
  182. content="Move to top of Queue"
  183. v-tippy
  184. >vertical_align_top</i
  185. >
  186. <i
  187. v-if="queue.length - 1 !== index"
  188. @click="moveSongToBottom(index)"
  189. class="material-icons"
  190. content="Move to bottom of Queue"
  191. v-tippy
  192. >vertical_align_bottom</i
  193. >
  194. </template>
  195. </media-item>
  196. </template>
  197. </draggable-list>
  198. </div>
  199. <p class="nothing-here-text has-text-centered" v-else>
  200. There are no songs currently queued
  201. </p>
  202. <button
  203. v-if="canRequest() && sector === 'station'"
  204. class="floating button is-primary"
  205. @click="$emit('onChangeTab', 'request')"
  206. >
  207. <i class="material-icons icon-with-button">playlist_play</i>
  208. <span>Add song to queue</span>
  209. </button>
  210. </div>
  211. </div>
  212. </template>
  213. <style lang="less" scoped>
  214. .night-mode {
  215. #queue {
  216. background-color: var(--dark-grey-3) !important;
  217. border: 0 !important;
  218. }
  219. }
  220. #queue {
  221. background-color: var(--white);
  222. border-radius: 0 0 @border-radius @border-radius;
  223. user-select: none;
  224. .inner-queue {
  225. position: relative;
  226. height: 100%;
  227. width: 100%;
  228. .actionable-button-hidden {
  229. max-height: 100%;
  230. }
  231. #queue-locked {
  232. display: flex;
  233. justify-content: center;
  234. }
  235. button.disabled {
  236. filter: grayscale(0.4);
  237. }
  238. > .scrollable-list:last-of-type:not(:last-child) {
  239. height: calc(100% - 56px);
  240. }
  241. .button.floating {
  242. position: sticky;
  243. z-index: 10;
  244. bottom: 10px;
  245. right: 10px;
  246. left: 10px;
  247. width: calc(100% - 20px);
  248. margin-top: 50px;
  249. }
  250. }
  251. }
  252. </style>