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';
|
2022-10-06 18:51:58 +03:00
|
|
|
import {action} from '@ember/object';
|
|
|
|
import {inject as service} from '@ember/service';
|
2020-05-11 13:37:35 +03:00
|
|
|
import {task} from 'ember-concurrency';
|
2022-10-06 18:51:58 +03:00
|
|
|
import {tracked} from '@glimmer/tracking';
|
2015-01-11 22:55:52 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
export default class NavigationController extends Controller {
|
|
|
|
@service config;
|
|
|
|
@service ghostPaths;
|
|
|
|
@service notifications;
|
|
|
|
@service session;
|
|
|
|
@service settings;
|
|
|
|
|
2022-10-06 18:51:58 +03:00
|
|
|
@tracked dirtyAttributes = false;
|
|
|
|
@tracked newNavItem = NavigationItem.create({isNew: true});
|
|
|
|
@tracked newSecondaryNavItem = NavigationItem.create({isNew: true, isSecondary: true});
|
2018-03-19 14:54:54 +03:00
|
|
|
|
2022-02-09 12:53:38 +03:00
|
|
|
get blogUrl() {
|
2022-10-07 17:24:03 +03:00
|
|
|
let url = this.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;
|
|
|
|
}
|
|
|
|
|
2022-10-07 16:23:21 +03:00
|
|
|
let navItems = item.isSecondary ? this.settings.secondaryNavigation : this.settings.navigation;
|
2022-02-09 12:53:38 +03:00
|
|
|
|
|
|
|
navItems.removeObject(item);
|
2022-10-06 18:51:58 +03:00
|
|
|
this.dirtyAttributes = true;
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|
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);
|
2022-10-06 18:51:58 +03:00
|
|
|
this.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);
|
2022-10-06 18:51:58 +03:00
|
|
|
this.dirtyAttributes = true;
|
2022-02-09 12:53:38 +03:00
|
|
|
}
|
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
|
|
|
|
reset() {
|
2022-10-06 18:51:58 +03:00
|
|
|
this.newNavItem = NavigationItem.create({isNew: true});
|
|
|
|
this.newSecondaryNavItem = NavigationItem.create({isNew: true, isSecondary: 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
|
|
|
addNewNavItem(item) {
|
2022-10-07 16:23:21 +03:00
|
|
|
let navItems = item.isSecondary ? this.settings.secondaryNavigation : this.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);
|
2022-10-06 18:51:58 +03:00
|
|
|
this.dirtyAttributes = true;
|
2022-02-09 12:53:38 +03:00
|
|
|
|
|
|
|
if (item.isSecondary) {
|
2022-10-06 18:51:58 +03:00
|
|
|
this.newSecondaryNavItem = NavigationItem.create({isNew: true, isSecondary: true});
|
2022-02-09 12:53:38 +03:00
|
|
|
} else {
|
2022-10-06 18:51:58 +03:00
|
|
|
this.newNavItem = NavigationItem.create({isNew: true});
|
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-09-09 18:50:05 +03:00
|
|
|
@task
|
|
|
|
*saveTask() {
|
2022-10-07 16:23:21 +03:00
|
|
|
let navItems = this.settings.navigation;
|
|
|
|
let secondaryNavItems = this.settings.secondaryNavigation;
|
2019-12-04 07:14:45 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-03 14:21:16 +03:00
|
|
|
navItems.forEach((item) => {
|
2018-01-11 20:43:23 +03:00
|
|
|
validationPromises.pushObject(item.validate());
|
|
|
|
});
|
|
|
|
|
2022-08-03 14:21:16 +03:00
|
|
|
secondaryNavItems.forEach((item) => {
|
2019-12-04 07:14:45 +03:00
|
|
|
validationPromises.pushObject(item.validate());
|
|
|
|
});
|
|
|
|
|
2018-01-11 20:43:23 +03:00
|
|
|
try {
|
|
|
|
yield RSVP.all(validationPromises);
|
2022-10-12 16:03:54 +03:00
|
|
|
|
|
|
|
// If some attributes have been changed, rebuild
|
|
|
|
// the model arrays or changes will not be detected
|
|
|
|
if (this.dirtyAttributes) {
|
|
|
|
this.settings.navigation = [...this.settings.navigation];
|
|
|
|
this.settings.secondaryNavigation = [...this.settings.secondaryNavigation];
|
|
|
|
}
|
|
|
|
|
2022-10-06 18:51:58 +03:00
|
|
|
this.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
|
|
|
}
|