2022-02-09 12:53:38 +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-05-29 21:50:03 +03:00
|
|
|
import $ from 'jquery';
|
2017-08-22 10:53:26 +03:00
|
|
|
import Controller from '@ember/controller';
|
2017-05-29 21:50:03 +03:00
|
|
|
import NavigationItem from 'ghost-admin/models/navigation-item';
|
|
|
|
import RSVP from 'rsvp';
|
2020-05-11 13:37:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@classic
|
|
|
|
export default class NavigationController extends Controller {
|
|
|
|
@service config;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service notifications;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
|
|
|
|
|
|
|
dirtyAttributes = false;
|
|
|
|
newNavItem = null;
|
|
|
|
newSecondaryNavItem = null;
|
|
|
|
|
2018-03-19 14:54:54 +03:00
|
|
|
init() {
|
2022-02-09 12:53:38 +03:00
|
|
|
super.init(...arguments);
|
2018-03-19 14:54:54 +03:00
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
2019-12-04 07:14:45 +03:00
|
|
|
this.set('newSecondaryNavItem', NavigationItem.create({isNew: true, isSecondary: true}));
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|
2018-03-19 14:54:54 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@computed('config.blogUrl')
|
|
|
|
get blogUrl() {
|
2015-10-28 14:36:45 +03:00
|
|
|
let url = this.get('config.blogUrl');
|
2015-01-18 03:16:54 +03:00
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
return url.slice(-1) !== '/' ? `${url}/` : url;
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|
2015-01-18 03:16:54 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
save() {
|
|
|
|
this.saveTask.perform();
|
|
|
|
}
|
2017-05-18 13:48:37 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
addNavItem(item) {
|
|
|
|
// If the url sent through is blank (user never edited the url)
|
|
|
|
if (item.get('url') === '') {
|
|
|
|
item.set('url', '/');
|
|
|
|
}
|
2016-03-18 18:32:03 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
return item.validate().then(() => {
|
|
|
|
this.addNewNavItem(item);
|
|
|
|
});
|
|
|
|
}
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
deleteNavItem(item) {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let navItems = item.isSecondary ? this.get('settings.secondaryNavigation') : this.get('settings.navigation');
|
|
|
|
|
|
|
|
navItems.removeObject(item);
|
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
}
|
2015-01-14 17:46:29 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
updateLabel(label, navItem) {
|
|
|
|
if (!navItem) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-03 19:29:01 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
if (navItem.get('label') !== label) {
|
|
|
|
navItem.set('label', label);
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('dirtyAttributes', true);
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
updateUrl(url, navItem) {
|
|
|
|
if (!navItem) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
if (navItem.get('url') !== url) {
|
|
|
|
navItem.set('url', url);
|
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
}
|
2015-02-25 20:20:42 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
return url;
|
|
|
|
}
|
2015-01-18 03:16:54 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
toggleLeaveSettingsModal(transition) {
|
|
|
|
let leaveTransition = this.leaveSettingsTransition;
|
2019-01-02 12:58:55 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
if (!transition && this.showLeaveSettingsModal) {
|
|
|
|
this.set('leaveSettingsTransition', null);
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
return;
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveSettingsTransition', transition);
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
// if a save is running, wait for it to finish then transition
|
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
|
|
|
transition.retry();
|
|
|
|
});
|
2017-10-31 18:27:25 +03:00
|
|
|
}
|
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
leaveSettings() {
|
|
|
|
let transition = this.leaveSettingsTransition;
|
|
|
|
let settings = this.settings;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
if (!transition) {
|
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
|
|
|
return;
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
// roll back changes on settings props
|
|
|
|
settings.rollbackAttributes();
|
|
|
|
this.set('dirtyAttributes', false);
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
return transition.retry();
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@action
|
|
|
|
reset() {
|
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
|
|
|
this.set('newSecondaryNavItem', NavigationItem.create({isNew: true, isSecondary: true}));
|
|
|
|
}
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
addNewNavItem(item) {
|
|
|
|
let navItems = item.isSecondary ? this.get('settings.secondaryNavigation') : this.get('settings.navigation');
|
2016-02-09 20:16:18 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
item.set('isNew', false);
|
|
|
|
navItems.pushObject(item);
|
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
|
|
|
|
if (item.isSecondary) {
|
2019-12-04 07:14:45 +03:00
|
|
|
this.set('newSecondaryNavItem', NavigationItem.create({isNew: true, isSecondary: true}));
|
2022-02-09 12:53:38 +03:00
|
|
|
$('.gh-blognav-container:last .gh-blognav-line:last input:first').focus();
|
|
|
|
} else {
|
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
|
|
|
$('.gh-blognav-container:first .gh-blognav-line:last input:first').focus();
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
@task *saveTask() {
|
2018-01-11 20:43:23 +03:00
|
|
|
let navItems = this.get('settings.navigation');
|
2019-12-04 07:14:45 +03:00
|
|
|
let secondaryNavItems = this.get('settings.secondaryNavigation');
|
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
let notifications = this.notifications;
|
2018-01-11 20:43:23 +03:00
|
|
|
let validationPromises = [];
|
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
if (!this.newNavItem.get('isBlank')) {
|
|
|
|
validationPromises.pushObject(this.send('addNavItem', this.newNavItem));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.newSecondaryNavItem.get('isBlank')) {
|
|
|
|
validationPromises.pushObject(this.send('addNavItem', this.newSecondaryNavItem));
|
2018-01-11 20:43:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
navItems.map((item) => {
|
|
|
|
validationPromises.pushObject(item.validate());
|
|
|
|
});
|
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
secondaryNavItems.map((item) => {
|
|
|
|
validationPromises.pushObject(item.validate());
|
|
|
|
});
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
try {
|
|
|
|
yield RSVP.all(validationPromises);
|
|
|
|
this.set('dirtyAttributes', false);
|
2019-03-06 16:53:54 +03:00
|
|
|
return yield this.settings.save();
|
2018-01-11 20:43:23 +03:00
|
|
|
} catch (error) {
|
|
|
|
if (error) {
|
|
|
|
notifications.showAPIError(error);
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|