logo

pleroma-fe

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

setting.js (7265B)


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