Changed the Style of GUI
[holmes/rule-management.git] / rulemgt-frontend / src / app / correlation-ruleList / alarmRule.component.ts
1 /*
2  Copyright 2018 ZTE Corporation.
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8      http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 import { Component, OnInit, ElementRef } from '@angular/core';
17 import { ModalService } from '../correlation-modal/modal.service';
18 import { RuleModel } from './alarmRule';
19 import { RuleRequest } from './ruleRequest';
20 import { Router } from '@angular/router';
21 import { AlarmRuleService } from './alarmRule.service';
22 import { Observable } from 'rxjs';
23
24 @Component({
25   selector: 'alarmRule',
26   templateUrl: './alarmRule.component.html',
27   styleUrls: ['./alarmRule.component.css']
28 })
29 export class AlarmRule implements OnInit {
30   ruleModel: RuleModel;
31   rules: any[];
32   queryRule: RuleModel;
33   activeText: string;
34   ruleName: string;
35   enable_on = "enabled";
36   enable_off = "disabled"
37   totalcount: number = 0;
38   model: any;
39   ruleRequest: RuleRequest;
40   solution = 'ANGULAR';
41   selection = 'A';
42   activeStatus = ["option_all", "common_enabled", "common_disabled"];
43
44   constructor(public _alarmRuleService: AlarmRuleService, private modalService: ModalService,
45     private router: Router, private ele: ElementRef) {
46   }
47
48   switch(select: string): void {
49     console.log(select);
50     if (select == "common_enabled") {
51       this.ruleModel.enabled = 1;
52     } else if (select == "common_disabled") {
53       this.ruleModel.enabled = 0;
54     } else {
55
56       this.ruleModel.enabled = null;
57     }
58     this.setActiveText();
59   }
60
61   setActiveText(): void {
62     if (this.ruleModel.enabled == 1) {
63       this.activeText = "common_enabled";
64       this.ruleRequest.enabled = 1;
65     }
66     else if (this.ruleModel.enabled == 0) {
67       this.activeText = "common_disabled";
68       this.ruleRequest.enabled = 0;
69     } else {
70       this.activeText = "option_all";
71       this.ruleRequest.enabled = 2;
72     }
73   }
74
75   getRules(): void {
76     this._alarmRuleService.getRules().then(rules => {
77       this.rules = rules.correlationRules;
78       this.rules.map(x => x['showModal'] = false);
79       console.log(this.rules);
80       this.totalcount = rules.totalCount;
81     });
82   }
83
84   public searchRules(): void {
85     if (this.ruleModel.enabled == null) {
86       this.ruleRequest.enabled = null;
87     }
88     this.ruleRequest.ruleName = this.ruleModel.ruleName;
89     console.log(this.ruleRequest.enabled, this.ruleRequest.ruleName);
90
91     this._alarmRuleService
92       .searchrules(this.ruleRequest)
93       .then(rules => {
94         this.rules = rules;
95         this.totalcount = rules.length;
96       });
97   }
98
99   public updateRule(rule: RuleModel): void {
100     this.router.navigate(['ruleInfo/', rule.ruleId]);
101   }
102
103   public delete(rule: any): void {
104     rule.showModal = true;
105   }
106
107   public on_off(rule: RuleModel) {
108     rule.enabled == 0 ? rule.enabled = 1 : rule.enabled = 0;
109     this._alarmRuleService
110       .updateRule(rule)
111       .then(res => {
112         rule = res;
113       });
114   }
115
116   public reset(): void {
117     this.ruleModel.ruleName = null;
118     this.activeText = 'option_all';
119     this.ruleModel.enabled = null;
120     this.getRules();
121   }
122
123   deleteRule(ruleId: string): void {
124     this._alarmRuleService.delete(ruleId).then(() => {
125       this.cancelModal(ruleId);
126       this.getRules();
127     }).catch(() => {
128       this.cancelModal(ruleId);
129     })
130   }
131
132   cancelModal(ruleId: string): void {
133     this.rules.find(x => x.ruleId === ruleId).showModal = false;
134   }
135
136   public ngOnInit(): void {
137     this.activeText = 'option_all';
138     this.ruleModel = {
139       ruleId: null,
140       ruleName: null,
141       description: '',
142       content: null,
143       createTime: null,
144       creator: null,
145       updateTime: null,
146       modifier: null,
147       enabled: 0,
148       loopControlName: ''
149     };
150     this.ruleRequest = {
151       ruleId: null,
152       ruleName: null,
153       creator: null,
154       modifier: null,
155       enabled: null,
156       loopControlName: ''
157     };
158     this.getRules();
159
160     this.ele.nativeElement.querySelector('.container-fluid').style.height = window.innerHeight + 'px';
161     Observable.fromEvent(window, 'resize')
162       .debounceTime(100)
163         .subscribe(() => {
164         this.ele.nativeElement.querySelector('.container-fluid').style.height = window.innerHeight + 'px';
165         });
166   }
167 }