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