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

73 lines
1.8 KiB
JavaScript
Raw Normal View History

2025-12-06 14:38:53 +02:00
import Setting from './setting.js'
export default {
...Setting,
props: {
...Setting.props,
allowNew: {
required: false,
type: Boolean,
2026-01-06 16:22:52 +02:00
default: true,
},
2025-12-06 14:38:53 +02:00
},
2026-01-06 16:22:52 +02:00
data() {
2025-12-06 14:38:53 +02:00
return {
2026-01-06 16:22:52 +02:00
newValue: ['', ''], // avoiding extra complexity by just using an array instead of an object
2025-12-06 14:38:53 +02:00
}
},
computed: {
...Setting.computed,
// state that we'll show in the UI, i.e. transforming map into list
2026-01-06 16:22:52 +02:00
displayState() {
2025-12-06 14:38:53 +02:00
return Object.entries(this.visibleState)
2026-01-06 16:22:52 +02:00
},
2025-12-06 14:38:53 +02:00
},
methods: {
...Setting.methods,
2026-01-06 16:22:52 +02:00
getValue({ event, key, eventType, isKey }) {
2025-12-06 14:38:53 +02:00
switch (eventType) {
case 'add': {
if (key === '') return this.visibleState
2026-01-06 16:22:52 +02:00
const res = {
...this.visibleState,
...Object.fromEntries([this.newValue]),
}
2025-12-06 14:38:53 +02:00
this.newValue = ['', '']
return res
}
case 'remove': {
// initial state for this type is empty array
if (Array.isArray(this.visibleState)) return this.visibleState
2026-01-06 16:22:52 +02:00
const newEntries = Object.entries(this.visibleState).filter(
([k]) => k !== key,
)
2025-12-06 14:38:53 +02:00
2026-01-06 16:22:52 +02:00
if (newEntries.length === 0) return []
2025-12-06 14:38:53 +02:00
return Object.fromEntries(newEntries)
}
case 'edit': {
const string = event.target.value
2026-01-06 16:22:52 +02:00
const newEntries = Object.entries(this.visibleState).map(([k, v]) => {
if (isKey) {
if (k === key) {
return [string, v]
} else {
return [k, v]
}
} else {
if (k === key) {
return [k, string]
2025-12-06 14:38:53 +02:00
} else {
2026-01-06 16:22:52 +02:00
return [k, v]
2025-12-06 14:38:53 +02:00
}
2026-01-06 16:22:52 +02:00
}
})
2025-12-06 14:38:53 +02:00
return Object.fromEntries(newEntries)
}
}
2026-01-06 16:22:52 +02:00
},
},
2025-12-06 14:38:53 +02:00
}