interface.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const defaultState = {
  2. settingsModalState: 'hidden',
  3. settingsModalLoaded: false,
  4. settingsModalTargetTab: null,
  5. settings: {
  6. currentSaveStateNotice: null,
  7. noticeClearTimeout: null,
  8. notificationPermission: null
  9. },
  10. browserSupport: {
  11. cssFilter: window.CSS && window.CSS.supports && (
  12. window.CSS.supports('filter', 'drop-shadow(0 0)') ||
  13. window.CSS.supports('-webkit-filter', 'drop-shadow(0 0)')
  14. )
  15. },
  16. layoutType: 'normal',
  17. globalNotices: [],
  18. layoutHeight: 0,
  19. lastTimeline: null
  20. }
  21. const interfaceMod = {
  22. state: defaultState,
  23. mutations: {
  24. settingsSaved (state, { success, error }) {
  25. if (success) {
  26. if (state.noticeClearTimeout) {
  27. clearTimeout(state.noticeClearTimeout)
  28. }
  29. state.settings.currentSaveStateNotice = { error: false, data: success }
  30. state.settings.noticeClearTimeout = setTimeout(() => delete state.settings.currentSaveStateNotice, 2000)
  31. } else {
  32. state.settings.currentSaveStateNotice = { error: true, errorData: error }
  33. }
  34. },
  35. setNotificationPermission (state, permission) {
  36. state.notificationPermission = permission
  37. },
  38. setLayoutType (state, value) {
  39. state.layoutType = value
  40. },
  41. closeSettingsModal (state) {
  42. state.settingsModalState = 'hidden'
  43. },
  44. togglePeekSettingsModal (state) {
  45. switch (state.settingsModalState) {
  46. case 'minimized':
  47. state.settingsModalState = 'visible'
  48. return
  49. case 'visible':
  50. state.settingsModalState = 'minimized'
  51. return
  52. default:
  53. throw new Error('Illegal minimization state of settings modal')
  54. }
  55. },
  56. openSettingsModal (state) {
  57. state.settingsModalState = 'visible'
  58. if (!state.settingsModalLoaded) {
  59. state.settingsModalLoaded = true
  60. }
  61. },
  62. setSettingsModalTargetTab (state, value) {
  63. state.settingsModalTargetTab = value
  64. },
  65. pushGlobalNotice (state, notice) {
  66. state.globalNotices.push(notice)
  67. },
  68. removeGlobalNotice (state, notice) {
  69. state.globalNotices = state.globalNotices.filter(n => n !== notice)
  70. },
  71. setLayoutHeight (state, value) {
  72. state.layoutHeight = value
  73. },
  74. setLayoutWidth (state, value) {
  75. state.layoutWidth = value
  76. },
  77. setLastTimeline (state, value) {
  78. state.lastTimeline = value
  79. }
  80. },
  81. actions: {
  82. setPageTitle ({ rootState }, option = '') {
  83. document.title = `${option} ${rootState.instance.name}`
  84. },
  85. settingsSaved ({ commit, dispatch }, { success, error }) {
  86. commit('settingsSaved', { success, error })
  87. },
  88. setNotificationPermission ({ commit }, permission) {
  89. commit('setNotificationPermission', permission)
  90. },
  91. closeSettingsModal ({ commit }) {
  92. commit('closeSettingsModal')
  93. },
  94. openSettingsModal ({ commit }) {
  95. commit('openSettingsModal')
  96. },
  97. togglePeekSettingsModal ({ commit }) {
  98. commit('togglePeekSettingsModal')
  99. },
  100. clearSettingsModalTargetTab ({ commit }) {
  101. commit('setSettingsModalTargetTab', null)
  102. },
  103. openSettingsModalTab ({ commit }, value) {
  104. commit('setSettingsModalTargetTab', value)
  105. commit('openSettingsModal')
  106. },
  107. pushGlobalNotice (
  108. { commit, dispatch, state },
  109. {
  110. messageKey,
  111. messageArgs = {},
  112. level = 'error',
  113. timeout = 0
  114. }) {
  115. const notice = {
  116. messageKey,
  117. messageArgs,
  118. level
  119. }
  120. commit('pushGlobalNotice', notice)
  121. // Adding a new element to array wraps it in a Proxy, which breaks the comparison
  122. // TODO: Generate UUID or something instead or relying on !== operator?
  123. const newNotice = state.globalNotices[state.globalNotices.length - 1]
  124. if (timeout) {
  125. setTimeout(() => dispatch('removeGlobalNotice', newNotice), timeout)
  126. }
  127. return newNotice
  128. },
  129. removeGlobalNotice ({ commit }, notice) {
  130. commit('removeGlobalNotice', notice)
  131. },
  132. setLayoutHeight ({ commit }, value) {
  133. commit('setLayoutHeight', value)
  134. },
  135. // value is optional, assuming it was cached prior
  136. setLayoutWidth ({ commit, state, rootGetters, rootState }, value) {
  137. let width = value
  138. if (value !== undefined) {
  139. commit('setLayoutWidth', value)
  140. } else {
  141. width = state.layoutWidth
  142. }
  143. const mobileLayout = width <= 800
  144. const normalOrMobile = mobileLayout ? 'mobile' : 'normal'
  145. const { thirdColumnMode } = rootGetters.mergedConfig
  146. if (thirdColumnMode === 'none' || !rootState.users.currentUser) {
  147. commit('setLayoutType', normalOrMobile)
  148. } else {
  149. const wideLayout = width >= 1300
  150. commit('setLayoutType', wideLayout ? 'wide' : normalOrMobile)
  151. }
  152. },
  153. setLastTimeline ({ commit }, value) {
  154. commit('setLastTimeline', value)
  155. }
  156. }
  157. }
  158. export default interfaceMod