5a9bfbcecba994f640edcada8218aa7d871d6ab0
[sdc.git] /
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 /**
22  * Created by rc2122 on 5/16/2017.
23  */
24 import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
25 import {ButtonModel, ButtonsModelMap, FilterPropertiesAssignmentData} from "app/models";
26 import * as sdcConfig from "../../../../../../configurations/dev"
27 import {PopoverComponent} from "../../ui/popover/popover.component";
28
29 @Component({
30     selector: 'filter-properties-assignment',
31     templateUrl: './filter-properties-assignment.component.html',
32     styleUrls: ['./filter-properties-assignment.component.less']
33 })
34
35 export class FilterPropertiesAssignmentComponent {
36     @Input() componentType: string;
37     @Output() searchProperties: EventEmitter<FilterPropertiesAssignmentData> = new EventEmitter<FilterPropertiesAssignmentData>();
38     footerButtons:ButtonsModelMap = {};
39     typesOptions:Array<string>;//All optional types
40     selectedTypes:Object = {};
41     allSelected:boolean = false;//if all option selected
42     filterData:FilterPropertiesAssignmentData = new FilterPropertiesAssignmentData();
43     @ViewChild('filterPopover') filterPopover: PopoverComponent;
44
45     ngOnInit() {
46         this.footerButtons['Apply'] = new ButtonModel('Apply', 'blue', this.search, this.someTypesSelectedAndThereIsPropertyName);
47         this.footerButtons['Close'] = new ButtonModel('Close', 'grey', this.close);
48         this.componentType = this.componentType.toLocaleLowerCase();
49         this.typesOptions = sdcConfig.resourceTypesFilter[this.componentType];
50     }
51
52     selectAll = () => {
53         _.forEach(this.typesOptions, (type) => {
54             this.selectedTypes[type] = this.allSelected;
55         });
56     }
57
58     onTypeSelected = (type:string) => {
59         if(!this.selectedTypes[type]){
60             this.allSelected = false;//unselected 'All'
61         }
62     };
63
64     search = () => {
65         console.log('search props');
66         this.filterData.selectedTypes = [];
67         _.forEach(sdcConfig.resourceTypesFilter[this.componentType], (type) => {
68             if(this.selectedTypes[type]){
69                 this.filterData.selectedTypes.push(type);
70             }
71         });
72         this.searchProperties.emit(this.filterData);
73         this.filterPopover.hide();
74     }
75
76     close = () => {
77         this.filterPopover.hide();
78     }
79
80     someTypesSelectedAndThereIsPropertyName = ():boolean => {
81         if( _.find(Object.keys(this.selectedTypes),(key) => {
82             return this.selectedTypes[key];
83         }) && this.filterData.propertyName ){
84             return null
85         }
86         return true;
87     }
88
89     clearAll = ():void => {
90         this.filterData.propertyName = "";
91         _.forEach(this.selectedTypes,(value, key) => {
92             this.selectedTypes[key] = false;
93         });
94         this.allSelected = false;
95     }
96
97 }