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';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {computed} from '@ember/object';
|
2017-10-30 12:38:01 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-11 13:37:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2017-03-08 16:55:35 +03:00
|
|
|
export default Controller.extend({
|
2017-10-30 12:38:01 +03:00
|
|
|
config: service(),
|
|
|
|
ghostPaths: service(),
|
|
|
|
notifications: service(),
|
|
|
|
session: service(),
|
2018-01-11 01:57:43 +03:00
|
|
|
settings: service(),
|
2015-05-26 05:10:50 +03:00
|
|
|
|
2017-10-31 18:27:25 +03:00
|
|
|
dirtyAttributes: false,
|
2019-03-25 15:20:14 +03:00
|
|
|
newNavItem: null,
|
2019-12-04 07:14:45 +03:00
|
|
|
newSecondaryNavItem: null,
|
2021-01-22 14:24:45 +03:00
|
|
|
|
2018-03-19 14:54:54 +03:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
this.set('newNavItem', NavigationItem.create({isNew: true}));
|
2019-12-04 07:14:45 +03:00
|
|
|
this.set('newSecondaryNavItem', NavigationItem.create({isNew: true, isSecondary: true}));
|
2018-03-19 14:54:54 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
blogUrl: computed('config.blogUrl', function () {
|
|
|
|
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;
|
2015-01-18 03:16:54 +03:00
|
|
|
}),
|
|
|
|
|
2015-01-11 22:55:52 +03:00
|
|
|
actions: {
|
2017-05-18 13:48:37 +03:00
|
|
|
save() {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.save.perform();
|
2017-05-18 13:48:37 +03:00
|
|
|
},
|
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
addNavItem(item) {
|
2016-03-18 18:32:03 +03:00
|
|
|
// If the url sent through is blank (user never edited the url)
|
2019-12-04 07:14:45 +03:00
|
|
|
if (item.get('url') === '') {
|
|
|
|
item.set('url', '/');
|
2016-03-18 18:32:03 +03:00
|
|
|
}
|
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
return item.validate().then(() => {
|
|
|
|
this.addNewNavItem(item);
|
2016-02-09 20:16:18 +03:00
|
|
|
});
|
2015-01-11 22:55:52 +03:00
|
|
|
},
|
|
|
|
|
2017-02-21 22:04:50 +03:00
|
|
|
deleteNavItem(item) {
|
2015-01-14 17:46:29 +03:00
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
let navItems = item.isSecondary ? this.get('settings.secondaryNavigation') : this.get('settings.navigation');
|
2015-02-03 19:29:01 +03:00
|
|
|
|
2015-02-25 20:20:42 +03:00
|
|
|
navItems.removeObject(item);
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateLabel(label, navItem) {
|
|
|
|
if (!navItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-18 15:06:17 +03:00
|
|
|
if (navItem.get('label') !== label) {
|
|
|
|
navItem.set('label', label);
|
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
}
|
2015-02-25 20:20:42 +03:00
|
|
|
},
|
|
|
|
|
2015-10-28 14:36:45 +03:00
|
|
|
updateUrl(url, navItem) {
|
2015-01-18 03:16:54 +03:00
|
|
|
if (!navItem) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-18 15:06:17 +03:00
|
|
|
if (navItem.get('url') !== url) {
|
|
|
|
navItem.set('url', url);
|
|
|
|
this.set('dirtyAttributes', true);
|
|
|
|
}
|
2019-01-02 12:58:55 +03:00
|
|
|
|
|
|
|
return url;
|
2017-10-31 18:27:25 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
toggleLeaveSettingsModal(transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
let leaveTransition = this.leaveSettingsTransition;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
2019-03-06 16:53:54 +03:00
|
|
|
if (!transition && this.showLeaveSettingsModal) {
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('leaveSettingsTransition', null);
|
|
|
|
this.set('showLeaveSettingsModal', false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!leaveTransition || transition.targetName === leaveTransition.targetName) {
|
|
|
|
this.set('leaveSettingsTransition', transition);
|
|
|
|
|
|
|
|
// if a save is running, wait for it to finish then transition
|
2020-05-11 13:37:35 +03:00
|
|
|
if (this.save.isRunning) {
|
|
|
|
return this.save.last.then(() => {
|
2017-10-31 18:27:25 +03:00
|
|
|
transition.retry();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// we genuinely have unsaved data, show the modal
|
|
|
|
this.set('showLeaveSettingsModal', true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
leaveSettings() {
|
2019-03-06 16:53:54 +03:00
|
|
|
let transition = this.leaveSettingsTransition;
|
|
|
|
let settings = this.settings;
|
2017-10-31 18:27:25 +03:00
|
|
|
|
|
|
|
if (!transition) {
|
2019-03-06 16:53:54 +03:00
|
|
|
this.notifications.showAlert('Sorry, there was an error in the application. Please let the Ghost team know what happened.', {type: 'error'});
|
2017-10-31 18:27:25 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-11 01:57:43 +03:00
|
|
|
// roll back changes on settings props
|
|
|
|
settings.rollbackAttributes();
|
2017-10-31 18:27:25 +03:00
|
|
|
this.set('dirtyAttributes', false);
|
|
|
|
|
|
|
|
return transition.retry();
|
2016-02-09 20:16:18 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
2016-04-26 12:45:59 +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}));
|
2015-01-11 22:55:52 +03:00
|
|
|
}
|
2018-01-11 20:43:23 +03:00
|
|
|
},
|
|
|
|
|
2020-05-11 13:37:35 +03:00
|
|
|
save: task(function* () {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
addNewNavItem(item) {
|
|
|
|
let navItems = item.isSecondary ? this.get('settings.secondaryNavigation') : this.get('settings.navigation');
|
2018-01-11 20:43:23 +03:00
|
|
|
|
2019-12-04 07:14:45 +03:00
|
|
|
item.set('isNew', false);
|
|
|
|
navItems.pushObject(item);
|
2018-01-11 20:43:23 +03:00
|
|
|
this.set('dirtyAttributes', true);
|
2019-12-04 07:14:45 +03:00
|
|
|
|
|
|
|
if (item.isSecondary) {
|
|
|
|
this.set('newSecondaryNavItem', NavigationItem.create({isNew: true, isSecondary: true}));
|
|
|
|
$('.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
|
|
|
}
|
|
|
|
});
|