From c3b3099ebbb5fa6d9aa5e1049c12d46cc74009bc Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 7 Sep 2017 14:17:56 +0100 Subject: [PATCH] 1st phase - Global CSS Former-commit-id: 508c4ab746f994bb3f4f5e86ff1ca5e6dc873f3a [formerly 8a29c22f817f54abefe21116e46f21b24306b6b8] [formerly e23c1a85571f61877b67656ef361f1c15acfcb3d [formerly 67fb6f8a78fee673edee1e1f30bbae33c70bb836]] Former-commit-id: 71e9024cf9107ef4e40f95bd388068fe052ea4f0 [formerly 7861ffe8d20f0777ae48c4f159efd7e32b2204d9] Former-commit-id: 7db04ac5016d182f13ac56911d0a10871fb261f3 --- assets/index.html | 1 + assets/src/views/GlobalSettings.vue | 29 +++++++++++++++++++++++++---- filemanager.go | 13 +++++++++++++ http/http.go | 1 + http/settings.go | 13 +++++++++++++ rice-box.go.REMOVED.git-id | 1 - 6 files changed, 53 insertions(+), 5 deletions(-) delete mode 100644 rice-box.go.REMOVED.git-id diff --git a/assets/index.html b/assets/index.html index 2f9926a9..bc92bef1 100644 --- a/assets/index.html +++ b/assets/index.html @@ -91,6 +91,7 @@ } } +
diff --git a/assets/src/views/GlobalSettings.vue b/assets/src/views/GlobalSettings.vue index 4b4a5085..af2d48bf 100644 --- a/assets/src/views/GlobalSettings.vue +++ b/assets/src/views/GlobalSettings.vue @@ -21,6 +21,12 @@

+
+

{{ $t('settings.customStylesheet') }}

+ +

+
+

{{ $t('settings.commands') }}

@@ -46,7 +52,8 @@ export default { data: function () { return { commands: [], - staticGen: [] + staticGen: [], + css: '' } }, computed: { @@ -65,8 +72,10 @@ export default { value: settings.commands[key].join('\n') }) } + + this.css = settings.css }) - .catch(error => { this.$showError(error) }) + .catch(this.$showError) }, methods: { capitalize (name, where = '_') { @@ -96,7 +105,19 @@ export default { updateSettings(commands, 'commands') .then(() => { this.$showSuccess(this.$t('settings.commandsUpdated')) }) - .catch(error => { this.$showError(error) }) + .catch(this.$showError) + }, + saveCSS (event) { + event.preventDefault() + + updateSettings(this.css, 'css') + .then(() => { + this.$showSuccess(this.$t('settings.settingsUpdated')) + let style = document.querySelector('style[title="global-css"]') + style.innerHTML = '' + style.appendChild(document.createTextNode(this.css)) + }) + .catch(this.$showError) }, saveStaticGen (event) { event.preventDefault() @@ -117,7 +138,7 @@ export default { updateSettings(staticGen, 'staticGen') .then(() => { this.$showSuccess(this.$t('settings.settingsUpdated')) }) - .catch(error => { this.$showError(error) }) + .catch(this.$showError) }, parseStaticGen (staticgen) { for (let option of staticgen) { diff --git a/filemanager.go b/filemanager.go index d008b709..72fb6b59 100644 --- a/filemanager.go +++ b/filemanager.go @@ -74,6 +74,9 @@ type FileManager struct { // A map of events to a slice of commands. Commands map[string][]string + // Global stylesheet. + CSS string + // NewFS should build a new file system for a given path. NewFS FSBuilder } @@ -111,6 +114,16 @@ func (m *FileManager) Setup() error { return err } + // Get the global CSS. + err = m.Store.Config.Get("css", &m.CSS) + if err != nil && err == ErrNotExist { + err = m.Store.Config.Save("css", "") + } + + if err != nil { + return err + } + // Tries to get the event commands from the database. // If they don't exist, initialize them. err = m.Store.Config.Get("commands", &m.Commands) diff --git a/http/http.go b/http/http.go index 5d5231d3..b78d4979 100644 --- a/http/http.go +++ b/http/http.go @@ -229,6 +229,7 @@ func renderFile(c *fm.Context, w http.ResponseWriter, file string) (int, error) "BaseURL": c.RootURL(), "NoAuth": c.NoAuth, "Version": fm.Version, + "CSS": template.CSS(c.CSS), } if c.StaticGen != nil { diff --git a/http/settings.go b/http/settings.go index f0d9b2a3..ae676bac 100644 --- a/http/settings.go +++ b/http/settings.go @@ -13,6 +13,7 @@ import ( type modifySettingsRequest struct { *modifyRequest Data struct { + CSS string `json:"css"` Commands map[string][]string `json:"commands"` StaticGen map[string]interface{} `json:"staticGen"` } `json:"data"` @@ -61,6 +62,7 @@ func settingsHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) (int } type settingsGetRequest struct { + CSS string `json:"css"` Commands map[string][]string `json:"commands"` StaticGen []option `json:"staticGen"` } @@ -73,6 +75,7 @@ func settingsGetHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) ( result := &settingsGetRequest{ Commands: c.Commands, StaticGen: []option{}, + CSS: c.CSS, } if c.StaticGen != nil { @@ -114,6 +117,16 @@ func settingsPutHandler(c *fm.Context, w http.ResponseWriter, r *http.Request) ( return http.StatusOK, nil } + // Update the global CSS. + if mod.Which == "css" { + if err := c.Store.Config.Save("css", mod.Data.CSS); err != nil { + return http.StatusInternalServerError, err + } + + c.CSS = mod.Data.CSS + return http.StatusOK, nil + } + // Update the static generator options. if mod.Which == "staticGen" { err = mapstructure.Decode(mod.Data.StaticGen, c.StaticGen) diff --git a/rice-box.go.REMOVED.git-id b/rice-box.go.REMOVED.git-id deleted file mode 100644 index ae82af19..00000000 --- a/rice-box.go.REMOVED.git-id +++ /dev/null @@ -1 +0,0 @@ -f6b2651e89474f0fcec0cfcc0bbfd11b023513d9 \ No newline at end of file