import Controller from 'ember-controller'; import computed, {notEmpty} from 'ember-computed'; import injectService from 'ember-service/inject'; import observer from 'ember-metal/observer'; import run from 'ember-runloop'; import SettingsSaveMixin from 'ghost-admin/mixins/settings-save'; import randomPassword from 'ghost-admin/utils/random-password'; import $ from 'jquery'; export default Controller.extend(SettingsSaveMixin, { availableTimezones: null, themeToDelete: null, showUploadLogoModal: false, showUploadCoverModal: false, showUploadIconModal: false, showDeleteThemeModal: notEmpty('themeToDelete'), ajax: injectService(), config: injectService(), ghostPaths: injectService(), notifications: injectService(), session: injectService(), _scratchFacebook: null, _scratchTwitter: null, iconMimeTypes: 'image/png,image/x-icon', iconExtensions: ['ico', 'png'], logoImageSource: computed('model.logo', function () { return this.get('model.logo') || ''; }), iconImageSource: computed('model.icon', function () { return this.get('model.icon') || ''; }), 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/'; } }), 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()); } }), _deleteTheme() { let theme = this.get('themeToDelete'); let themeURL = `${this.get('ghostPaths.apiRoot')}/themes/${theme.name}/`; if (!theme) { return; } return this.get('ajax').del(themeURL).then(() => { this.send('reloadSettings'); }).catch((error) => { this.get('notifications').showAPIError(error); }); }, save() { let notifications = this.get('notifications'); let config = this.get('config'); return this.get('model').save().then((model) => { config.set('blogTitle', model.get('title')); // this forces the document title to recompute after // a blog title change this.send('collectTitleTokens', []); return model; }).catch((error) => { if (error) { notifications.showAPIError(error, {key: 'settings.save'}); } throw error; }); }, actions: { setTheme(theme) { this.set('model.activeTheme', theme.name); this.send('save'); }, downloadTheme(theme) { let themeURL = `${this.get('ghostPaths.apiRoot')}/themes/${theme.name}`; let accessToken = this.get('session.data.authenticated.access_token'); let downloadURL = `${themeURL}/download/?access_token=${accessToken}`; let iframe = $('#iframeDownload'); if (iframe.length === 0) { iframe = $('