modals.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { defineStore } from "pinia";
  2. import { defineAsyncComponent } from "vue";
  3. import ws from "@/ws";
  4. import { useEditUserStore } from "@/stores/editUser";
  5. import { useEditSongStore } from "@/stores/editSong";
  6. import { useBulkActionsStore } from "@/stores/bulkActions";
  7. import { useConfirmStore } from "@/stores/confirm";
  8. import { useCreateStationStore } from "@/stores/createStation";
  9. import { useEditNewsStore } from "@/stores/editNews";
  10. import { useEditPlaylistStore } from "@/stores/editPlaylist";
  11. import { useImportAlbumStore } from "@/stores/importAlbum";
  12. import { useManageStationStore } from "@/stores/manageStation";
  13. import { useRemoveAccountStore } from "@/stores/removeAccount";
  14. import { useReportStore } from "@/stores/report";
  15. import { useViewApiRequestStore } from "@/stores/viewApiRequest";
  16. import { useViewPunishmentStore } from "@/stores/viewPunishment";
  17. import { useViewReportStore } from "@/stores/viewReport";
  18. import { useViewYoutubeVideoStore } from "@/stores/viewYoutubeVideo";
  19. import { useWhatIsNewStore } from "@/stores/whatIsNew";
  20. export const useModalsStore = defineStore("modals", {
  21. state: () => ({
  22. modals: {},
  23. activeModals: []
  24. }),
  25. actions: {
  26. closeModal(modal) {
  27. if (modal === "register")
  28. lofig.get("recaptcha.enabled").then(enabled => {
  29. if (enabled) window.location.reload();
  30. });
  31. Object.entries(this.modals).forEach(([uuid, _modal]) => {
  32. if (modal === _modal) {
  33. ws.destroyModalListeners(uuid);
  34. this.activeModals.splice(
  35. this.activeModals.indexOf(uuid),
  36. 1
  37. );
  38. delete this.modals[uuid];
  39. }
  40. });
  41. },
  42. openModal(dataOrModal) {
  43. const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
  44. /[xy]/g,
  45. symbol => {
  46. let array;
  47. if (symbol === "y") {
  48. array = ["8", "9", "a", "b"];
  49. return array[Math.floor(Math.random() * array.length)];
  50. }
  51. array = new Uint8Array(1);
  52. window.crypto.getRandomValues(array);
  53. return (array[0] % 16).toString(16);
  54. }
  55. );
  56. let modal;
  57. let data;
  58. if (typeof dataOrModal === "string") modal = dataOrModal;
  59. else {
  60. modal = dataOrModal.modal;
  61. data = dataOrModal.data;
  62. }
  63. this.modals[uuid] = modal;
  64. let store;
  65. switch (modal) {
  66. case "editUser":
  67. store = useEditUserStore({ modalUuid: uuid });
  68. break;
  69. case "editSong":
  70. store = useEditSongStore({ modalUuid: uuid });
  71. break;
  72. case "bulkActions":
  73. store = useBulkActionsStore({ modalUuid: uuid });
  74. break;
  75. case "confirm":
  76. store = useConfirmStore({ modalUuid: uuid });
  77. break;
  78. case "createStation":
  79. store = useCreateStationStore({ modalUuid: uuid });
  80. break;
  81. case "editNews":
  82. store = useEditNewsStore({ modalUuid: uuid });
  83. break;
  84. case "editPlaylist":
  85. store = useEditPlaylistStore({ modalUuid: uuid });
  86. break;
  87. case "importAlbum":
  88. store = useImportAlbumStore({ modalUuid: uuid });
  89. break;
  90. case "manageStation":
  91. store = useManageStationStore({ modalUuid: uuid });
  92. break;
  93. case "removeAccount":
  94. store = useRemoveAccountStore({ modalUuid: uuid });
  95. break;
  96. case "report":
  97. store = useReportStore({ modalUuid: uuid });
  98. break;
  99. case "viewApiRequest":
  100. store = useViewApiRequestStore({ modalUuid: uuid });
  101. break;
  102. case "viewPunishment":
  103. store = useViewPunishmentStore({ modalUuid: uuid });
  104. break;
  105. case "viewReport":
  106. store = useViewReportStore({ modalUuid: uuid });
  107. break;
  108. case "viewYoutubeVideo":
  109. store = useViewYoutubeVideoStore({ modalUuid: uuid });
  110. break;
  111. case "whatIsNew":
  112. store = useWhatIsNewStore({ modalUuid: uuid });
  113. break;
  114. default:
  115. break;
  116. }
  117. if (store && typeof store.init === "function" && data)
  118. store.init(data);
  119. this.activeModals.push(uuid);
  120. return { uuid };
  121. },
  122. closeCurrentModal() {
  123. const currentlyActiveModalUuid =
  124. this.activeModals[this.activeModals.length - 1];
  125. // TODO: make sure to only destroy/register modal listeners for a unique modal
  126. // remove any websocket listeners for the modal
  127. ws.destroyModalListeners(currentlyActiveModalUuid);
  128. this.activeModals.pop();
  129. delete this.modals[currentlyActiveModalUuid];
  130. },
  131. closeAllModals() {
  132. this.activeModals.forEach(modalUuid => {
  133. ws.destroyModalListeners(modalUuid);
  134. });
  135. this.activeModals = [];
  136. this.modals = {};
  137. }
  138. }
  139. });
  140. export const useModalComponents = (baseDirectory, map) => {
  141. const modalComponents = {};
  142. Object.entries(map).forEach(([mapKey, mapValue]) => {
  143. modalComponents[mapKey] = defineAsyncComponent(
  144. () => import(`@/${baseDirectory}/${mapValue}`)
  145. );
  146. });
  147. return modalComponents;
  148. };