/
audit-record-list-bar.component.ts
172 lines (150 loc) · 3.85 KB
/
audit-record-list-bar.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { ChangeDetectionStrategy, Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
import { AuditRecordListParams } from '../audit.interface';
import { Page } from '../../../shared/model/page';
import { AuditOperationType, AuditActionType } from '../../../shared/model/audit-record.model';
import { Subject } from 'rxjs';
import { DateTime } from 'luxon';
/**
* Audit Record List Bar
*
* @author Gunnar Hillert
*/
@Component({
selector: 'app-list-bar-audit-record',
styleUrls: ['./styles.scss'],
templateUrl: './audit-record-list-bar.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class AuditRecordListBarComponent implements OnInit {
/**
* Params
*/
@Input() params: AuditRecordListParams;
/**
* Page
*/
@Input() page: Page<any>;
/**
* Refresh
*/
@Output() refresh = new EventEmitter();
/**
* Search
*/
@Output() search = new EventEmitter<AuditRecordListParams>();
/**
* Action
*/
@Output() action = new EventEmitter<any>();
/**
* Count selected checkbox
*/
@Input() countSelected = 0;
/**
* Actions
*/
@Input() actions: Array<object>;
/**
* Operation Types
*/
@Input() operationTypes: Subject<AuditOperationType>;
/**
* Actions
*/
@Input() actionTypes: Subject<AuditActionType>;
/**
* Form
*/
form = {
q: '',
operation: null,
action: null,
dateRange: null
};
/**
* Constructor
*/
constructor() {
}
/**
* On Init
*/
ngOnInit() {
this.form.q = this.params.q;
this.form.operation = this.params.operation;
this.form.action = this.params.action;
if (this.params.fromDate && this.params.toDate) {
this.form.dateRange = [
this.params.fromDate.toJSDate(), this.params.toDate.toJSDate()
];
}
}
/**
* Determine if there is no audit record
*/
isEmpty(): boolean {
if (this.page && this.page.totalPages < 2 && this.page.totalElements < 1) {
return (this.params.q === '' || !this.params.q)
&& (!this.params.action) && (!this.params.operation) && (!this.params.toDate) && (!this.params.fromDate);
}
return false;
}
/**
* Used to determinate the state of the query parameters
* @returns {boolean} Search is active
*/
isSearchActive() {
let paramRange = '';
let formRange = '';
const values = this.splitControlDate();
if (values.fromDate && values.toDate) {
formRange = `${values.fromDate.toISO()}-${values.toDate.toISO()}`;
}
if (this.params.fromDate && this.params.toDate) {
paramRange = `${this.params.fromDate.toISO()}-${this.params.toDate.toISO()}`;
}
return (this.form.action !== this.params.action)
|| (this.form.operation !== this.params.operation)
|| (this.form.q !== this.params.q)
|| (paramRange !== formRange);
}
splitControlDate() {
let fromDate, toDate;
if (this.form.dateRange && Array.isArray(this.form.dateRange) && this.form.dateRange.length === 2) {
fromDate = DateTime.fromJSDate(this.form.dateRange[0]).startOf('day');
toDate = DateTime.fromJSDate(this.form.dateRange[1]).endOf('day');
}
return {
fromDate: fromDate,
toDate: toDate
};
}
/**
* Reset the search parameters and run the search
*/
clearSearch() {
this.form.q = '';
this.form.action = null;
this.form.operation = null;
this.form.dateRange = null;
this.doSearch();
}
doSearch() {
const values = this.splitControlDate();
const params: AuditRecordListParams = Object.assign(this.params, {
q: this.form.q,
action: this.form.action,
operation: this.form.operation,
fromDate: values.fromDate,
toDate: values.toDate,
page: 1
});
this.search.emit(params);
}
doRefresh() {
this.refresh.emit();
}
fire(action) {
this.action.emit({ action: action });
}
}