function managementController()

in ui-modules/app-inspector/app/views/main/inspect/management/management.controller.js [47:143]


function managementController($scope, $state, $stateParams, entityApi, catalogApi) {
    $scope.$emit(HIDE_INTERSTITIAL_SPINNER_EVENT);
    $scope.filterResult = []; // array/subset of vm.adjuncts
    $scope.filterValue = $stateParams.search || '';

    const {
        applicationId,
        entityId
    } = $stateParams;

    let vm = this;
    vm.error = {};
    vm.applicationId = applicationId;
    vm.entityId = entityId;
    vm.masterPolicy = {
        type : '',
        show : false,
        configKeys : [
            {
                'key': '',
                'value': ''
            }
        ]
    };
    vm.newPolicy = angular.copy(vm.masterPolicy);

    let observers = [];

    entityApi.entity(applicationId, entityId).then((response)=> {
        vm.entity = response.data;
    });

    entityApi.entityAdjuncts(applicationId, entityId).then((response)=> {
        vm.adjuncts = response.data;
        vm.error.adjuncts = undefined;
        observers.push(response.subscribe((response)=> {
            vm.adjuncts = response.data;
            vm.error.adjuncts = undefined;
        }));
    }).catch((error)=> {
        vm.error.adjuncts = 'Cannot load adjuncts for entity with ID: ' + entityId;
    });

    vm.addConfigKeyRow = function () {
        vm.newPolicy.configKeys.push({
            key: '',
            value: ''
        });
    };
    vm.removeConfigKeyRow = function (index) {
        vm.newPolicy.configKeys.splice(index, 1);
    };
    vm.onPolicySelect = function ($item) {
        vm.newPolicy.configKeyOptions = $item.config;
    };
    vm.onPolicyConfigSelect = function (configKey, $item) {
        configKey.value = $item.defaultValue;
    };
    vm.getPoliciesFromCatalog = function (term) {
        return catalogApi.getTypes({params: {supertype: 'org.apache.brooklyn.api.policy.Policy', fragment: term}}).then(function (response) {
            return response;
        }).catch(function () {
            return [];
        });
    };

    // TODO support new items besides just policies
    vm.addNewPolicy = function () {
        var mappedConfigKeys = {};
        angular.forEach(vm.newPolicy.configKeys, function(configKey) {
            mappedConfigKeys[configKey.key] = configKey.value;
        });

        entityApi.addEntityPolicy(applicationId, entityId, vm.newPolicy.type, mappedConfigKeys).then((response)=> {
            vm.newPolicy = angular.copy(vm.masterPolicy);
            vm.error.add = undefined;
            $state.go($state.current, {}, {reload: true});
        }).catch((error)=> {
            vm.error.add = error.data.message || "Unexpected error";
        });
    };
    vm.cancelNewPolicy = function () {
        vm.newPolicy = angular.copy(vm.masterPolicy);
    };

    $scope.$watchGroup(['filterValue', 'vm.adjuncts'], () => {
        if (vm.adjuncts) {
            $scope.filterResult = vm.adjuncts.filter(filterFor($scope.filterValue));
        }
    });

    $scope.$on('$destroy', ()=> {
        observers.forEach((observer)=> {
            observer.unsubscribe();
        });
    });
}