react-hook-core 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/formutil.js +36 -22
- package/lib/index.js +0 -1
- package/lib/useEdit.js +28 -9
- package/lib/useView.js +107 -107
- package/package.json +1 -1
- package/src/formutil.ts +31 -20
- package/src/index.ts +0 -1
- package/src/useEdit.ts +25 -7
- package/src/useView.ts +3 -3
- package/lib/components.js +0 -1215
- package/src/components.ts +0 -1285
package/src/components.ts
DELETED
|
@@ -1,1285 +0,0 @@
|
|
|
1
|
-
import * as React from 'react';
|
|
2
|
-
import {BaseDiffState, DiffApprService, DiffParameter, DiffState, handleToggle, hideLoading, showLoading} from './core';
|
|
3
|
-
import {Attributes, error, ErrorMessage, Filter, getCurrencyCode, getModelName as getModelName2, initForm, LoadingService, Locale, message, messageByHttpStatus, PageChange, pageSizes, removePhoneFormat, ResourceService, SearchParameter, SearchResult, SearchService, SearchState, StringMap, UIService, ViewParameter, ViewService} from './core';
|
|
4
|
-
import {formatDiffModel, getDataFields} from './diff';
|
|
5
|
-
import {build, createModel as createModel2, EditParameter, GenericService, handleVersion, initPropertyNullInModel} from './edit';
|
|
6
|
-
import {focusFirstError, readOnly} from './formutil';
|
|
7
|
-
import {getAutoSearch, getConfirmFunc, getErrorFunc, getLoadingFunc, getLocaleFunc, getMsgFunc, getResource, getUIService} from './input';
|
|
8
|
-
import {clone, diff, makeDiff} from './reflect';
|
|
9
|
-
import {buildFromUrl} from './route';
|
|
10
|
-
import {addParametersIntoUrl, append, buildMessage, changePage, changePageSize, formatResults, getFieldsFromForm, getModel, handleAppend, handleSortEvent, initFilter, mergeFilter as mergeFilter2, more, Pagination, reset, showPaging, Sortable, validate} from './search';
|
|
11
|
-
import {buildFlatState, buildState, enLocale, handleEvent, handleProps, localeOf} from './state';
|
|
12
|
-
|
|
13
|
-
interface Searchable extends Pagination, Sortable {
|
|
14
|
-
}
|
|
15
|
-
export class ViewComponent<T, ID, P, S> extends React.Component<P, S> {
|
|
16
|
-
constructor(props: P, sv: ((id: ID, ctx?: any) => Promise<T>)|ViewService<T, ID>,
|
|
17
|
-
param: ResourceService|ViewParameter,
|
|
18
|
-
showError?: (msg: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
19
|
-
loading?: LoadingService,
|
|
20
|
-
getLocale?: (profile?: string) => Locale) {
|
|
21
|
-
super(props);
|
|
22
|
-
this.resourceService = getResource(param);
|
|
23
|
-
this.resource = this.resourceService.resource();
|
|
24
|
-
this.showError = getErrorFunc(param, showError);
|
|
25
|
-
this.loading = getLoadingFunc(param, loading);
|
|
26
|
-
this.getLocale = getLocaleFunc(param, getLocale);
|
|
27
|
-
if (sv) {
|
|
28
|
-
if (typeof sv === 'function') {
|
|
29
|
-
this.loadData = sv;
|
|
30
|
-
} else {
|
|
31
|
-
this.loadData = sv.load;
|
|
32
|
-
if (sv.metadata) {
|
|
33
|
-
const m = sv.metadata();
|
|
34
|
-
if (m) {
|
|
35
|
-
this.metadata = m;
|
|
36
|
-
const meta = build(m);
|
|
37
|
-
if (meta) {
|
|
38
|
-
this.keys = meta.keys;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
this.getModelName = this.getModelName.bind(this);
|
|
45
|
-
this.load = this.load.bind(this);
|
|
46
|
-
this.getModel = this.getModel.bind(this);
|
|
47
|
-
this.showModel = this.showModel.bind(this);
|
|
48
|
-
this.ref = React.createRef();
|
|
49
|
-
}
|
|
50
|
-
name?: string;
|
|
51
|
-
running?: boolean;
|
|
52
|
-
resourceService: ResourceService;
|
|
53
|
-
resource: StringMap;
|
|
54
|
-
loading?: LoadingService;
|
|
55
|
-
showError: (msg: string, title?: string, detail?: string, callback?: () => void) => void;
|
|
56
|
-
getLocale?: (profile?: string) => Locale;
|
|
57
|
-
loadData?: (id: ID) => Promise<T|null>;
|
|
58
|
-
// protected service: ViewService<T, ID>;
|
|
59
|
-
form?: HTMLFormElement;
|
|
60
|
-
ref: any;
|
|
61
|
-
keys?: string[];
|
|
62
|
-
metadata?: Attributes;
|
|
63
|
-
|
|
64
|
-
getModelName(): string {
|
|
65
|
-
if (this.name && this.name.length > 0) {
|
|
66
|
-
return this.name;
|
|
67
|
-
}
|
|
68
|
-
const n = getModelName2(this.form);
|
|
69
|
-
if (!n || n.length === 0) {
|
|
70
|
-
return 'model';
|
|
71
|
-
} else {
|
|
72
|
-
return n;
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
load(_id: ID, callback?: (m: T, showF: (model: T) => void) => void) {
|
|
76
|
-
const id: any = _id;
|
|
77
|
-
if (id != null && id !== '' && this.loadData) {
|
|
78
|
-
this.running = true;
|
|
79
|
-
showLoading(this.loading);
|
|
80
|
-
const com = this;
|
|
81
|
-
this.loadData(id).then(obj => {
|
|
82
|
-
if (!obj) {
|
|
83
|
-
com.handleNotFound(com.form);
|
|
84
|
-
} else {
|
|
85
|
-
if (callback) {
|
|
86
|
-
callback(obj, com.showModel);
|
|
87
|
-
} else {
|
|
88
|
-
com.showModel(obj);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
com.running = false;
|
|
92
|
-
hideLoading(com.loading);
|
|
93
|
-
}).catch(err => {
|
|
94
|
-
const data = (err && err.response) ? err.response : err;
|
|
95
|
-
if (data && data.status === 404) {
|
|
96
|
-
com.handleNotFound(com.form);
|
|
97
|
-
} else {
|
|
98
|
-
error(err, com.resourceService.value, com.showError);
|
|
99
|
-
}
|
|
100
|
-
com.running = false;
|
|
101
|
-
hideLoading(com.loading);
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
handleNotFound(form?: HTMLFormElement): void {
|
|
106
|
-
const msg = message(this.resourceService.value, 'error_not_found', 'error');
|
|
107
|
-
if (form) {
|
|
108
|
-
readOnly(form);
|
|
109
|
-
}
|
|
110
|
-
this.showError(msg.message, msg.title);
|
|
111
|
-
}
|
|
112
|
-
getModel(): T {
|
|
113
|
-
return (this.state as any)[this.getModelName()];
|
|
114
|
-
}
|
|
115
|
-
showModel(model: T) {
|
|
116
|
-
const modelName = this.getModelName();
|
|
117
|
-
const objSet: any = {};
|
|
118
|
-
objSet[modelName] = model;
|
|
119
|
-
this.setState(objSet);
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export class BaseComponent<P, S> extends React.Component<P, S> {
|
|
124
|
-
constructor(props: P,
|
|
125
|
-
public getLocale?: () => Locale,
|
|
126
|
-
private removeErr?: (ctrl: HTMLInputElement) => void) {
|
|
127
|
-
super(props);
|
|
128
|
-
this.getModelName = this.getModelName.bind(this);
|
|
129
|
-
this.updateState = this.updateState.bind(this);
|
|
130
|
-
this.updateFlatState = this.updateFlatState.bind(this);
|
|
131
|
-
this.updatePhoneState = this.updatePhoneState.bind(this);
|
|
132
|
-
this.updateDateState = this.updateDateState.bind(this);
|
|
133
|
-
this.prepareCustomData = this.prepareCustomData.bind(this);
|
|
134
|
-
}
|
|
135
|
-
running?: boolean;
|
|
136
|
-
form?: HTMLFormElement|null;
|
|
137
|
-
/*
|
|
138
|
-
protected handleSubmitForm(e) {
|
|
139
|
-
if (e.which === 13) {
|
|
140
|
-
if (document.getElementById('sysAlert').style.display !== 'none') {
|
|
141
|
-
document.getElementById('sysYes').click();
|
|
142
|
-
} else {
|
|
143
|
-
document.getElementById('btnSave').click();
|
|
144
|
-
}
|
|
145
|
-
} else if (e.which === 27) {
|
|
146
|
-
document.getElementById('sysNo').click();
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
*/
|
|
150
|
-
|
|
151
|
-
prepareCustomData(data: any) { }
|
|
152
|
-
|
|
153
|
-
updatePhoneState = (event: any) => {
|
|
154
|
-
const re = /^[0-9\b]+$/;
|
|
155
|
-
const target = event.currentTarget as HTMLInputElement;
|
|
156
|
-
const value = removePhoneFormat(target.value);
|
|
157
|
-
if (re.test(value) || !value) {
|
|
158
|
-
this.updateState(event);
|
|
159
|
-
} else {
|
|
160
|
-
const splitArr = value.split('');
|
|
161
|
-
let responseStr = '';
|
|
162
|
-
splitArr.forEach(element => {
|
|
163
|
-
if (re.test(element)) {
|
|
164
|
-
responseStr += element;
|
|
165
|
-
}
|
|
166
|
-
});
|
|
167
|
-
target.value = responseStr;
|
|
168
|
-
this.updateState(event);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
updateDateState = (name: string, value: any) => {
|
|
173
|
-
const props: any = this.props;
|
|
174
|
-
const modelName = this.getModelName(this.form);
|
|
175
|
-
const state = (this.state as any)[modelName];
|
|
176
|
-
if (props.setGlobalState) {
|
|
177
|
-
const data = props.shouldBeCustomized ? this.prepareCustomData({ [name]: value }) : { [name]: value };
|
|
178
|
-
props.setGlobalState({ [modelName]: { ...state, ...data } });
|
|
179
|
-
} else {
|
|
180
|
-
const objSet: any = {[modelName]: {...state, [name]: value}};
|
|
181
|
-
this.setState(objSet);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
getModelName(f?: HTMLFormElement|null): string {
|
|
185
|
-
let f2 = f;
|
|
186
|
-
if (!f2) {
|
|
187
|
-
f2 = this.form;
|
|
188
|
-
}
|
|
189
|
-
if (f2) {
|
|
190
|
-
const a = getModelName2(f2);
|
|
191
|
-
if (a && a.length > 0) {
|
|
192
|
-
return a;
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
return 'model';
|
|
196
|
-
}
|
|
197
|
-
updateState = (e: any, callback?: () => void, lc?: Locale) => {
|
|
198
|
-
const ctrl = e.currentTarget as HTMLInputElement;
|
|
199
|
-
const modelName = this.getModelName(ctrl.form);
|
|
200
|
-
const l = localeOf(lc, this.getLocale);
|
|
201
|
-
const props = this.props;
|
|
202
|
-
handleEvent(e, this.removeErr);
|
|
203
|
-
if ((props as any).setGlobalState) {
|
|
204
|
-
handleProps(e, props as any, ctrl, modelName, l, this.prepareCustomData);
|
|
205
|
-
} else {
|
|
206
|
-
const objSet: any = buildState(e, this.state, ctrl, modelName, l);
|
|
207
|
-
if (objSet) {
|
|
208
|
-
if (callback) {
|
|
209
|
-
this.setState(objSet, callback);
|
|
210
|
-
} else {
|
|
211
|
-
this.setState(objSet);
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
updateFlatState(e: any, callback?: () => void, lc?: Locale) {
|
|
217
|
-
const l = localeOf(lc, this.getLocale);
|
|
218
|
-
const objSet: any = buildFlatState(e, this.state, l);
|
|
219
|
-
if (objSet != null) {
|
|
220
|
-
if (callback) {
|
|
221
|
-
this.setState(objSet, callback);
|
|
222
|
-
} else {
|
|
223
|
-
this.setState(objSet);
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
export interface MessageOnlyState {
|
|
229
|
-
message?: string;
|
|
230
|
-
}
|
|
231
|
-
export class MessageComponent<S extends MessageOnlyState, P> extends BaseComponent<P, S> {
|
|
232
|
-
constructor(props: P,
|
|
233
|
-
getLocale?: () => Locale,
|
|
234
|
-
removeErr?: (ctrl: HTMLInputElement) => void) {
|
|
235
|
-
super(props, getLocale, removeErr);
|
|
236
|
-
this.getModelName = this.getModelName.bind(this);
|
|
237
|
-
this.showMessage = this.showMessage.bind(this);
|
|
238
|
-
this.showError = this.showError.bind(this);
|
|
239
|
-
this.hideMessage = this.hideMessage.bind(this);
|
|
240
|
-
this.ref = React.createRef();
|
|
241
|
-
}
|
|
242
|
-
ref: any;
|
|
243
|
-
name?: string;
|
|
244
|
-
alertClass = '';
|
|
245
|
-
getModelName(f?: HTMLFormElement|null): string {
|
|
246
|
-
if (this.name && this.name.length > 0) {
|
|
247
|
-
return this.name;
|
|
248
|
-
}
|
|
249
|
-
let f2 = f;
|
|
250
|
-
if (!f2) {
|
|
251
|
-
f2 = this.form;
|
|
252
|
-
}
|
|
253
|
-
if (f2) {
|
|
254
|
-
const a = getModelName2(f2);
|
|
255
|
-
if (a && a.length > 0) {
|
|
256
|
-
return a;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
return 'model';
|
|
260
|
-
}
|
|
261
|
-
showMessage = (msg: string) => {
|
|
262
|
-
this.alertClass = 'alert alert-info';
|
|
263
|
-
this.setState({ message: msg });
|
|
264
|
-
}
|
|
265
|
-
showError = (msg: string|ErrorMessage[]) => {
|
|
266
|
-
this.alertClass = 'alert alert-error';
|
|
267
|
-
if (typeof msg === 'string') {
|
|
268
|
-
this.setState({ message: msg });
|
|
269
|
-
} else if (Array.isArray(msg) && msg.length > 0) {
|
|
270
|
-
this.setState({ message: msg[0].message });
|
|
271
|
-
} else {
|
|
272
|
-
const x = JSON.stringify(msg);
|
|
273
|
-
this.setState({ message: x });
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
hideMessage = () => {
|
|
277
|
-
this.alertClass = '';
|
|
278
|
-
this.setState({ message: '' });
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
export class BaseSearchComponent<T, F extends Filter, P, I extends SearchState<T, F>> extends BaseComponent<P, I> implements Searchable {
|
|
282
|
-
constructor(props: P,
|
|
283
|
-
protected resourceService: ResourceService,
|
|
284
|
-
protected showMessage: (msg: string) => void,
|
|
285
|
-
getLocale?: () => Locale,
|
|
286
|
-
protected ui?: UIService,
|
|
287
|
-
protected loading?: LoadingService,
|
|
288
|
-
protected listFormId?: string) {
|
|
289
|
-
super(props, getLocale, (ui ? ui.removeError : undefined));
|
|
290
|
-
this.resource = resourceService.resource();
|
|
291
|
-
this.hideFilter = true;
|
|
292
|
-
this.getModelName = this.getModelName.bind(this);
|
|
293
|
-
this.showMessage = this.showMessage.bind(this);
|
|
294
|
-
|
|
295
|
-
this.toggleFilter = this.toggleFilter.bind(this);
|
|
296
|
-
this.load = this.load.bind(this);
|
|
297
|
-
this.getSearchForm = this.getSearchForm.bind(this);
|
|
298
|
-
this.setSearchForm = this.setSearchForm.bind(this);
|
|
299
|
-
|
|
300
|
-
this.setFilter = this.setFilter.bind(this);
|
|
301
|
-
this.getFilter = this.getFilter.bind(this);
|
|
302
|
-
this.getFields = this.getFields.bind(this);
|
|
303
|
-
|
|
304
|
-
this.pageSizeChanged = this.pageSizeChanged.bind(this);
|
|
305
|
-
this.clearQ = this.clearQ.bind(this);
|
|
306
|
-
this.search = this.search.bind(this);
|
|
307
|
-
|
|
308
|
-
this.resetAndSearch = this.resetAndSearch.bind(this);
|
|
309
|
-
this.doSearch = this.doSearch.bind(this);
|
|
310
|
-
this.call = this.call.bind(this);
|
|
311
|
-
this.validateSearch = this.validateSearch.bind(this);
|
|
312
|
-
this.showResults = this.showResults.bind(this);
|
|
313
|
-
this.setList = this.setList.bind(this);
|
|
314
|
-
this.getList = this.getList.bind(this);
|
|
315
|
-
this.sort = this.sort.bind(this);
|
|
316
|
-
this.showMore = this.showMore.bind(this);
|
|
317
|
-
this.pageChanged = this.pageChanged.bind(this);
|
|
318
|
-
|
|
319
|
-
const currentUrl = window.location.host + window.location.pathname;
|
|
320
|
-
this.url = removeUrlParams(currentUrl);
|
|
321
|
-
/*
|
|
322
|
-
this.locationSearch = '';
|
|
323
|
-
const location = (props.location ? props.location : props['props'].location);
|
|
324
|
-
if (location && location.search) {
|
|
325
|
-
this.locationSearch = location.search;
|
|
326
|
-
}
|
|
327
|
-
*/
|
|
328
|
-
}
|
|
329
|
-
resource: StringMap;
|
|
330
|
-
url: string;
|
|
331
|
-
|
|
332
|
-
filter?: F;
|
|
333
|
-
// Pagination
|
|
334
|
-
initPageSize = 24;
|
|
335
|
-
pageSize = 24;
|
|
336
|
-
pageIndex?: number = 1;
|
|
337
|
-
nextPageToken?: string;
|
|
338
|
-
total = 0;
|
|
339
|
-
pages = 0;
|
|
340
|
-
showPaging?: boolean;
|
|
341
|
-
append?: boolean;
|
|
342
|
-
appendMode?: boolean;
|
|
343
|
-
appendable?: boolean;
|
|
344
|
-
|
|
345
|
-
// Sortable
|
|
346
|
-
sortField?: string;
|
|
347
|
-
sortType?: string;
|
|
348
|
-
sortTarget?: HTMLElement;
|
|
349
|
-
|
|
350
|
-
keys?: string[];
|
|
351
|
-
format?: (obj: T, locale?: Locale) => T;
|
|
352
|
-
fields?: string[];
|
|
353
|
-
initFields?: boolean;
|
|
354
|
-
sequenceNo = 'sequenceNo';
|
|
355
|
-
triggerSearch?: boolean;
|
|
356
|
-
tmpPageIndex?: number = 1;
|
|
357
|
-
|
|
358
|
-
pageMaxSize = 7;
|
|
359
|
-
pageSizes: number[] = pageSizes;
|
|
360
|
-
|
|
361
|
-
list?: T[];
|
|
362
|
-
excluding?: string[]|number[];
|
|
363
|
-
hideFilter?: boolean;
|
|
364
|
-
|
|
365
|
-
ignoreUrlParam?: boolean;
|
|
366
|
-
// locationSearch: string;
|
|
367
|
-
// _currentSortField: string;
|
|
368
|
-
|
|
369
|
-
viewable?: boolean = true;
|
|
370
|
-
addable?: boolean = true;
|
|
371
|
-
editable?: boolean = true;
|
|
372
|
-
approvable?: boolean;
|
|
373
|
-
deletable?: boolean;
|
|
374
|
-
|
|
375
|
-
getModelName(): string {
|
|
376
|
-
return 'filter';
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
toggleFilter(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
|
|
380
|
-
const x = !this.hideFilter;
|
|
381
|
-
handleToggle(event.target as HTMLInputElement, !x);
|
|
382
|
-
this.hideFilter = x;
|
|
383
|
-
}
|
|
384
|
-
load(s: F, autoSearch: boolean): void {
|
|
385
|
-
const obj2 = initFilter(s, this);
|
|
386
|
-
this.setFilter(obj2);
|
|
387
|
-
const com = this;
|
|
388
|
-
if (autoSearch) {
|
|
389
|
-
setTimeout(() => {
|
|
390
|
-
com.doSearch(true);
|
|
391
|
-
}, 0);
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
setSearchForm(form: HTMLFormElement): void {
|
|
396
|
-
this.form = form;
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
getSearchForm(): HTMLFormElement|undefined|null {
|
|
400
|
-
if (!this.form && this.listFormId) {
|
|
401
|
-
this.form = document.getElementById(this.listFormId) as HTMLFormElement;
|
|
402
|
-
}
|
|
403
|
-
return this.form;
|
|
404
|
-
}
|
|
405
|
-
setFilter(filter: F): void {
|
|
406
|
-
const modelName = this.getModelName();
|
|
407
|
-
const objSet: any = {};
|
|
408
|
-
objSet[modelName] = filter;
|
|
409
|
-
this.setState(objSet);
|
|
410
|
-
}
|
|
411
|
-
getCurrencyCode(): string|undefined {
|
|
412
|
-
return getCurrencyCode(this.form);
|
|
413
|
-
}
|
|
414
|
-
getFilter(): F {
|
|
415
|
-
const name = this.getModelName();
|
|
416
|
-
let lc: Locale|undefined;
|
|
417
|
-
if (this.getLocale) {
|
|
418
|
-
lc = this.getLocale();
|
|
419
|
-
}
|
|
420
|
-
if (!lc) {
|
|
421
|
-
lc = enLocale;
|
|
422
|
-
}
|
|
423
|
-
const fields = this.getFields();
|
|
424
|
-
const obj3 = getModel<F>(this.state, name, this, fields, this.excluding);
|
|
425
|
-
return obj3;
|
|
426
|
-
}
|
|
427
|
-
getFields(): string[]|undefined {
|
|
428
|
-
const fs = getFieldsFromForm(this.fields, this.initFields, this.form);
|
|
429
|
-
this.initFields = true;
|
|
430
|
-
return fs;
|
|
431
|
-
}
|
|
432
|
-
/*
|
|
433
|
-
protected pagingOnClick = (size, e) => {
|
|
434
|
-
this.setState(prevState => ({ isPageSizeOpenDropDown: !(prevState as any).isPageSizeOpenDropDown } as any));
|
|
435
|
-
this.pageSizeChanged(size);
|
|
436
|
-
}
|
|
437
|
-
*/
|
|
438
|
-
pageSizeOnClick = () => {
|
|
439
|
-
this.setState(prevState => ({ isPageSizeOpenDropDown: !(prevState as any).isPageSizeOpenDropDown } as any));
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
clearQ(e?: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
|
|
443
|
-
if (e) {
|
|
444
|
-
e.preventDefault();
|
|
445
|
-
}
|
|
446
|
-
const n = this.getModelName();
|
|
447
|
-
if (n && n.length > 0) {
|
|
448
|
-
const m = (this.state as any)[n];
|
|
449
|
-
if (m) {
|
|
450
|
-
m.q = '';
|
|
451
|
-
const setObj: any = {};
|
|
452
|
-
setObj[n] = m;
|
|
453
|
-
this.setState(setObj);
|
|
454
|
-
return;
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
search(event: React.MouseEvent<HTMLButtonElement, MouseEvent>): void {
|
|
459
|
-
if (event) {
|
|
460
|
-
event.preventDefault();
|
|
461
|
-
if (!this.getSearchForm()) {
|
|
462
|
-
const f = (event.target as HTMLInputElement).form;
|
|
463
|
-
if (f) {
|
|
464
|
-
this.setSearchForm(f);
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
this.resetAndSearch();
|
|
469
|
-
}
|
|
470
|
-
|
|
471
|
-
resetAndSearch(): void {
|
|
472
|
-
this.pageIndex = 1;
|
|
473
|
-
if (this.running === true) {
|
|
474
|
-
this.triggerSearch = true;
|
|
475
|
-
return;
|
|
476
|
-
}
|
|
477
|
-
reset(this);
|
|
478
|
-
this.tmpPageIndex = 1;
|
|
479
|
-
this.doSearch();
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
doSearch(isFirstLoad?: boolean): void {
|
|
483
|
-
const listForm = this.getSearchForm();
|
|
484
|
-
if (listForm && this.ui) {
|
|
485
|
-
this.ui.removeFormError(listForm);
|
|
486
|
-
}
|
|
487
|
-
const s = this.getFilter();
|
|
488
|
-
const com = this;
|
|
489
|
-
this.validateSearch(s, () => {
|
|
490
|
-
if (com.running === true) {
|
|
491
|
-
return;
|
|
492
|
-
}
|
|
493
|
-
com.running = true;
|
|
494
|
-
showLoading(this.loading);
|
|
495
|
-
if (!this.ignoreUrlParam) {
|
|
496
|
-
addParametersIntoUrl(s, isFirstLoad);
|
|
497
|
-
}
|
|
498
|
-
com.call(s);
|
|
499
|
-
});
|
|
500
|
-
}
|
|
501
|
-
|
|
502
|
-
call(s: F): void {
|
|
503
|
-
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
validateSearch(se: F, callback: () => void): void {
|
|
507
|
-
const u = this.ui;
|
|
508
|
-
const vl = (u ? u.validateForm : undefined);
|
|
509
|
-
validate(se, callback, this.getSearchForm(), localeOf(undefined, this.getLocale), vl);
|
|
510
|
-
}
|
|
511
|
-
showResults(s: F, sr: SearchResult<T>) {
|
|
512
|
-
const com = this;
|
|
513
|
-
const results = sr.list;
|
|
514
|
-
if (results && results.length > 0) {
|
|
515
|
-
const lc = localeOf(undefined, this.getLocale);
|
|
516
|
-
// formatResultsByComponent(results, com, lc);
|
|
517
|
-
formatResults(results, com.pageIndex, com.pageSize, com.pageSize, com.sequenceNo, com.format, lc);
|
|
518
|
-
}
|
|
519
|
-
const am = com.appendMode;
|
|
520
|
-
com.pageIndex = (s.page && s.page >= 1 ? s.page : 1);
|
|
521
|
-
if (sr.total) {
|
|
522
|
-
com.total = sr.total;
|
|
523
|
-
}
|
|
524
|
-
if (am) {
|
|
525
|
-
let limit = s.limit;
|
|
526
|
-
if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
|
|
527
|
-
limit = s.firstLimit;
|
|
528
|
-
}
|
|
529
|
-
com.nextPageToken = sr.next;
|
|
530
|
-
handleAppend(com, sr.list, limit, sr.next);
|
|
531
|
-
if (com.append && (s.page && s.page > 1)) {
|
|
532
|
-
com.appendList(results);
|
|
533
|
-
} else {
|
|
534
|
-
com.setList(results);
|
|
535
|
-
}
|
|
536
|
-
} else {
|
|
537
|
-
showPaging(com, sr.list, s.limit, sr.total);
|
|
538
|
-
com.setList(results);
|
|
539
|
-
com.tmpPageIndex = s.page;
|
|
540
|
-
if (s.limit) {
|
|
541
|
-
this.showMessage(buildMessage(this.resourceService, s.page, s.limit, sr.list, sr.total));
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
com.running = undefined;
|
|
545
|
-
hideLoading(com.loading);
|
|
546
|
-
if (com.triggerSearch) {
|
|
547
|
-
com.triggerSearch = undefined;
|
|
548
|
-
com.resetAndSearch();
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
appendList(results: T[]) {
|
|
553
|
-
const list = this.state.list;
|
|
554
|
-
const arr = append(list, results);
|
|
555
|
-
|
|
556
|
-
const listForm = this.getSearchForm();
|
|
557
|
-
const props: any = this.props;
|
|
558
|
-
const setGlobalState = props.props.setGlobalState;
|
|
559
|
-
if (setGlobalState && listForm) {
|
|
560
|
-
setGlobalState({ [listForm.name]: arr });
|
|
561
|
-
} else {
|
|
562
|
-
this.setState({ list: arr });
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
setList(list: T[]) {
|
|
567
|
-
const props: any = this.props;
|
|
568
|
-
const setGlobalState = props.props.setGlobalState;
|
|
569
|
-
this.list = list;
|
|
570
|
-
const listForm = this.getSearchForm();
|
|
571
|
-
if (setGlobalState && listForm) {
|
|
572
|
-
setGlobalState({ [listForm.name]: list });
|
|
573
|
-
} else {
|
|
574
|
-
this.setState({ list });
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
getList(): T[]|undefined {
|
|
579
|
-
return this.list;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
sort(event: any) {
|
|
583
|
-
event.preventDefault();
|
|
584
|
-
handleSortEvent(event, this);
|
|
585
|
-
if (!this.appendMode) {
|
|
586
|
-
this.doSearch();
|
|
587
|
-
} else {
|
|
588
|
-
this.resetAndSearch();
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
|
-
showMore(event: any) {
|
|
592
|
-
event.preventDefault();
|
|
593
|
-
this.tmpPageIndex = this.pageIndex;
|
|
594
|
-
more(this);
|
|
595
|
-
this.doSearch();
|
|
596
|
-
}
|
|
597
|
-
pageSizeChanged = (event: any) => {
|
|
598
|
-
const size = parseInt((event.currentTarget as HTMLInputElement).value, 10);
|
|
599
|
-
changePageSize(this, size);
|
|
600
|
-
this.tmpPageIndex = 1;
|
|
601
|
-
this.doSearch();
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
pageChanged(data: PageChange) {
|
|
605
|
-
const { page, size } = data;
|
|
606
|
-
changePage(this, page, size);
|
|
607
|
-
this.doSearch();
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
export class SearchComponent<T, S extends Filter, P, I extends SearchState<T, S>> extends BaseSearchComponent<T, S, P, I> {
|
|
611
|
-
constructor(props: P, sv: ((s: S, ctx?: any) => Promise<SearchResult<T>>) | SearchService<T, S>,
|
|
612
|
-
param: ResourceService|SearchParameter,
|
|
613
|
-
showMessage?: (msg: string, option?: string) => void,
|
|
614
|
-
showError?: (m: string, header?: string, detail?: string, callback?: () => void) => void,
|
|
615
|
-
getLocale?: (profile?: string) => Locale,
|
|
616
|
-
uis?: UIService,
|
|
617
|
-
loading?: LoadingService,
|
|
618
|
-
listFormId?: string) {
|
|
619
|
-
super(props, getResource(param), getMsgFunc(param, showMessage), getLocaleFunc(param, getLocale), getUIService(param, uis), getLoadingFunc(param, loading), listFormId);
|
|
620
|
-
this.autoSearch = getAutoSearch(param);
|
|
621
|
-
if (sv) {
|
|
622
|
-
if (typeof sv === 'function') {
|
|
623
|
-
const x: any = sv;
|
|
624
|
-
this.service = x;
|
|
625
|
-
} else {
|
|
626
|
-
this.service = sv.search;
|
|
627
|
-
if (sv.keys) {
|
|
628
|
-
this.keys = sv.keys();
|
|
629
|
-
}
|
|
630
|
-
}
|
|
631
|
-
}
|
|
632
|
-
this.call = this.call.bind(this);
|
|
633
|
-
this.showError = getErrorFunc(param, showError);
|
|
634
|
-
this.componentDidMount = this.componentDidMount.bind(this);
|
|
635
|
-
this.mergeFilter = this.mergeFilter.bind(this);
|
|
636
|
-
this.createFilter = this.createFilter.bind(this);
|
|
637
|
-
this.ref = React.createRef();
|
|
638
|
-
}
|
|
639
|
-
protected showError: (m: string, header?: string, detail?: string, callback?: () => void) => void;
|
|
640
|
-
protected service?: (s: S, limit?: number, offset?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
|
|
641
|
-
// protected service: SearchService<T, S>;
|
|
642
|
-
protected ref: any;
|
|
643
|
-
protected autoSearch: boolean;
|
|
644
|
-
componentDidMount() {
|
|
645
|
-
const k = (this.ui ? this.ui.registerEvents : undefined);
|
|
646
|
-
this.form = initForm(this.ref.current, k);
|
|
647
|
-
const s = this.mergeFilter(buildFromUrl<S>(), this.createFilter());
|
|
648
|
-
this.load(s, this.autoSearch);
|
|
649
|
-
}
|
|
650
|
-
mergeFilter(obj: S, b?: S, arrs?: string[]|any): S {
|
|
651
|
-
return mergeFilter2<S>(obj, b, this.pageSizes, arrs);
|
|
652
|
-
}
|
|
653
|
-
createFilter(): S {
|
|
654
|
-
const s: any = {};
|
|
655
|
-
return s;
|
|
656
|
-
}
|
|
657
|
-
call(se: S) {
|
|
658
|
-
this.running = true;
|
|
659
|
-
const s = clone(se);
|
|
660
|
-
let page = this.pageIndex;
|
|
661
|
-
if (!page || page < 1) {
|
|
662
|
-
page = 1;
|
|
663
|
-
}
|
|
664
|
-
let offset: number|undefined;
|
|
665
|
-
if (se.limit) {
|
|
666
|
-
if (se.firstLimit && se.firstLimit > 0) {
|
|
667
|
-
offset = se.limit * (page - 2) + se.firstLimit;
|
|
668
|
-
} else {
|
|
669
|
-
offset = se.limit * (page - 1);
|
|
670
|
-
}
|
|
671
|
-
}
|
|
672
|
-
const limit = (page <= 1 && se.firstLimit && se.firstLimit > 0 ? se.firstLimit : se.limit);
|
|
673
|
-
const next = (this.nextPageToken && this.nextPageToken.length > 0 ? this.nextPageToken : offset);
|
|
674
|
-
const fields = se.fields;
|
|
675
|
-
delete se['page'];
|
|
676
|
-
delete se['fields'];
|
|
677
|
-
delete se['limit'];
|
|
678
|
-
delete se['firstLimit'];
|
|
679
|
-
showLoading(this.loading);
|
|
680
|
-
const com = this;
|
|
681
|
-
if (this.service) {
|
|
682
|
-
this.service(s, limit, next, fields).then(sr => {
|
|
683
|
-
com.showResults(s, sr);
|
|
684
|
-
com.running = undefined;
|
|
685
|
-
hideLoading(com.loading);
|
|
686
|
-
}).catch(err => {
|
|
687
|
-
com.pageIndex = com.tmpPageIndex;
|
|
688
|
-
error(err, com.resourceService.value, com.showError);
|
|
689
|
-
com.running = undefined;
|
|
690
|
-
hideLoading(com.loading);
|
|
691
|
-
});
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
}
|
|
695
|
-
|
|
696
|
-
export abstract class BaseEditComponent<T, P, S> extends BaseComponent<P, S> {
|
|
697
|
-
constructor(props: P,
|
|
698
|
-
protected resourceService: ResourceService,
|
|
699
|
-
protected showMessage: (msg: string) => void,
|
|
700
|
-
protected showError: (m: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
701
|
-
protected confirm: (m2: string, header: string, yesCallback?: () => void, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void,
|
|
702
|
-
getLocale?: () => Locale,
|
|
703
|
-
protected ui?: UIService,
|
|
704
|
-
protected loading?: LoadingService,
|
|
705
|
-
// status?: EditStatusConfig,
|
|
706
|
-
patchable?: boolean, backOnSaveSuccess?: boolean) {
|
|
707
|
-
super(props, getLocale, (ui ? ui.removeError : undefined));
|
|
708
|
-
this.resource = resourceService.resource();
|
|
709
|
-
// this.status = createEditStatus(status);
|
|
710
|
-
if (patchable === false) {
|
|
711
|
-
this.patchable = patchable;
|
|
712
|
-
}
|
|
713
|
-
if (backOnSaveSuccess === false) {
|
|
714
|
-
this.backOnSuccess = backOnSaveSuccess;
|
|
715
|
-
}
|
|
716
|
-
this.insertSuccessMsg = resourceService.value('msg_save_success');
|
|
717
|
-
this.updateSuccessMsg = resourceService.value('msg_save_success');
|
|
718
|
-
|
|
719
|
-
this.showMessage = this.showMessage.bind(this);
|
|
720
|
-
this.showError = this.showError.bind(this);
|
|
721
|
-
this.confirm = this.confirm.bind(this);
|
|
722
|
-
|
|
723
|
-
this.back = this.back.bind(this);
|
|
724
|
-
this.getModelName = this.getModelName.bind(this);
|
|
725
|
-
|
|
726
|
-
this.resetState = this.resetState.bind(this);
|
|
727
|
-
this.handleNotFound = this.handleNotFound.bind(this);
|
|
728
|
-
this.showModel = this.showModel.bind(this);
|
|
729
|
-
this.getModel = this.getModel.bind(this);
|
|
730
|
-
this.createModel = this.createModel.bind(this);
|
|
731
|
-
|
|
732
|
-
this.create = this.create.bind(this);
|
|
733
|
-
this.save = this.save.bind(this);
|
|
734
|
-
this.onSave = this.onSave.bind(this);
|
|
735
|
-
this.validate = this.validate.bind(this);
|
|
736
|
-
this.doSave = this.doSave.bind(this);
|
|
737
|
-
this.succeed = this.succeed.bind(this);
|
|
738
|
-
this.fail = this.fail.bind(this);
|
|
739
|
-
this.postSave = this.postSave.bind(this);
|
|
740
|
-
this.handleDuplicateKey = this.handleDuplicateKey.bind(this);
|
|
741
|
-
}
|
|
742
|
-
// status: EditStatusConfig;
|
|
743
|
-
protected name?: string;
|
|
744
|
-
protected backOnSuccess = true;
|
|
745
|
-
protected resource: StringMap;
|
|
746
|
-
protected metadata?: Attributes;
|
|
747
|
-
protected keys?: string[];
|
|
748
|
-
protected version?: string;
|
|
749
|
-
protected newMode?: boolean;
|
|
750
|
-
protected setBack?: boolean;
|
|
751
|
-
protected patchable = true;
|
|
752
|
-
protected orginalModel?: T;
|
|
753
|
-
|
|
754
|
-
addable?: boolean = true;
|
|
755
|
-
readOnly?: boolean;
|
|
756
|
-
deletable?: boolean;
|
|
757
|
-
|
|
758
|
-
insertSuccessMsg: string;
|
|
759
|
-
updateSuccessMsg: string;
|
|
760
|
-
back(event: any) {
|
|
761
|
-
if (event) {
|
|
762
|
-
event.preventDefault();
|
|
763
|
-
}
|
|
764
|
-
}
|
|
765
|
-
resetState(newMod: boolean, model: T, originalModel?: T) {
|
|
766
|
-
this.newMode = newMod;
|
|
767
|
-
this.orginalModel = originalModel;
|
|
768
|
-
this.showModel(model);
|
|
769
|
-
}
|
|
770
|
-
handleNotFound(form?: HTMLFormElement|null): void {
|
|
771
|
-
const msg = message(this.resourceService.value, 'error_not_found', 'error');
|
|
772
|
-
if (form) {
|
|
773
|
-
readOnly(form);
|
|
774
|
-
}
|
|
775
|
-
this.showError(msg.message, msg.title);
|
|
776
|
-
}
|
|
777
|
-
getModelName(f?: HTMLFormElement): string {
|
|
778
|
-
if (this.name && this.name.length > 0) {
|
|
779
|
-
return this.name;
|
|
780
|
-
}
|
|
781
|
-
return super.getModelName(f);
|
|
782
|
-
}
|
|
783
|
-
getModel(): T {
|
|
784
|
-
const n = this.getModelName();
|
|
785
|
-
return (this.props as any)[n] || (this.state as any)[n];
|
|
786
|
-
}
|
|
787
|
-
showModel(model: T) {
|
|
788
|
-
const f = this.form;
|
|
789
|
-
const modelName = this.getModelName();
|
|
790
|
-
const objSet: any = {};
|
|
791
|
-
objSet[modelName] = model;
|
|
792
|
-
this.setState(objSet, () => {
|
|
793
|
-
if (this.readOnly) {
|
|
794
|
-
readOnly(f);
|
|
795
|
-
}
|
|
796
|
-
});
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
// end of: can be in ViewComponent
|
|
800
|
-
createModel(): T {
|
|
801
|
-
if (this.metadata) {
|
|
802
|
-
const obj = createModel2<T>(this.metadata);
|
|
803
|
-
return obj;
|
|
804
|
-
} else {
|
|
805
|
-
const obj: any = {};
|
|
806
|
-
return obj;
|
|
807
|
-
}
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
create = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
811
|
-
if (event) {
|
|
812
|
-
event.preventDefault();
|
|
813
|
-
}
|
|
814
|
-
if (!this.form && event && event.target && (event.target as HTMLInputElement).form) {
|
|
815
|
-
this.form = (event.target as HTMLInputElement).form;
|
|
816
|
-
}
|
|
817
|
-
const obj = this.createModel();
|
|
818
|
-
this.resetState(true, obj, undefined);
|
|
819
|
-
const u = this.ui;
|
|
820
|
-
const f = this.form;
|
|
821
|
-
if (u && f) {
|
|
822
|
-
setTimeout(() => {
|
|
823
|
-
u.removeFormError(f);
|
|
824
|
-
}, 100);
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
save = (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
|
|
828
|
-
event.preventDefault();
|
|
829
|
-
event.persist();
|
|
830
|
-
if (!this.form && event && event.target) {
|
|
831
|
-
this.form = (event.target as HTMLInputElement).form;
|
|
832
|
-
}
|
|
833
|
-
this.onSave(this.backOnSuccess);
|
|
834
|
-
}
|
|
835
|
-
onSave(isBack?: boolean) {
|
|
836
|
-
const r = this.resourceService;
|
|
837
|
-
if (this.newMode && !this.addable) {
|
|
838
|
-
const m = message(r.value, 'error_permission_add', 'error_permission');
|
|
839
|
-
this.showError(m.message, m.title);
|
|
840
|
-
return;
|
|
841
|
-
} else if (!this.newMode && this.readOnly) {
|
|
842
|
-
const msg = message(r.value, 'error_permission_edit', 'error_permission');
|
|
843
|
-
this.showError(msg.message, msg.title);
|
|
844
|
-
return;
|
|
845
|
-
} else {
|
|
846
|
-
if (this.running) {
|
|
847
|
-
return;
|
|
848
|
-
}
|
|
849
|
-
const com = this;
|
|
850
|
-
const obj = com.getModel();
|
|
851
|
-
if (this.newMode) {
|
|
852
|
-
com.validate(obj, () => {
|
|
853
|
-
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
854
|
-
this.confirm(msg.message, msg.title, () => {
|
|
855
|
-
com.doSave(obj, obj, isBack);
|
|
856
|
-
}, msg.no, msg.yes);
|
|
857
|
-
});
|
|
858
|
-
} else {
|
|
859
|
-
const diffObj = makeDiff(initPropertyNullInModel(this.orginalModel, this.metadata), obj, this.keys, this.version);
|
|
860
|
-
const keys = Object.keys(diffObj as any);
|
|
861
|
-
if (keys.length === 0) {
|
|
862
|
-
this.showMessage(r.value('msg_no_change'));
|
|
863
|
-
} else {
|
|
864
|
-
com.validate(obj, () => {
|
|
865
|
-
const msg = message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
|
|
866
|
-
this.confirm(msg.message, msg.title, () => {
|
|
867
|
-
com.doSave(obj, diffObj, isBack);
|
|
868
|
-
}, msg.no, msg.yes);
|
|
869
|
-
});
|
|
870
|
-
}
|
|
871
|
-
}
|
|
872
|
-
}
|
|
873
|
-
}
|
|
874
|
-
validate(obj: T, callback: (obj2?: T) => void) {
|
|
875
|
-
if (this.ui && this.form) {
|
|
876
|
-
const valid = this.ui.validateForm(this.form, localeOf(undefined, this.getLocale));
|
|
877
|
-
if (valid) {
|
|
878
|
-
callback(obj);
|
|
879
|
-
}
|
|
880
|
-
} else {
|
|
881
|
-
callback(obj);
|
|
882
|
-
}
|
|
883
|
-
}
|
|
884
|
-
|
|
885
|
-
doSave(obj: T, dif?: Partial<T>, isBack?: boolean) {
|
|
886
|
-
}
|
|
887
|
-
|
|
888
|
-
succeed(msg: string, origin: T, isBack?: boolean, model?: T) {
|
|
889
|
-
if (model) {
|
|
890
|
-
this.newMode = false;
|
|
891
|
-
if (model && this.setBack) {
|
|
892
|
-
this.resetState(false, model, clone(model));
|
|
893
|
-
} else {
|
|
894
|
-
handleVersion(origin, this.version);
|
|
895
|
-
}
|
|
896
|
-
} else {
|
|
897
|
-
handleVersion(origin, this.version);
|
|
898
|
-
}
|
|
899
|
-
const isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
|
|
900
|
-
this.showMessage(msg);
|
|
901
|
-
if (isBackO) {
|
|
902
|
-
this.back(null);
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
fail(result: ErrorMessage[]) {
|
|
906
|
-
const f = this.form;
|
|
907
|
-
const u = this.ui;
|
|
908
|
-
if (u && f) {
|
|
909
|
-
const unmappedErrors = u.showFormError(f, result);
|
|
910
|
-
focusFirstError(f);
|
|
911
|
-
if (unmappedErrors && unmappedErrors.length > 0) {
|
|
912
|
-
const t = this.resourceService.value('error');
|
|
913
|
-
if (u && u.buildErrorMessage) {
|
|
914
|
-
const msg = u.buildErrorMessage(unmappedErrors);
|
|
915
|
-
this.showError(msg, t);
|
|
916
|
-
} else {
|
|
917
|
-
this.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
|
|
918
|
-
}
|
|
919
|
-
}
|
|
920
|
-
} else {
|
|
921
|
-
const t = this.resourceService.value('error');
|
|
922
|
-
if (result.length > 0) {
|
|
923
|
-
this.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
|
|
924
|
-
} else {
|
|
925
|
-
this.showError(t, t);
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
|
|
930
|
-
postSave(res: number|string|T|ErrorMessage[], origin: T, isPatch: boolean, backOnSave?: boolean) {
|
|
931
|
-
this.running = false;
|
|
932
|
-
hideLoading(this.loading);
|
|
933
|
-
// const st = this.status;
|
|
934
|
-
const newMod = this.newMode;
|
|
935
|
-
const successMsg = (newMod ? this.insertSuccessMsg : this.updateSuccessMsg);
|
|
936
|
-
const x: any = res;
|
|
937
|
-
const r = this.resourceService;
|
|
938
|
-
if (Array.isArray(x)) {
|
|
939
|
-
this.fail(x);
|
|
940
|
-
} else if (!isNaN(x)) {
|
|
941
|
-
if (x > 0) {
|
|
942
|
-
this.succeed(successMsg, origin, backOnSave);
|
|
943
|
-
} else {
|
|
944
|
-
if (newMod && x <= 0) {
|
|
945
|
-
this.handleDuplicateKey();
|
|
946
|
-
} else if (!newMod && x === 0) {
|
|
947
|
-
this.handleNotFound();
|
|
948
|
-
} else {
|
|
949
|
-
this.showError(r.value('error_version'), r.value('error'));
|
|
950
|
-
}
|
|
951
|
-
}
|
|
952
|
-
} else {
|
|
953
|
-
const result: T = x;
|
|
954
|
-
if (isPatch) {
|
|
955
|
-
const keys = Object.keys(result as any);
|
|
956
|
-
const a: any = origin;
|
|
957
|
-
for (const k of keys) {
|
|
958
|
-
a[k] = (result as any)[k];
|
|
959
|
-
}
|
|
960
|
-
this.succeed(successMsg, a, backOnSave);
|
|
961
|
-
} else {
|
|
962
|
-
this.succeed(successMsg, origin, backOnSave, result);
|
|
963
|
-
}
|
|
964
|
-
this.showMessage(successMsg);
|
|
965
|
-
}
|
|
966
|
-
}
|
|
967
|
-
handleDuplicateKey(result?: T) {
|
|
968
|
-
const msg = message(this.resourceService.value, 'error_duplicate_key', 'error');
|
|
969
|
-
this.showError(msg.message, msg.title);
|
|
970
|
-
}
|
|
971
|
-
}
|
|
972
|
-
export class EditComponent<T, ID, P, S> extends BaseEditComponent<T, P, S> {
|
|
973
|
-
constructor(props: P, protected service: GenericService<T, ID, number|T|ErrorMessage[]>,
|
|
974
|
-
param: ResourceService|EditParameter,
|
|
975
|
-
showMessage?: (msg: string, option?: string) => void,
|
|
976
|
-
showError?: (m: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
977
|
-
confirm?: (m2: string, header: string, yesCallback?: () => void, btnLeftText?: string, btnRightText?: string, noCallback?: () => void) => void,
|
|
978
|
-
getLocale?: (profile?: string) => Locale,
|
|
979
|
-
uis?: UIService,
|
|
980
|
-
loading?: LoadingService, patchable?: boolean, backOnSaveSuccess?: boolean) {
|
|
981
|
-
super(props, getResource(param), getMsgFunc(param, showMessage), getErrorFunc(param, showError), getConfirmFunc(param, confirm), getLocaleFunc(param, getLocale), getUIService(param, uis), getLoadingFunc(param, loading), patchable, backOnSaveSuccess);
|
|
982
|
-
if (service.metadata) {
|
|
983
|
-
const metadata = service.metadata();
|
|
984
|
-
if (metadata) {
|
|
985
|
-
const meta = build(metadata);
|
|
986
|
-
if (meta) {
|
|
987
|
-
this.keys = meta.keys;
|
|
988
|
-
this.version = meta.version;
|
|
989
|
-
}
|
|
990
|
-
this.metadata = metadata;
|
|
991
|
-
}
|
|
992
|
-
}
|
|
993
|
-
if (!this.keys && service.keys) {
|
|
994
|
-
const k = service.keys();
|
|
995
|
-
if (k) {
|
|
996
|
-
this.keys = k;
|
|
997
|
-
}
|
|
998
|
-
}
|
|
999
|
-
if (!this.keys) {
|
|
1000
|
-
this.keys = [];
|
|
1001
|
-
}
|
|
1002
|
-
this.load = this.load.bind(this);
|
|
1003
|
-
this.doSave = this.doSave.bind(this);
|
|
1004
|
-
this.ref = React.createRef();
|
|
1005
|
-
}
|
|
1006
|
-
ref: any;
|
|
1007
|
-
load(_id: ID|null, callback?: (m: T, showM: (m2: T) => void) => void) {
|
|
1008
|
-
const id: any = _id;
|
|
1009
|
-
if (id != null && id !== '') {
|
|
1010
|
-
const com = this;
|
|
1011
|
-
this.running = true;
|
|
1012
|
-
showLoading(com.loading);
|
|
1013
|
-
this.service.load(id).then(obj => {
|
|
1014
|
-
if (!obj) {
|
|
1015
|
-
com.handleNotFound(this.form);
|
|
1016
|
-
} else {
|
|
1017
|
-
com.newMode = false;
|
|
1018
|
-
com.orginalModel = clone(obj);
|
|
1019
|
-
if (!callback) {
|
|
1020
|
-
com.showModel(obj);
|
|
1021
|
-
} else {
|
|
1022
|
-
callback(obj, com.showModel);
|
|
1023
|
-
}
|
|
1024
|
-
}
|
|
1025
|
-
com.running = false;
|
|
1026
|
-
hideLoading(com.loading);
|
|
1027
|
-
}).catch(err => {
|
|
1028
|
-
const data = (err && err.response) ? err.response : err;
|
|
1029
|
-
const r = com.resourceService;
|
|
1030
|
-
const gv = r.value;
|
|
1031
|
-
const title = gv('error');
|
|
1032
|
-
let msg = gv('error_internal');
|
|
1033
|
-
if (data && data.status === 404) {
|
|
1034
|
-
com.handleNotFound(com.form);
|
|
1035
|
-
} else {
|
|
1036
|
-
if (data.status && !isNaN(data.status)) {
|
|
1037
|
-
msg = messageByHttpStatus(data.status, gv);
|
|
1038
|
-
}
|
|
1039
|
-
if (data && (data.status === 401 || data.status === 403)) {
|
|
1040
|
-
readOnly(com.form);
|
|
1041
|
-
}
|
|
1042
|
-
com.showError(msg, title);
|
|
1043
|
-
}
|
|
1044
|
-
com.running = false;
|
|
1045
|
-
hideLoading(com.loading);
|
|
1046
|
-
});
|
|
1047
|
-
} else {
|
|
1048
|
-
// Call service state
|
|
1049
|
-
this.newMode = true;
|
|
1050
|
-
this.orginalModel = undefined;
|
|
1051
|
-
const obj = this.createModel();
|
|
1052
|
-
if (callback) {
|
|
1053
|
-
callback(obj, this.showModel);
|
|
1054
|
-
} else {
|
|
1055
|
-
this.showModel(obj);
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1059
|
-
doSave(obj: T, body?: Partial<T>, isBack?: boolean) {
|
|
1060
|
-
this.running = true;
|
|
1061
|
-
showLoading(this.loading);
|
|
1062
|
-
const isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
|
|
1063
|
-
const com = this;
|
|
1064
|
-
let m: T|Partial<T> = obj;
|
|
1065
|
-
let fn = this.newMode ? this.service.insert : this.service.update;
|
|
1066
|
-
if (!this.newMode) {
|
|
1067
|
-
if (this.patchable === true && this.service.patch && body && Object.keys(body).length > 0) {
|
|
1068
|
-
m = body;
|
|
1069
|
-
fn = this.service.patch;
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
fn(m as any).then(result => {
|
|
1073
|
-
com.postSave(result, obj, isBackO);
|
|
1074
|
-
com.running = false;
|
|
1075
|
-
hideLoading(com.loading);
|
|
1076
|
-
}).then(err => {
|
|
1077
|
-
error(err, com.resourceService.value, com.showError);
|
|
1078
|
-
com.running = false;
|
|
1079
|
-
hideLoading(com.loading);
|
|
1080
|
-
});
|
|
1081
|
-
}
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
export class BaseDiffApprComponent<T, ID, P, S extends BaseDiffState> extends React.Component<P, S & any> {
|
|
1085
|
-
constructor(props: P, protected keys: string[], protected resourceService: ResourceService,
|
|
1086
|
-
protected showMessage: (msg: string, option?: string) => void,
|
|
1087
|
-
protected showError: (m: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
1088
|
-
protected loading?: LoadingService,
|
|
1089
|
-
// status?: DiffStatusConfig,
|
|
1090
|
-
) {
|
|
1091
|
-
super(props);
|
|
1092
|
-
// this._id = props['props'].match.params.id || props['props'].match.params.cId || props.match.params.cId;
|
|
1093
|
-
// this.callBackAfterUpdate = this.callBackAfterUpdate.bind(this);
|
|
1094
|
-
this.resource = resourceService.resource();
|
|
1095
|
-
this.showMessage = this.showMessage.bind(this);
|
|
1096
|
-
this.showError = this.showError.bind(this);
|
|
1097
|
-
this.initModel = this.initModel.bind(this);
|
|
1098
|
-
this.postApprove = this.postApprove.bind(this);
|
|
1099
|
-
this.postReject = this.postReject.bind(this);
|
|
1100
|
-
this.format = this.format.bind(this);
|
|
1101
|
-
this.handleNotFound = this.handleNotFound.bind(this);
|
|
1102
|
-
// this.status = createDiffStatus(status);
|
|
1103
|
-
this.state = {
|
|
1104
|
-
disabled: false
|
|
1105
|
-
};
|
|
1106
|
-
}
|
|
1107
|
-
// status: DiffStatusConfig;
|
|
1108
|
-
id?: ID;
|
|
1109
|
-
form?: HTMLFormElement;
|
|
1110
|
-
running?: boolean;
|
|
1111
|
-
resource: StringMap;
|
|
1112
|
-
|
|
1113
|
-
initModel(): T {
|
|
1114
|
-
const x: any = {};
|
|
1115
|
-
return x;
|
|
1116
|
-
}
|
|
1117
|
-
|
|
1118
|
-
postApprove(s: number|string, err?: any) {
|
|
1119
|
-
this.setState({ disabled: true });
|
|
1120
|
-
const r = this.resourceService;
|
|
1121
|
-
// const st = this.status;
|
|
1122
|
-
if (s > 0) {
|
|
1123
|
-
this.showMessage(r.value('msg_approve_success'));
|
|
1124
|
-
} else if (s === 0) {
|
|
1125
|
-
this.handleNotFound();
|
|
1126
|
-
} else {
|
|
1127
|
-
const msg = message(r.value, 'msg_approve_version_error', 'error');
|
|
1128
|
-
this.showError(msg.message, msg.title);
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
|
|
1132
|
-
postReject(status: number|string, err?: any) {
|
|
1133
|
-
this.setState({ disabled: true });
|
|
1134
|
-
const r = this.resourceService;
|
|
1135
|
-
// const st = this.status;
|
|
1136
|
-
if (status > 0) {
|
|
1137
|
-
this.showMessage(r.value('msg_reject_success'));
|
|
1138
|
-
} else if (status === 0) {
|
|
1139
|
-
this.handleNotFound();
|
|
1140
|
-
} else {
|
|
1141
|
-
const msg = message(r.value, 'msg_approve_version_error', 'error');
|
|
1142
|
-
this.showError(msg.message, msg.title);
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
|
|
1146
|
-
format() {
|
|
1147
|
-
const p = this.props as any;
|
|
1148
|
-
const diffModel = p['diffModel'];
|
|
1149
|
-
if (diffModel) {
|
|
1150
|
-
const differentKeys = diff(diffModel.origin, diffModel.value);
|
|
1151
|
-
const dataFields = getDataFields(this.form);
|
|
1152
|
-
dataFields.forEach(e => {
|
|
1153
|
-
const x = e.getAttribute('data-field');
|
|
1154
|
-
if (x) {
|
|
1155
|
-
if (differentKeys.indexOf(x) >= 0) {
|
|
1156
|
-
if (e.childNodes.length === 3) {
|
|
1157
|
-
(e.childNodes[1] as HTMLElement).classList.add('highlight');
|
|
1158
|
-
(e.childNodes[2] as HTMLElement).classList.add('highlight');
|
|
1159
|
-
} else {
|
|
1160
|
-
e.classList.add('highlight');
|
|
1161
|
-
}
|
|
1162
|
-
}
|
|
1163
|
-
}
|
|
1164
|
-
});
|
|
1165
|
-
} else {
|
|
1166
|
-
const { origin, value } = this.state;
|
|
1167
|
-
const differentKeys = diff(origin, value);
|
|
1168
|
-
const dataFields = getDataFields(this.form);
|
|
1169
|
-
dataFields.forEach(e => {
|
|
1170
|
-
const x = e.getAttribute('data-field');
|
|
1171
|
-
if (x) {
|
|
1172
|
-
if (differentKeys.indexOf(x) >= 0) {
|
|
1173
|
-
if (e.childNodes.length === 3) {
|
|
1174
|
-
(e.childNodes[1] as HTMLElement).classList.add('highlight');
|
|
1175
|
-
(e.childNodes[2] as HTMLElement).classList.add('highlight');
|
|
1176
|
-
} else {
|
|
1177
|
-
e.classList.add('highlight');
|
|
1178
|
-
}
|
|
1179
|
-
}
|
|
1180
|
-
}
|
|
1181
|
-
});
|
|
1182
|
-
}
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
handleNotFound() {
|
|
1186
|
-
this.setState({ disabled: true });
|
|
1187
|
-
const msg = message(this.resourceService.value, 'error_not_found', 'error');
|
|
1188
|
-
this.showError(msg.message, msg.title);
|
|
1189
|
-
}
|
|
1190
|
-
}
|
|
1191
|
-
export class DiffApprComponent<T, ID, P, S extends DiffState<T>> extends BaseDiffApprComponent<T, ID, P, S> {
|
|
1192
|
-
constructor(props: P, protected service: DiffApprService<T, ID>,
|
|
1193
|
-
param: ResourceService|DiffParameter,
|
|
1194
|
-
showMessage?: (msg: string, option?: string) => void,
|
|
1195
|
-
showError?: (m: string, title?: string, detail?: string, callback?: () => void) => void,
|
|
1196
|
-
loading?: LoadingService) {
|
|
1197
|
-
super(props, service.keys(), getResource(param), getMsgFunc(param, showMessage), getErrorFunc(param, showError), getLoadingFunc(param, loading));
|
|
1198
|
-
this.approve = this.approve.bind(this);
|
|
1199
|
-
this.reject = this.reject.bind(this);
|
|
1200
|
-
this.formatFields = this.formatFields.bind(this);
|
|
1201
|
-
this.ref = React.createRef();
|
|
1202
|
-
this.state = {
|
|
1203
|
-
origin: this.initModel(),
|
|
1204
|
-
value: this.initModel(),
|
|
1205
|
-
disabled: false,
|
|
1206
|
-
};
|
|
1207
|
-
}
|
|
1208
|
-
ref: any;
|
|
1209
|
-
|
|
1210
|
-
formatFields(value: T): T {
|
|
1211
|
-
return value;
|
|
1212
|
-
}
|
|
1213
|
-
|
|
1214
|
-
load(_id: ID) {
|
|
1215
|
-
const id: any = _id;
|
|
1216
|
-
if (id != null && id !== '') {
|
|
1217
|
-
this.id = _id;
|
|
1218
|
-
const com = this;
|
|
1219
|
-
this.running = true;
|
|
1220
|
-
showLoading(this.loading);
|
|
1221
|
-
this.service.diff(id).then(dobj => {
|
|
1222
|
-
if (!dobj) {
|
|
1223
|
-
com.handleNotFound();
|
|
1224
|
-
} else {
|
|
1225
|
-
const formatdDiff = formatDiffModel(dobj, com.formatFields);
|
|
1226
|
-
com.setState({
|
|
1227
|
-
origin: formatdDiff.origin,
|
|
1228
|
-
value: formatdDiff.value
|
|
1229
|
-
}, com.format);
|
|
1230
|
-
}
|
|
1231
|
-
com.running = false;
|
|
1232
|
-
hideLoading(com.loading);
|
|
1233
|
-
}).catch(err => {
|
|
1234
|
-
const data = (err && err.response) ? err.response : err;
|
|
1235
|
-
if (data && data.status === 404) {
|
|
1236
|
-
com.handleNotFound();
|
|
1237
|
-
} else {
|
|
1238
|
-
error(err, com.resourceService.value, com.showError);
|
|
1239
|
-
}
|
|
1240
|
-
com.running = false;
|
|
1241
|
-
hideLoading(com.loading);
|
|
1242
|
-
});
|
|
1243
|
-
}
|
|
1244
|
-
}
|
|
1245
|
-
|
|
1246
|
-
approve(event: any) {
|
|
1247
|
-
event.preventDefault();
|
|
1248
|
-
const com = this;
|
|
1249
|
-
this.running = true;
|
|
1250
|
-
showLoading(this.loading);
|
|
1251
|
-
if (this.id) {
|
|
1252
|
-
this.service.approve(this.id).then(status => {
|
|
1253
|
-
com.postApprove(status, null);
|
|
1254
|
-
com.running = false;
|
|
1255
|
-
hideLoading(com.loading);
|
|
1256
|
-
}).catch(err => {
|
|
1257
|
-
com.postApprove(4, err);
|
|
1258
|
-
com.running = false;
|
|
1259
|
-
hideLoading(com.loading);
|
|
1260
|
-
});
|
|
1261
|
-
}
|
|
1262
|
-
}
|
|
1263
|
-
|
|
1264
|
-
reject(event: any) {
|
|
1265
|
-
event.preventDefault();
|
|
1266
|
-
const com = this;
|
|
1267
|
-
this.running = true;
|
|
1268
|
-
showLoading(this.loading);
|
|
1269
|
-
if (this.id) {
|
|
1270
|
-
this.service.reject(this.id).then(status => {
|
|
1271
|
-
com.postReject(status, null);
|
|
1272
|
-
com.running = false;
|
|
1273
|
-
hideLoading(com.loading);
|
|
1274
|
-
}).catch(err => {
|
|
1275
|
-
com.postReject(4, err);
|
|
1276
|
-
com.running = false;
|
|
1277
|
-
hideLoading(com.loading);
|
|
1278
|
-
});
|
|
1279
|
-
}
|
|
1280
|
-
}
|
|
1281
|
-
}
|
|
1282
|
-
export function removeUrlParams(url: string): string {
|
|
1283
|
-
const startParams = url.indexOf('?');
|
|
1284
|
-
return startParams !== -1 ? url.substring(0, startParams) : url;
|
|
1285
|
-
}
|