Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / cds-ui / designer-client / src / app / modules / feature-modules / packages / packages-dashboard / filter-by-tags / filter-by-tags.component.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2019 Orange. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import {Component, ElementRef, OnInit, QueryList, ViewChildren} from '@angular/core';
23 import {PackagesStore} from '../../packages.store';
24 import {BlueprintModel, BluePrintPage} from '../../model/BluePrint.model';
25
26 @Component({
27     selector: 'app-filter-by-tags',
28     templateUrl: './filter-by-tags.component.html',
29     styleUrls: ['./filter-by-tags.component.css']
30 })
31
32 export class TagsFilteringComponent implements OnInit {
33
34     page: BluePrintPage;
35     tags: string[] = [];
36     viewedTags: string[] = [];
37     searchTag = '';
38     viewedPackages: BlueprintModel[] = [];
39     checkBoxTages = '';
40     currentPage = 0;
41     @ViewChildren('checkboxes') checkboxes: QueryList<ElementRef>;
42
43     constructor(private packagesStore: PackagesStore,
44     ) {
45         this.refreshTags();
46     }
47
48     private refreshTags() {
49         this.packagesStore.state$.subscribe(state => {
50             console.log(state);
51             if (state.page) {
52                 this.viewedPackages = state.page.content;
53                 this.tags = [];
54                 if (state.currentPage !== this.currentPage) {
55                     this.checkBoxTages = '';
56                     this.currentPage = state.currentPage;
57                 }
58                 this.viewedPackages.forEach(element => {
59                     element.tags.split(',').forEach(tag => {
60                         this.tags.push(tag.trim());
61                     });
62                     this.tags.push('All');
63                     this.tags = this.tags.filter((value, index, self) => self.indexOf(value) === index && value);
64                     this.assignTags();
65                 });
66             }
67         });
68     }
69
70     ngOnInit() {
71
72     }
73
74     reloadChanges(event: any) {
75         this.searchTag = event.target.value;
76         this.filterItem(this.searchTag);
77     }
78
79     private assignTags() {
80         this.viewedTags = this.tags;
81     }
82
83     private filterItem(value) {
84         if (!value) {
85             this.assignTags();
86         }
87         this.viewedTags = this.tags.filter(
88             item => item.toLowerCase().indexOf(value.toLowerCase()) > -1
89         );
90
91
92     }
93
94     reloadPackages(event: any) {
95         if (!event.target.checked) {
96             this.checkBoxTages = this.checkBoxTages.replace(event.target.id + ',', '')
97                 .replace(event.target.id, '');
98         } else {
99             this.checkBoxTages += event.target.id.trim() + ',';
100         }
101         const tagsSelected = this.checkBoxTages.split(',').filter(item => {
102             if (item) {
103                 return true;
104             }
105         }).map((item) => {
106             return item.trim();
107         });
108
109         this.packagesStore.filterByTags(tagsSelected);
110     }
111
112
113     resetFilter() {
114         this.checkBoxTages = '';
115         this.checkboxes.forEach((element) => {
116             element.nativeElement.checked = false;
117             this.packagesStore.getAll();
118         });
119     }
120 }