mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
use ConfigPanels in DomainConfig and AppConfigPanel
This commit is contained in:
parent
7cd07ebd39
commit
5ae814c616
2 changed files with 71 additions and 277 deletions
|
@ -1,53 +1,31 @@
|
||||||
<template>
|
<template>
|
||||||
<view-base :queries="queries" @queries-response="onQueriesResponse" skeleton="card-form-skeleton">
|
<view-base
|
||||||
<b-card v-if="panels" no-body>
|
:queries="queries" @queries-response="onQueriesResponse"
|
||||||
<b-tabs fill pills card>
|
ref="view" skeleton="card-form-skeleton"
|
||||||
<tab-form
|
>
|
||||||
v-for="{ name, id: id_, sections, help, serverError } in panels" :key="id_"
|
<config-panels v-if="config.panels" v-bind="config" @submit="applyConfig" />
|
||||||
v-bind="{ name, id: id_ + '-form', validation: $v.forms[id_], serverError }"
|
|
||||||
@submit.prevent="applyConfig(id_)"
|
|
||||||
>
|
|
||||||
<template v-if="help" #disclaimer>
|
|
||||||
<div class="alert alert-info" v-html="help" />
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<template v-for="section in sections">
|
<b-alert v-else-if="config.panels === null" variant="warning">
|
||||||
<div v-if="isVisible(section.visible, section)" :key="section.id" class="mb-5">
|
|
||||||
<b-card-title v-if="section.name" title-tag="h3">
|
|
||||||
{{ section.name }} <small v-if="section.help">{{ section.help }}</small>
|
|
||||||
</b-card-title>
|
|
||||||
|
|
||||||
<template v-for="(field, fname) in section.fields">
|
|
||||||
<form-field
|
|
||||||
v-if="isVisible(field.visible, field)" :key="fname"
|
|
||||||
v-model="forms[id_][fname]" v-bind="field" :validation="$v.forms[id_][fname]"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</tab-form>
|
|
||||||
</b-tabs>
|
|
||||||
</b-card>
|
|
||||||
|
|
||||||
<!-- if no config panel -->
|
|
||||||
<b-alert v-else-if="panels === null" variant="warning">
|
|
||||||
<icon iname="exclamation-triangle" /> {{ $t('app_config_panel_no_panel') }}
|
<icon iname="exclamation-triangle" /> {{ $t('app_config_panel_no_panel') }}
|
||||||
</b-alert>
|
</b-alert>
|
||||||
</view-base>
|
</view-base>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { validationMixin } from 'vuelidate'
|
|
||||||
import evaluate from 'simple-evaluate'
|
|
||||||
|
|
||||||
// FIXME needs test and rework
|
|
||||||
import api, { objectToParams } from '@/api'
|
import api, { objectToParams } from '@/api'
|
||||||
import { formatI18nField, formatYunoHostArguments, formatFormData, pFileReader } from '@/helpers/yunohostArguments'
|
import {
|
||||||
|
formatFormData,
|
||||||
|
formatYunoHostConfigPanels
|
||||||
|
} from '@/helpers/yunohostArguments'
|
||||||
|
import ConfigPanels from '@/components/ConfigPanels'
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'AppConfigPanel',
|
name: 'AppConfigPanel',
|
||||||
|
|
||||||
mixins: [validationMixin],
|
components: {
|
||||||
|
ConfigPanels
|
||||||
|
},
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
id: { type: String, required: true }
|
id: { type: String, required: true }
|
||||||
|
@ -56,129 +34,41 @@ export default {
|
||||||
data () {
|
data () {
|
||||||
return {
|
return {
|
||||||
queries: [
|
queries: [
|
||||||
['GET', `apps/${this.id}/config-panel?full`],
|
['GET', `apps/${this.id}/config-panel?full`]
|
||||||
['GET', { uri: 'domains' }],
|
|
||||||
['GET', { uri: 'domains/main', storeKey: 'main_domain' }],
|
|
||||||
['GET', { uri: 'users' }]
|
|
||||||
],
|
],
|
||||||
panels: undefined,
|
config: {}
|
||||||
forms: undefined,
|
|
||||||
errors: undefined,
|
|
||||||
validations: null
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
validations () {
|
|
||||||
return this.validations
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
isVisible (expression, field) {
|
onQueriesResponse (config) {
|
||||||
if (!expression || !field) return true
|
if (!config.panels || config.panels.length === 0) {
|
||||||
const context = {}
|
this.config = null
|
||||||
|
} else {
|
||||||
const promises = []
|
this.config = formatYunoHostConfigPanels(config)
|
||||||
for (const args of Object.values(this.forms)) {
|
|
||||||
for (const shortname in args) {
|
|
||||||
if (args[shortname] instanceof File) {
|
|
||||||
if (expression.includes(shortname)) {
|
|
||||||
promises.push(pFileReader(args[shortname], context, shortname, false))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
context[shortname] = args[shortname]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// Allow to use match(var,regexp) function
|
|
||||||
const matchRe = new RegExp('match\\(\\s*(\\w+)\\s*,\\s*"([^"]+)"\\s*\\)', 'g')
|
|
||||||
let i = 0
|
|
||||||
Promise.all(promises).then((value) => {
|
|
||||||
for (const matched of expression.matchAll(matchRe)) {
|
|
||||||
i++
|
|
||||||
const varName = matched[1] + '__re' + i.toString()
|
|
||||||
context[varName] = new RegExp(matched[2], 'm').test(context[matched[1]])
|
|
||||||
expression = expression.replace(matched[0], varName)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
field.isVisible = evaluate(context, expression)
|
|
||||||
} catch (error) {
|
|
||||||
field.isVisible = false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// This value should be updated magically when vuejs will detect isVisible changed
|
|
||||||
return field.isVisible
|
|
||||||
},
|
},
|
||||||
|
|
||||||
onQueriesResponse (data) {
|
async applyConfig (id_) {
|
||||||
if (!data.panels || data.panels.length === 0) {
|
const formatedData = await formatFormData(
|
||||||
this.panels = null
|
this.config.forms[id_],
|
||||||
return
|
{ removeEmpty: false, removeNull: true, multipart: false }
|
||||||
}
|
)
|
||||||
|
|
||||||
const forms = {}
|
api.put(
|
||||||
const validations_ = {}
|
`apps/${this.id}/config`,
|
||||||
const errors_ = {}
|
{ key: id_, args: objectToParams(formatedData) },
|
||||||
const panels_ = []
|
{ key: 'apps.update_config', name: this.id }
|
||||||
for (const { id, name, help, sections } of data.panels) {
|
).then(response => {
|
||||||
const panel_ = { id, sections: [] }
|
this.$refs.view.fetchQueries({ triggerLoading: true })
|
||||||
if (name) panel_.name = formatI18nField(name)
|
}).catch(err => {
|
||||||
if (help) panel_.help = formatI18nField(help)
|
if (err.name !== 'APIBadRequestError') throw err
|
||||||
forms[id] = {}
|
const panel = this.config.panels.find(({ id }) => id_ === id)
|
||||||
validations_[id] = {}
|
if (err.data.name) {
|
||||||
errors_[id] = {}
|
this.config.errors[id_][err.data.name].message = err.message
|
||||||
for (const { id_, name, help, visible, options } of sections) {
|
} else this.$set(panel, 'serverError', err.message)
|
||||||
const section_ = { id: id_, isVisible: true, visible }
|
|
||||||
if (help) section_.help = formatI18nField(help)
|
|
||||||
if (name) section_.name = formatI18nField(name)
|
|
||||||
const { form, fields, validations, errors } = formatYunoHostArguments(options)
|
|
||||||
Object.assign(forms[id], form)
|
|
||||||
Object.assign(validations_[id], validations)
|
|
||||||
Object.assign(errors_[id], errors)
|
|
||||||
section_.fields = fields
|
|
||||||
panel_.sections.push(section_)
|
|
||||||
}
|
|
||||||
panels_.push(panel_)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.forms = forms
|
|
||||||
this.validations = { forms: validations_ }
|
|
||||||
this.panels = panels_
|
|
||||||
this.errors = errors_
|
|
||||||
},
|
|
||||||
|
|
||||||
applyConfig (id_) {
|
|
||||||
formatFormData(this.forms[id_], { removeEmpty: false, removeNull: true, multipart: false }).then((formatedData) => {
|
|
||||||
const args = objectToParams(formatedData)
|
|
||||||
|
|
||||||
api.put(
|
|
||||||
`apps/${this.id}/config`, { key: id_, args }, { key: 'apps.update_config', name: this.id }
|
|
||||||
).then(response => {
|
|
||||||
api.get(
|
|
||||||
`apps/${this.id}/config-panel?full`, {}, { key: 'apps.get_config', name: this.id }
|
|
||||||
).then(response => {
|
|
||||||
this.onQueriesResponse(response)
|
|
||||||
}).catch(err => {
|
|
||||||
if (err.name !== 'APIBadRequestError') throw err
|
|
||||||
const panel = this.panels.find(({ id }) => id_ === id)
|
|
||||||
this.$set(panel, 'serverError', err.message)
|
|
||||||
})
|
|
||||||
}).catch(err => {
|
|
||||||
if (err.name !== 'APIBadRequestError') throw err
|
|
||||||
const panel = this.panels.find(({ id }) => id_ === id)
|
|
||||||
if (err.data.name) {
|
|
||||||
this.errors[id_][err.data.name].message = err.message
|
|
||||||
} else this.$set(panel, 'serverError', err.message)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
|
||||||
h3 {
|
|
||||||
margin-bottom: 1em;
|
|
||||||
border-bottom: solid 1px #aaa;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
|
@ -1,55 +1,27 @@
|
||||||
<template>
|
<template>
|
||||||
<view-base :queries="queries" @queries-response="onQueriesResponse" skeleton="card-info-skeleton">
|
<view-base
|
||||||
<b-tabs pills card vertical>
|
:queries="queries" @queries-response="onQueriesResponse"
|
||||||
<b-tab v-for="{ name, id: id_, sections, help, serverError } in panels"
|
ref="view" skeleton="card-form-skeleton"
|
||||||
:key="id_"
|
>
|
||||||
:title="name"
|
<config-panels v-if="config.panels" v-bind="config" @submit="applyConfig" />
|
||||||
>
|
|
||||||
<template #title>
|
|
||||||
<icon iname="wrench" /> {{ name }}
|
|
||||||
</template>
|
|
||||||
<card-form
|
|
||||||
:key="id_"
|
|
||||||
:title="name" icon="wrench" title-tag="h2"
|
|
||||||
:validation="$v.forms[id_]" :id="id_ + '-form'" :server-error="serverError"
|
|
||||||
@submit.prevent="applyConfig(id_)"
|
|
||||||
>
|
|
||||||
<template v-if="help" #disclaimer>
|
|
||||||
<div class="alert alert-info" v-html="help" />
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<template v-for="section in sections">
|
|
||||||
<div :key="section.id" class="mb-5" v-if="isVisible(section.visible, section)">
|
|
||||||
<b-card-title v-if="section.name" title-tag="h3">
|
|
||||||
{{ section.name }} <small v-if="section.help">{{ section.help }}</small>
|
|
||||||
</b-card-title>
|
|
||||||
<template v-for="(field, fname) in section.fields">
|
|
||||||
<form-field :key="fname" v-model="forms[id_][fname]"
|
|
||||||
:validation="$v.forms[id_][fname]"
|
|
||||||
v-if="isVisible(field.visible, field)" v-bind="field"
|
|
||||||
/>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</card-form>
|
|
||||||
</b-tab>
|
|
||||||
</b-tabs>
|
|
||||||
</view-base>
|
</view-base>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { validationMixin } from 'vuelidate'
|
|
||||||
import evaluate from 'simple-evaluate'
|
|
||||||
|
|
||||||
import api, { objectToParams } from '@/api'
|
import api, { objectToParams } from '@/api'
|
||||||
|
import {
|
||||||
import { formatI18nField, formatYunoHostArguments, formatFormData, pFileReader } from '@/helpers/yunohostArguments'
|
formatFormData,
|
||||||
|
formatYunoHostConfigPanels
|
||||||
|
} from '@/helpers/yunohostArguments'
|
||||||
|
import ConfigPanels from '@/components/ConfigPanels'
|
||||||
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'DomainConfig',
|
name: 'DomainConfig',
|
||||||
|
|
||||||
mixins: [validationMixin],
|
components: {
|
||||||
|
ConfigPanels
|
||||||
|
},
|
||||||
|
|
||||||
props: {
|
props: {
|
||||||
name: { type: String, required: true }
|
name: { type: String, required: true }
|
||||||
|
@ -60,101 +32,33 @@ export default {
|
||||||
queries: [
|
queries: [
|
||||||
['GET', `domains/${this.name}/config?full`]
|
['GET', `domains/${this.name}/config?full`]
|
||||||
],
|
],
|
||||||
panels: undefined,
|
config: {}
|
||||||
forms: undefined,
|
|
||||||
errors: undefined,
|
|
||||||
validations: null
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
validations () {
|
|
||||||
return this.validations
|
|
||||||
},
|
|
||||||
|
|
||||||
methods: {
|
methods: {
|
||||||
onQueriesResponse (config) {
|
onQueriesResponse (config) {
|
||||||
const forms = {}
|
this.config = formatYunoHostConfigPanels(config)
|
||||||
const validations_ = {}
|
|
||||||
const errors_ = {}
|
|
||||||
const panels_ = []
|
|
||||||
for (const { id, name, help, sections } of config.panels) {
|
|
||||||
const panel_ = { id, sections: [] }
|
|
||||||
if (name) panel_.name = formatI18nField(name)
|
|
||||||
if (help) panel_.help = formatI18nField(help)
|
|
||||||
forms[id] = {}
|
|
||||||
validations_[id] = {}
|
|
||||||
errors_[id] = {}
|
|
||||||
for (const { id_, name, help, visible, options } of sections) {
|
|
||||||
const section_ = { id: id_, isVisible: true, visible }
|
|
||||||
if (help) section_.help = formatI18nField(help)
|
|
||||||
if (name) section_.name = formatI18nField(name)
|
|
||||||
const { form, fields, validations, errors } = formatYunoHostArguments(options)
|
|
||||||
Object.assign(forms[id], form)
|
|
||||||
Object.assign(validations_[id], validations)
|
|
||||||
Object.assign(errors_[id], errors)
|
|
||||||
section_.fields = fields
|
|
||||||
panel_.sections.push(section_)
|
|
||||||
}
|
|
||||||
panels_.push(panel_)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.forms = forms
|
|
||||||
this.validations = { forms: validations_ }
|
|
||||||
this.panels = panels_
|
|
||||||
this.errors = errors_
|
|
||||||
},
|
},
|
||||||
|
|
||||||
isVisible (expression, field) {
|
async applyConfig (id_) {
|
||||||
if (!expression || !field) return true
|
const formatedData = await formatFormData(
|
||||||
const context = {}
|
this.config.forms[id_],
|
||||||
|
{ removeEmpty: false, removeNull: true, multipart: false }
|
||||||
|
)
|
||||||
|
|
||||||
const promises = []
|
api.put(
|
||||||
for (const args of Object.values(this.forms)) {
|
`domains/${this.name}/config`,
|
||||||
for (const shortname in args) {
|
{ key: id_, args: objectToParams(formatedData) },
|
||||||
if (args[shortname] instanceof File) {
|
{ key: 'domains.update_config', name: this.name }
|
||||||
if (expression.includes(shortname)) {
|
).then(() => {
|
||||||
promises.push(pFileReader(args[shortname], context, shortname, false))
|
this.$refs.view.fetchQueries({ triggerLoading: true })
|
||||||
}
|
}).catch(err => {
|
||||||
} else {
|
if (err.name !== 'APIBadRequestError') throw err
|
||||||
context[shortname] = args[shortname]
|
const panel = this.config.panels.find(({ id }) => id_ === id)
|
||||||
}
|
if (err.data.name) {
|
||||||
}
|
this.config.errors[id_][err.data.name].message = err.message
|
||||||
}
|
} else this.$set(panel, 'serverError', err.message)
|
||||||
// Allow to use match(var,regexp) function
|
|
||||||
const matchRe = new RegExp('match\\(\\s*(\\w+)\\s*,\\s*"([^"]+)"\\s*\\)', 'g')
|
|
||||||
let i = 0
|
|
||||||
Promise.all(promises).then((value) => {
|
|
||||||
for (const matched of expression.matchAll(matchRe)) {
|
|
||||||
i++
|
|
||||||
const varName = matched[1] + '__re' + i.toString()
|
|
||||||
context[varName] = new RegExp(matched[2], 'm').test(context[matched[1]])
|
|
||||||
expression = expression.replace(matched[0], varName)
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
field.isVisible = evaluate(context, expression)
|
|
||||||
} catch (error) {
|
|
||||||
field.isVisible = false
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// This value should be updated magically when vuejs will detect isVisible changed
|
|
||||||
return field.isVisible
|
|
||||||
},
|
|
||||||
|
|
||||||
applyConfig (id_) {
|
|
||||||
formatFormData(this.forms[id_], { removeEmpty: false, removeNull: true, multipart: false }).then((formatedData) => {
|
|
||||||
const args = objectToParams(formatedData)
|
|
||||||
|
|
||||||
api.put(
|
|
||||||
`domains/${this.name}/config`, { key: id_, args }, { key: 'domains.update_config', name: this.name }
|
|
||||||
).then(response => {
|
|
||||||
}).catch(err => {
|
|
||||||
if (err.name !== 'APIBadRequestError') throw err
|
|
||||||
const panel = this.panels.find(({ id }) => id_ === id)
|
|
||||||
if (err.data.name) {
|
|
||||||
this.errors[id_][err.data.name].message = err.message
|
|
||||||
} else this.$set(panel, 'serverError', err.message)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue