pleroma-fe/src/components/settings_modal/helpers/setting.js

310 lines
8.5 KiB
JavaScript
Raw Normal View History

import ModifiedIndicator from './modified_indicator.vue'
import ProfileSettingIndicator from './profile_setting_indicator.vue'
import DraftButtons from './draft_buttons.vue'
import { get, set, cloneDeep, isEqual } from 'lodash'
export default {
components: {
ModifiedIndicator,
DraftButtons,
ProfileSettingIndicator
},
props: {
2024-09-24 03:07:27 +03:00
modelValue: {
type: String,
default: null
},
path: {
type: [String, Array],
2024-09-24 03:07:27 +03:00
required: false
},
2025-12-10 19:57:34 +02:00
showDescription: {
type: Boolean,
required: false
},
2025-12-08 17:09:07 +02:00
descriptionPathOverride: {
type: [String, Array],
required: false
},
2025-12-03 23:05:46 +02:00
suggestions: {
type: [String, Array],
required: false
},
subgroup: {
type: String,
required: false
},
disabled: {
type: Boolean,
default: false
},
parentPath: {
type: [String, Array]
},
parentInvert: {
type: Boolean,
default: false
},
expert: {
type: [Number, String],
default: 0
},
source: {
type: String,
2023-03-21 22:46:40 +02:00
default: undefined
},
2025-12-03 12:19:20 +02:00
hideDraftButtons: { // this is for the weird backend hybrid (Boolean|String or Boolean|Number) settings
2025-12-07 23:11:54 +02:00
required: false,
2025-12-03 12:19:20 +02:00
type: Boolean
},
hideLabel: {
type: Boolean
},
hideDescription: {
type: Boolean
},
swapDescriptionAndLabel: {
type: Boolean
},
2025-12-03 23:05:46 +02:00
backendDescriptionPath: {
type: [String, Array]
},
overrideBackendDescription: {
type: Boolean
},
overrideBackendDescriptionLabel: {
2025-12-10 19:57:34 +02:00
type: [Boolean, String]
},
draftMode: {
type: Boolean,
2023-03-21 22:46:40 +02:00
default: undefined
},
timedApplyMode: {
type: Boolean,
default: false
2023-03-21 22:46:40 +02:00
}
},
inject: {
defaultSource: {
default: 'default'
},
defaultDraftMode: {
default: false
}
},
data () {
return {
localDraft: null
}
},
created () {
2024-09-24 03:07:27 +03:00
if (this.realDraftMode && (this.realSource !== 'admin' || this.path == null)) {
2025-12-01 20:09:05 +02:00
this.draft = cloneDeep(this.state)
}
},
computed: {
draft: {
get () {
2024-09-24 03:07:27 +03:00
if (this.realSource === 'admin' || this.path == null) {
return get(this.$store.state.adminSettings.draft, this.canonPath)
} else {
return this.localDraft
}
},
set (value) {
2024-09-24 03:07:27 +03:00
if (this.realSource === 'admin' || this.path == null) {
this.$store.commit('updateAdminDraft', { path: this.canonPath, value })
} else {
this.localDraft = value
}
}
},
state () {
2024-09-24 03:07:27 +03:00
if (this.path == null) {
return this.modelValue
}
const value = get(this.configSource, this.canonPath)
if (value === undefined) {
return this.defaultState
} else {
return value
}
},
visibleState () {
return this.realDraftMode ? this.draft : this.state
},
2023-03-21 22:46:40 +02:00
realSource () {
return this.source || this.defaultSource
},
realDraftMode () {
return typeof this.draftMode === 'undefined' ? this.defaultDraftMode : this.draftMode
},
2023-03-19 21:27:07 +02:00
backendDescription () {
return get(this.$store.state.adminSettings.descriptions, this.descriptionPath)
2023-03-19 21:27:07 +02:00
},
backendDescriptionLabel () {
if (this.realSource !== 'admin') return ''
2025-12-08 22:29:47 +02:00
if (this.overrideBackendDescriptionLabel !== '' && typeof this.overrideBackendDescriptionLabel === 'string') {
return this.overrideBackendDescriptionLabel
}
if (!this.backendDescription || this.overrideBackendDescriptionLabel) {
return this.$t([
'admin_dash',
'temp_overrides',
...this.canonPath.map(p => p.replace(/\./g, '_DOT_')),
'label'
].join('.'))
} else {
return this.swapDescriptionAndLabel
? this.backendDescription?.description
: this.backendDescription?.label
}
2023-03-19 21:27:07 +02:00
},
backendDescriptionDescription () {
2025-12-10 19:57:34 +02:00
if (this.description) return this.description
if (this.realSource !== 'admin') return ''
if (this.hideDescription) return null
if (!this.backendDescription || this.overrideBackendDescription) {
return this.$t([
'admin_dash',
'temp_overrides',
...this.canonPath.map(p => p.replace(/\./g, '_DOT_')),
'description'
].join('.'))
} else {
return this.swapDescriptionAndLabel
? this.backendDescription?.label
: this.backendDescription?.description
}
2023-03-19 21:27:07 +02:00
},
backendDescriptionSuggestions () {
2025-12-03 23:05:46 +02:00
return this.backendDescription?.suggestions || this.suggestions
},
shouldBeDisabled () {
2024-09-24 03:07:27 +03:00
if (this.path == null) {
return this.disabled
}
2025-12-01 20:09:05 +02:00
let parentValue = null
if (this.parentPath !== undefined && this.realSource === 'admin') {
if (this.realDraftMode) {
parentValue = get(this.$store.state.adminSettings.draft, this.parentPath)
} else {
parentValue = get(this.configSource, this.parentPath)
}
}
return this.disabled || (parentValue !== null ? (this.parentInvert ? parentValue : !parentValue) : false)
},
configSource () {
2023-03-21 22:46:40 +02:00
switch (this.realSource) {
case 'profile':
return this.$store.state.profileConfig
2023-03-14 21:50:43 +02:00
case 'admin':
return this.$store.state.adminSettings.config
default:
return this.$store.getters.mergedConfig
}
},
configSink () {
2024-09-24 03:07:27 +03:00
if (this.path == null) {
return (k, v) => this.$emit('update:modelValue', v)
2024-09-24 03:07:27 +03:00
}
2023-03-21 22:46:40 +02:00
switch (this.realSource) {
case 'profile':
return (k, v) => this.$store.dispatch('setProfileOption', { name: k, value: v })
2023-03-14 21:50:43 +02:00
case 'admin':
return (k, v) => this.$store.dispatch('pushAdminSetting', { path: k, value: v })
default:
if (this.timedApplyMode) {
return (k, v) => this.$store.dispatch('setOptionTemporarily', { name: k, value: v })
} else {
return (k, v) => this.$store.dispatch('setOption', { name: k, value: v })
}
}
},
defaultState () {
2023-03-21 22:46:40 +02:00
switch (this.realSource) {
case 'profile':
return {}
default:
return get(this.$store.getters.defaultConfig, this.path)
}
},
2023-03-12 17:11:20 +02:00
isProfileSetting () {
2023-03-21 22:46:40 +02:00
return this.realSource === 'profile'
},
isChanged () {
2024-09-24 03:07:27 +03:00
if (this.path == null) return false
2023-03-21 22:46:40 +02:00
switch (this.realSource) {
2023-03-14 21:50:43 +02:00
case 'profile':
case 'admin':
return false
2023-03-14 21:50:43 +02:00
default:
return this.state !== this.defaultState
}
},
canonPath () {
2024-09-24 03:07:27 +03:00
if (this.path == null) return null
return Array.isArray(this.path) ? this.path : this.path.split('.')
},
descriptionPath () {
if (this.path == null) return null
2025-12-08 17:09:07 +02:00
if (this.descriptionPathOverride) return this.descriptionPathOverride
const path = Array.isArray(this.path) ? this.path : this.path.split('.')
if (this.subgroup) {
return [
2025-11-28 02:02:29 +02:00
...path.slice(0, path.length - 1),
':subgroup,' + this.subgroup,
...path.slice(path.length - 1)
]
}
return path
},
isDirty () {
2024-09-24 03:07:27 +03:00
if (this.path == null) return false
if (this.realSource === 'admin' && this.canonPath.length > 3) {
return false // should not show draft buttons for "grouped" values
} else {
return this.realDraftMode && !isEqual(this.draft, this.state)
}
},
canHardReset () {
2025-12-07 23:11:54 +02:00
return this.realSource === 'admin' && this.$store.state.adminSettings.modifiedPaths?.has(this.canonPath.join(' -> '))
},
matchesExpertLevel () {
return (this.expert || 0) <= this.$store.state.config.expertLevel > 0
}
},
methods: {
getValue (e) {
return e.target.value
},
update (e) {
2023-03-21 22:46:40 +02:00
if (this.realDraftMode) {
this.draft = this.getValue(e)
} else {
this.configSink(this.path, this.getValue(e))
}
},
commitDraft () {
2023-03-21 22:46:40 +02:00
if (this.realDraftMode) {
this.configSink(this.path, this.draft)
}
},
reset () {
2023-03-21 22:46:40 +02:00
if (this.realDraftMode) {
2023-05-24 11:44:41 +03:00
this.draft = cloneDeep(this.state)
} else {
2023-05-24 11:44:41 +03:00
set(this.$store.getters.mergedConfig, this.path, cloneDeep(this.defaultState))
}
},
hardReset () {
2023-03-21 22:46:40 +02:00
switch (this.realSource) {
case 'admin':
return this.$store.dispatch('resetAdminSetting', { path: this.path })
.then(() => { this.draft = this.state })
default:
console.warn('Hard reset not implemented yet!')
}
}
}
}