logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

setting.js (6534B)


  1. import ModifiedIndicator from './modified_indicator.vue'
  2. import ProfileSettingIndicator from './profile_setting_indicator.vue'
  3. import DraftButtons from './draft_buttons.vue'
  4. import { get, set, cloneDeep } from 'lodash'
  5. export default {
  6. components: {
  7. ModifiedIndicator,
  8. DraftButtons,
  9. ProfileSettingIndicator
  10. },
  11. props: {
  12. path: {
  13. type: [String, Array],
  14. required: true
  15. },
  16. disabled: {
  17. type: Boolean,
  18. default: false
  19. },
  20. parentPath: {
  21. type: [String, Array]
  22. },
  23. parentInvert: {
  24. type: Boolean,
  25. default: false
  26. },
  27. expert: {
  28. type: [Number, String],
  29. default: 0
  30. },
  31. source: {
  32. type: String,
  33. default: undefined
  34. },
  35. hideDescription: {
  36. type: Boolean
  37. },
  38. swapDescriptionAndLabel: {
  39. type: Boolean
  40. },
  41. overrideBackendDescription: {
  42. type: Boolean
  43. },
  44. overrideBackendDescriptionLabel: {
  45. type: Boolean
  46. },
  47. draftMode: {
  48. type: Boolean,
  49. default: undefined
  50. }
  51. },
  52. inject: {
  53. defaultSource: {
  54. default: 'default'
  55. },
  56. defaultDraftMode: {
  57. default: false
  58. }
  59. },
  60. data () {
  61. return {
  62. localDraft: null
  63. }
  64. },
  65. created () {
  66. if (this.realDraftMode && this.realSource !== 'admin') {
  67. this.draft = this.state
  68. }
  69. },
  70. computed: {
  71. draft: {
  72. // TODO allow passing shared draft object?
  73. get () {
  74. if (this.realSource === 'admin') {
  75. return get(this.$store.state.adminSettings.draft, this.canonPath)
  76. } else {
  77. return this.localDraft
  78. }
  79. },
  80. set (value) {
  81. if (this.realSource === 'admin') {
  82. this.$store.commit('updateAdminDraft', { path: this.canonPath, value })
  83. } else {
  84. this.localDraft = value
  85. }
  86. }
  87. },
  88. state () {
  89. const value = get(this.configSource, this.canonPath)
  90. if (value === undefined) {
  91. return this.defaultState
  92. } else {
  93. return value
  94. }
  95. },
  96. visibleState () {
  97. return this.realDraftMode ? this.draft : this.state
  98. },
  99. realSource () {
  100. return this.source || this.defaultSource
  101. },
  102. realDraftMode () {
  103. return typeof this.draftMode === 'undefined' ? this.defaultDraftMode : this.draftMode
  104. },
  105. backendDescription () {
  106. return get(this.$store.state.adminSettings.descriptions, this.path)
  107. },
  108. backendDescriptionLabel () {
  109. if (this.realSource !== 'admin') return ''
  110. if (!this.backendDescription || this.overrideBackendDescriptionLabel) {
  111. return this.$t([
  112. 'admin_dash',
  113. 'temp_overrides',
  114. ...this.canonPath.map(p => p.replace(/\./g, '_DOT_')),
  115. 'label'
  116. ].join('.'))
  117. } else {
  118. return this.swapDescriptionAndLabel
  119. ? this.backendDescription?.description
  120. : this.backendDescription?.label
  121. }
  122. },
  123. backendDescriptionDescription () {
  124. if (this.realSource !== 'admin') return ''
  125. if (this.hideDescription) return null
  126. if (!this.backendDescription || this.overrideBackendDescription) {
  127. return this.$t([
  128. 'admin_dash',
  129. 'temp_overrides',
  130. ...this.canonPath.map(p => p.replace(/\./g, '_DOT_')),
  131. 'description'
  132. ].join('.'))
  133. } else {
  134. return this.swapDescriptionAndLabel
  135. ? this.backendDescription?.label
  136. : this.backendDescription?.description
  137. }
  138. },
  139. backendDescriptionSuggestions () {
  140. return this.backendDescription?.suggestions
  141. },
  142. shouldBeDisabled () {
  143. const parentValue = this.parentPath !== undefined ? get(this.configSource, this.parentPath) : null
  144. return this.disabled || (parentValue !== null ? (this.parentInvert ? parentValue : !parentValue) : false)
  145. },
  146. configSource () {
  147. switch (this.realSource) {
  148. case 'profile':
  149. return this.$store.state.profileConfig
  150. case 'admin':
  151. return this.$store.state.adminSettings.config
  152. default:
  153. return this.$store.getters.mergedConfig
  154. }
  155. },
  156. configSink () {
  157. switch (this.realSource) {
  158. case 'profile':
  159. return (k, v) => this.$store.dispatch('setProfileOption', { name: k, value: v })
  160. case 'admin':
  161. return (k, v) => this.$store.dispatch('pushAdminSetting', { path: k, value: v })
  162. default:
  163. return (k, v) => this.$store.dispatch('setOption', { name: k, value: v })
  164. }
  165. },
  166. defaultState () {
  167. switch (this.realSource) {
  168. case 'profile':
  169. return {}
  170. default:
  171. return get(this.$store.getters.defaultConfig, this.path)
  172. }
  173. },
  174. isProfileSetting () {
  175. return this.realSource === 'profile'
  176. },
  177. isChanged () {
  178. switch (this.realSource) {
  179. case 'profile':
  180. case 'admin':
  181. return false
  182. default:
  183. return this.state !== this.defaultState
  184. }
  185. },
  186. canonPath () {
  187. return Array.isArray(this.path) ? this.path : this.path.split('.')
  188. },
  189. isDirty () {
  190. if (this.realSource === 'admin' && this.canonPath.length > 3) {
  191. return false // should not show draft buttons for "grouped" values
  192. } else {
  193. return this.realDraftMode && this.draft !== this.state
  194. }
  195. },
  196. canHardReset () {
  197. return this.realSource === 'admin' && this.$store.state.adminSettings.modifiedPaths &&
  198. this.$store.state.adminSettings.modifiedPaths.has(this.canonPath.join(' -> '))
  199. },
  200. matchesExpertLevel () {
  201. return (this.expert || 0) <= this.$store.state.config.expertLevel > 0
  202. }
  203. },
  204. methods: {
  205. getValue (e) {
  206. return e.target.value
  207. },
  208. update (e) {
  209. if (this.realDraftMode) {
  210. this.draft = this.getValue(e)
  211. } else {
  212. this.configSink(this.path, this.getValue(e))
  213. }
  214. },
  215. commitDraft () {
  216. if (this.realDraftMode) {
  217. this.configSink(this.path, this.draft)
  218. }
  219. },
  220. reset () {
  221. if (this.realDraftMode) {
  222. this.draft = cloneDeep(this.state)
  223. } else {
  224. set(this.$store.getters.mergedConfig, this.path, cloneDeep(this.defaultState))
  225. }
  226. },
  227. hardReset () {
  228. switch (this.realSource) {
  229. case 'admin':
  230. return this.$store.dispatch('resetAdminSetting', { path: this.path })
  231. .then(() => { this.draft = this.state })
  232. default:
  233. console.warn('Hard reset not implemented yet!')
  234. }
  235. }
  236. }
  237. }