2022-02-01 12:34:03 +03:00
|
|
|
import classic from 'ember-classic-decorator';
|
|
|
|
import {action, computed} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2018-01-11 01:57:43 +03:00
|
|
|
/* eslint-disable ghost/ember/alias-model-in-controller */
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2019-06-17 13:41:04 +03:00
|
|
|
import generatePassword from 'ghost-admin/utils/password-generator';
|
2017-05-23 11:50:04 +03:00
|
|
|
import {
|
2017-05-29 21:50:03 +03:00
|
|
|
IMAGE_EXTENSIONS,
|
|
|
|
IMAGE_MIME_TYPES
|
2017-05-23 11:50:04 +03:00
|
|
|
} from 'ghost-admin/components/gh-image-uploader';
|
2022-10-04 19:32:49 +03:00
|
|
|
import {TrackedObject} from 'tracked-built-ins';
|
2022-11-03 14:14:36 +03:00
|
|
|
import {inject} from 'ghost-admin/decorators/inject';
|
2022-10-04 19:55:20 +03:00
|
|
|
import {run} from '@ember/runloop';
|
2020-05-11 13:37:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2022-10-04 19:32:49 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2015-05-11 18:35:55 +03:00
|
|
|
|
2019-06-17 13:41:04 +03:00
|
|
|
function randomPassword() {
|
|
|
|
let word = generatePassword(6);
|
|
|
|
let randomN = Math.floor(Math.random() * 1000);
|
|
|
|
|
|
|
|
return word + randomN;
|
|
|
|
}
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@classic
|
|
|
|
export default class GeneralController extends Controller {
|
2022-02-01 20:03:45 +03:00
|
|
|
@service ghostPaths;
|
|
|
|
@service notifications;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
|
|
|
@service frontend;
|
|
|
|
@service ui;
|
2022-02-01 12:34:03 +03:00
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
@inject config;
|
|
|
|
|
2022-10-04 19:32:49 +03:00
|
|
|
@tracked scratchValues = new TrackedObject();
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
imageExtensions = IMAGE_EXTENSIONS;
|
|
|
|
imageMimeTypes = IMAGE_MIME_TYPES;
|
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
get availableTimezones() {
|
|
|
|
return this.config.availableTimezones;
|
|
|
|
}
|
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@computed('config.blogUrl', 'settings.publicHash')
|
|
|
|
get privateRSSUrl() {
|
2022-10-07 17:24:03 +03:00
|
|
|
let blogUrl = this.config.blogUrl;
|
2022-10-07 16:23:21 +03:00
|
|
|
let publicHash = this.settings.publicHash;
|
2017-10-05 12:59:14 +03:00
|
|
|
|
|
|
|
return `${blogUrl}/${publicHash}/rss`;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
save() {
|
2022-02-10 13:20:03 +03:00
|
|
|
this.saveTask.perform();
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
setTimezone(timezone) {
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings.timezone = timezone.name;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
removeImage(image) {
|
|
|
|
// setting `null` here will error as the server treats it as "null"
|
2022-10-07 16:23:21 +03:00
|
|
|
this.settings[image] = '';
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Opens a file selection dialog - Triggered by "Upload Image" buttons,
|
|
|
|
* searches for the hidden file input within the .gh-setting element
|
|
|
|
* containing the clicked button then simulates a click
|
|
|
|
* @param {MouseEvent} event - MouseEvent fired by the button click
|
|
|
|
*/
|
|
|
|
@action
|
|
|
|
triggerFileDialog(event) {
|
2022-05-29 13:18:49 +03:00
|
|
|
event?.target.closest('.gh-setting-action')?.querySelector('input[type="file"]')?.click();
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fired after an image upload completes
|
|
|
|
* @param {string} property - Property name to be set on `this.settings`
|
|
|
|
* @param {UploadResult[]} results - Array of UploadResult objects
|
|
|
|
* @return {string} The URL that was set on `this.settings.property`
|
|
|
|
*/
|
|
|
|
@action
|
|
|
|
imageUploaded(property, results) {
|
|
|
|
if (results[0]) {
|
2022-10-07 16:23:21 +03:00
|
|
|
return this.settings[property] = results[0].url;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
}
|
2018-03-09 17:42:27 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@action
|
|
|
|
toggleIsPrivate(isPrivate) {
|
|
|
|
let settings = this.settings;
|
2018-03-09 17:42:27 +03:00
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
settings.isPrivate = isPrivate;
|
|
|
|
settings.errors.remove('password');
|
2018-01-11 17:16:42 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
let changedAttrs = settings.changedAttributes();
|
2018-03-09 17:42:27 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
// set a new random password when isPrivate is enabled
|
|
|
|
if (isPrivate && changedAttrs.isPrivate) {
|
2022-10-07 16:23:21 +03:00
|
|
|
settings.password = randomPassword();
|
2018-01-11 17:16:42 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
// reset the password when isPrivate is disabled
|
|
|
|
} else if (changedAttrs.password) {
|
2022-10-07 16:23:21 +03:00
|
|
|
settings.password = changedAttrs.password[0];
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-04 13:49:30 +03:00
|
|
|
|
2022-02-01 12:34:03 +03:00
|
|
|
@action
|
2022-10-04 19:32:49 +03:00
|
|
|
setScratchValue(property, value) {
|
|
|
|
this.scratchValues[property] = value;
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2016-05-17 21:14:14 +03:00
|
|
|
|
2022-10-04 19:32:49 +03:00
|
|
|
clearScratchValues() {
|
|
|
|
this.scratchValues = new TrackedObject();
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
_deleteTheme() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let theme = this.store.peekRecord('theme', this.themeToDelete.name);
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
if (!theme) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return theme.destroyRecord().catch((error) => {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAPIError(error);
|
2018-01-11 20:43:23 +03:00
|
|
|
});
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-10-04 19:32:49 +03:00
|
|
|
@task
|
|
|
|
*saveTask() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
try {
|
2021-10-28 15:59:41 +03:00
|
|
|
let changedAttrs = this.settings.changedAttributes();
|
2019-03-06 16:53:54 +03:00
|
|
|
let settings = yield this.settings.save();
|
2022-10-04 19:32:49 +03:00
|
|
|
|
|
|
|
this.clearScratchValues();
|
|
|
|
|
2022-11-03 14:14:36 +03:00
|
|
|
this.config.blogTitle = settings.title;
|
2022-10-04 19:32:49 +03:00
|
|
|
|
2021-10-28 15:59:41 +03:00
|
|
|
if (changedAttrs.password) {
|
|
|
|
this.frontend.loginIfNeeded();
|
|
|
|
}
|
2022-10-04 19:32:49 +03:00
|
|
|
|
2019-05-20 18:16:19 +03:00
|
|
|
// this forces the document title to recompute after a blog title change
|
|
|
|
this.ui.updateDocumentTitle();
|
2018-01-11 20:43:23 +03:00
|
|
|
|
|
|
|
return settings;
|
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
notifications.showAPIError(error, {key: 'settings.save'});
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
}
|
2022-10-04 19:32:49 +03:00
|
|
|
}
|
2022-10-04 19:55:20 +03:00
|
|
|
|
|
|
|
@action
|
|
|
|
saveViaKeyboard(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
|
|
|
|
// trigger any set-on-blur actions
|
|
|
|
const focusedElement = document.activeElement;
|
|
|
|
focusedElement?.blur();
|
|
|
|
|
|
|
|
// schedule save for when set-on-blur actions have finished
|
|
|
|
run.schedule('actions', this, function () {
|
|
|
|
focusedElement?.focus();
|
|
|
|
this.saveTask.perform();
|
|
|
|
});
|
|
|
|
}
|
2022-02-01 12:34:03 +03:00
|
|
|
}
|