re base code
[sdc.git] / catalog-ui / src / app / view-models / forms / property-forms / select-datatype-modal / select-datatype-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 {DataTypesService} from "app/services/data-types-service";
23 import {PropertyModel, InputPropertyBase, Component} from "app/models";
24 import {IPropertyFormBaseViewScope, PropertyFormBaseView} from "../base-property-form/property-form-base-model";
25 import {PROPERTY_TYPES} from "app/utils/constants";
26
27 interface ISelectDataTypeViewModelScope extends IPropertyFormBaseViewScope {
28     selectedPropertiesName:string;
29     dataTypesService:DataTypesService;
30     path:string;
31     isTypeDataType:boolean;
32     myValue:any;
33     isReadOnly:boolean;
34 }
35
36 export class SelectDataTypeViewModel extends PropertyFormBaseView {
37
38     static '$inject' = [
39         '$scope',
40         '$templateCache',
41         '$uibModalInstance',
42         '$injector',
43         'originalProperty',
44         'component',
45         'filteredProperties',
46         'Sdc.Services.DataTypesService',
47         'propertiesMap',
48         '$q'
49     ];
50
51     constructor(protected $scope:ISelectDataTypeViewModelScope,
52                 protected $templateCache:ng.ITemplateCacheService,
53                 protected $uibModalInstance:ng.ui.bootstrap.IModalServiceInstance,
54                 protected $injector:ng.auto.IInjectorService,
55                 protected originalProperty:PropertyModel,
56                 protected component:Component,
57                 protected filteredProperties:Array<PropertyModel>,
58                 protected DataTypesService:DataTypesService,
59                 private propertiesMap:Array<InputPropertyBase>,
60                 private $q:ng.IQService) {
61         super($scope, $uibModalInstance, $injector, originalProperty, component, filteredProperties, DataTypesService);
62
63         this.$templateCache.put("select-datatype-modal-view.html", require('app/view-models/forms/property-forms/select-datatype-modal/select-datatype-modal-view.html'));
64         this.$scope.innerViewSrcUrl = "select-datatype-modal-view.html";
65         this.initChildScope();
66     }
67
68     //scope methods
69     save(isNeedToCloseModal):ng.IPromise<boolean> {
70         let deferred = this.$q.defer<boolean>();
71         this.$scope.property.propertiesName = this.DataTypesService.selectedPropertiesName;
72         this.$scope.property.input = this.DataTypesService.selectedInput;
73         this.$scope.property.isAlreadySelected = true;
74         this.$uibModalInstance.close(this.$scope.property);
75         deferred.resolve(true);
76         return deferred.promise;
77     };
78
79     private initForNotSimpleType = ():void => {
80         let property = this.$scope.property;
81         this.$scope.isTypeDataType = this.DataTypesService.isDataTypeForPropertyType(this.$scope.property);
82         if (property.type && this.$scope.simpleTypes.indexOf(property.type) == -1) {
83             if (!(property.value || property.defaultValue)) {
84                 switch (property.type) {
85                     case PROPERTY_TYPES.MAP:
86                         this.$scope.myValue = {'': null};
87                         break;
88                     case PROPERTY_TYPES.LIST:
89                         this.$scope.myValue = [];
90                         break;
91                     default:
92                         this.$scope.myValue = {};
93                 }
94             } else {
95                 this.$scope.myValue = JSON.parse(property.value || property.defaultValue);
96             }
97         }
98     };
99
100     //remove selection property on the modal
101     private removeSelected = ():void => {
102         this.DataTypesService.selectedPropertiesName = null;
103         this.DataTypesService.selectedInput = null;
104     };
105
106     private initChildScope = ():void => {
107         //scope properties
108         this.$scope.forms = {};
109         this.$scope.path = this.$scope.property.name;
110         this.$scope.isArrowsDisabled = true;
111         this.DataTypesService.alreadySelectedProperties = this.propertiesMap;
112         this.$scope.dataTypesService = this.DataTypesService;
113         this.$scope.isReadOnly = true;
114         this.initForNotSimpleType();
115         this.removeSelected();
116     }
117 }