import type { IComponentOptions, IController, IScope } from 'angular'; import { module } from 'angular'; import type { Application } from '../../application/application.model'; import type { IClusterSummary } from '../../domain/ICluster'; import { ClusterState } from '../../state'; import './onDemandClusterPicker.component.less'; class OnDemandClusterPickerController implements IController { public application: Application; public availableClusters: IClusterSummary[]; public lastSelection: string; public totalClusterCount: number; public optionTemplate: string = require('./onDemandOptionTemplate.html'); public static $inject = ['$scope']; constructor(private $scope: IScope) {} public $onInit(): void { this.setAvailableClusters(); this.application.getDataSource('serverGroups').onRefresh(this.$scope, () => this.setAvailableClusters()); } private setAvailableClusters(): void { this.totalClusterCount = this.application.getDataSource('serverGroups').clusters.length; const selectedClusters: string[] = Object.keys(ClusterState.filterModel.asFilterModel.sortFilter.clusters); this.availableClusters = this.application .getDataSource('serverGroups') .clusters.filter((cluster: IClusterSummary) => !selectedClusters.includes(this.makeKey(cluster))); } public selectCluster(cluster: IClusterSummary): void { this.lastSelection = undefined; ClusterState.filterModel.asFilterModel.sortFilter.clusters[this.makeKey(cluster)] = true; ClusterState.filterModel.asFilterModel.applyParamsToUrl(); this.application.getDataSource('serverGroups').refresh(); } private makeKey(cluster: IClusterSummary): string { return `${cluster.account}:${cluster.name}`; } } const template = `

{{$ctrl.totalClusterCount}} clusters found in this application

Not all clusters are shown. Select or enter a cluster name below to view:

`; const onDemandClusterPickerComponent: IComponentOptions = { template, controller: OnDemandClusterPickerController, bindings: { application: '<', }, }; export const ON_DEMAND_CLUSTER_PICKER_COMPONENT = 'spinnaker.core.cluster.onDemandClusterPicker.component'; module(ON_DEMAND_CLUSTER_PICKER_COMPONENT, []).component('onDemandClusterPicker', onDemandClusterPickerComponent);