2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
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";
28 interface IAttributesViewModelScope extends IWorkspaceViewModelScope {
29 tableHeadersList:Array<any>;
33 addOrUpdateAttribute(attribute?:AttributeModel):void;
34 delete(attribute:AttributeModel):void;
35 sort(sortBy:string):void;
38 export class AttributesViewModel {
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) {
55 this.initComponentAttributes();
58 private initComponentAttributes = () => {
59 if(this.$scope.component.attributes) {
62 this.ComponentServiceNg2.getComponentAttributes(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
63 this.$scope.component.attributes = response.attributes;
70 private initScope = ():void => {
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'}
80 this.$scope.sort = (sortBy:string):void => {
81 this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
82 this.$scope.sortBy = sortBy;
85 this.$scope.addOrUpdateAttribute = (attribute?:AttributeModel):void => {
86 this.ModalsHandler.openEditAttributeModal(attribute ? attribute : new AttributeModel(), this.$scope.component);
89 this.$scope.delete = (attribute:AttributeModel):void => {
91 let onOk = ():void => {
92 this.$scope.component.deleteAttribute(attribute.uniqueId);
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);