Catalog alignment
[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 } 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): 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): 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             }
99         };
100
101         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
102         deferred.resolve(modalInstance.result);
103         return deferred.promise;
104     }
105
106     /**
107      *
108      * This function openes up the edit property modal
109      *
110      * @param property - the property to edit
111      * @param filteredProperties - the filtered properties list to scroll between in the edit modal
112      * @param isPropertyValueOwner - boolean telling if the component is eligible of editing the property
113      * @returns {IPromise<T>} - Promise telling if the modal has opened or not
114      */
115     newOpenEditPropertyModal = (property: PropertyModel, filteredProperties: PropertyModel[], isPropertyValueOwner: boolean, propertyOwnerType: string, propertyOwnerId: string): ng.IPromise<any> => {
116         const deferred = this.$q.defer();
117
118         const modalOptions: ng.ui.bootstrap.IModalSettings = {
119             templateUrl: '../view-models/forms/property-forms/component-property-form/property-form-view.html',
120             controller: 'Sdc.ViewModels.PropertyFormViewModel',
121             size: 'sdc-l',
122             backdrop: 'static',
123             keyboard: false,
124             resolve: {
125                 property: (): PropertyModel => {
126                     return property;
127                 },
128                 filteredProperties: (): PropertyModel[] => {
129                     return filteredProperties;
130                 },
131                 isPropertyValueOwner: (): boolean => {
132                     return isPropertyValueOwner;
133                 },
134                 propertyOwnerType: (): string => {
135                     return propertyOwnerType;
136                 },
137                 propertyOwnerId: (): string => {
138                     return propertyOwnerId;
139                 }
140             }
141         };
142
143         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
144         deferred.resolve(modalInstance.result);
145         return deferred.promise;
146     }
147
148     openEditModulePropertyModal = (property: PropertyModel, component: Component, selectedModule: DisplayModule, filteredProperties: PropertyModel[]): ng.IPromise<any> => {
149         const deferred = this.$q.defer();
150
151         const modalOptions: ng.ui.bootstrap.IModalSettings = {
152             templateUrl: '../view-models/forms/property-forms/base-property-form/property-form-base-view.html',
153             controller: 'Sdc.ViewModels.ModulePropertyView',
154             size: 'sdc-l',
155             backdrop: 'static',
156             keyboard: false,
157             resolve: {
158                 originalProperty: (): PropertyModel => {
159                     return property;
160                 },
161                 component: (): Component => {
162                     return component as Component;
163                 },
164                 selectedModule: (): DisplayModule => {
165                     return selectedModule;
166                 },
167                 filteredProperties: (): PropertyModel[] => {
168                     return filteredProperties;
169                 }
170             }
171         };
172
173         const modalInstance: ng.ui.bootstrap.IModalServiceInstance = this.$uibModal.open(modalOptions);
174         deferred.resolve(modalInstance.result);
175         return deferred.promise;
176     }
177
178 }