react-hook-core 0.2.1 → 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,1199 +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.setReadOnly(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.pageSizeOnClick = function () {
304
- _this.setState(function (prevState) { return ({ isPageSizeOpenDropDown: !prevState.isPageSizeOpenDropDown }); });
305
- };
306
- _this.pageSizeChanged = function (event) {
307
- var size = parseInt(event.currentTarget.value, 10);
308
- search_1.changePageSize(_this, size);
309
- _this.tmpPageIndex = 1;
310
- _this.doSearch();
311
- };
312
- _this.resource = resourceService.resource();
313
- _this.hideFilter = true;
314
- _this.getModelName = _this.getModelName.bind(_this);
315
- _this.showMessage = _this.showMessage.bind(_this);
316
- _this.toggleFilter = _this.toggleFilter.bind(_this);
317
- _this.load = _this.load.bind(_this);
318
- _this.getSearchForm = _this.getSearchForm.bind(_this);
319
- _this.setSearchForm = _this.setSearchForm.bind(_this);
320
- _this.setFilter = _this.setFilter.bind(_this);
321
- _this.getFilter = _this.getFilter.bind(_this);
322
- _this.getFields = _this.getFields.bind(_this);
323
- _this.pageSizeChanged = _this.pageSizeChanged.bind(_this);
324
- _this.clearQ = _this.clearQ.bind(_this);
325
- _this.search = _this.search.bind(_this);
326
- _this.resetAndSearch = _this.resetAndSearch.bind(_this);
327
- _this.doSearch = _this.doSearch.bind(_this);
328
- _this.call = _this.call.bind(_this);
329
- _this.validateSearch = _this.validateSearch.bind(_this);
330
- _this.showResults = _this.showResults.bind(_this);
331
- _this.setList = _this.setList.bind(_this);
332
- _this.getList = _this.getList.bind(_this);
333
- _this.sort = _this.sort.bind(_this);
334
- _this.showMore = _this.showMore.bind(_this);
335
- _this.pageChanged = _this.pageChanged.bind(_this);
336
- var currentUrl = window.location.host + window.location.pathname;
337
- _this.url = removeUrlParams(currentUrl);
338
- return _this;
339
- }
340
- BaseSearchComponent.prototype.getModelName = function () {
341
- return 'filter';
342
- };
343
- BaseSearchComponent.prototype.toggleFilter = function (event) {
344
- var x = !this.hideFilter;
345
- core_1.handleToggle(event.target, !x);
346
- this.hideFilter = x;
347
- };
348
- BaseSearchComponent.prototype.load = function (s, autoSearch) {
349
- var obj2 = search_1.initFilter(s, this);
350
- this.setFilter(obj2);
351
- var com = this;
352
- if (autoSearch) {
353
- setTimeout(function () {
354
- com.doSearch(true);
355
- }, 0);
356
- }
357
- };
358
- BaseSearchComponent.prototype.setSearchForm = function (form) {
359
- this.form = form;
360
- };
361
- BaseSearchComponent.prototype.getSearchForm = function () {
362
- if (!this.form && this.listFormId) {
363
- this.form = document.getElementById(this.listFormId);
364
- }
365
- return this.form;
366
- };
367
- BaseSearchComponent.prototype.setFilter = function (filter) {
368
- var modelName = this.getModelName();
369
- var objSet = {};
370
- objSet[modelName] = filter;
371
- this.setState(objSet);
372
- };
373
- BaseSearchComponent.prototype.getCurrencyCode = function () {
374
- return core_2.getCurrencyCode(this.form);
375
- };
376
- BaseSearchComponent.prototype.getFilter = function () {
377
- var name = this.getModelName();
378
- var lc;
379
- if (this.getLocale) {
380
- lc = this.getLocale();
381
- }
382
- if (!lc) {
383
- lc = state_1.enLocale;
384
- }
385
- var fields = this.getFields();
386
- var obj3 = search_1.getModel(this.state, name, this, fields, this.excluding);
387
- return obj3;
388
- };
389
- BaseSearchComponent.prototype.getFields = function () {
390
- var fs = search_1.getFieldsFromForm(this.fields, this.initFields, this.form);
391
- this.initFields = true;
392
- return fs;
393
- };
394
- BaseSearchComponent.prototype.clearQ = function (e) {
395
- if (e) {
396
- e.preventDefault();
397
- }
398
- var n = this.getModelName();
399
- if (n && n.length > 0) {
400
- var m = this.state[n];
401
- if (m) {
402
- m.q = '';
403
- var setObj = {};
404
- setObj[n] = m;
405
- this.setState(setObj);
406
- return;
407
- }
408
- }
409
- };
410
- BaseSearchComponent.prototype.search = function (event) {
411
- if (event) {
412
- event.preventDefault();
413
- if (!this.getSearchForm()) {
414
- var f = event.target.form;
415
- if (f) {
416
- this.setSearchForm(f);
417
- }
418
- }
419
- }
420
- this.resetAndSearch();
421
- };
422
- BaseSearchComponent.prototype.resetAndSearch = function () {
423
- this.pageIndex = 1;
424
- if (this.running === true) {
425
- this.triggerSearch = true;
426
- return;
427
- }
428
- search_1.reset(this);
429
- this.tmpPageIndex = 1;
430
- this.doSearch();
431
- };
432
- BaseSearchComponent.prototype.doSearch = function (isFirstLoad) {
433
- var _this = this;
434
- var listForm = this.getSearchForm();
435
- if (listForm && this.ui) {
436
- this.ui.removeFormError(listForm);
437
- }
438
- var s = this.getFilter();
439
- var com = this;
440
- this.validateSearch(s, function () {
441
- if (com.running === true) {
442
- return;
443
- }
444
- com.running = true;
445
- core_1.showLoading(_this.loading);
446
- if (!_this.ignoreUrlParam) {
447
- search_1.addParametersIntoUrl(s, isFirstLoad);
448
- }
449
- com.call(s);
450
- });
451
- };
452
- BaseSearchComponent.prototype.call = function (s) {
453
- };
454
- BaseSearchComponent.prototype.validateSearch = function (se, callback) {
455
- var u = this.ui;
456
- var vl = (u ? u.validateForm : undefined);
457
- search_1.validate(se, callback, this.getSearchForm(), state_1.localeOf(undefined, this.getLocale), vl);
458
- };
459
- BaseSearchComponent.prototype.showResults = function (s, sr) {
460
- var com = this;
461
- var results = sr.list;
462
- if (results && results.length > 0) {
463
- var lc = state_1.localeOf(undefined, this.getLocale);
464
- search_1.formatResults(results, com.pageIndex, com.pageSize, com.pageSize, com.sequenceNo, com.format, lc);
465
- }
466
- var am = com.appendMode;
467
- com.pageIndex = (s.page && s.page >= 1 ? s.page : 1);
468
- if (sr.total) {
469
- com.total = sr.total;
470
- }
471
- if (am) {
472
- var limit = s.limit;
473
- if ((!s.page || s.page <= 1) && s.firstLimit && s.firstLimit > 0) {
474
- limit = s.firstLimit;
475
- }
476
- com.nextPageToken = sr.next;
477
- search_1.handleAppend(com, sr.list, limit, sr.next);
478
- if (com.append && (s.page && s.page > 1)) {
479
- com.appendList(results);
480
- }
481
- else {
482
- com.setList(results);
483
- }
484
- }
485
- else {
486
- search_1.showPaging(com, sr.list, s.limit, sr.total);
487
- com.setList(results);
488
- com.tmpPageIndex = s.page;
489
- if (s.limit) {
490
- this.showMessage(search_1.buildMessage(this.resourceService, s.page, s.limit, sr.list, sr.total));
491
- }
492
- }
493
- com.running = undefined;
494
- core_1.hideLoading(com.loading);
495
- if (com.triggerSearch) {
496
- com.triggerSearch = undefined;
497
- com.resetAndSearch();
498
- }
499
- };
500
- BaseSearchComponent.prototype.appendList = function (results) {
501
- var _a;
502
- var list = this.state.list;
503
- var arr = search_1.append(list, results);
504
- var listForm = this.getSearchForm();
505
- var props = this.props;
506
- var setGlobalState = props.props.setGlobalState;
507
- if (setGlobalState && listForm) {
508
- setGlobalState((_a = {}, _a[listForm.name] = arr, _a));
509
- }
510
- else {
511
- this.setState({ list: arr });
512
- }
513
- };
514
- BaseSearchComponent.prototype.setList = function (list) {
515
- var _a;
516
- var props = this.props;
517
- var setGlobalState = props.props.setGlobalState;
518
- this.list = list;
519
- var listForm = this.getSearchForm();
520
- if (setGlobalState && listForm) {
521
- setGlobalState((_a = {}, _a[listForm.name] = list, _a));
522
- }
523
- else {
524
- this.setState({ list: list });
525
- }
526
- };
527
- BaseSearchComponent.prototype.getList = function () {
528
- return this.list;
529
- };
530
- BaseSearchComponent.prototype.sort = function (event) {
531
- event.preventDefault();
532
- search_1.handleSortEvent(event, this);
533
- if (!this.appendMode) {
534
- this.doSearch();
535
- }
536
- else {
537
- this.resetAndSearch();
538
- }
539
- };
540
- BaseSearchComponent.prototype.showMore = function (event) {
541
- event.preventDefault();
542
- this.tmpPageIndex = this.pageIndex;
543
- search_1.more(this);
544
- this.doSearch();
545
- };
546
- BaseSearchComponent.prototype.pageChanged = function (data) {
547
- var page = data.page, size = data.size;
548
- search_1.changePage(this, page, size);
549
- this.doSearch();
550
- };
551
- return BaseSearchComponent;
552
- }(BaseComponent));
553
- exports.BaseSearchComponent = BaseSearchComponent;
554
- var SearchComponent = (function (_super) {
555
- __extends(SearchComponent, _super);
556
- function SearchComponent(props, sv, param, showMessage, showError, getLocale, uis, loading, listFormId) {
557
- 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;
558
- _this.autoSearch = input_1.getAutoSearch(param);
559
- if (sv) {
560
- if (typeof sv === 'function') {
561
- var x = sv;
562
- _this.service = x;
563
- }
564
- else {
565
- _this.service = sv.search;
566
- if (sv.keys) {
567
- _this.keys = sv.keys();
568
- }
569
- }
570
- }
571
- _this.call = _this.call.bind(_this);
572
- _this.showError = input_1.getErrorFunc(param, showError);
573
- _this.componentDidMount = _this.componentDidMount.bind(_this);
574
- _this.mergeFilter = _this.mergeFilter.bind(_this);
575
- _this.createFilter = _this.createFilter.bind(_this);
576
- _this.ref = React.createRef();
577
- return _this;
578
- }
579
- SearchComponent.prototype.componentDidMount = function () {
580
- var k = (this.ui ? this.ui.registerEvents : undefined);
581
- this.form = core_2.initForm(this.ref.current, k);
582
- var s = this.mergeFilter(route_1.buildFromUrl(), this.createFilter());
583
- this.load(s, this.autoSearch);
584
- };
585
- SearchComponent.prototype.mergeFilter = function (obj, b, arrs) {
586
- return search_1.mergeFilter(obj, b, this.pageSizes, arrs);
587
- };
588
- SearchComponent.prototype.createFilter = function () {
589
- var s = {};
590
- return s;
591
- };
592
- SearchComponent.prototype.call = function (se) {
593
- this.running = true;
594
- var s = reflect_1.clone(se);
595
- var page = this.pageIndex;
596
- if (!page || page < 1) {
597
- page = 1;
598
- }
599
- var offset;
600
- if (se.limit) {
601
- if (se.firstLimit && se.firstLimit > 0) {
602
- offset = se.limit * (page - 2) + se.firstLimit;
603
- }
604
- else {
605
- offset = se.limit * (page - 1);
606
- }
607
- }
608
- var limit = (page <= 1 && se.firstLimit && se.firstLimit > 0 ? se.firstLimit : se.limit);
609
- var next = (this.nextPageToken && this.nextPageToken.length > 0 ? this.nextPageToken : offset);
610
- var fields = se.fields;
611
- delete se['page'];
612
- delete se['fields'];
613
- delete se['limit'];
614
- delete se['firstLimit'];
615
- core_1.showLoading(this.loading);
616
- var com = this;
617
- if (this.service) {
618
- this.service(s, limit, next, fields).then(function (sr) {
619
- com.showResults(s, sr);
620
- com.running = undefined;
621
- core_1.hideLoading(com.loading);
622
- }).catch(function (err) {
623
- com.pageIndex = com.tmpPageIndex;
624
- core_2.error(err, com.resourceService.value, com.showError);
625
- com.running = undefined;
626
- core_1.hideLoading(com.loading);
627
- });
628
- }
629
- };
630
- return SearchComponent;
631
- }(BaseSearchComponent));
632
- exports.SearchComponent = SearchComponent;
633
- var BaseEditComponent = (function (_super) {
634
- __extends(BaseEditComponent, _super);
635
- function BaseEditComponent(props, resourceService, showMessage, showError, confirm, getLocale, ui, loading, patchable, backOnSaveSuccess) {
636
- var _this = _super.call(this, props, getLocale, (ui ? ui.removeError : undefined)) || this;
637
- _this.resourceService = resourceService;
638
- _this.showMessage = showMessage;
639
- _this.showError = showError;
640
- _this.confirm = confirm;
641
- _this.ui = ui;
642
- _this.loading = loading;
643
- _this.backOnSuccess = true;
644
- _this.patchable = true;
645
- _this.create = function (event) {
646
- if (event) {
647
- event.preventDefault();
648
- }
649
- if (!_this.form && event && event.target && event.target.form) {
650
- _this.form = event.target.form;
651
- }
652
- var obj = _this.createModel();
653
- _this.resetState(true, obj, undefined);
654
- var u = _this.ui;
655
- var f = _this.form;
656
- if (u && f) {
657
- setTimeout(function () {
658
- u.removeFormError(f);
659
- }, 100);
660
- }
661
- };
662
- _this.save = function (event) {
663
- event.preventDefault();
664
- event.persist();
665
- if (!_this.form && event && event.target) {
666
- _this.form = event.target.form;
667
- }
668
- _this.onSave(_this.backOnSuccess);
669
- };
670
- _this.resource = resourceService.resource();
671
- if (patchable === false) {
672
- _this.patchable = patchable;
673
- }
674
- if (backOnSaveSuccess === false) {
675
- _this.backOnSuccess = backOnSaveSuccess;
676
- }
677
- _this.insertSuccessMsg = resourceService.value('msg_save_success');
678
- _this.updateSuccessMsg = resourceService.value('msg_save_success');
679
- _this.showMessage = _this.showMessage.bind(_this);
680
- _this.showError = _this.showError.bind(_this);
681
- _this.confirm = _this.confirm.bind(_this);
682
- _this.back = _this.back.bind(_this);
683
- _this.getModelName = _this.getModelName.bind(_this);
684
- _this.resetState = _this.resetState.bind(_this);
685
- _this.handleNotFound = _this.handleNotFound.bind(_this);
686
- _this.showModel = _this.showModel.bind(_this);
687
- _this.getModel = _this.getModel.bind(_this);
688
- _this.createModel = _this.createModel.bind(_this);
689
- _this.create = _this.create.bind(_this);
690
- _this.save = _this.save.bind(_this);
691
- _this.onSave = _this.onSave.bind(_this);
692
- _this.validate = _this.validate.bind(_this);
693
- _this.doSave = _this.doSave.bind(_this);
694
- _this.succeed = _this.succeed.bind(_this);
695
- _this.fail = _this.fail.bind(_this);
696
- _this.postSave = _this.postSave.bind(_this);
697
- _this.handleDuplicateKey = _this.handleDuplicateKey.bind(_this);
698
- return _this;
699
- }
700
- BaseEditComponent.prototype.back = function (event) {
701
- if (event) {
702
- event.preventDefault();
703
- }
704
- };
705
- BaseEditComponent.prototype.resetState = function (newMod, model, originalModel) {
706
- this.newMode = newMod;
707
- this.orginalModel = originalModel;
708
- this.showModel(model);
709
- };
710
- BaseEditComponent.prototype.handleNotFound = function (form) {
711
- var msg = core_2.message(this.resourceService.value, 'error_not_found', 'error');
712
- if (form) {
713
- formutil_1.setReadOnly(form);
714
- }
715
- this.showError(msg.message, msg.title);
716
- };
717
- BaseEditComponent.prototype.getModelName = function (f) {
718
- if (this.name && this.name.length > 0) {
719
- return this.name;
720
- }
721
- return _super.prototype.getModelName.call(this, f);
722
- };
723
- BaseEditComponent.prototype.getModel = function () {
724
- var n = this.getModelName();
725
- return this.props[n] || this.state[n];
726
- };
727
- BaseEditComponent.prototype.showModel = function (model) {
728
- var _this = this;
729
- var f = this.form;
730
- var modelName = this.getModelName();
731
- var objSet = {};
732
- objSet[modelName] = model;
733
- this.setState(objSet, function () {
734
- if (_this.readOnly) {
735
- formutil_1.setReadOnly(f);
736
- }
737
- });
738
- };
739
- BaseEditComponent.prototype.createModel = function () {
740
- if (this.metadata) {
741
- var obj = edit_1.createModel(this.metadata);
742
- return obj;
743
- }
744
- else {
745
- var obj = {};
746
- return obj;
747
- }
748
- };
749
- BaseEditComponent.prototype.onSave = function (isBack) {
750
- var _this = this;
751
- var r = this.resourceService;
752
- if (this.running) {
753
- return;
754
- }
755
- var com = this;
756
- var obj = com.getModel();
757
- if (this.newMode) {
758
- com.validate(obj, function () {
759
- var msg = core_2.message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
760
- _this.confirm(msg.message, msg.title, function () {
761
- com.doSave(obj, obj, isBack);
762
- }, msg.no, msg.yes);
763
- });
764
- }
765
- else {
766
- var diffObj_1 = reflect_1.makeDiff(edit_1.initPropertyNullInModel(this.orginalModel, this.metadata), obj, this.keys, this.version);
767
- var keys = Object.keys(diffObj_1);
768
- if (keys.length === 0) {
769
- this.showMessage(r.value('msg_no_change'));
770
- }
771
- else {
772
- com.validate(obj, function () {
773
- var msg = core_2.message(r.value, 'msg_confirm_save', 'confirm', 'yes', 'no');
774
- _this.confirm(msg.message, msg.title, function () {
775
- com.doSave(obj, diffObj_1, isBack);
776
- }, msg.no, msg.yes);
777
- });
778
- }
779
- }
780
- };
781
- BaseEditComponent.prototype.validate = function (obj, callback) {
782
- if (this.ui && this.form) {
783
- var valid = this.ui.validateForm(this.form, state_1.localeOf(undefined, this.getLocale));
784
- if (valid) {
785
- callback(obj);
786
- }
787
- }
788
- else {
789
- callback(obj);
790
- }
791
- };
792
- BaseEditComponent.prototype.doSave = function (obj, dif, isBack) {
793
- };
794
- BaseEditComponent.prototype.succeed = function (msg, origin, isBack, model) {
795
- if (model) {
796
- this.newMode = false;
797
- if (model && this.setBack) {
798
- this.resetState(false, model, reflect_1.clone(model));
799
- }
800
- else {
801
- edit_1.handleVersion(origin, this.version);
802
- }
803
- }
804
- else {
805
- edit_1.handleVersion(origin, this.version);
806
- }
807
- var isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
808
- this.showMessage(msg);
809
- if (isBackO) {
810
- this.back(null);
811
- }
812
- };
813
- BaseEditComponent.prototype.fail = function (result) {
814
- var f = this.form;
815
- var u = this.ui;
816
- if (u && f) {
817
- var unmappedErrors = u.showFormError(f, result);
818
- formutil_1.focusFirstError(f);
819
- if (unmappedErrors && unmappedErrors.length > 0) {
820
- var t = this.resourceService.value('error');
821
- if (u && u.buildErrorMessage) {
822
- var msg = u.buildErrorMessage(unmappedErrors);
823
- this.showError(msg, t);
824
- }
825
- else {
826
- this.showError(unmappedErrors[0].field + ' ' + unmappedErrors[0].code + ' ' + unmappedErrors[0].message, t);
827
- }
828
- }
829
- }
830
- else {
831
- var t = this.resourceService.value('error');
832
- if (result.length > 0) {
833
- this.showError(result[0].field + ' ' + result[0].code + ' ' + result[0].message, t);
834
- }
835
- else {
836
- this.showError(t, t);
837
- }
838
- }
839
- };
840
- BaseEditComponent.prototype.postSave = function (res, origin, isPatch, backOnSave) {
841
- this.running = false;
842
- core_1.hideLoading(this.loading);
843
- var newMod = this.newMode;
844
- var successMsg = (newMod ? this.insertSuccessMsg : this.updateSuccessMsg);
845
- var x = res;
846
- var r = this.resourceService;
847
- if (Array.isArray(x)) {
848
- this.fail(x);
849
- }
850
- else if (!isNaN(x)) {
851
- if (x > 0) {
852
- this.succeed(successMsg, origin, backOnSave);
853
- }
854
- else {
855
- if (newMod && x <= 0) {
856
- this.handleDuplicateKey();
857
- }
858
- else if (!newMod && x === 0) {
859
- this.handleNotFound();
860
- }
861
- else {
862
- this.showError(r.value('error_version'), r.value('error'));
863
- }
864
- }
865
- }
866
- else {
867
- var result = x;
868
- if (isPatch) {
869
- var keys = Object.keys(result);
870
- var a = origin;
871
- for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
872
- var k = keys_1[_i];
873
- a[k] = result[k];
874
- }
875
- this.succeed(successMsg, a, backOnSave);
876
- }
877
- else {
878
- this.succeed(successMsg, origin, backOnSave, result);
879
- }
880
- this.showMessage(successMsg);
881
- }
882
- };
883
- BaseEditComponent.prototype.handleDuplicateKey = function (result) {
884
- var msg = core_2.message(this.resourceService.value, 'error_duplicate_key', 'error');
885
- this.showError(msg.message, msg.title);
886
- };
887
- return BaseEditComponent;
888
- }(BaseComponent));
889
- exports.BaseEditComponent = BaseEditComponent;
890
- var EditComponent = (function (_super) {
891
- __extends(EditComponent, _super);
892
- function EditComponent(props, service, param, showMessage, showError, confirm, getLocale, uis, loading, patchable, backOnSaveSuccess) {
893
- 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;
894
- _this.service = service;
895
- if (service.metadata) {
896
- var metadata = service.metadata();
897
- if (metadata) {
898
- var meta = edit_1.build(metadata);
899
- if (meta) {
900
- _this.keys = meta.keys;
901
- _this.version = meta.version;
902
- }
903
- _this.metadata = metadata;
904
- }
905
- }
906
- if (!_this.keys && service.keys) {
907
- var k = service.keys();
908
- if (k) {
909
- _this.keys = k;
910
- }
911
- }
912
- if (!_this.keys) {
913
- _this.keys = [];
914
- }
915
- _this.load = _this.load.bind(_this);
916
- _this.doSave = _this.doSave.bind(_this);
917
- _this.ref = React.createRef();
918
- return _this;
919
- }
920
- EditComponent.prototype.load = function (_id, callback) {
921
- var _this = this;
922
- var id = _id;
923
- if (id != null && id !== '') {
924
- var com_2 = this;
925
- this.running = true;
926
- core_1.showLoading(com_2.loading);
927
- this.service.load(id).then(function (obj) {
928
- if (!obj) {
929
- com_2.handleNotFound(_this.form);
930
- }
931
- else {
932
- com_2.newMode = false;
933
- com_2.orginalModel = reflect_1.clone(obj);
934
- if (!callback) {
935
- com_2.showModel(obj);
936
- }
937
- else {
938
- callback(obj, com_2.showModel);
939
- }
940
- }
941
- com_2.running = false;
942
- core_1.hideLoading(com_2.loading);
943
- }).catch(function (err) {
944
- var data = (err && err.response) ? err.response : err;
945
- var r = com_2.resourceService;
946
- var gv = r.value;
947
- var title = gv('error');
948
- var msg = gv('error_internal');
949
- if (data && data.status === 404) {
950
- com_2.handleNotFound(com_2.form);
951
- }
952
- else {
953
- if (data.status && !isNaN(data.status)) {
954
- msg = core_2.messageByHttpStatus(data.status, gv);
955
- }
956
- if (data && (data.status === 401 || data.status === 403)) {
957
- formutil_1.setReadOnly(com_2.form);
958
- }
959
- com_2.showError(msg, title);
960
- }
961
- com_2.running = false;
962
- core_1.hideLoading(com_2.loading);
963
- });
964
- }
965
- else {
966
- this.newMode = true;
967
- this.orginalModel = undefined;
968
- var obj = this.createModel();
969
- if (callback) {
970
- callback(obj, this.showModel);
971
- }
972
- else {
973
- this.showModel(obj);
974
- }
975
- }
976
- };
977
- EditComponent.prototype.doSave = function (obj, body, isBack) {
978
- this.running = true;
979
- core_1.showLoading(this.loading);
980
- var isBackO = (isBack == null || isBack === undefined ? this.backOnSuccess : isBack);
981
- var com = this;
982
- var m = obj;
983
- var fn = this.newMode ? this.service.insert : this.service.update;
984
- if (!this.newMode) {
985
- if (this.patchable === true && this.service.patch && body && Object.keys(body).length > 0) {
986
- m = body;
987
- fn = this.service.patch;
988
- }
989
- }
990
- fn(m).then(function (result) {
991
- com.postSave(result, obj, isBackO);
992
- com.running = false;
993
- core_1.hideLoading(com.loading);
994
- }).then(function (err) {
995
- core_2.error(err, com.resourceService.value, com.showError);
996
- com.running = false;
997
- core_1.hideLoading(com.loading);
998
- });
999
- };
1000
- return EditComponent;
1001
- }(BaseEditComponent));
1002
- exports.EditComponent = EditComponent;
1003
- var BaseDiffApprComponent = (function (_super) {
1004
- __extends(BaseDiffApprComponent, _super);
1005
- function BaseDiffApprComponent(props, keys, resourceService, showMessage, showError, loading) {
1006
- var _this = _super.call(this, props) || this;
1007
- _this.keys = keys;
1008
- _this.resourceService = resourceService;
1009
- _this.showMessage = showMessage;
1010
- _this.showError = showError;
1011
- _this.loading = loading;
1012
- _this.resource = resourceService.resource();
1013
- _this.showMessage = _this.showMessage.bind(_this);
1014
- _this.showError = _this.showError.bind(_this);
1015
- _this.initModel = _this.initModel.bind(_this);
1016
- _this.postApprove = _this.postApprove.bind(_this);
1017
- _this.postReject = _this.postReject.bind(_this);
1018
- _this.format = _this.format.bind(_this);
1019
- _this.handleNotFound = _this.handleNotFound.bind(_this);
1020
- _this.state = {
1021
- disabled: false
1022
- };
1023
- return _this;
1024
- }
1025
- BaseDiffApprComponent.prototype.initModel = function () {
1026
- var x = {};
1027
- return x;
1028
- };
1029
- BaseDiffApprComponent.prototype.postApprove = function (s, err) {
1030
- this.setState({ disabled: true });
1031
- var r = this.resourceService;
1032
- if (s > 0) {
1033
- this.showMessage(r.value('msg_approve_success'));
1034
- }
1035
- else if (s === 0) {
1036
- this.handleNotFound();
1037
- }
1038
- else {
1039
- var msg = core_2.message(r.value, 'msg_approve_version_error', 'error');
1040
- this.showError(msg.message, msg.title);
1041
- }
1042
- };
1043
- BaseDiffApprComponent.prototype.postReject = function (status, err) {
1044
- this.setState({ disabled: true });
1045
- var r = this.resourceService;
1046
- if (status > 0) {
1047
- this.showMessage(r.value('msg_reject_success'));
1048
- }
1049
- else if (status === 0) {
1050
- this.handleNotFound();
1051
- }
1052
- else {
1053
- var msg = core_2.message(r.value, 'msg_approve_version_error', 'error');
1054
- this.showError(msg.message, msg.title);
1055
- }
1056
- };
1057
- BaseDiffApprComponent.prototype.format = function () {
1058
- var p = this.props;
1059
- var diffModel = p['diffModel'];
1060
- if (diffModel) {
1061
- var differentKeys_1 = reflect_1.diff(diffModel.origin, diffModel.value);
1062
- var dataFields = diff_1.getDataFields(this.form);
1063
- dataFields.forEach(function (e) {
1064
- var x = e.getAttribute('data-field');
1065
- if (x) {
1066
- if (differentKeys_1.indexOf(x) >= 0) {
1067
- if (e.childNodes.length === 3) {
1068
- e.childNodes[1].classList.add('highlight');
1069
- e.childNodes[2].classList.add('highlight');
1070
- }
1071
- else {
1072
- e.classList.add('highlight');
1073
- }
1074
- }
1075
- }
1076
- });
1077
- }
1078
- else {
1079
- var _a = this.state, origin_1 = _a.origin, value = _a.value;
1080
- var differentKeys_2 = reflect_1.diff(origin_1, value);
1081
- var dataFields = diff_1.getDataFields(this.form);
1082
- dataFields.forEach(function (e) {
1083
- var x = e.getAttribute('data-field');
1084
- if (x) {
1085
- if (differentKeys_2.indexOf(x) >= 0) {
1086
- if (e.childNodes.length === 3) {
1087
- e.childNodes[1].classList.add('highlight');
1088
- e.childNodes[2].classList.add('highlight');
1089
- }
1090
- else {
1091
- e.classList.add('highlight');
1092
- }
1093
- }
1094
- }
1095
- });
1096
- }
1097
- };
1098
- BaseDiffApprComponent.prototype.handleNotFound = function () {
1099
- this.setState({ disabled: true });
1100
- var msg = core_2.message(this.resourceService.value, 'error_not_found', 'error');
1101
- this.showError(msg.message, msg.title);
1102
- };
1103
- return BaseDiffApprComponent;
1104
- }(React.Component));
1105
- exports.BaseDiffApprComponent = BaseDiffApprComponent;
1106
- var DiffApprComponent = (function (_super) {
1107
- __extends(DiffApprComponent, _super);
1108
- function DiffApprComponent(props, service, param, showMessage, showError, loading) {
1109
- 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;
1110
- _this.service = service;
1111
- _this.approve = _this.approve.bind(_this);
1112
- _this.reject = _this.reject.bind(_this);
1113
- _this.formatFields = _this.formatFields.bind(_this);
1114
- _this.ref = React.createRef();
1115
- _this.state = {
1116
- origin: _this.initModel(),
1117
- value: _this.initModel(),
1118
- disabled: false,
1119
- };
1120
- return _this;
1121
- }
1122
- DiffApprComponent.prototype.formatFields = function (value) {
1123
- return value;
1124
- };
1125
- DiffApprComponent.prototype.load = function (_id) {
1126
- var id = _id;
1127
- if (id != null && id !== '') {
1128
- this.id = _id;
1129
- var com_3 = this;
1130
- this.running = true;
1131
- core_1.showLoading(this.loading);
1132
- this.service.diff(id).then(function (dobj) {
1133
- if (!dobj) {
1134
- com_3.handleNotFound();
1135
- }
1136
- else {
1137
- var formatdDiff = diff_1.formatDiffModel(dobj, com_3.formatFields);
1138
- com_3.setState({
1139
- origin: formatdDiff.origin,
1140
- value: formatdDiff.value
1141
- }, com_3.format);
1142
- }
1143
- com_3.running = false;
1144
- core_1.hideLoading(com_3.loading);
1145
- }).catch(function (err) {
1146
- var data = (err && err.response) ? err.response : err;
1147
- if (data && data.status === 404) {
1148
- com_3.handleNotFound();
1149
- }
1150
- else {
1151
- core_2.error(err, com_3.resourceService.value, com_3.showError);
1152
- }
1153
- com_3.running = false;
1154
- core_1.hideLoading(com_3.loading);
1155
- });
1156
- }
1157
- };
1158
- DiffApprComponent.prototype.approve = function (event) {
1159
- event.preventDefault();
1160
- var com = this;
1161
- this.running = true;
1162
- core_1.showLoading(this.loading);
1163
- if (this.id) {
1164
- this.service.approve(this.id).then(function (status) {
1165
- com.postApprove(status, null);
1166
- com.running = false;
1167
- core_1.hideLoading(com.loading);
1168
- }).catch(function (err) {
1169
- com.postApprove(4, err);
1170
- com.running = false;
1171
- core_1.hideLoading(com.loading);
1172
- });
1173
- }
1174
- };
1175
- DiffApprComponent.prototype.reject = function (event) {
1176
- event.preventDefault();
1177
- var com = this;
1178
- this.running = true;
1179
- core_1.showLoading(this.loading);
1180
- if (this.id) {
1181
- this.service.reject(this.id).then(function (status) {
1182
- com.postReject(status, null);
1183
- com.running = false;
1184
- core_1.hideLoading(com.loading);
1185
- }).catch(function (err) {
1186
- com.postReject(4, err);
1187
- com.running = false;
1188
- core_1.hideLoading(com.loading);
1189
- });
1190
- }
1191
- };
1192
- return DiffApprComponent;
1193
- }(BaseDiffApprComponent));
1194
- exports.DiffApprComponent = DiffApprComponent;
1195
- function removeUrlParams(url) {
1196
- var startParams = url.indexOf('?');
1197
- return startParams !== -1 ? url.substring(0, startParams) : url;
1198
- }
1199
- exports.removeUrlParams = removeUrlParams;