From b86d9b16b9c13836180faa2c671b6302d35500f8 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 18 Jul 2017 14:43:16 +0100 Subject: [PATCH] Plugins settings working --- assets/src/components/Files.vue | 2 +- assets/src/components/GlobalSettings.vue | 93 +++++++++++++++++++++++- assets/src/css/base.css | 3 + assets/src/utils/api.js | 24 +++++- caddy/hugo/hugo.js | 8 +- settings.go | 23 +++++- 6 files changed, 142 insertions(+), 11 deletions(-) diff --git a/assets/src/components/Files.vue b/assets/src/components/Files.vue index 5a9b5be3..65309c65 100644 --- a/assets/src/components/Files.vue +++ b/assets/src/components/Files.vue @@ -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 diff --git a/assets/src/components/GlobalSettings.vue b/assets/src/components/GlobalSettings.vue index 29db2e9a..3ee1c82e 100644 --- a/assets/src/components/GlobalSettings.vue +++ b/assets/src/components/GlobalSettings.vue @@ -7,6 +7,21 @@
  • Go to User Management
  • +
    + + +

    +
    +

    Commands

    @@ -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) }) } } } diff --git a/assets/src/css/base.css b/assets/src/css/base.css index 5aa1da46..6f025faf 100644 --- a/assets/src/css/base.css +++ b/assets/src/css/base.css @@ -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 { diff --git a/assets/src/utils/api.js b/assets/src/utils/api.js index c13aa6b5..b16d30f8 100644 --- a/assets/src/utils/api.js +++ b/assets/src/utils/api.js @@ -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 } diff --git a/caddy/hugo/hugo.js b/caddy/hugo/hugo.js index aca8f9b3..c86a762e 100644 --- a/caddy/hugo/hugo.js +++ b/caddy/hugo/hugo.js @@ -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: [ { diff --git a/settings.go b/settings.go index 819cb7f0..542ff2d3 100644 --- a/settings.go +++ b/settings.go @@ -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 }