Skip to content

Commit

Permalink
[Feature] Show theme settings
Browse files Browse the repository at this point in the history
Implement switching themes on settings page.
  • Loading branch information
wwebfor committed Oct 19, 2017
1 parent 7e4592f commit 3d79228
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<meta name="msapplication-square310x310logo" content="images/icon/icon-310x310.png" />

<link rel="stylesheet" href="styles/core.css">
<link rel="stylesheet" href="styles/theme-default.css">
<link rel="stylesheet" href="styles/theme-default.css" id="lav--theme">
</head>
<body class="-noscroll">

Expand Down
2 changes: 2 additions & 0 deletions app/scripts/collections/configNames.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const configNames = {
*
* @prop {Array} appProfiles - profile names stored in JSON format
* @prop {String} appLang - localization (en|fr...etc)
* @prop {String} theme - theme
* @prop {String} useDefaultConfigs - (0|1) 1 if the profile should
* use settings from the main profile.
* @prop {String} pagination - the number of notes shown per page
Expand All @@ -31,6 +32,7 @@ const configNames = {
general: {
appProfiles : ['default'],
appLang : '',
theme : '',
useDefaultConfigs : '1',

pagination : '10',
Expand Down
35 changes: 35 additions & 0 deletions app/scripts/components/settings/show/general/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import Mn from 'backbone.marionette';
import _ from 'underscore';
import i18n from 'i18next';
import locales from '../../../../../locales/locales.json';
import themes from '../../../../../styles/themes.json';
import Behavior from '../Behavior';
import Radio from 'backbone.radio';

/**
* General settings view.
Expand All @@ -31,6 +33,25 @@ export default class View extends Mn.View {
return [Behavior];
}

events() {
return {
'change @ui.theme': 'previewTheme',
};
}

ui() {
return {
theme: '#theme',
};
}

/**
* Preview a theme.
*/
previewTheme() {
Radio.trigger('components/settings', 'changeTheme', {name: this.ui.theme.val()});
}

/**
* serializeData.
*
Expand All @@ -42,7 +63,9 @@ export default class View extends Mn.View {
return {
locales,
models,
themes,
appLang : (models.appLang || i18n.language) || 'en',
theme : models.theme || 'default',
profileId : this.options.profileId,
useDefault : this.options.useDefault.attributes,
};
Expand Down Expand Up @@ -78,6 +101,18 @@ export default class View extends Mn.View {
}
},

/**
* Select a theme if it's active.
*
* @param {String} theme - theme name
* @returns {String}
*/
selectTheme(theme) {
if (this.theme === theme) {
return ' selected';
}
},

};
}

Expand Down
13 changes: 13 additions & 0 deletions app/scripts/components/settings/show/general/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label" for="theme">
<i class="fa fa-cloud"></i> {{ _.i18n('Theme') }}
</label>
<div class="col-sm-10">
<select class="form-control" name="theme" id="theme">
<% _.forEach(themes, function(name, theme) { %>
<option value="{{theme}}"{{ selectTheme(theme) }}>{{name}}</option>
<% }); %>
</select>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label" for="pagination">{{ _.i18n('Notes per page') }}</label>
<div class="col-sm-10">
Expand Down
1 change: 1 addition & 0 deletions app/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import App from './App';
import './workers/Delegator';

// Utils
import './utils/theme';
import './utils/Env';
import './utils/underscore';
import './utils/I18n';
Expand Down
34 changes: 34 additions & 0 deletions app/scripts/utils/theme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import Radio from 'backbone.radio';

const theme = {
/**
* Apply a theme.
*
* @param {Object} data
* @param {String} data.name - theme name
*/
applyTheme(data = {}) {
const theme = data.name || Radio.request('collections/Configs', 'findConfig', {
name: 'theme',
});

$('#lav--theme').attr('href', `styles/theme-${theme || 'default'}.css`);
},

/**
* Initializer.
*/
initializer() {
Radio.on('components/settings', 'changeTheme', theme.applyTheme);
theme.applyTheme();
},
};

Radio.once('App', 'init', () => {
Radio.request('utils/Initializer', 'add', {
name : 'App:utils',
callback: theme.initializer,
});
});

export default theme;
4 changes: 4 additions & 0 deletions app/styles/themes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"default": "Default theme",
"dark": "Dark"
}

0 comments on commit 3d79228

Please sign in to comment.