c23e08bc0d03f7b1076e17215760dda55e860b1e
[sdc.git] /
1 /**
2  * Created by rc2122 on 5/16/2017.
3  */
4 import {Component, Input, Output, EventEmitter, ViewChild} from '@angular/core';
5 import {ButtonModel, ButtonsModelMap, FilterPropertiesAssignmentData} from "app/models";
6 import {PopoverComponent} from "../popover/popover.component";
7 import * as sdcConfig from "../../../../../configurations/dev"
8
9 @Component({
10     selector: 'filter-properties-assignment',
11     templateUrl: './filter-properties-assignment.component.html',
12     styleUrls: ['./filter-properties-assignment.component.less']
13 })
14
15 export class FilterPropertiesAssignmentComponent {
16     @Input() componentType: string;
17     @Output() searchProperties: EventEmitter<FilterPropertiesAssignmentData> = new EventEmitter<FilterPropertiesAssignmentData>();
18     footerButtons:ButtonsModelMap = {};
19     typesOptions:Array<string>;//All optional types
20     selectedTypes:Object = {};
21     allSelected:boolean = false;//if all option selected
22     filterData:FilterPropertiesAssignmentData = new FilterPropertiesAssignmentData();
23     @ViewChild('filterPopover') filterPopover: PopoverComponent;
24
25     ngOnInit() {
26         this.footerButtons['Apply'] = new ButtonModel('Apply', 'blue', this.search, this.someTypesSelectedAndThereIsPropertyName);
27         this.footerButtons['Close'] = new ButtonModel('Close', 'grey', this.close);
28         this.componentType = this.componentType.toLocaleLowerCase();
29         this.typesOptions = sdcConfig.resourceTypesFilter[this.componentType];
30     }
31
32     selectAll = () => {
33         _.forEach(this.typesOptions, (type) => {
34             this.selectedTypes[type] = this.allSelected;
35         });
36     }
37
38     onTypeSelected = (type:string) => {
39         if(!this.selectedTypes[type]){
40             this.allSelected = false;//unselected 'All'
41         }
42     };
43
44     search = () => {
45         console.log('search props');
46         this.filterData.selectedTypes = [];
47         _.forEach(sdcConfig.resourceTypesFilter[this.componentType], (type) => {
48             if(this.selectedTypes[type]){
49                 this.filterData.selectedTypes.push(type);
50             }
51         });
52         this.searchProperties.emit(this.filterData);
53         this.filterPopover.hide();
54     }
55
56     close = () => {
57         this.filterPopover.hide();
58     }
59
60     someTypesSelectedAndThereIsPropertyName = ():boolean => {
61         if( _.find(Object.keys(this.selectedTypes),(key) => {
62             return this.selectedTypes[key];
63         }) && this.filterData.propertyName ){
64             return null
65         }
66         return true;
67     }
68
69     clearAll = ():void => {
70         this.filterData.propertyName = "";
71         _.forEach(this.selectedTypes,(value, key) => {
72             this.selectedTypes[key] = false;
73         });
74         this.allSelected = false;
75     }
76
77 }