Skip to content

Commit

Permalink
refactor(core): allow dataSources to specify a required dataSource (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherchrisberry committed Mar 29, 2018
1 parent 7e66219 commit e995e1e
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IController, module } from 'angular';
import { Application } from '../../application.model';
import { ApplicationDataSource } from '../../service/applicationDataSource';
import { APPLICATION_WRITE_SERVICE, ApplicationWriter } from 'core/application/service/application.write.service';
import { APPLICATION_READ_SERVICE, ApplicationReader } from 'core/application/service/application.read.service';

import './applicationDataSourceEditor.component.less';

Expand All @@ -20,7 +21,7 @@ export class DataSourceEditorController implements IController {

public dataSources: ApplicationDataSource[];

constructor(private applicationWriter: ApplicationWriter) { 'ngInject'; }
constructor(private applicationWriter: ApplicationWriter, private applicationReader: ApplicationReader) { 'ngInject'; }

public $onInit() {
if (this.application.notFound) {
Expand Down Expand Up @@ -71,12 +72,7 @@ export class DataSourceEditorController implements IController {
})
.then(() => {
this.application.attributes.dataSources = newDataSources;
this.explicitlyEnabled
.filter(k => this.application.getDataSource(k))
.forEach(key => this.application.getDataSource(key).disabled = false);
this.explicitlyDisabled
.filter(k => this.application.getDataSource(k))
.forEach(key => this.application.getDataSource(key).disabled = true);
this.applicationReader.setDisabledDataSources(this.application);
this.application.refresh(true);
this.saving = false;
this.isDirty = false;
Expand All @@ -98,6 +94,7 @@ class ApplicationDataSourceEditorComponent implements ng.IComponentOptions {

export const APPLICATION_DATA_SOURCE_EDITOR = 'spinnaker.core.application.config.applicationDataSourceEditor';
module(APPLICATION_DATA_SOURCE_EDITOR, [
APPLICATION_READ_SERVICE,
APPLICATION_WRITE_SERVICE
])
.component('applicationDataSourceEditor', new ApplicationDataSourceEditorComponent());
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class ApplicationHeader extends React.Component<IApplicationHeaderProps,
}

private getNavigationCategories(dataSources: ApplicationDataSource[], primary: boolean): IDataSourceCategory[] {
const appSources = dataSources.filter(ds => ds.visible !== false && !ds.disabled);
const appSources = dataSources.filter(ds => ds.visible !== false && !ds.disabled && ds.sref);
const categories: IDataSourceCategory[] = [];
const allCategories = navigationCategoryRegistry.getAll();
allCategories.forEach(c => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class ApplicationReader {
this.setDisabledDataSources(application);
}

private setDisabledDataSources(application: Application) {
public setDisabledDataSources(application: Application) {
const allDataSources: ApplicationDataSource[] = application.dataSources,
appDataSources: IApplicationDataSourceAttribute = application.attributes.dataSources;
if (!appDataSources) {
Expand All @@ -113,6 +113,12 @@ export class ApplicationReader {
}
});
}
allDataSources.filter(ds => ds.requiresDataSource).forEach(ds => {
const parent = allDataSources.find(p => p.key === ds.requiresDataSource);
if (parent && parent.disabled) {
this.disableDataSource(ds, application);
}
});
}

private disableDataSource(dataSource: ApplicationDataSource, application: Application) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export interface IDataSourceConfig {
* the link in the application's header.
*
* If the "visible" field is set to false, this value is ignored; if "visible" is true and this field is omitted, the
* tab will use ".insight.{key}" as the sref value in the application header tab
* data source will not generate any navigation elements
*/
sref?: string;

Expand All @@ -159,6 +159,11 @@ export interface IDataSourceConfig {
*/
visible?: boolean;

/**
* (Optional) a data source that will only be available and visible if this data source (by key) is enabled
*/
requiresDataSource?: string;

/**
* (Optional) Determines which second-level navigation menu this data source will belong to
*/
Expand Down Expand Up @@ -192,6 +197,7 @@ export class ApplicationDataSource implements IDataSourceConfig {
public sref: string;
public visible = true;
public hidden = false;
public requiresDataSource: string;

/**
* State flag that indicates whether the data source has been loaded. If the data source does not have a declared
Expand Down Expand Up @@ -277,10 +283,6 @@ export class ApplicationDataSource implements IDataSourceConfig {
$uiRouter: UIRouter) {
Object.assign(this, config);

if (!config.sref && config.visible !== false) {
this.sref = '.insight.' + config.key;
}

if (!config.label && this.$filter) {
this.label = this.$filter('robotToHuman')(config.key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module(LOAD_BALANCER_DATA_SOURCE, [

applicationDataSourceRegistry.registerDataSource({
key: 'loadBalancers',
sref: '.insight.loadBalancers',
category: INFRASTRUCTURE_KEY,
optional: true,
icon: 'fa fa-xs fa-fw icon-sitemap',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module(SECURITY_GROUP_DATA_SOURCE, [
applicationDataSourceRegistry.registerDataSource({
key: 'securityGroups',
category: INFRASTRUCTURE_KEY,
sref: '.insight.securityGroups',
optional: true,
icon: 'fa fa-xs fa-fw fa-lock',
loader: loadSecurityGroups,
Expand Down

0 comments on commit e995e1e

Please sign in to comment.