Provide add/edit constraints capability to inputs in properties page
[sdc.git] / catalog-ui / src / app / utils / modals-handler.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import {  Component, DisplayModule , PropertyModel, InputFEModel } from '../models';
22 import { ComponentMetadata } from '../models/component-metadata';
23
24 export interface IModalsHandler {
25
26     openEditPropertyModal(property: PropertyModel, component: Component, filteredProperties: PropertyModel[], isPropertyOwnValue: boolean,
27                           propertyOwnerType: string, propertyOwnerId: string, isViewOnly?: boolean): ng.IPromise<any>;
28 }
29
30 export class ModalsHandler implements IModalsHandler {
31
32     static '$inject' = [
33         '$uibModal',
34         '$q'
35     ];
36
37     constructor(private $uibModal: ng.ui.bootstrap.IModalService,
38                 private $q: ng.IQService) {
39     }
40
41     openUpdateIconModal = (component: Component): ng.IPromise<any> => {
42         const deferred = this.$q.defer();
43         const modalOptions: ng.ui.bootstrap.IModalSettings = {
44             templateUrl: '../view-models/modals/icons-modal/icons-modal-view.html',
45             controller: 'Sdc.ViewModels.IconsModalViewModel',
46             size: 'sdc-auto',
47             backdrop: 'static',
48             resolve: {
49                 component: (): Component => {
50                     return component;
51                 }
52             }
53         };
54         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
55         deferred.resolve(modalInstance.result);
56         return deferred.promise;
57     }
58
59     /**
60      *
61      * This function openes up the edit property modal
62      *
63      * @param property - the property to edit
64      * @param component - the component who is the owner of the property
65      * @param filteredProperties - the filtered properties list to scroll between in the edit modal
66      * @param isPropertyValueOwner - boolean telling if the component is eligible of editing the property
67      * @returns {IPromise<T>} - Promise telling if the modal has opened or not
68      */
69     openEditPropertyModal = (property: PropertyModel, component: Component | ComponentMetadata, filteredProperties: PropertyModel[],
70                              isPropertyValueOwner: boolean, propertyOwnerType: string, propertyOwnerId: string, isViewOnly: boolean = false): ng.IPromise<any> => {
71         const deferred = this.$q.defer();
72
73         const modalOptions: ng.ui.bootstrap.IModalSettings = {
74             templateUrl: '../view-models/forms/property-forms/component-property-form/property-form-view.html',
75             controller: 'Sdc.ViewModels.PropertyFormViewModel',
76             size: 'sdc-l',
77             backdrop: 'static',
78             keyboard: false,
79             resolve: {
80                 property: (): PropertyModel => {
81                     return property;
82                 },
83                 component: (): Component => {
84                     return component as Component;
85                 },
86                 filteredProperties: (): PropertyModel[] => {
87                     return filteredProperties;
88                 },
89                 isPropertyValueOwner: (): boolean => {
90                     return isPropertyValueOwner;
91                 },
92                 propertyOwnerType: (): string => {
93                     return propertyOwnerType;
94                 },
95                 propertyOwnerId: (): string => {
96                     return propertyOwnerId;
97                 },
98                 isViewOnly: (): boolean => {
99                     return isViewOnly;
100                 },
101                 inputProperty: (): InputFEModel => {
102                     return null;
103                 }
104             }
105         };
106
107         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
108         deferred.resolve(modalInstance.result);
109         return deferred.promise;
110     }
111
112     /**
113      *
114      * This function openes up the edit property modal
115      *
116      * @param property - the property to edit
117      * @param filteredProperties - the filtered properties list to scroll between in the edit modal
118      * @param isPropertyValueOwner - boolean telling if the component is eligible of editing the property
119      * @returns {IPromise<T>} - Promise telling if the modal has opened or not
120      */
121     newOpenEditPropertyModal = (property: PropertyModel, filteredProperties: PropertyModel[], isPropertyValueOwner: boolean, propertyOwnerType: string, propertyOwnerId: string, component: Component, inputProperty: InputFEModel): ng.IPromise<any> => {
122         const deferred = this.$q.defer();
123
124         const modalOptions: ng.ui.bootstrap.IModalSettings = {
125             templateUrl: '../view-models/forms/property-forms/component-property-form/property-form-view.html',
126             controller: 'Sdc.ViewModels.PropertyFormViewModel',
127             size: 'sdc-l',
128             backdrop: 'static',
129             keyboard: false,
130             resolve: {
131                 property: (): PropertyModel => {
132                     return property;
133                 },
134                 filteredProperties: (): PropertyModel[] => {
135                     return filteredProperties;
136                 },
137                 isPropertyValueOwner: (): boolean => {
138                     return isPropertyValueOwner;
139                 },
140                 propertyOwnerType: (): string => {
141                     return propertyOwnerType;
142                 },
143                 propertyOwnerId: (): string => {
144                     return propertyOwnerId;
145                 },
146                 isViewOnly: (): boolean => {
147                     return false;
148                 },
149                 component: (): Component => {
150                     return component as Component;
151                 },
152                 inputProperty: (): InputFEModel => {
153                     return inputProperty;
154                 }
155             }
156         };
157
158         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
159         deferred.resolve(modalInstance.result);
160         return deferred.promise;
161     }
162
163     openEditModulePropertyModal = (property: PropertyModel, component: Component, selectedModule: DisplayModule, filteredProperties: PropertyModel[]): ng.IPromise<any> => {
164         const deferred = this.$q.defer();
165
166         const modalOptions: ng.ui.bootstrap.IModalSettings = {
167             templateUrl: '../view-models/forms/property-forms/base-property-form/property-form-base-view.html',
168             controller: 'Sdc.ViewModels.ModulePropertyView',
169             size: 'sdc-l',
170             backdrop: 'static',
171             keyboard: false,
172             resolve: {
173                 originalProperty: (): PropertyModel => {
174                     return property;
175                 },
176                 component: (): Component => {
177                     return component as Component;
178                 },
179                 selectedModule: (): DisplayModule => {
180                     return selectedModule;
181                 },
182                 filteredProperties: (): PropertyModel[] => {
183                     return filteredProperties;
184                 }
185             }
186         };
187
188         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
189         deferred.resolve(modalInstance.result);
190         return deferred.promise;
191     }
192
193 }