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