Ghost/ghost/admin/app/controllers/settings/navigation.js

147 lines
4.0 KiB
JavaScript
Raw Normal View History

import Controller from '@ember/controller';
import NavigationItem from 'ghost-admin/models/navigation-item';
import RSVP from 'rsvp';
import {action} from '@ember/object';
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
import {inject} from 'ghost-admin/decorators/inject';
import {inject as service} from '@ember/service';
import {task} from 'ember-concurrency';
import {tracked} from '@glimmer/tracking';
export default class NavigationController extends Controller {
@service ghostPaths;
@service notifications;
@service session;
@service settings;
Fixed hosting management screen not loading after sign-in process (#15763) refs https://github.com/TryGhost/Team/issues/2110 - dynamically defined properties on the config service did not have autotracking set up properly if they were accessed in any way before the property was defined, this caused problems in a number of areas because we have both "unauthed" and "authed" sets of config and when not logged in we had parts of the app checking for authed config properties that don't exist until after sign-in and subsequent config re-fetch - renamed `config` service to `configManager` and updated to only contain methods for fetching config data - added a `config` instance initializer that sets up a `TrackedObject` instance with some custom properties/methods and registers it on `config:main` - uses application instance initializer rather than a standard initializer because standard initializers are only called once when setting up the test suite so we'd end up with config leaking across tests - added an `@inject` decorator that when used takes the property name and injects whatever is registered at `${propertyName}:main`, this allows us to use dependency injection for any object rather than just services or controllers - using `application.inject()` in the initializer was initially used but that only works for objects that extend from `EmberObject`, the injections weren't available in native-class glimmer components so this decorator keeps the injection syntax consistent - swapped all `@service config` uses to `@inject config`
2022-11-03 14:14:36 +03:00
@inject config;
@tracked dirtyAttributes = false;
@tracked newNavItem = NavigationItem.create({isNew: true});
@tracked newSecondaryNavItem = NavigationItem.create({isNew: true, isSecondary: true});
get blogUrl() {
let url = this.config.blogUrl;
return url.slice(-1) !== '/' ? `${url}/` : url;
}
@action
save() {
this.saveTask.perform();
}
@action
addNavItem(item) {
// If the url sent through is blank (user never edited the url)
if (item.get('url') === '') {
item.set('url', '/');
}
return item.validate().then(() => {
this.addNewNavItem(item);
});
}
@action
deleteNavItem(item) {
if (!item) {
return;
}
let navItems = item.isSecondary ? this.settings.secondaryNavigation : this.settings.navigation;
navItems.removeObject(item);
this.dirtyAttributes = true;
}
@action
updateLabel(label, navItem) {
if (!navItem) {
return;
}
if (navItem.get('label') !== label) {
navItem.set('label', label);
this.dirtyAttributes = true;
}
}
@action
updateUrl(url, navItem) {
if (!navItem) {
return;
}
if (navItem.get('url') !== url) {
navItem.set('url', url);
this.dirtyAttributes = true;
}
2015-02-25 20:20:42 +03:00
return url;
}
@action
reset() {
this.newNavItem = NavigationItem.create({isNew: true});
this.newSecondaryNavItem = NavigationItem.create({isNew: true, isSecondary: true});
}
addNewNavItem(item) {
let navItems = item.isSecondary ? this.settings.secondaryNavigation : this.settings.navigation;
item.set('isNew', false);
navItems.pushObject(item);
this.dirtyAttributes = true;
if (item.isSecondary) {
this.newSecondaryNavItem = NavigationItem.create({isNew: true, isSecondary: true});
} else {
this.newNavItem = NavigationItem.create({isNew: true});
}
}
@task
*saveTask() {
let navItems = this.settings.navigation;
let secondaryNavItems = this.settings.secondaryNavigation;
let notifications = this.notifications;
let validationPromises = [];
if (!this.newNavItem.get('isBlank')) {
validationPromises.pushObject(this.send('addNavItem', this.newNavItem));
}
if (!this.newSecondaryNavItem.get('isBlank')) {
validationPromises.pushObject(this.send('addNavItem', this.newSecondaryNavItem));
}
navItems.forEach((item) => {
validationPromises.pushObject(item.validate());
});
secondaryNavItems.forEach((item) => {
validationPromises.pushObject(item.validate());
});
try {
yield RSVP.all(validationPromises);
// 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];
}
this.dirtyAttributes = false;
return yield this.settings.save();
} catch (error) {
if (error) {
notifications.showAPIError(error);
throw error;
}
}
}
}