]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
e10eb69722deee987627bebdac7f3e539483e1d3
[thirdparty/paperless-ngx.git] /
1 import {
2 Component,
3 EventEmitter,
4 Input,
5 Output,
6 QueryList,
7 ViewChild,
8 ViewChildren,
9 } from '@angular/core'
10 import { NgbDropdown } from '@ng-bootstrap/ng-bootstrap'
11 import { NgSelectComponent } from '@ng-select/ng-select'
12 import { Subject, first, takeUntil } from 'rxjs'
13 import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field'
14 import {
15 CustomFieldQueryElementType,
16 CustomFieldQueryOperator,
17 CUSTOM_FIELD_QUERY_OPERATOR_GROUPS_BY_TYPE,
18 CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP,
19 CustomFieldQueryOperatorGroups,
20 CUSTOM_FIELD_QUERY_OPERATOR_LABELS,
21 CUSTOM_FIELD_QUERY_MAX_DEPTH,
22 CUSTOM_FIELD_QUERY_MAX_ATOMS,
23 } from 'src/app/data/custom-field-query'
24 import { CustomFieldsService } from 'src/app/services/rest/custom-fields.service'
25 import {
26 CustomFieldQueryElement,
27 CustomFieldQueryExpression,
28 CustomFieldQueryAtom,
29 } from 'src/app/utils/custom-field-query-element'
30 import { popperOptionsReenablePreventOverflow } from 'src/app/utils/popper-options'
31 import { LoadingComponentWithPermissions } from '../../loading-component/loading.component'
32
33 export class CustomFieldQueriesModel {
34 public queries: CustomFieldQueryElement[] = []
35
36 public readonly changed = new Subject<CustomFieldQueriesModel>()
37
38 public clear(fireEvent = true) {
39 this.queries = []
40 if (fireEvent) {
41 this.changed.next(this)
42 }
43 }
44
45 public isValid(): boolean {
46 return (
47 this.queries.length > 0 &&
48 this.validateExpression(this.queries[0] as CustomFieldQueryExpression)
49 )
50 }
51
52 public isEmpty(): boolean {
53 return (
54 this.queries.length === 0 ||
55 (this.queries.length === 1 && this.queries[0].value.length === 0)
56 )
57 }
58
59 private validateAtom(atom: CustomFieldQueryAtom) {
60 let valid = !!(atom.field && atom.operator && atom.value !== null)
61 if (
62 [
63 CustomFieldQueryOperator.In.valueOf(),
64 CustomFieldQueryOperator.Contains.valueOf(),
65 ].includes(atom.operator) &&
66 atom.value
67 ) {
68 valid = valid && atom.value.length > 0
69 }
70 return valid
71 }
72
73 private validateExpression(expression: CustomFieldQueryExpression) {
74 return (
75 expression.operator &&
76 expression.value.length > 0 &&
77 (expression.value as CustomFieldQueryElement[]).every((e) =>
78 e.type === CustomFieldQueryElementType.Atom
79 ? this.validateAtom(e as CustomFieldQueryAtom)
80 : this.validateExpression(e as CustomFieldQueryExpression)
81 )
82 )
83 }
84
85 public addAtom(atom: CustomFieldQueryAtom) {
86 if (this.queries.length === 0) {
87 this.addExpression()
88 }
89 ;(this.queries[0].value as CustomFieldQueryElement[]).push(atom)
90 atom.changed.subscribe(() => {
91 if (atom.field && atom.operator && atom.value) {
92 this.changed.next(this)
93 }
94 })
95 }
96
97 public addExpression(
98 expression: CustomFieldQueryExpression = new CustomFieldQueryExpression()
99 ) {
100 if (this.queries.length > 0) {
101 ;(
102 (this.queries[0] as CustomFieldQueryExpression)
103 .value as CustomFieldQueryElement[]
104 ).push(expression)
105 } else {
106 this.queries.push(expression)
107 }
108 expression.changed.subscribe(() => {
109 this.changed.next(this)
110 })
111 }
112
113 private findElement(
114 queryElement: CustomFieldQueryElement,
115 elements: any[]
116 ): CustomFieldQueryElement {
117 let foundElement
118 for (let i = 0; i < elements.length; i++) {
119 if (elements[i] === queryElement) {
120 foundElement = elements.splice(i, 1)[0]
121 } else if (elements[i].type === CustomFieldQueryElementType.Expression) {
122 foundElement = this.findElement(
123 queryElement,
124 elements[i].value as CustomFieldQueryElement[]
125 )
126 }
127 if (foundElement) {
128 break
129 }
130 }
131 return foundElement
132 }
133
134 public removeElement(queryElement: CustomFieldQueryElement) {
135 let foundComponent
136 for (let i = 0; i < this.queries.length; i++) {
137 let query = this.queries[i]
138 if (query === queryElement) {
139 foundComponent = this.queries.splice(i, 1)[0]
140 break
141 } else if (query.type === CustomFieldQueryElementType.Expression) {
142 foundComponent = this.findElement(queryElement, query.value as any[])
143 }
144 }
145 if (foundComponent) {
146 foundComponent.changed.complete()
147 if (this.isEmpty()) {
148 this.clear()
149 }
150 this.changed.next(this)
151 }
152 }
153 }
154
155 @Component({
156 selector: 'pngx-custom-fields-query-dropdown',
157 templateUrl: './custom-fields-query-dropdown.component.html',
158 styleUrls: ['./custom-fields-query-dropdown.component.scss'],
159 })
160 export class CustomFieldsQueryDropdownComponent extends LoadingComponentWithPermissions {
161 public CustomFieldQueryComponentType = CustomFieldQueryElementType
162 public CustomFieldQueryOperator = CustomFieldQueryOperator
163 public CustomFieldDataType = CustomFieldDataType
164 public CUSTOM_FIELD_QUERY_MAX_DEPTH = CUSTOM_FIELD_QUERY_MAX_DEPTH
165 public CUSTOM_FIELD_QUERY_MAX_ATOMS = CUSTOM_FIELD_QUERY_MAX_ATOMS
166 public popperOptions = popperOptionsReenablePreventOverflow
167
168 @Input()
169 title: string
170
171 @Input()
172 filterPlaceholder: string = ''
173
174 @Input()
175 icon: string
176
177 @Input()
178 allowSelectNone: boolean = false
179
180 @Input()
181 editing = false
182
183 @Input()
184 applyOnClose = false
185
186 get name(): string {
187 return this.title ? this.title.replace(/\s/g, '_').toLowerCase() : null
188 }
189
190 @Input()
191 disabled: boolean = false
192
193 @ViewChild('dropdown') dropdown: NgbDropdown
194
195 @ViewChildren(NgSelectComponent) fieldSelects!: QueryList<NgSelectComponent>
196
197 private _selectionModel: CustomFieldQueriesModel
198
199 @Input()
200 set selectionModel(model: CustomFieldQueriesModel) {
201 if (this._selectionModel) {
202 this._selectionModel.changed.complete()
203 }
204 model.changed.subscribe(() => {
205 this.onModelChange()
206 })
207 this._selectionModel = model
208 }
209
210 get selectionModel(): CustomFieldQueriesModel {
211 return this._selectionModel
212 }
213
214 private onModelChange() {
215 if (this.selectionModel.isEmpty() || this.selectionModel.isValid()) {
216 this.selectionModelChange.next(this.selectionModel)
217 this.selectionModel.isEmpty() && this.dropdown?.close()
218 }
219 }
220
221 @Output()
222 selectionModelChange = new EventEmitter<CustomFieldQueriesModel>()
223
224 customFields: CustomField[] = []
225
226 constructor(protected customFieldsService: CustomFieldsService) {
227 super()
228 this.selectionModel = new CustomFieldQueriesModel()
229 this.getFields()
230 this.reset()
231 }
232
233 public onOpenChange(open: boolean) {
234 if (open) {
235 if (this.selectionModel.queries.length === 0) {
236 this.selectionModel.addAtom(
237 new CustomFieldQueryAtom([
238 null,
239 CustomFieldQueryOperator.Exists,
240 'true',
241 ])
242 )
243 }
244 if (
245 this.selectionModel.queries.length === 1 &&
246 (
247 (this.selectionModel.queries[0] as CustomFieldQueryExpression)
248 ?.value[0] as CustomFieldQueryAtom
249 )?.field === null
250 ) {
251 setTimeout(() => {
252 this.fieldSelects.first?.focus()
253 }, 0)
254 }
255 }
256 }
257
258 public get isActive(): boolean {
259 return this.selectionModel.isValid()
260 }
261
262 private getFields() {
263 this.customFieldsService
264 .listAll()
265 .pipe(first(), takeUntil(this.unsubscribeNotifier))
266 .subscribe((result) => {
267 this.customFields = result.results
268 })
269 }
270
271 public getCustomFieldByID(id: number): CustomField {
272 return this.customFields.find((field) => field.id === id)
273 }
274
275 public addAtom(expression: CustomFieldQueryExpression) {
276 expression.addAtom()
277 }
278
279 public addExpression(expression: CustomFieldQueryExpression) {
280 expression.addExpression()
281 }
282
283 public removeElement(element: CustomFieldQueryElement) {
284 this.selectionModel.removeElement(element)
285 }
286
287 public reset() {
288 this.selectionModel.clear(false)
289 this.selectionModel.changed.next(this.selectionModel)
290 }
291
292 getOperatorsForField(
293 fieldID: number
294 ): Array<{ value: string; label: string }> {
295 const field = this.customFields.find((field) => field.id === fieldID)
296 const groups: CustomFieldQueryOperatorGroups[] = field
297 ? CUSTOM_FIELD_QUERY_OPERATOR_GROUPS_BY_TYPE[field.data_type]
298 : [CustomFieldQueryOperatorGroups.Basic]
299 const operators = groups.flatMap(
300 (group) => CUSTOM_FIELD_QUERY_OPERATORS_BY_GROUP[group]
301 )
302 return operators.map((operator) => ({
303 value: operator,
304 label: CUSTOM_FIELD_QUERY_OPERATOR_LABELS[operator],
305 }))
306 }
307
308 getSelectOptionsForField(
309 fieldID: number
310 ): Array<{ label: string; id: string }> {
311 const field = this.customFields.find((field) => field.id === fieldID)
312 if (field) {
313 return field.extra_data['select_options']
314 }
315 return []
316 }
317 }