modalVisibility.js 3.0 KB

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