CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / modals / email-modal / email-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 {IAppConfigurtaion, Component, AsdcComment} from "app/models";
23 import {ValidationUtils} from "app/utils";
24
25 export interface IEmailModalModel_Email {
26     to:string;
27     subject:string;
28     message:string;
29 }
30
31 export interface IEmailModalModel_Data {
32     component:Component;
33     stateUrl:string;
34 }
35
36 export interface IEmailModalModel {
37     title:string;
38     email:IEmailModalModel_Email;
39     data:IEmailModalModel_Data;
40 }
41
42 interface IEmailModalViewModelScope {
43     modalInstanceEmail:ng.ui.bootstrap.IModalServiceInstance;
44     emailModalModel:IEmailModalModel;
45     submitInProgress:boolean;
46     commentValidationPattern:RegExp;
47     isLoading:boolean;
48     submit():any;
49     cancel():void;
50     validateField(field:any):boolean;
51 }
52
53 export class EmailModalViewModel {
54
55     static '$inject' = ['$scope', '$filter', 'sdcConfig', '$uibModalInstance', 'emailModalModel', 'ValidationUtils', 'CommentValidationPattern'];
56
57     constructor(private $scope:IEmailModalViewModelScope,
58                 private $filter:ng.IFilterService,
59                 private sdcConfig:IAppConfigurtaion,
60                 private $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
61                 private emailModalModel:IEmailModalModel,
62                 private ValidationUtils:ValidationUtils,
63                 private CommentValidationPattern:RegExp) {
64
65         this.initScope(emailModalModel);
66     }
67
68     private initScope = (emailModalModel:IEmailModalModel):void => {
69         this.$scope.emailModalModel = emailModalModel;
70         this.$scope.submitInProgress = false;
71         this.$scope.commentValidationPattern = this.CommentValidationPattern;
72         this.$scope.modalInstanceEmail = this.$uibModalInstance;
73
74         this.$scope.submit = ():any => {
75
76             let onSuccess = (component:Component) => {
77                 this.$scope.isLoading = false;
78                 this.$scope.submitInProgress = false;
79                 // showing the outlook modal according to the config json
80                 if (this.sdcConfig.showOutlook) {
81                     let link:string = encodeURI(this.sdcConfig.api.baseUrl + "?folder=Ready_For_Testing");
82                     let outlook:string = this.$filter('translate')("EMAIL_OUTLOOK_MESSAGE", "{'to': '" + emailModalModel.email.to + "','subject': '" + emailModalModel.email.subject + "','message': '" + emailModalModel.email.message + "', 'entityNameAndVersion': '" + emailModalModel.email.subject + "','link': '" + link + "'}");
83                     window.location.href = outlook; // Open outlook with the email to send
84                 }
85                 this.$uibModalInstance.close(component); // Close the dialog
86             };
87
88             let onError = () => {
89                 this.$scope.isLoading = false;
90                 this.$scope.submitInProgress = false;
91                 this.$uibModalInstance.close(); // Close the dialog
92             };
93
94             // Submit to server
95             // Prevent from user pressing multiple times on submit.
96             if (this.$scope.submitInProgress === false) {
97                 this.$scope.isLoading = true;
98                 this.$scope.submitInProgress = true;
99                 let comment:AsdcComment = new AsdcComment();
100                 comment.userRemarks = emailModalModel.email.message;
101                 emailModalModel.data.component.changeLifecycleState(emailModalModel.data.stateUrl, comment).then(onSuccess, onError);
102             }
103         };
104
105         this.$scope.cancel = ():void => {
106             this.$uibModalInstance.dismiss();
107         };
108
109         this.$scope.validateField = (field:any):boolean => {
110             if (field && field.$dirty && field.$invalid) {
111                 return true;
112             }
113             return false;
114         };
115     }
116 }