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 (6772B)


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