CSIT Fix for SDC-2585
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / attributes / attributes-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 {IWorkspaceViewModelScope} from "app/view-models/workspace/workspace-view-model";
23 import {Component, AttributeModel} from "app/models";
24 import {ModalsHandler} from "app/utils";
25 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
26 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
27
28 interface IAttributesViewModelScope extends IWorkspaceViewModelScope {
29     tableHeadersList:Array<any>;
30     reverse:boolean;
31     sortBy:string;
32
33     addOrUpdateAttribute(attribute?:AttributeModel):void;
34     delete(attribute:AttributeModel):void;
35     sort(sortBy:string):void;
36 }
37
38 export class AttributesViewModel {
39
40     static '$inject' = [
41         '$scope',
42         '$filter',
43         '$uibModal',
44         'ModalsHandler',
45         'ComponentServiceNg2'
46     ];
47
48
49     constructor(private $scope:IAttributesViewModelScope,
50                 private $filter:ng.IFilterService,
51                 private $uibModal:ng.ui.bootstrap.IModalService,
52                 private ModalsHandler:ModalsHandler,
53                 private ComponentServiceNg2: ComponentServiceNg2) {
54
55         this.initComponentAttributes();
56     }
57
58     private initComponentAttributes = () => {
59         if(this.$scope.component.attributes) {
60             this.initScope();
61         } else {
62             this.ComponentServiceNg2.getComponentAttributes(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
63                 this.$scope.component.attributes = response.attributes;
64                 this.initScope();
65             });
66         }
67     }
68
69
70     private initScope = ():void => {
71
72         this.$scope.sortBy = 'name';
73         this.$scope.reverse = false;
74         this.$scope.setValidState(true);
75         this.$scope.tableHeadersList = [
76             {title: 'Name', property: 'name'},
77             {title: 'Type', property: 'type'},
78             {title: 'Default Value', property: 'defaultValue'}
79         ];
80         this.$scope.sort = (sortBy:string):void => {
81             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
82             this.$scope.sortBy = sortBy;
83         };
84
85         this.$scope.addOrUpdateAttribute = (attribute?:AttributeModel):void => {
86             this.ModalsHandler.openEditAttributeModal(attribute ? attribute : new AttributeModel(), this.$scope.component);
87         };
88
89         this.$scope.delete = (attribute:AttributeModel):void => {
90
91             let onOk = ():void => {
92                 this.$scope.component.deleteAttribute(attribute.uniqueId);
93             };
94             let title:string = this.$filter('translate')("ATTRIBUTE_VIEW_DELETE_MODAL_TITLE");
95             let message:string = this.$filter('translate')("ATTRIBUTE_VIEW_DELETE_MODAL_TEXT", "{'name': '" + attribute.name + "'}");
96             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
97         };
98     }
99 }