CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / modals / confirmation-modal / confirmation-modal-view-model.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 'use strict';
22 import {ValidationUtils, ModalType} from "app/utils";
23
24 export interface IConfirmationModalModel {
25     title:string;
26     message:string;
27     showComment:boolean;
28     type:ModalType;
29 }
30
31 interface IConfirmationModalViewModelScope {
32     modalInstanceConfirmation:ng.ui.bootstrap.IModalServiceInstance;
33     confirmationModalModel:IConfirmationModalModel;
34     comment:any;
35     commentValidationPattern:RegExp;
36     editForm:ng.IFormController;
37     okButtonColor:string;
38     hideCancelButton:boolean;
39     ok():any;
40     cancel():void;
41 }
42
43 export class ConfirmationModalViewModel {
44
45     static '$inject' = ['$scope', '$uibModalInstance', 'confirmationModalModel', 'CommentValidationPattern', 'ValidationUtils'];
46
47     constructor(private $scope:IConfirmationModalViewModelScope,
48                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
49                 confirmationModalModel:IConfirmationModalModel,
50                 private CommentValidationPattern:RegExp,
51                 private ValidationUtils:ValidationUtils) {
52
53         this.initScope(confirmationModalModel);
54     }
55
56     private initScope = (confirmationModalModel:IConfirmationModalModel):void => {
57         let self = this;
58         this.$scope.hideCancelButton = false;
59         this.$scope.modalInstanceConfirmation = this.$uibModalInstance;
60         this.$scope.confirmationModalModel = confirmationModalModel;
61         this.$scope.comment = {"text": ''};
62         this.$scope.commentValidationPattern = this.CommentValidationPattern;
63
64         this.$scope.ok = ():any => {
65             self.$uibModalInstance.close(this.ValidationUtils.stripAndSanitize(self.$scope.comment.text));
66         };
67
68         this.$scope.cancel = ():void => {
69             console.info('Cancel pressed on: ' + this.$scope.confirmationModalModel.title);
70             self.$uibModalInstance.dismiss();
71         };
72
73         // Set the OK button color according to modal type (standard, error, alert)
74         let _okButtonColor = 'blue'; // Default
75         switch (confirmationModalModel.type) {
76             case ModalType.STANDARD:
77                 _okButtonColor = 'blue';
78                 break;
79             case ModalType.ERROR:
80                 _okButtonColor = 'red';
81                 break;
82             case ModalType.ALERT:
83                 this.$scope.hideCancelButton = true;
84                 _okButtonColor = 'grey';
85                 break;
86             default:
87                 _okButtonColor = 'blue';
88                 break;
89         }
90         this.$scope.okButtonColor = _okButtonColor;
91
92     }
93 }