Plugins settings working

This commit is contained in:
Henrique Dias 2017-07-18 14:43:16 +01:00
parent d00af0c9b6
commit b86d9b16b9
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
6 changed files with 142 additions and 11 deletions

View File

@ -117,7 +117,7 @@ export default {
mounted () {
window.addEventListener('keydown', this.keyEvent)
window.addEventListener('scroll', event => {
if (this.req.kind !== 'listing') return
if (this.req.kind !== 'listing' || this.$store.state.req.display === 'mosaic') return
let top = 112 - window.scrollY

View File

@ -7,6 +7,21 @@
<li><router-link to="/users">Go to User Management</router-link></li>
</ul>
<form @submit="savePlugin">
<template v-for="plugin in plugins">
<h2>{{ capitalize(plugin.name) }}</h2>
<p v-for="field in plugin.fields" :key="field.name">
<label v-if="field.type !== 'checkbox'">{{ field.name }}</label>
<input v-if="field.type === 'text'" type="text" v-model.trim="field.value">
<input v-else-if="field.type === 'checkbox'" type="checkbox" v-model.trim="field.value">
<template v-if="field.type === 'checkbox'">{{ capitalize(field.name, 'caps') }}</template>
</p>
</template>
<p><input type="submit" value="Save"></p>
</form>
<form @submit="saveCommands">
<h2>Commands</h2>
@ -34,8 +49,7 @@ export default {
data: function () {
return {
commands: [],
beforeSave: '',
afterSave: ''
plugins: []
}
},
computed: {
@ -56,13 +70,55 @@ export default {
api.getPlugins()
.then(plugins => {
console.log(plugins)
let plugin = {}
for (let key in plugins) {
plugin.name = key
plugin.fields = []
for (let field in plugins[key]) {
let value = plugins[key][field]
if (Array.isArray(value)) {
plugin.fields.push({
name: field,
type: 'text',
original: 'array',
value: value.join(' ')
})
continue
}
switch (typeof value) {
case 'boolean':
plugin.fields.push({
name: field,
type: 'checkbox',
original: 'boolean',
value: value
})
break
default:
plugin.fields.push({
name: field,
type: 'text',
original: 'text',
value: value
})
}
}
this.plugins.push(plugin)
}
})
.catch(error => { this.showError(error) })
},
methods: {
...mapMutations([ 'showSuccess', 'showError' ]),
capitalize (name) {
let splitted = name.split('_')
capitalize (name, where = '_') {
if (where === 'caps') where = /(?=[A-Z])/
let splitted = name.split(where)
name = ''
for (let i = 0; i < splitted.length; i++) {
@ -88,6 +144,35 @@ export default {
api.updateCommands(commands)
.then(() => { this.showSuccess('Commands updated!') })
.catch(error => { this.showError(error) })
},
savePlugin (event) {
event.preventDefault()
let plugins = {}
for (let plugin of this.plugins) {
let p = {}
for (let field of plugin.fields) {
p[field.name] = field.value
if (field.original === 'array') {
let val = field.value.split(' ')
if (val[0] === '') {
val.shift()
}
p[field.name] = val
}
}
plugins[plugin.name] = p
}
console.log(plugins)
api.updatePlugins(plugins)
.then(() => { this.showSuccess('Plugins settings updated!') })
.catch(error => { this.showError(error) })
}
}
}

View File

@ -101,6 +101,9 @@ nav .action {
border-radius: 0;
font-size: 1.1em;
padding: .5em;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
nav>div {

View File

@ -379,6 +379,27 @@ function getPlugins () {
})
}
function updatePlugins (data) {
return new Promise((resolve, reject) => {
let request = new window.XMLHttpRequest()
request.open('PUT', `${store.state.baseURL}/api/plugins/`, true)
request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`)
request.onload = () => {
switch (request.status) {
case 200:
resolve()
break
default:
reject(request.responseText)
break
}
}
request.onerror = (error) => reject(error)
request.send(JSON.stringify(data))
})
}
export default {
delete: rm,
fetch,
@ -398,5 +419,6 @@ export default {
getCommands,
updateCommands,
removePrefix,
getPlugins
getPlugins,
updatePlugins
}

View File

@ -132,7 +132,7 @@
data.router.push({ path: '/files/settings' })
},
icon: 'settings',
name: 'Hugo settings'
name: 'Hugo Settings'
},
{
click: function (event, data, route) {
@ -142,15 +142,15 @@
return data.store.state.user.allowNew
},
icon: 'merge_type',
name: 'Hugo new'
}/*,
name: 'Hugo New'
} /* ,
{
click: function (event, data, route) {
console.log('evt')
},
icon: 'remove_red_eye',
name: 'Preview'
}*/
} */
],
prompts: [
{

View File

@ -4,6 +4,8 @@ import (
"encoding/json"
"errors"
"net/http"
"github.com/mitchellh/mapstructure"
)
func commandsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) {
@ -78,6 +80,25 @@ func pluginsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request
return http.StatusBadGateway, errors.New("Empty request body")
}
// TODO
var raw map[string]map[string]interface{}
// Parses the user and checks for error.
err := json.NewDecoder(r.Body).Decode(&raw)
if err != nil {
return http.StatusBadRequest, err
}
for name, plugin := range raw {
err = mapstructure.Decode(plugin, c.FM.Plugins[name])
if err != nil {
return http.StatusInternalServerError, err
}
err = c.FM.db.Set("plugins", name, c.FM.Plugins[name])
if err != nil {
return http.StatusInternalServerError, err
}
}
return http.StatusOK, nil
}