amis 1.9.1-beta.24 → 1.9.1-beta.27

Sign up to get free protection for your applications and to get access to all the features.
@@ -418,265 +418,269 @@ var Form = /** @class */ (function (_super) {
418
418
  }, this.props.store.data);
419
419
  };
420
420
  Form.prototype.handleAction = function (e, action, data, throwErrors, delegate) {
421
- var _this = this;
422
421
  if (throwErrors === void 0) { throwErrors = false; }
423
- var _a = this.props, store = _a.store, onSubmit = _a.onSubmit, api = _a.api, asyncApi = _a.asyncApi, finishedField = _a.finishedField, checkInterval = _a.checkInterval, _b = _a.messages, saveSuccess = _b.saveSuccess, saveFailed = _b.saveFailed, resetAfterSubmit = _a.resetAfterSubmit, clearAfterSubmit = _a.clearAfterSubmit, onAction = _a.onAction, onSaved = _a.onSaved, onReset = _a.onReset, onFinished = _a.onFinished, onFailed = _a.onFailed, redirect = _a.redirect, reload = _a.reload, target = _a.target, env = _a.env, onChange = _a.onChange, clearPersistDataAfterSubmit = _a.clearPersistDataAfterSubmit, trimValues = _a.trimValues, dispatchEvent = _a.dispatchEvent, __ = _a.translate;
424
- // 做动作之前,先把数据同步一下。
425
- this.flush();
426
- if (trimValues) {
427
- store.trimValues();
428
- }
429
- // 如果 data 就是当前层,则 flush 一下。
430
- if (data === this.props.data) {
431
- data = store.data;
432
- }
433
- if (Array.isArray(action.required) && action.required.length) {
434
- store.clearErrors(); // 如果是按钮指定了required,则校验前先清空一下遗留的校验报错
435
- var fields = action.required.map(function (item) { return ({
436
- name: item,
437
- rules: { isRequired: true }
438
- }); });
439
- return store.validateFields(fields).then(function (result) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
440
- var dispatcher;
441
- return (0, tslib_1.__generator)(this, function (_a) {
442
- switch (_a.label) {
443
- case 0:
444
- if (!!result) return [3 /*break*/, 2];
445
- return [4 /*yield*/, dispatchEvent('validateError', this.props.data)];
446
- case 1:
447
- dispatcher = _a.sent();
448
- if (!(dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher.prevented)) {
449
- env.notify('error', __('Form.validateFailed'));
422
+ return (0, tslib_1.__awaiter)(this, void 0, Promise, function () {
423
+ var _a, store, onSubmit, api, asyncApi, finishedField, checkInterval, _b, saveSuccess, saveFailed, resetAfterSubmit, clearAfterSubmit, onAction, onSaved, onReset, onFinished, onFailed, redirect, reload, target, env, onChange, clearPersistDataAfterSubmit, trimValues, dispatchEvent, __, fields, validationRes, dispatcher;
424
+ var _this = this;
425
+ return (0, tslib_1.__generator)(this, function (_c) {
426
+ switch (_c.label) {
427
+ case 0:
428
+ _a = this.props, store = _a.store, onSubmit = _a.onSubmit, api = _a.api, asyncApi = _a.asyncApi, finishedField = _a.finishedField, checkInterval = _a.checkInterval, _b = _a.messages, saveSuccess = _b.saveSuccess, saveFailed = _b.saveFailed, resetAfterSubmit = _a.resetAfterSubmit, clearAfterSubmit = _a.clearAfterSubmit, onAction = _a.onAction, onSaved = _a.onSaved, onReset = _a.onReset, onFinished = _a.onFinished, onFailed = _a.onFailed, redirect = _a.redirect, reload = _a.reload, target = _a.target, env = _a.env, onChange = _a.onChange, clearPersistDataAfterSubmit = _a.clearPersistDataAfterSubmit, trimValues = _a.trimValues, dispatchEvent = _a.dispatchEvent, __ = _a.translate;
429
+ // 做动作之前,先把数据同步一下。
430
+ this.flush();
431
+ if (trimValues) {
432
+ store.trimValues();
433
+ }
434
+ // 如果 data 就是当前层,则 flush 一下。
435
+ if (data === this.props.data) {
436
+ data = store.data;
437
+ }
438
+ if (!(Array.isArray(action.required) && action.required.length)) return [3 /*break*/, 4];
439
+ store.clearErrors(); // 如果是按钮指定了required,则校验前先清空一下遗留的校验报错
440
+ fields = action.required.map(function (item) { return ({
441
+ name: item,
442
+ rules: { isRequired: true }
443
+ }); });
444
+ return [4 /*yield*/, store.validateFields(fields)];
445
+ case 1:
446
+ validationRes = _c.sent();
447
+ if (!!validationRes) return [3 /*break*/, 3];
448
+ return [4 /*yield*/, dispatchEvent('validateError', this.props.data)];
449
+ case 2:
450
+ dispatcher = _c.sent();
451
+ if (!(dispatcher === null || dispatcher === void 0 ? void 0 : dispatcher.prevented)) {
452
+ env.notify('error', __('Form.validateFailed'));
453
+ }
454
+ /** 抛异常是为了在dialog中catch这个错误,避免弹窗直接关闭 */
455
+ return [2 /*return*/, Promise.reject(__('Form.validateFailed'))];
456
+ case 3:
457
+ /** 重置validated状态,保证submit时触发表单中的校验项 */
458
+ store.clearErrors();
459
+ _c.label = 4;
460
+ case 4:
461
+ if (action.type === 'submit' ||
462
+ action.actionType === 'submit' ||
463
+ action.actionType === 'confirm' ||
464
+ action.actionType === 'reset-and-submit' ||
465
+ action.actionType === 'clear-and-submit') {
466
+ store.setCurrentAction(action);
467
+ if (action.actionType === 'reset-and-submit') {
468
+ store.reset(onReset);
450
469
  }
451
- /** 抛异常是为了在dialog中catch这个错误,避免弹窗直接关闭 */
452
- return [2 /*return*/, Promise.reject(__('Form.validateFailed'))];
453
- case 2:
454
- dispatchEvent('validateSucc', this.props.data);
455
- this.handleAction(e, (0, tslib_1.__assign)((0, tslib_1.__assign)({}, action), { required: undefined }), data, throwErrors, delegate);
456
- return [2 /*return*/];
457
- }
458
- });
459
- }); });
460
- }
461
- if (action.type === 'submit' ||
462
- action.actionType === 'submit' ||
463
- action.actionType === 'confirm' ||
464
- action.actionType === 'reset-and-submit' ||
465
- action.actionType === 'clear-and-submit') {
466
- store.setCurrentAction(action);
467
- if (action.actionType === 'reset-and-submit') {
468
- store.reset(onReset);
469
- }
470
- else if (action.actionType === 'clear-and-submit') {
471
- store.clear(onReset);
472
- }
473
- return this.submit(function (values) {
474
- var _a;
475
- if (onSubmit && onSubmit(values, action) === false) {
476
- return Promise.resolve(false);
477
- }
478
- // 走到这里代表校验成功了
479
- dispatchEvent('validateSucc', _this.props.data);
480
- if (target) {
481
- _this.submitToTarget(target, values);
482
- dispatchEvent('submitSucc', (0, helper_1.createObject)(_this.props.data, values));
483
- }
484
- else if (action.actionType === 'reload') {
485
- action.target && _this.reloadTarget(action.target, values);
486
- }
487
- else if (action.actionType === 'dialog') {
488
- store.openDialog(data);
489
- }
490
- else if (action.actionType === 'drawer') {
491
- store.openDrawer(data);
492
- }
493
- else if ((0, api_1.isEffectiveApi)(action.api || api, values)) {
494
- var finnalAsyncApi_1 = action.asyncApi || asyncApi;
495
- (0, api_1.isEffectiveApi)(finnalAsyncApi_1, store.data) &&
496
- store.updateData((_a = {},
497
- _a[finishedField || 'finished'] = false,
498
- _a));
499
- return store
500
- .saveRemote(action.api || api, values, {
501
- successMessage: saveSuccess,
502
- errorMessage: saveFailed,
503
- onSuccess: function (result) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
504
- var dispatcher, cbResult;
505
- var _this = this;
506
- return (0, tslib_1.__generator)(this, function (_a) {
507
- switch (_a.label) {
508
- case 0: return [4 /*yield*/, dispatchEvent('submitSucc', (0, helper_1.createObject)(this.props.data, { result: result }))];
509
- case 1:
510
- dispatcher = _a.sent();
511
- if (!(0, api_1.isEffectiveApi)(finnalAsyncApi_1, store.data) ||
512
- store.data[finishedField || 'finished']) {
513
- return [2 /*return*/, {
514
- cbResult: null,
515
- dispatcher: dispatcher
516
- }];
517
- }
518
- cbResult = (0, helper_1.until)(function () { return store.checkRemote(finnalAsyncApi_1, store.data); }, function (ret) { return ret && ret[finishedField || 'finished']; }, function (cancel) { return (_this.asyncCancel = cancel); }, checkInterval);
519
- return [2 /*return*/, {
520
- cbResult: cbResult,
521
- dispatcher: dispatcher
522
- }];
523
- }
524
- });
525
- }); },
526
- onFailed: function (result) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
527
- var dispatcher;
528
- return (0, tslib_1.__generator)(this, function (_a) {
529
- switch (_a.label) {
530
- case 0: return [4 /*yield*/, dispatchEvent('submitFail', (0, helper_1.createObject)(this.props.data, { error: result }))];
531
- case 1:
532
- dispatcher = _a.sent();
533
- return [2 /*return*/, {
534
- dispatcher: dispatcher
535
- }];
536
- }
537
- });
538
- }); }
539
- })
540
- .then(function (response) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
541
- var feedback, confirmed;
542
- return (0, tslib_1.__generator)(this, function (_a) {
543
- switch (_a.label) {
544
- case 0:
545
- onSaved && onSaved(values, response);
546
- feedback = action.feedback || this.props.feedback;
547
- if (!(feedback && (0, helper_1.isVisible)(feedback, store.data))) return [3 /*break*/, 2];
548
- return [4 /*yield*/, this.openFeedback(feedback, store.data)];
549
- case 1:
550
- confirmed = _a.sent();
551
- // 如果 feedback 配置了,取消就跳过原有逻辑。
552
- if (feedback.skipRestOnCancel && !confirmed) {
553
- throw new helper_1.SkipOperation();
470
+ else if (action.actionType === 'clear-and-submit') {
471
+ store.clear(onReset);
472
+ }
473
+ return [2 /*return*/, this.submit(function (values) {
474
+ var _a;
475
+ if (onSubmit && onSubmit(values, action) === false) {
476
+ return Promise.resolve(false);
477
+ }
478
+ // 走到这里代表校验成功了
479
+ dispatchEvent('validateSucc', _this.props.data);
480
+ if (target) {
481
+ _this.submitToTarget(target, values);
482
+ dispatchEvent('submitSucc', (0, helper_1.createObject)(_this.props.data, values));
483
+ }
484
+ else if (action.actionType === 'reload') {
485
+ action.target && _this.reloadTarget(action.target, values);
486
+ }
487
+ else if (action.actionType === 'dialog') {
488
+ store.openDialog(data);
489
+ }
490
+ else if (action.actionType === 'drawer') {
491
+ store.openDrawer(data);
492
+ }
493
+ else if ((0, api_1.isEffectiveApi)(action.api || api, values)) {
494
+ var finnalAsyncApi_1 = action.asyncApi || asyncApi;
495
+ (0, api_1.isEffectiveApi)(finnalAsyncApi_1, store.data) &&
496
+ store.updateData((_a = {},
497
+ _a[finishedField || 'finished'] = false,
498
+ _a));
499
+ return store
500
+ .saveRemote(action.api || api, values, {
501
+ successMessage: saveSuccess,
502
+ errorMessage: saveFailed,
503
+ onSuccess: function (result) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
504
+ var dispatcher, cbResult;
505
+ var _this = this;
506
+ return (0, tslib_1.__generator)(this, function (_a) {
507
+ switch (_a.label) {
508
+ case 0: return [4 /*yield*/, dispatchEvent('submitSucc', (0, helper_1.createObject)(this.props.data, { result: result }))];
509
+ case 1:
510
+ dispatcher = _a.sent();
511
+ if (!(0, api_1.isEffectiveApi)(finnalAsyncApi_1, store.data) ||
512
+ store.data[finishedField || 'finished']) {
513
+ return [2 /*return*/, {
514
+ cbResult: null,
515
+ dispatcher: dispatcher
516
+ }];
517
+ }
518
+ cbResult = (0, helper_1.until)(function () { return store.checkRemote(finnalAsyncApi_1, store.data); }, function (ret) { return ret && ret[finishedField || 'finished']; }, function (cancel) { return (_this.asyncCancel = cancel); }, checkInterval);
519
+ return [2 /*return*/, {
520
+ cbResult: cbResult,
521
+ dispatcher: dispatcher
522
+ }];
523
+ }
524
+ });
525
+ }); },
526
+ onFailed: function (result) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
527
+ var dispatcher;
528
+ return (0, tslib_1.__generator)(this, function (_a) {
529
+ switch (_a.label) {
530
+ case 0: return [4 /*yield*/, dispatchEvent('submitFail', (0, helper_1.createObject)(this.props.data, { error: result }))];
531
+ case 1:
532
+ dispatcher = _a.sent();
533
+ return [2 /*return*/, {
534
+ dispatcher: dispatcher
535
+ }];
536
+ }
537
+ });
538
+ }); }
539
+ })
540
+ .then(function (response) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
541
+ var feedback, confirmed;
542
+ return (0, tslib_1.__generator)(this, function (_a) {
543
+ switch (_a.label) {
544
+ case 0:
545
+ onSaved && onSaved(values, response);
546
+ feedback = action.feedback || this.props.feedback;
547
+ if (!(feedback && (0, helper_1.isVisible)(feedback, store.data))) return [3 /*break*/, 2];
548
+ return [4 /*yield*/, this.openFeedback(feedback, store.data)];
549
+ case 1:
550
+ confirmed = _a.sent();
551
+ // 如果 feedback 配置了,取消就跳过原有逻辑。
552
+ if (feedback.skipRestOnCancel && !confirmed) {
553
+ throw new helper_1.SkipOperation();
554
+ }
555
+ else if (feedback.skipRestOnConfirm && confirmed) {
556
+ throw new helper_1.SkipOperation();
557
+ }
558
+ _a.label = 2;
559
+ case 2: return [2 /*return*/];
560
+ }
561
+ });
562
+ }); });
554
563
  }
555
- else if (feedback.skipRestOnConfirm && confirmed) {
556
- throw new helper_1.SkipOperation();
564
+ else {
565
+ // type为submit,但是没有配api以及target时,只派发事件
566
+ dispatchEvent('submitSucc', (0, helper_1.createObject)(_this.props.data, values));
557
567
  }
558
- _a.label = 2;
559
- case 2: return [2 /*return*/];
568
+ return Promise.resolve(null);
569
+ })
570
+ .then(function (values) {
571
+ // 有可能 onSubmit return false 了,那么后面的就不应该再执行了。
572
+ if (values === false) {
573
+ return store.data;
574
+ }
575
+ if (onFinished && onFinished(values, action) === false) {
576
+ return values;
577
+ }
578
+ resetAfterSubmit && store.reset(onReset);
579
+ clearAfterSubmit && store.clear(onReset);
580
+ clearPersistDataAfterSubmit && store.clearLocalPersistData();
581
+ if (action.redirect || redirect) {
582
+ var finalRedirect = (0, tpl_1.filter)(action.redirect || redirect, store.data);
583
+ finalRedirect && env.jumpTo(finalRedirect, action);
584
+ }
585
+ else if (action.reload || reload) {
586
+ _this.reloadTarget(action.reload || reload, store.data);
587
+ }
588
+ action.close && _this.closeTarget(action.close);
589
+ return values;
590
+ })
591
+ .catch(function (reason) {
592
+ if (reason instanceof helper_1.SkipOperation) {
593
+ return;
594
+ }
595
+ onFailed && onFailed(reason, store.errors);
596
+ if (throwErrors) {
597
+ throw reason;
598
+ }
599
+ })];
600
+ }
601
+ else if (action.type === 'reset' || action.actionType === 'reset') {
602
+ store.setCurrentAction(action);
603
+ store.reset(onReset);
604
+ }
605
+ else if (action.actionType === 'clear') {
606
+ store.setCurrentAction(action);
607
+ store.clear(onReset);
608
+ }
609
+ else if (action.actionType === 'validate') {
610
+ store.setCurrentAction(action);
611
+ this.validate(true);
612
+ }
613
+ else if (action.actionType === 'dialog') {
614
+ store.setCurrentAction(action);
615
+ store.openDialog(data);
616
+ }
617
+ else if (action.actionType === 'drawer') {
618
+ store.setCurrentAction(action);
619
+ store.openDrawer(data);
620
+ }
621
+ else if (action.actionType === 'ajax') {
622
+ store.setCurrentAction(action);
623
+ if (!(0, api_1.isEffectiveApi)(action.api)) {
624
+ return [2 /*return*/, env.alert(__("\u5F53 actionType \u4E3A ajax \u65F6\uFF0C\u8BF7\u8BBE\u7F6E api \u5C5E\u6027"))];
560
625
  }
561
- });
562
- }); });
563
- }
564
- else {
565
- // type为submit,但是没有配api以及target时,只派发事件
566
- dispatchEvent('submitSucc', (0, helper_1.createObject)(_this.props.data, values));
567
- }
568
- return Promise.resolve(null);
569
- })
570
- .then(function (values) {
571
- // 有可能 onSubmit return false 了,那么后面的就不应该再执行了。
572
- if (values === false) {
573
- return store.data;
574
- }
575
- if (onFinished && onFinished(values, action) === false) {
576
- return values;
577
- }
578
- resetAfterSubmit && store.reset(onReset);
579
- clearAfterSubmit && store.clear(onReset);
580
- clearPersistDataAfterSubmit && store.clearLocalPersistData();
581
- if (action.redirect || redirect) {
582
- var finalRedirect = (0, tpl_1.filter)(action.redirect || redirect, store.data);
583
- finalRedirect && env.jumpTo(finalRedirect, action);
584
- }
585
- else if (action.reload || reload) {
586
- _this.reloadTarget(action.reload || reload, store.data);
587
- }
588
- action.close && _this.closeTarget(action.close);
589
- return values;
590
- })
591
- .catch(function (reason) {
592
- if (reason instanceof helper_1.SkipOperation) {
593
- return;
594
- }
595
- onFailed && onFailed(reason, store.errors);
596
- if (throwErrors) {
597
- throw reason;
598
- }
599
- });
600
- }
601
- else if (action.type === 'reset' || action.actionType === 'reset') {
602
- store.setCurrentAction(action);
603
- store.reset(onReset);
604
- }
605
- else if (action.actionType === 'clear') {
606
- store.setCurrentAction(action);
607
- store.clear(onReset);
608
- }
609
- else if (action.actionType === 'validate') {
610
- store.setCurrentAction(action);
611
- this.validate(true);
612
- }
613
- else if (action.actionType === 'dialog') {
614
- store.setCurrentAction(action);
615
- store.openDialog(data);
616
- }
617
- else if (action.actionType === 'drawer') {
618
- store.setCurrentAction(action);
619
- store.openDrawer(data);
620
- }
621
- else if (action.actionType === 'ajax') {
622
- store.setCurrentAction(action);
623
- if (!(0, api_1.isEffectiveApi)(action.api)) {
624
- return env.alert(__("\u5F53 actionType \u4E3A ajax \u65F6\uFF0C\u8BF7\u8BBE\u7F6E api \u5C5E\u6027"));
625
- }
626
- return store
627
- .saveRemote(action.api, data, {
628
- successMessage: __((action.messages && action.messages.success) || saveSuccess),
629
- errorMessage: __((action.messages && action.messages.failed) || saveFailed)
630
- })
631
- .then(function (response) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
632
- var redirect;
633
- return (0, tslib_1.__generator)(this, function (_a) {
634
- switch (_a.label) {
635
- case 0:
636
- response &&
637
- onChange &&
638
- onChange(store.data, (0, helper_1.difference)(store.data, store.pristine), this.props);
639
- if (!store.validated) return [3 /*break*/, 2];
640
- return [4 /*yield*/, this.validate(true)];
641
- case 1:
642
- _a.sent();
643
- _a.label = 2;
644
- case 2:
645
- if (!(action.feedback && (0, helper_1.isVisible)(action.feedback, store.data))) return [3 /*break*/, 4];
646
- return [4 /*yield*/, this.openFeedback(action.feedback, store.data)];
647
- case 3:
648
- _a.sent();
649
- _a.label = 4;
650
- case 4:
651
- redirect = action.redirect && (0, tpl_1.filter)(action.redirect, store.data);
652
- redirect && env.jumpTo(redirect, action);
653
- action.reload && this.reloadTarget(action.reload, store.data);
654
- action.close && this.closeTarget(action.close);
655
- return [2 /*return*/];
656
- }
657
- });
658
- }); })
659
- .catch(function (e) {
660
- onFailed && onFailed(e, store.errors);
661
- if (throwErrors || action.countDown) {
662
- throw e;
626
+ return [2 /*return*/, store
627
+ .saveRemote(action.api, data, {
628
+ successMessage: __((action.messages && action.messages.success) || saveSuccess),
629
+ errorMessage: __((action.messages && action.messages.failed) || saveFailed)
630
+ })
631
+ .then(function (response) { return (0, tslib_1.__awaiter)(_this, void 0, void 0, function () {
632
+ var redirect;
633
+ return (0, tslib_1.__generator)(this, function (_a) {
634
+ switch (_a.label) {
635
+ case 0:
636
+ response &&
637
+ onChange &&
638
+ onChange(store.data, (0, helper_1.difference)(store.data, store.pristine), this.props);
639
+ if (!store.validated) return [3 /*break*/, 2];
640
+ return [4 /*yield*/, this.validate(true)];
641
+ case 1:
642
+ _a.sent();
643
+ _a.label = 2;
644
+ case 2:
645
+ if (!(action.feedback && (0, helper_1.isVisible)(action.feedback, store.data))) return [3 /*break*/, 4];
646
+ return [4 /*yield*/, this.openFeedback(action.feedback, store.data)];
647
+ case 3:
648
+ _a.sent();
649
+ _a.label = 4;
650
+ case 4:
651
+ redirect = action.redirect && (0, tpl_1.filter)(action.redirect, store.data);
652
+ redirect && env.jumpTo(redirect, action);
653
+ action.reload && this.reloadTarget(action.reload, store.data);
654
+ action.close && this.closeTarget(action.close);
655
+ return [2 /*return*/];
656
+ }
657
+ });
658
+ }); })
659
+ .catch(function (e) {
660
+ onFailed && onFailed(e, store.errors);
661
+ if (throwErrors || action.countDown) {
662
+ throw e;
663
+ }
664
+ })];
665
+ }
666
+ else if (action.actionType === 'reload') {
667
+ store.setCurrentAction(action);
668
+ if (action.target) {
669
+ this.reloadTarget(action.target, data);
670
+ }
671
+ else {
672
+ this.receive(data);
673
+ }
674
+ // action.target && this.reloadTarget(action.target, data);
675
+ }
676
+ else if (onAction) {
677
+ // 不识别的丢给上层去处理。
678
+ return [2 /*return*/, onAction(e, action, data, throwErrors, delegate || this.context)];
679
+ }
680
+ return [2 /*return*/];
663
681
  }
664
682
  });
665
- }
666
- else if (action.actionType === 'reload') {
667
- store.setCurrentAction(action);
668
- if (action.target) {
669
- this.reloadTarget(action.target, data);
670
- }
671
- else {
672
- this.receive(data);
673
- }
674
- // action.target && this.reloadTarget(action.target, data);
675
- }
676
- else if (onAction) {
677
- // 不识别的丢给上层去处理。
678
- return onAction(e, action, data, throwErrors, delegate || this.context);
679
- }
683
+ });
680
684
  };
681
685
  Form.prototype.handleQuery = function (query) {
682
686
  var _a, _b;