modalVisibility.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* eslint no-param-reassign: 0 */
  2. import ws from "@/ws";
  3. import editUser from "./modals/editUser";
  4. import whatIsNew from "./modals/whatIsNew";
  5. import createStation from "./modals/createStation";
  6. import editNews from "./modals/editNews";
  7. import manageStation from "./modals/manageStation";
  8. import editPlaylist from "./modals/editPlaylist";
  9. import report from "./modals/report";
  10. import viewReport from "./modals/viewReport";
  11. import bulkActions from "./modals/bulkActions";
  12. import viewApiRequest from "./modals/viewApiRequest";
  13. import viewPunishment from "./modals/viewPunishment";
  14. import importAlbum from "./modals/importAlbum";
  15. import confirm from "./modals/confirm";
  16. import editSongs from "./modals/editSongs";
  17. import editSong from "./modals/editSong";
  18. import viewYoutubeVideo from "./modals/viewYoutubeVideo";
  19. const state = {
  20. modals: {},
  21. activeModals: []
  22. };
  23. const modalModules = {
  24. editUser,
  25. whatIsNew,
  26. createStation,
  27. editNews,
  28. manageStation,
  29. editPlaylist,
  30. report,
  31. viewReport,
  32. bulkActions,
  33. viewApiRequest,
  34. viewPunishment,
  35. importAlbum,
  36. confirm,
  37. editSongs,
  38. editSong,
  39. viewYoutubeVideo
  40. };
  41. const getters = {};
  42. const actions = {
  43. closeModal: ({ commit }, modal) => {
  44. if (modal === "register")
  45. lofig.get("recaptcha.enabled").then(enabled => {
  46. if (enabled) window.location.reload();
  47. });
  48. commit("closeModal", modal);
  49. },
  50. openModal: ({ commit }, dataOrModal) =>
  51. new Promise(resolve => {
  52. const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
  53. /[xy]/g,
  54. symbol => {
  55. let array;
  56. if (symbol === "y") {
  57. array = ["8", "9", "a", "b"];
  58. return array[Math.floor(Math.random() * array.length)];
  59. }
  60. array = new Uint8Array(1);
  61. window.crypto.getRandomValues(array);
  62. return (array[0] % 16).toString(16);
  63. }
  64. );
  65. if (typeof dataOrModal === "string")
  66. commit("openModal", { modal: dataOrModal, uuid });
  67. else commit("openModal", { ...dataOrModal, uuid });
  68. resolve({ uuid });
  69. }),
  70. closeCurrentModal: ({ commit }) => {
  71. commit("closeCurrentModal");
  72. },
  73. closeAllModals: ({ commit }) => {
  74. commit("closeAllModals");
  75. }
  76. };
  77. const mutations = {
  78. closeModal(state, modal) {
  79. Object.entries(state.modals).forEach(([uuid, _modal]) => {
  80. if (modal === _modal) {
  81. ws.destroyModalListeners(uuid);
  82. state.activeModals.splice(state.activeModals.indexOf(uuid), 1);
  83. delete state.modals[uuid];
  84. }
  85. });
  86. },
  87. openModal(state, { modal, uuid, data }) {
  88. state.modals[uuid] = modal;
  89. if (modalModules[modal]) {
  90. this.registerModule(["modals", modal, uuid], modalModules[modal]);
  91. if (data) this.dispatch(`modals/${modal}/${uuid}/init`, data);
  92. }
  93. state.activeModals.push(uuid);
  94. },
  95. closeCurrentModal(state) {
  96. const currentlyActiveModalUuid =
  97. state.activeModals[state.activeModals.length - 1];
  98. // TODO: make sure to only destroy/register modal listeners for a unique modal
  99. // remove any websocket listeners for the modal
  100. ws.destroyModalListeners(currentlyActiveModalUuid);
  101. state.activeModals.pop();
  102. delete state.modals[currentlyActiveModalUuid];
  103. },
  104. closeAllModals(state) {
  105. state.activeModals.forEach(modalUuid => {
  106. ws.destroyModalListeners(modalUuid);
  107. });
  108. state.activeModals = [];
  109. state.modals = {};
  110. }
  111. };
  112. export default {
  113. namespaced: true,
  114. state,
  115. getters,
  116. actions,
  117. mutations
  118. };