modalVisibility.js 4.3 KB

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