modalVisibility.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 importPlaylist from "./modals/importPlaylist";
  9. import editPlaylist from "./modals/editPlaylist";
  10. import report from "./modals/report";
  11. import viewReport from "./modals/viewReport";
  12. const state = {
  13. modals: {
  14. removeAccount: false,
  15. editSong: false,
  16. editSongs: false,
  17. importAlbum: false,
  18. viewPunishment: false,
  19. confirm: false,
  20. editSongConfirm: false,
  21. editSongsConfirm: false,
  22. bulkActions: false
  23. },
  24. currentlyActive: [],
  25. new: {
  26. activeModals: [],
  27. modalMap: {}
  28. }
  29. };
  30. const modalModules = {
  31. editUser,
  32. whatIsNew,
  33. createStation,
  34. editNews,
  35. manageStation,
  36. importPlaylist,
  37. editPlaylist,
  38. report,
  39. viewReport
  40. };
  41. const migratedModules = {
  42. whatIsNew: true,
  43. manageStation: true,
  44. login: true,
  45. register: true,
  46. createStation: true,
  47. importPlaylist: true,
  48. editPlaylist: true,
  49. createPlaylist: true,
  50. report: true,
  51. removeAccount: false,
  52. editNews: true,
  53. editSong: false,
  54. editSongs: false,
  55. editUser: true,
  56. importAlbum: false,
  57. viewReport: true,
  58. viewPunishment: false,
  59. confirm: false,
  60. editSongConfirm: false,
  61. editSongsConfirm: false,
  62. bulkActions: false
  63. };
  64. const getters = {};
  65. const actions = {
  66. closeModal: ({ commit }, modal) => {
  67. if (modal === "register")
  68. lofig.get("recaptcha.enabled").then(enabled => {
  69. if (enabled) window.location.reload();
  70. });
  71. commit("closeModal", modal);
  72. },
  73. openModal: ({ commit }, dataOrModal) =>
  74. new Promise(resolve => {
  75. const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
  76. /[xy]/g,
  77. symbol => {
  78. let array;
  79. if (symbol === "y") {
  80. array = ["8", "9", "a", "b"];
  81. return array[Math.floor(Math.random() * array.length)];
  82. }
  83. array = new Uint8Array(1);
  84. window.crypto.getRandomValues(array);
  85. return (array[0] % 16).toString(16);
  86. }
  87. );
  88. if (typeof dataOrModal === "string")
  89. commit("openModal", { modal: dataOrModal, uuid });
  90. else commit("openModal", { ...dataOrModal, uuid });
  91. resolve({ uuid });
  92. }),
  93. closeCurrentModal: ({ commit }) => {
  94. commit("closeCurrentModal");
  95. }
  96. };
  97. const mutations = {
  98. closeModal(state, modal) {
  99. if (!migratedModules[modal]) {
  100. state.modals[modal] = false;
  101. const index = state.currentlyActive.indexOf(modal);
  102. if (index > -1) state.currentlyActive.splice(index, 1);
  103. } else {
  104. Object.entries(state.new.modalMap).forEach(([uuid, _modal]) => {
  105. if (modal === _modal) {
  106. state.new.activeModals.splice(
  107. state.new.activeModals.indexOf(uuid),
  108. 1
  109. );
  110. state.currentlyActive.splice(
  111. state.currentlyActive.indexOf(`${modal}-${uuid}`),
  112. 1
  113. );
  114. delete state.new.modalMap[uuid];
  115. }
  116. });
  117. }
  118. },
  119. openModal(state, { modal, uuid, data }) {
  120. if (!migratedModules[modal]) {
  121. state.modals[modal] = true;
  122. state.currentlyActive.push(modal);
  123. } else {
  124. state.new.modalMap[uuid] = modal;
  125. if (modalModules[modal]) {
  126. this.registerModule(
  127. ["modals", modal, uuid],
  128. modalModules[modal]
  129. );
  130. if (data) this.dispatch(`modals/${modal}/${uuid}/init`, data);
  131. }
  132. state.new.activeModals.push(uuid);
  133. state.currentlyActive.push(`${modal}-${uuid}`);
  134. }
  135. },
  136. closeCurrentModal(state) {
  137. const currentlyActiveModal =
  138. state.currentlyActive[state.currentlyActive.length - 1];
  139. // TODO: make sure to only destroy/register modal listeners for a unique modal
  140. // remove any websocket listeners for the modal
  141. ws.destroyModalListeners(currentlyActiveModal);
  142. if (
  143. !migratedModules[
  144. currentlyActiveModal.substring(
  145. 0,
  146. currentlyActiveModal.indexOf("-")
  147. )
  148. ]
  149. ) {
  150. state.modals[currentlyActiveModal] = false;
  151. state.currentlyActive.pop();
  152. } else {
  153. state.currentlyActive.pop();
  154. state.new.activeModals.pop();
  155. // const modal = currentlyActiveModal.substring(
  156. // 0,
  157. // currentlyActiveModal.indexOf("-")
  158. // );
  159. const uuid = currentlyActiveModal.substring(
  160. currentlyActiveModal.indexOf("-") + 1
  161. );
  162. delete state.new.modalMap[uuid];
  163. }
  164. }
  165. };
  166. export default {
  167. namespaced: true,
  168. state,
  169. getters,
  170. actions,
  171. mutations
  172. };