/
servicelookupeditor.ts
193 lines (150 loc) · 5.93 KB
/
servicelookupeditor.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { ColumnSelection, Criteria, ListRequest, ListResponse, ServiceOptions, resolveServiceUrl, serviceCall } from "../../base";
import { Decorators } from "../../types/decorators";
import { ComboboxSearchQuery, ComboboxSearchResult } from "./combobox";
import { ComboboxEditor, ComboboxEditorOptions } from "./comboboxeditor";
import { EditorProps } from "./editorwidget";
export interface ServiceLookupEditorOptions extends ComboboxEditorOptions {
service?: string;
idField?: string;
textField?: string;
pageSize?: number;
minimumResultsForSearch?: any;
sort?: string[];
columnSelection?: ColumnSelection;
includeColumns?: string[];
excludeColumns?: string[];
includeDeleted?: boolean;
containsField?: string;
equalityFilter?: any;
criteria?: any[];
}
@Decorators.registerEditor("Serenity.ServiceLookupEditorBase")
export abstract class ServiceLookupEditorBase<P extends ServiceLookupEditorOptions, TItem> extends ComboboxEditor<P, TItem> {
protected getDialogTypeKey() {
var dialogTypeKey = super.getDialogTypeKey();
if (dialogTypeKey)
return dialogTypeKey;
var service = this.getService();
if (service.startsWith("~/Services/"))
service = service.substring("~/Services/".length);
if (service.split('/').length == 3)
service = service.substring(0, service.lastIndexOf('/'));
return service.replace("/", ".");
}
protected getService(): string {
return this.options.service;
}
protected getServiceUrl() {
var url = this.getService();
if (url == null)
throw new Error("ServiceLookupEditor requires 'service' option to be configured!");
return resolveServiceUrl(url);
}
protected getIncludeColumns() {
var include = this.options.includeColumns || [];
var idField = this.getIdField();
if (idField && include.indexOf(idField) < 0)
include.push(idField);
var textField = this.getTextField();
if (textField && include.indexOf(textField) < 0)
include.push(textField);
return include;
}
protected getSort() {
return this.options.sort || (this.getTextField() ? [this.getTextField()] : null);
}
protected getCascadeCriteria(): any[] {
var val = this.get_cascadeValue();
if (val == null || val === '') {
if (this.get_cascadeField()) {
return ['0', '=', '1'];
}
return null;
}
var fld = this.get_cascadeField();
return Criteria(fld).eq(val);
}
protected getFilterCriteria(): any[] {
var val = this.get_filterValue();
if (val == null || val === '') {
return null;
}
var fld = this.get_filterField();
return [[fld], '=', val];
}
protected getIdListCriteria(idList: any[]): any[] {
if (idList == null)
return null;
if (idList.length == 0)
return ['0', '=', '1'];
var idField = this.getIdField();
if (idField == null)
throw new Error("ServiceLookupEditor requires 'idField' option to be configured!");
return Criteria(idField).in(idList);
}
protected getCriteria(query: ComboboxSearchQuery): any[] {
return Criteria.and(
Criteria.and(this.getIdListCriteria(query.idList), this.options.criteria),
Criteria.and(this.getCascadeCriteria(), this.getFilterCriteria()));
}
protected getListRequest(query: ComboboxSearchQuery): ListRequest {
var request: ListRequest = {};
if (query.searchTerm)
request.ContainsText = query.searchTerm;
request.Sort = this.getSort();
request.ColumnSelection = this.options.columnSelection || ColumnSelection.KeyOnly;
request.IncludeColumns = this.getIncludeColumns();
request.ExcludeColumns = this.options.excludeColumns;
request.ContainsField = this.options.containsField;
request.EqualityFilter = this.options.equalityFilter;
request.Criteria = this.getCriteria(query);
request.Skip = query.skip || 0;
request.Take = query.take ? (query.checkMore ? query.take + 1 : query.take) : 0;
request.IncludeDeleted = this.options.includeDeleted;
request.ExcludeTotalCount = true;
return request;
}
protected getServiceCallOptions(query: ComboboxSearchQuery): ServiceOptions<ListResponse<TItem>> {
return {
blockUI: false,
service: this.getServiceUrl(),
request: this.getListRequest(query),
signal: query.signal
}
}
protected hasAsyncSource() {
return true;
}
protected canSearch(byId: boolean) {
if (!byId && this.get_cascadeField()) {
var val = this.get_cascadeValue();
if (val == null || val === '')
return false;
}
return true;
}
protected override async asyncSearch(query: ComboboxSearchQuery): Promise<ComboboxSearchResult<TItem>> {
if (!this.canSearch(query.idList != null)) {
return Promise.resolve({
items: [],
more: false
});
}
var opt = this.getServiceCallOptions(query);
var response = await serviceCall(opt);
var itemsPlus1 = response.Entities || [];
var items = itemsPlus1;
if (query.take && query.checkMore)
items = items.slice(0, query.take);
return {
items: items,
more: query.checkMore && query.take && itemsPlus1.length > query.take
};
}
}
@Decorators.registerEditor('Serenity.ServiceLookupEditor')
export class ServiceLookupEditor<P extends ServiceLookupEditorOptions = ServiceLookupEditorOptions, TItem = any> extends ServiceLookupEditorBase<ServiceLookupEditorOptions, TItem> {
constructor(props: EditorProps<P>) {
super(props);
}
}