Ghost-Admin/app/controllers/settings/general.js

116 lines
3.1 KiB
JavaScript
Raw Normal View History

2015-02-13 05:22:32 +01:00
import Ember from 'ember';
import SettingsSaveMixin from 'ghost/mixins/settings-save';
import randomPassword from 'ghost/utils/random-password';
2016-01-19 14:03:27 +01:00
const {
Controller,
computed,
inject: {service},
observer
} = Ember;
export default Controller.extend(SettingsSaveMixin, {
showUploadLogoModal: false,
showUploadCoverModal: false,
2016-01-19 14:03:27 +01:00
notifications: service(),
config: service(),
selectedTheme: computed('model.activeTheme', 'themes', function () {
let activeTheme = this.get('model.activeTheme');
let themes = this.get('themes');
let selectedTheme;
themes.forEach((theme) => {
if (theme.name === activeTheme) {
selectedTheme = theme;
}
});
return selectedTheme;
}),
logoImageSource: computed('model.logo', function () {
return this.get('model.logo') || '';
}),
coverImageSource: computed('model.cover', function () {
return this.get('model.cover') || '';
}),
isDatedPermalinks: computed('model.permalinks', {
set(key, value) {
this.set('model.permalinks', value ? '/:year/:month/:day/:slug/' : '/:slug/');
let slugForm = this.get('model.permalinks');
return slugForm !== '/:slug/';
},
get() {
let slugForm = this.get('model.permalinks');
return slugForm !== '/:slug/';
}
}),
themes: computed(function () {
return this.get('model.availableThemes').reduce(function (themes, t) {
let theme = {};
theme.name = t.name;
theme.label = t.package ? `${t.package.name} - ${t.package.version}` : t.name;
theme.package = t.package;
theme.active = !!t.active;
themes.push(theme);
return themes;
}, []);
}).readOnly(),
generatePassword: observer('model.isPrivate', function () {
this.get('model.errors').remove('password');
if (this.get('model.isPrivate') && this.get('model.hasDirtyAttributes')) {
this.get('model').set('password', randomPassword());
}
}),
save() {
let notifications = this.get('notifications');
let config = this.get('config');
return this.get('model').save().then((model) => {
config.set('blogTitle', model.get('title'));
return model;
}).catch((error) => {
if (error) {
notifications.showAPIError(error, {key: 'settings.save'});
}
});
},
actions: {
checkPostsPerPage() {
let postsPerPage = this.get('model.postsPerPage');
if (postsPerPage < 1 || postsPerPage > 1000 || isNaN(postsPerPage)) {
this.set('model.postsPerPage', 5);
}
},
setTheme(theme) {
this.set('model.activeTheme', theme.name);
},
toggleUploadCoverModal() {
this.toggleProperty('showUploadCoverModal');
},
toggleUploadLogoModal() {
this.toggleProperty('showUploadLogoModal');
}
}
});