From 729064ffc8fb140d667ff39a04ddd122529a19cb Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 12 Jul 2017 16:18:13 +0100 Subject: [PATCH] Commands and such --- assets/src/components/Editor.vue | 2 +- assets/src/components/Files.vue | 4 +- assets/src/components/GlobalSettings.vue | 49 +++++++++++++++++------- assets/src/utils/api.js | 24 +++++++++++- caddy/hugo/hugo.js | 3 +- filemanager.go | 1 - http.go | 2 + settings.go | 32 ++++++++++++++++ 8 files changed, 98 insertions(+), 19 deletions(-) diff --git a/assets/src/components/Editor.vue b/assets/src/components/Editor.vue index 31375221..af789b1d 100644 --- a/assets/src/components/Editor.vue +++ b/assets/src/components/Editor.vue @@ -46,7 +46,7 @@ export default { this.content = CodeMirror(document.getElementById('editor'), { value: this.req.content, lineNumbers: (this.req.language !== 'markdown'), - viewportMargin: Infinity, + viewportMargin: 500, autofocus: true, mode: this.req.language, theme: (this.req.language === 'markdown') ? 'markdown' : 'ttcn', diff --git a/assets/src/components/Files.vue b/assets/src/components/Files.vue index 91296bdd..5a9b5be3 100644 --- a/assets/src/components/Files.vue +++ b/assets/src/components/Files.vue @@ -81,9 +81,9 @@ export default { for (let i = 0; i < parts.length; i++) { if (i === 0) { - breadcrumbs.push({ name: parts[i], url: '/' + parts[i] + '/' }) + breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: '/' + parts[i] + '/' }) } else { - breadcrumbs.push({ name: parts[i], url: breadcrumbs[i - 1].url + parts[i] + '/' }) + breadcrumbs.push({ name: decodeURIComponent(parts[i]), url: breadcrumbs[i - 1].url + parts[i] + '/' }) } } diff --git a/assets/src/components/GlobalSettings.vue b/assets/src/components/GlobalSettings.vue index 41ccd078..29db2e9a 100644 --- a/assets/src/components/GlobalSettings.vue +++ b/assets/src/components/GlobalSettings.vue @@ -14,11 +14,10 @@ per line. If the event is related to files, such as before and after saving, the environment variable file will be available with the path of the file.

-

Before Save

- - -

After Save

- +

@@ -34,6 +33,7 @@ export default { name: 'settings', data: function () { return { + commands: [], beforeSave: '', afterSave: '' } @@ -44,23 +44,46 @@ export default { created () { api.getCommands() .then(commands => { - this.beforeSave = commands['before_save'].join('\n') - this.afterSave = commands['after_save'].join('\n') + for (let key in commands) { + this.commands.push({ + name: key, + value: commands[key].join('\n') + }) + } + }) + .catch(error => { this.showError(error) }) + + api.getPlugins() + .then(plugins => { + console.log(plugins) }) .catch(error => { this.showError(error) }) }, methods: { ...mapMutations([ 'showSuccess', 'showError' ]), + capitalize (name) { + let splitted = name.split('_') + name = '' + + for (let i = 0; i < splitted.length; i++) { + name += splitted[i].charAt(0).toUpperCase() + splitted[i].slice(1) + ' ' + } + + return name.slice(0, -1) + }, saveCommands (event) { event.preventDefault() - let commands = { - 'before_save': this.beforeSave.split('\n'), - 'after_save': this.afterSave.split('\n') - } + let commands = {} - if (commands['before_save'].length === 1 && commands['before_save'][0] === '') commands['before_save'] = [] - if (commands['after_save'].length === 1 && commands['after_save'][0] === '') commands['after_save'] = [] + for (let command of this.commands) { + let value = command.value.split('\n') + if (value.length === 1 && value[0] === '') { + value = [] + } + + commands[command.name] = value + } api.updateCommands(commands) .then(() => { this.showSuccess('Commands updated!') }) diff --git a/assets/src/utils/api.js b/assets/src/utils/api.js index 8d4278b6..c13aa6b5 100644 --- a/assets/src/utils/api.js +++ b/assets/src/utils/api.js @@ -358,6 +358,27 @@ function updateCommands (commands) { }) } +function getPlugins () { + return new Promise((resolve, reject) => { + let request = new window.XMLHttpRequest() + request.open('GET', `${store.state.baseURL}/api/plugins/`, true) + request.setRequestHeader('Authorization', `Bearer ${store.state.jwt}`) + + request.onload = () => { + switch (request.status) { + case 200: + resolve(JSON.parse(request.responseText)) + break + default: + reject(request.responseText) + break + } + } + request.onerror = (error) => reject(error) + request.send() + }) +} + export default { delete: rm, fetch, @@ -376,5 +397,6 @@ export default { updateCSS, getCommands, updateCommands, - removePrefix + removePrefix, + getPlugins } diff --git a/caddy/hugo/hugo.js b/caddy/hugo/hugo.js index e25096e6..df6bf4eb 100644 --- a/caddy/hugo/hugo.js +++ b/caddy/hugo/hugo.js @@ -94,6 +94,7 @@ regenerate(data, route.path) .then(() => { data.buttons.done('publish') + data.store.commit('showSuccess', 'Post published!') data.store.commit('setReload', true) }) .catch((error) => { @@ -209,7 +210,7 @@ schedule(data, route.path, date) .then(() => { data.buttons.done('schedule') - data.store.commit('setReload', true) + data.store.commit('showSuccess', 'Post scheduled!') }) .catch((error) => { data.buttons.done('schedule') diff --git a/filemanager.go b/filemanager.go index 627279e7..5b8cd82f 100644 --- a/filemanager.go +++ b/filemanager.go @@ -278,7 +278,6 @@ func (m *FileManager) RegisterEventType(name string) error { // ServeHTTP determines if the request is for this plugin, and if all prerequisites are met. func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { - // TODO: Handle errors here and make it compatible with http.Handler code, err := serveHTTP(&RequestContext{ FM: m, User: nil, diff --git a/http.go b/http.go index 39c6aa40..4e41d245 100644 --- a/http.go +++ b/http.go @@ -140,6 +140,8 @@ func apiHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, code, err = usersHandler(c, w, r) case "commands": code, err = commandsHandler(c, w, r) + case "plugins": + code, err = pluginsHandler(c, w, r) } if code >= 300 || err != nil { diff --git a/settings.go b/settings.go index 9e4b10ea..819cb7f0 100644 --- a/settings.go +++ b/settings.go @@ -49,3 +49,35 @@ func commandsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Reques c.FM.Commands = commands return http.StatusOK, nil } + +func pluginsHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { + switch r.Method { + case http.MethodGet: + return pluginsGetHandler(c, w, r) + case http.MethodPut: + return pluginsPutHandler(c, w, r) + } + + return http.StatusMethodNotAllowed, nil +} + +func pluginsGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { + if !c.User.Admin { + return http.StatusForbidden, nil + } + + return renderJSON(w, c.FM.Plugins) +} + +func pluginsPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) (int, error) { + if !c.User.Admin { + return http.StatusForbidden, nil + } + + if r.Body == nil { + return http.StatusBadGateway, errors.New("Empty request body") + } + + // TODO + return http.StatusOK, nil +}