Initialize the UI code
[holmes/rule-management.git] / rulemgt / src / main / frontend / src / alarm / app / correlation-ruleList / alarmRule.component.ts
1 /*
2  Copyright 2017 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, ViewChild, ViewEncapsulation } 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 { Http, Response, Jsonp, Headers, RequestOptions } from '@angular/http';
22 import { AlarmRuleService } from './alarmRule.service';
23 declare var jQuery: any;
24
25 @Component({
26     selector: 'alarmRule',
27     templateUrl: './alarmRule.component.html',
28
29 })
30 export class AlarmRule implements OnInit {
31     ruleModel: RuleModel;
32     rules: RuleModel[];
33     queryRule: RuleModel;
34     activeText: string;
35     ruleName: string;
36     enable_on = "enabled";
37     enable_off = "disabled"
38     totalcount: number;
39     model: any;
40     ruleRequest: RuleRequest;
41     solution = 'ANGULAR';
42     selection = 'A'
43     activeStatus = ["option_all", "common_enabled", "common_disabled"];
44
45     constructor(public _alarmRuleService: AlarmRuleService, private modalService: ModalService,
46         private router: Router) { };
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 = null;
72         }
73     };
74
75     getRules(): Promise<any> {
76         return this._alarmRuleService
77             .getRules()
78             .then(rules => {
79                 this.rules = rules.rules;
80                 this.totalcount = rules.totalcount;
81             });
82     }
83
84     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     updateRule(rule: RuleModel): void {
99         this.router.navigate(['ruleInfo/', rule.ruleid]);
100     }
101
102     delete(rule: RuleModel): void {
103         rule.enabled == 1 ? this.deleteActiveRule(rule) : this.deleteModel(rule.ruleid, this._alarmRuleService, this);
104     }
105
106     on_off(rule: RuleModel) {
107         rule.enabled == 0 ? rule.enabled = 1 : rule.enabled = 0;
108         this._alarmRuleService
109             .updateRule(rule)
110             .then(res => {
111                 rule = res;
112             });
113     }
114
115     reset(): void {
116         this.ruleModel.rulename = null;
117         this.activeText = "option_all";
118         this.ruleModel.enabled = null;
119         this.getRules();
120     }
121
122     deleteActiveRule(rule: RuleModel): void {
123         jQuery("#" + rule.ruleid).popModal({
124             html: jQuery('#deleteActiveRuleContent'),
125             placement: 'leftTop',
126             showCloseBut: false,
127             onDocumentClickClose: true,
128             onOkBut: function () {
129             },
130         });
131     }
132     deleteModel(ruleid: string, alarm: AlarmRuleService, obj: any): void {
133         jQuery("#" + ruleid).popModal({
134             html: jQuery('#deleteTimingTaskContent'),
135             placement: 'leftTop',
136             showCloseBut: false,
137             onDocumentClickClose: true,
138             onOkBut: function () {
139                 jQuery("#deleteTimingTaskDlg").append(jQuery('#deleteTimingTaskContent'));
140                 alarm.delete(ruleid);
141                 obj.getRules();
142             },
143             onCancelBut: function () {
144             }
145         });
146     }
147
148     ngOnInit(): void {
149         this.activeText = "option_all";
150         this.ruleModel = {
151             ruleid: null,
152             rulename: null,
153             description: "",
154             content: null,
155             createtime: null,
156             creator: null,
157             updatetime: null,
158             modifier: null,
159             enabled: 0,
160         }
161         this.ruleRequest = {
162             ruleid: null,
163             rulename: null,
164             creator: null,
165             modifier: null,
166             enabled: null,
167         }
168         this.getRules();
169     }
170 }