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