10 import {ICompositionViewModelScope} from "../../composition-view-model";
11 import {ModalsHandler} from "app/utils";
12 import {ComponentServiceNg2} from "app/ng2/services/component-services/component.service";
13 import {ComponentGenericResponse} from "app/ng2/services/responses/component-generic-response";
15 interface IResourcePropertiesAndAttributesViewModelScope extends ICompositionViewModelScope {
16 properties:PropertiesGroup;
17 attributes:AttributesGroup;
18 propertiesMessage:string;
19 groupPropertiesByInstance:boolean;
20 showGroupsOfInstanceProperties:Array<boolean>;
22 updateProperty(property:PropertyModel):void;
23 deleteProperty(property:PropertyModel):void;
24 viewAttribute(attribute:AttributeModel):void;
25 groupNameByKey(key:string):string;
26 isPropertyOwner():boolean;
27 getComponentInstanceNameFromInstanceByKey(key:string):string;
30 export class ResourcePropertiesViewModel {
42 constructor(private $scope:IResourcePropertiesAndAttributesViewModelScope,
43 private $filter:ng.IFilterService,
44 private $uibModal:ng.ui.bootstrap.IModalService,
45 private ModalsHandler:ModalsHandler,
46 private ComponentServiceNg2:ComponentServiceNg2) {
48 this.getComponentInstancesPropertiesAndAttributes();
51 private initComponentProperties = ():void => {
52 let result:PropertiesGroup = {};
54 if (this.$scope.selectedComponent) {
55 this.$scope.propertiesMessage = undefined;
56 this.$scope.groupPropertiesByInstance = false;
57 if (this.$scope.isComponentInstanceSelected()) {
58 if (this.$scope.currentComponent.selectedInstance.originType === 'VF') {
59 this.$scope.groupPropertiesByInstance = true;
61 result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesProperties[this.$scope.currentComponent.selectedInstance.uniqueId];
62 } else if (this.$scope.currentComponent.isService()) {
63 // Temporally fix to hide properties for service (UI stack when there are many properties)
64 result = this.$scope.currentComponent.componentInstancesProperties;
65 this.$scope.propertiesMessage = "Note: properties for service are disabled";
67 let key = this.$scope.selectedComponent.uniqueId;
68 result[key] = Array<PropertyModel>();
69 let derived = Array<PropertyModel>();
70 _.forEach(this.$scope.selectedComponent.properties, (property:PropertyModel) => {
71 if (key == property.parentUniqueId) {
72 result[key].push(property);
74 property.readonly = true;
75 derived.push(property);
79 result['derived'] = derived;
82 this.$scope.properties = result;
87 private initComponentAttributes = ():void => {
88 let result:AttributesGroup = {};
90 if (this.$scope.selectedComponent) {
91 if (this.$scope.isComponentInstanceSelected()) {
92 result[this.$scope.currentComponent.selectedInstance.uniqueId] = this.$scope.currentComponent.componentInstancesAttributes[this.$scope.currentComponent.selectedInstance.uniqueId];
93 } else if (this.$scope.currentComponent.isService()) {
94 result = this.$scope.currentComponent.componentInstancesAttributes;
96 this.$scope.attributes = result;
101 * This function is checking if the component is the value owner of the current property
102 * in order to notify the edit property modal which fields to disable
104 private isPropertyValueOwner = ():boolean => {
105 return this.$scope.currentComponent.isService() || !!this.$scope.currentComponent.selectedInstance;
109 * The function opens the edit property modal.
110 * It checks if the property is from the VF or from one of it's resource instances and sends the needed property list.
111 * For create property reasons an empty array is transferd
113 * @param property the wanted property to edit/create
115 private openEditPropertyModal = (property:PropertyModel):void => {
116 this.ModalsHandler.openEditPropertyModal(property,
117 this.$scope.component,
118 (this.$scope.isPropertyOwner() ?
119 this.$scope.properties[property.parentUniqueId] :
120 this.$scope.properties[property.resourceInstanceUniqueId]) || [],
121 this.isPropertyValueOwner()).then((updatedProperty:PropertyModel) => {
122 let oldProp = _.find(this.$scope.properties[updatedProperty.resourceInstanceUniqueId], (prop:PropertyModel) => {return prop.uniqueId == updatedProperty.uniqueId;});
123 oldProp.value = updatedProperty.value;
127 private openAttributeModal = (atrribute:AttributeModel):void => {
129 let modalOptions:ng.ui.bootstrap.IModalSettings = {
130 template: 'app/view-models/forms/attribute-form/attribute-form-view.html',
131 controller: 'Sdc.ViewModels.AttributeFormViewModel',
136 attribute: ():AttributeModel => {
139 component: ():Component => {
140 return this.$scope.currentComponent;
144 this.$uibModal.open(modalOptions);
147 private getComponentInstancesPropertiesAndAttributes = () => {
149 this.ComponentServiceNg2.getComponentInstanceAttributesAndProperties(this.$scope.currentComponent).subscribe((genericResponse:ComponentGenericResponse) => {
150 this.$scope.currentComponent.componentInstancesAttributes = genericResponse.componentInstancesAttributes;
151 this.$scope.currentComponent.componentInstancesProperties = genericResponse.componentInstancesProperties;
156 private initScope = ():void => {
159 this.initComponentProperties();
160 this.initComponentAttributes();
162 this.$scope.$watchCollection('currentComponent.properties', (newData:any):void => {
163 this.initComponentProperties();
166 this.$scope.$watch('currentComponent.selectedInstance', (newInstance:ComponentInstance):void => {
167 if (angular.isDefined(newInstance)) {
168 this.initComponentProperties();
169 this.initComponentAttributes();
174 this.$scope.isPropertyOwner = ():boolean => {
175 return this.$scope.currentComponent && this.$scope.currentComponent.isResource() && !this.$scope.isComponentInstanceSelected();
178 this.$scope.updateProperty = (property:PropertyModel):void => {
179 this.openEditPropertyModal(property);
182 this.$scope.deleteProperty = (property:PropertyModel):void => {
184 let onOk = ():void => {
185 this.$scope.currentComponent.deleteProperty(property.uniqueId);
188 let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
189 let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
190 this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
193 this.$scope.viewAttribute = (attribute:AttributeModel):void => {
194 this.openAttributeModal(attribute);
197 this.$scope.groupNameByKey = (key:string):string => {
202 case this.$scope.currentComponent.uniqueId:
203 return this.$filter("resourceName")(this.$scope.currentComponent.name);
206 return this.$filter("resourceName")((_.find(this.$scope.currentComponent.componentInstances, {uniqueId: key})).name);
210 this.$scope.getComponentInstanceNameFromInstanceByKey = (key:string):string => {
211 let instanceName:string = "";
212 if (key !== undefined && this.$scope.selectedComponent.uniqueId == this.$scope.currentComponent.selectedInstance.componentUid) {
213 instanceName = this.$filter("resourceName")((_.find(this.$scope.selectedComponent.componentInstances, {uniqueId: key})).name);