2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
18 import {ICompositionViewModelScope} from "../../composition-view-model";
19 import {Service, ComponentInstance, PropertiesGroup, ServiceInstanceObject, PropertyBEModel} from 'app/models';
20 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
21 import {ConstraintObject} from "app/ng2/components/logic/service-dependencies/service-dependencies.component";
22 import {ComponentGenericResponse} from 'app/ng2/services/responses/component-generic-response';
23 import {DEPENDENCY_EVENTS} from "app/utils/constants";
24 import {EventListenerService} from 'app/services';
26 interface IServiceDependenciesViewModelScope extends ICompositionViewModelScope {
28 selectedInstanceSiblings: Array<ServiceInstanceObject>;
29 componentInstancesConstraints: Array<any>;
30 selectedInstanceConstraints: Array<ConstraintObject>;
31 selectedInstanceProperties: Array<PropertyBEModel>;
32 updateSelectedInstanceConstraints(constraintsList:Array<ConstraintObject>): void;
33 loadConstraints(): void;
34 componentInstanceProperties: PropertiesGroup;
35 notifyDependencyEventsObserver: Function;
40 export class ServiceDependenciesViewModel {
44 'ComponentServiceNg2',
45 'EventListenerService'
48 constructor(private $scope:IServiceDependenciesViewModelScope, private ComponentServiceNg2:ComponentServiceNg2, private eventListenerService: EventListenerService) {
49 this.$scope.service = <Service>this.$scope.currentComponent;
50 this.$scope.notifyDependencyEventsObserver = this.notifyDependencyEventsObserver;
51 this.initInstancesWithProperties();
52 this.loadConstraints();
57 private initInstancesWithProperties = ():void => {
58 this.ComponentServiceNg2.getComponentInstanceProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
59 this.$scope.componentInstanceProperties = genericResponse.componentInstancesProperties;
60 this.updateInstanceAttributes();
64 private updateInstanceAttributes = ():void => {
65 if (this.$scope.isComponentInstanceSelected() && this.$scope.componentInstanceProperties) {
66 let instancesMappedList = this.$scope.service.componentInstances.map(coInstance => new ServiceInstanceObject({
67 id: coInstance.uniqueId,
68 name: coInstance.name,
69 properties: this.$scope.componentInstanceProperties[coInstance.uniqueId] || []
71 this.$scope.selectedInstanceProperties = this.$scope.componentInstanceProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
72 this.$scope.selectedInstanceSiblings = instancesMappedList.filter(coInstance => coInstance.id !== this.$scope.currentComponent.selectedInstance.uniqueId);
76 private initScope = ():void => {
77 this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
78 if (angular.isDefined(newInstance) && this.$scope.componentInstancesConstraints) {
79 this.updateInstanceAttributes();
80 this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId] ?
81 this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties :
85 this.$scope.$watch('componentInstancesConstraints', (constraints: Array<any>):void => {
86 if (angular.isDefined(constraints)) {
87 if(this.$scope.isComponentInstanceSelected()) {
88 this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId] ?
89 this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties || [] :
95 this.$scope.updateSelectedInstanceConstraints = (constraintsList:Array<ConstraintObject>):void => {
96 this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties = constraintsList;
97 this.$scope.selectedInstanceConstraints = this.$scope.componentInstancesConstraints[this.$scope.currentComponent.selectedInstance.uniqueId].properties;
100 this.$scope.loadConstraints = ():void => {
101 this.loadConstraints();
104 this.$scope.registerCreateInstanceEvent(() => {
105 this.initInstancesWithProperties();
108 this.$scope.registerChangeComponentInstanceNameEvent((updatedComponentInstance) => {
109 this.$scope.currentComponent.selectedInstance = updatedComponentInstance;
112 this.$scope.$on('$destroy', this.$scope.unregisterCreateInstanceEvent);
113 this.$scope.$on('$destroy', this.$scope.unregisterChangeComponentInstanceNameEvent);
116 private loadConstraints = ():void => {
117 this.ComponentServiceNg2.getServiceFilterConstraints(this.$scope.service).subscribe((response) => {
118 this.$scope.componentInstancesConstraints = response.nodeFilterData;
122 public notifyDependencyEventsObserver = (isChecked: boolean):void => {
123 this.eventListenerService.notifyObservers(DEPENDENCY_EVENTS.ON_DEPENDENCY_CHANGE, isChecked);