bdf6bd1eab10265e9bb28266e4d03497e6011f94
[ccsdk/cds.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : CDS
4 * ================================================================================
5 * Copyright (C) 2020 TechMahindra
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 import { Component, OnInit } from '@angular/core';
22 import { DictionaryModel, DictionaryPage } from '../../model/dictionary.model';
23 import { DictionaryStore } from '../../dictionary.store';
24
25 @Component({
26   selector: 'app-filterby-tags',
27   templateUrl: './filterby-tags.component.html',
28   styleUrls: ['./filterby-tags.component.css']
29 })
30 export class FilterbyTagsComponent implements OnInit {
31   page: DictionaryPage;
32   tags: string[] = [];
33   viewedTags: string[] = [];
34   searchTag = '';
35   viewedDictionary: DictionaryModel[] = [];
36   private checkBoxTages = '';
37   currentPage = 0;
38
39   constructor(private dictionaryStore: DictionaryStore) {
40       this.dictionaryStore.state$.subscribe(state => {
41           console.log(state);
42           if (state.page) {
43               this.viewedDictionary = state.page.content;
44               this.tags = [];
45               if (state.currentPage !== this.currentPage) {
46                   this.checkBoxTages = '';
47                   this.currentPage = state.currentPage;
48               }
49               this.viewedDictionary.forEach(element => {
50                   element.tags.split(',').forEach(tag => {
51                       this.tags.push(tag.trim());
52                   });
53                   this.tags.push('All');
54                   this.tags = this.tags.filter((value, index, self) => self.indexOf(value) === index);
55                   this.assignTags();
56               });
57           }
58       });
59   }
60
61   ngOnInit() {
62
63   }
64
65   reloadChanges(event: any) {
66       this.searchTag = event.target.value;
67       this.filterItem(this.searchTag);
68   }
69
70   private assignTags() {
71       this.viewedTags = this.tags;
72   }
73
74   private filterItem(value) {
75       if (!value) {
76           this.assignTags();
77       }
78       this.viewedTags = this.tags.filter(
79           item => item.toLowerCase().indexOf(value.toLowerCase()) > -1
80       );
81   }
82
83   reloadDictionary(event: any) {
84       if (!event.target.checked) {
85           this.checkBoxTages = this.checkBoxTages.replace(event.target.id + ',', '')
86               .replace(event.target.id, '');
87       } else {
88           this.checkBoxTages += event.target.id.trim() + ',';
89       }
90       const tagsSelected = this.checkBoxTages.split(',').filter(item => {
91           if (item) {
92               return true;
93           }
94       }).map((item) => {
95           return item.trim();
96       });
97       this.dictionaryStore.filterByTags(tagsSelected);
98   }
99
100 }