aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/view-models/forms/property-forms/base-property-form/property-form-base-model.ts
blob: 1f922cdaf9d739abcee2d74c1c49b1452e58aed5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

/**
 * Created by obarda on 1/19/2017.
 */
'use strict';
import * as _ from "lodash";
import {DataTypesService} from "app/services/data-types-service";
import {PropertyModel, DataTypesMap, Component} from "app/models";
import {ValidationUtils, PROPERTY_DATA} from "app/utils";

export interface IPropertyFormBaseViewScope extends ng.IScope {

    forms:any;
    editForm:ng.IFormController;

    property:PropertyModel;
    types:Array<string>;
    nonPrimitiveTypes:Array<string>;
    simpleTypes:Array<string>;

    footerButtons:Array<any>;
    modalPropertyFormBase:ng.ui.bootstrap.IModalServiceInstance;
    currentPropertyIndex:number;
    isLastProperty:boolean;
    innerViewSrcUrl:string;

    //Disabling filed - each child controller can change this when needed
    isNew:boolean;
    isTypeSelectorDisable:boolean;
    isDeleteDisable:boolean;
    isNameDisable:boolean;
    isDescriptionDisable:boolean;
    isPropertyValueDisable:boolean;
    isArrowsDisabled:boolean;

    //Validation pattern
    validationPattern:RegExp;
    propertyNameValidationPattern:RegExp;
    commentValidationPattern:RegExp;
    numberValidationPattern:RegExp;

    dataTypes:DataTypesMap;

    isLoading:boolean;

    save():void;
    close():void;
    getNext():void;
    getPrev():void;
    getValidationPattern(type:string):RegExp;
}

export abstract class PropertyFormBaseView {


    constructor(protected $scope:IPropertyFormBaseViewScope,
                protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
                protected $injector:ng.auto.IInjectorService,
                protected originalProperty:PropertyModel,
                protected component:Component,
                protected filteredProperties:Array<PropertyModel>,
                protected DataTypesService:DataTypesService) {

        this.initScope();

    }

    protected validationUtils:ValidationUtils;

    protected isPropertyValueOwner = ():boolean => {
        return this.component.isService() || !!this.component.selectedInstance;
    };

    private isDisable = ():boolean => {
        return this.isPropertyValueOwner() || this.$scope.property.readonly;
    };


    //This is the difault state, Childs screens can change if needed
    protected initButtonsState = ():void => {
        let isDisable = this.isDisable();

        this.$scope.isArrowsDisabled = false;
        this.$scope.isDeleteDisable = isDisable;
        this.$scope.isDescriptionDisable = isDisable;
        this.$scope.isNameDisable = isDisable;
        this.$scope.isTypeSelectorDisable = isDisable;
        this.$scope.isPropertyValueDisable = this.$scope.property.readonly && !this.isPropertyValueOwner();
    };

    protected initValidations = ():void => {

        this.$scope.validationPattern = this.$injector.get('ValidationPattern');
        this.$scope.propertyNameValidationPattern = this.$injector.get('PropertyNameValidationPattern');
        this.$scope.commentValidationPattern = this.$injector.get('CommentValidationPattern');
        this.$scope.numberValidationPattern = this.$injector.get('NumberValidationPattern');
        this.validationUtils = this.$injector.get('ValidationUtils');
    };

    //Functions implemented on child's scope if needed
    abstract save(doNotCloseModal?:boolean):ng.IPromise<boolean>;

    protected onPropertyChange():void {
    };

    private updatePropertyByIndex = (index:number):void => {
        this.$scope.property = new PropertyModel(this.filteredProperties[index]);
        this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);
        this.onPropertyChange();
    };

    private initScope = ():void => {

        this.$scope.forms = {};
        this.$scope.isLoading = false;
        this.$scope.property = new PropertyModel(this.originalProperty); //we create a new Object so if user press cance we won't update the property
        this.$scope.types = PROPERTY_DATA.TYPES; //All types - simple type + map + list
        this.$scope.simpleTypes = PROPERTY_DATA.SIMPLE_TYPES; //All simple types
        this.$scope.dataTypes = this.DataTypesService.getAllDataTypes(); //Get all data types in service
        this.$scope.modalPropertyFormBase = this.$uibModalInstance;
        this.$scope.isNew = !angular.isDefined(this.$scope.property.name);

        this.initValidations();
        this.initButtonsState();
        this.filteredProperties = _.sortBy(this.filteredProperties, 'name');
        this.$scope.currentPropertyIndex = _.findIndex(this.filteredProperties, propety => propety.uniqueId == this.$scope.property.uniqueId);
        this.$scope.isLastProperty = this.$scope.currentPropertyIndex == (this.filteredProperties.length - 1);

        this.$scope.nonPrimitiveTypes = _.filter(Object.keys(this.$scope.dataTypes), (type:string)=> {
            return this.$scope.types.indexOf(type) == -1;
        });

        this.$scope.close = ():void => {
            this.$uibModalInstance.close();
        };

        this.$scope.save = ():void => {

            let onSuccess = ():void => {
                this.$scope.isLoading = false;
            };
            let onFailed = ():void => {
                this.$scope.isLoading = false;
            };

            this.$scope.isLoading = true;
            this.save(true).then(onSuccess, onFailed); // Child controller implement save logic
        };

        // Add the done button at the footer.
        this.$scope.footerButtons = [
            {'name': 'Save', 'css': 'blue', 'callback': this.$scope.save},
            {'name': 'Cancel', 'css': 'grey', 'callback': this.$scope.close}
        ];


        this.$scope.getPrev = ():void=> {

            let onSuccess = ():void => {
                this.$scope.isLoading = false;
                this.updatePropertyByIndex(--this.$scope.currentPropertyIndex);
            };
            let onFailed = ():void => {
                this.$scope.isLoading = false;
            };

            if (!this.$scope.property.readonly) {
                this.$scope.isLoading = true;
                this.save(false).then(onSuccess, onFailed);

            } else {
                this.updatePropertyByIndex(--this.$scope.currentPropertyIndex);
            }

        };

        this.$scope.getNext = ():void=> {

            let onSuccess = ():void => {
                this.$scope.isLoading = false;
                this.updatePropertyByIndex(++this.$scope.currentPropertyIndex);
            };
            let onFailed = ():void => {
                this.$scope.isLoading = false;
            };

            if (!this.$scope.property.readonly) {
                this.$scope.isLoading = true;
                this.save(false).then(onSuccess, onFailed);
            } else {
                this.updatePropertyByIndex(++this.$scope.currentPropertyIndex);
            }

        };


        this.$scope.$watch("forms.editForm.$invalid", (newVal, oldVal) => {
            this.$scope.footerButtons[0].disabled = this.$scope.forms.editForm.$invalid;
        });


        this.$scope.getValidationPattern = (type:string):RegExp => {
            return this.validationUtils.getValidationPattern(type);
        };
    }
}