Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default theme config option #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Hajimari supports the following configuration options that can be modified by ei
| instanceName | Name of the Hajimari instance | "" | string |
| customApps | A list of custom apps that you would like to add to the Hajimari instance | {} | []CustomApp |
| groups | A list of bookmark groups to add to the Hajimari instance | {} | []groups |
| defaultThem | Default theme name. See previous image for a list of available themes | "gazette" | string |

#### NamespaceSelector

Expand Down
1 change: 1 addition & 0 deletions config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespaceSelector:
- downloads
title: null
name: "you"
defaultTheme: "gazette"
customApps:
- name: Test
url: https://example.com
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct {
Title string
InstanceName string
Name string
DefaultTheme string
CustomApps []CustomApp
Groups []Group
Providers []Provider
Expand Down
30 changes: 16 additions & 14 deletions internal/handlers/startpage.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,23 @@ func (sr *startpageResource) GetStartpage(w http.ResponseWriter, r *http.Request
w.Header().Add("Content-Type", "text/html")

err = sr.tpl.Execute(w, struct {
Title string
Greeting string
Date string
Apps []hajimari.App
Groups []config.Group
Providers []config.Provider
Modules []config.Module
Title string
Greeting string
Date string
DefaultTheme string
Apps []hajimari.App
Groups []config.Group
Providers []config.Provider
Modules []config.Module
}{
Title: appConfig.Title,
Greeting: tplutil.Greet(appConfig.Name, time.Now().Hour()),
Date: time.Now().Format("Mon, Jan 02"),
Apps: hajimariApps,
Groups: appConfig.Groups,
Providers: appConfig.Providers,
Modules: appConfig.Modules,
Title: appConfig.Title,
Greeting: tplutil.Greet(appConfig.Name, time.Now().Hour()),
Date: time.Now().Format("Mon, Jan 02"),
DefaultTheme: appConfig.DefaultTheme,
Apps: hajimariApps,
Groups: appConfig.Groups,
Providers: appConfig.Providers,
Modules: appConfig.Modules,
})

if err != nil {
Expand Down
269 changes: 140 additions & 129 deletions web/static/js/themer.js
Original file line number Diff line number Diff line change
@@ -1,139 +1,150 @@
const setValue = (property, value) => {
if (value) {
document.documentElement.style.setProperty(`--${property}`, value);

const input = document.querySelector(`#${property}`);
if (input) {
value = value.replace('px', '');
input.value = value;
}
}
};

const setValueFromLocalStorage = property => {
let value = localStorage.getItem(property);
setValue(property, value);
};

const setTheme = options => {
for (let option of Object.keys(options)) {
const property = option;
const value = options[option];
const dataThemeButtons = document.querySelectorAll('[data-theme]');

setValue(property, value);
localStorage.setItem(property, value);
}
for (let i = 0; i < dataThemeButtons.length; i++) {
dataThemeButtons[i].addEventListener('click', () => {
const theme = dataThemeButtons[i].dataset.theme;
applyTheme(theme);
});
}

document.addEventListener('DOMContentLoaded', () => {
if (isThemeSet()) {
setValueFromLocalStorage('color-background');
setValueFromLocalStorage('color-text-pri');
setValueFromLocalStorage('color-text-acc');
} else {
applyTheme(window.defaultTheme);
}
});

const dataThemeButtons = document.querySelectorAll('[data-theme]');
const setValue = (property, value) => {
if (value) {
document.documentElement.style.setProperty(`--${property}`, value);

for (let i = 0; i < dataThemeButtons.length; i++) {
dataThemeButtons[i].addEventListener('click', () => {
const theme = dataThemeButtons[i].dataset.theme;

switch (theme) {
case 'blackboard':
setTheme({
'color-background': '#1a1a1a',
'color-text-pri': '#FFFDEA',
'color-text-acc': '#5c5c5c'
});
return;

case 'gazette':
setTheme({
'color-background': '#F2F7FF',
'color-text-pri': '#000000',
'color-text-acc': '#5c5c5c'
});
return;

case 'espresso':
setTheme({
'color-background': '#21211F',
'color-text-pri': '#D1B59A',
'color-text-acc': '#4E4E4E'
});
return;

case 'cab':
setTheme({
'color-background': '#F6D305',
'color-text-pri': '#1F1F1F',
'color-text-acc': '#424242'
});
return;

case 'cloud':
setTheme({
'color-background': '#f1f2f0',
'color-text-pri': '#35342f',
'color-text-acc': '#37bbe4'
});
return;

case 'lime':
setTheme({
'color-background': '#263238',
'color-text-pri': '#AABBC3',
'color-text-acc': '#aeea00'
});
return;

case 'white':
setTheme({
'color-background': '#ffffff',
'color-text-pri': '#222222',
'color-text-acc': '#dddddd'
});
return;

case 'tron':
setTheme({
'color-background': '#242B33',
'color-text-pri': '#EFFBFF',
'color-text-acc': '#6EE2FF'
});
return;

case 'blues':
setTheme({
'color-background': '#2B2C56',
'color-text-pri': '#EFF1FC',
'color-text-acc': '#6677EB'
});
return;

case 'passion':
setTheme({
'color-background': '#f5f5f5',
'color-text-pri': '#12005e',
'color-text-acc': '#8e24aa'
});
return;

case 'chalk':
setTheme({
'color-background': '#263238',
'color-text-pri': '#AABBC3',
'color-text-acc': '#FF869A'
});
return;

case 'paper':
setTheme({
'color-background': '#F8F6F1',
'color-text-pri': '#4C432E',
'color-text-acc': '#AA9A73'
});
return;

}
})
}
const input = document.querySelector(`#${property}`);
if (input) {
value = value.replace('px', '');
input.value = value;
}
}
};

const setValueFromLocalStorage = (property) => {
let value = localStorage.getItem(property);
setValue(property, value);
};

const setTheme = (options) => {
for (let option of Object.keys(options)) {
const property = option;
const value = options[option];

setValue(property, value);
localStorage.setItem(property, value);
}
};

const isThemeSet = () =>
['color-background', 'color-text-pri', 'color-text-acc']
.map((key) => localStorage.getItem(key))
.filter(Boolean).length > 0;

const applyTheme = (theme) => {
switch (theme) {
case 'blackboard':
setTheme({
'color-background': '#1a1a1a',
'color-text-pri': '#FFFDEA',
'color-text-acc': '#5c5c5c',
});
return;

case 'gazette':
setTheme({
'color-background': '#F2F7FF',
'color-text-pri': '#000000',
'color-text-acc': '#5c5c5c',
});
return;

case 'espresso':
setTheme({
'color-background': '#21211F',
'color-text-pri': '#D1B59A',
'color-text-acc': '#4E4E4E',
});
return;

case 'cab':
setTheme({
'color-background': '#F6D305',
'color-text-pri': '#1F1F1F',
'color-text-acc': '#424242',
});
return;

case 'cloud':
setTheme({
'color-background': '#f1f2f0',
'color-text-pri': '#35342f',
'color-text-acc': '#37bbe4',
});
return;

case 'lime':
setTheme({
'color-background': '#263238',
'color-text-pri': '#AABBC3',
'color-text-acc': '#aeea00',
});
return;

case 'white':
setTheme({
'color-background': '#ffffff',
'color-text-pri': '#222222',
'color-text-acc': '#dddddd',
});
return;

case 'tron':
setTheme({
'color-background': '#242B33',
'color-text-pri': '#EFFBFF',
'color-text-acc': '#6EE2FF',
});
return;

case 'blues':
setTheme({
'color-background': '#2B2C56',
'color-text-pri': '#EFF1FC',
'color-text-acc': '#6677EB',
});
return;

case 'passion':
setTheme({
'color-background': '#f5f5f5',
'color-text-pri': '#12005e',
'color-text-acc': '#8e24aa',
});
return;

case 'chalk':
setTheme({
'color-background': '#263238',
'color-text-pri': '#AABBC3',
'color-text-acc': '#FF869A',
});
return;

case 'paper':
setTheme({
'color-background': '#F8F6F1',
'color-text-pri': '#4C432E',
'color-text-acc': '#AA9A73',
});
return;
}
};
3 changes: 2 additions & 1 deletion web/template/index.html.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>{{or .Title "Hajimari"}}</title>
<meta charset="utf-8">
<meta http-equiv="Default-Style" content="">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" name="viewport" />
<link type="text/css" rel="stylesheet" href="./static/css/styles.css" media="screen,projection"/>
<link href="//fonts.googleapis.com/css?family=Roboto:400,500,700,900" rel="stylesheet">
Expand All @@ -13,6 +13,7 @@
<link href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/theme/dracula.min.css" rel="stylesheet" type="text/css">
<link href="//cdnjs.cloudflare.com/ajax/libs/codemirror/5.62.2/addon/fold/foldgutter.min.css" rel="stylesheet" type="text/css">
<script src="//code.iconify.design/1/1.0.7/iconify.min.js"></script>
<script type="text/javascript">window.defaultTheme = "{{.DefaultTheme}}"</script>
</head>

<body>
Expand Down