s4y-ui 5.2.1 → 5.3.1

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.
@@ -3351,6 +3351,25 @@ async function toMaybeBooleanPromise(value) {
3351
3351
  return await firstValueFrom(value);
3352
3352
  return value;
3353
3353
  }
3354
+ async function toHandlerResult(ret) {
3355
+ if (ret instanceof Promise)
3356
+ return await ret;
3357
+ if (ret instanceof EventEmitter)
3358
+ return await firstValueFrom(ret);
3359
+ if (isObservable(ret))
3360
+ return await firstValueFrom(ret);
3361
+ return ret;
3362
+ }
3363
+ async function allow(canDismiss, reason) {
3364
+ if (!canDismiss)
3365
+ return true;
3366
+ try {
3367
+ return await Promise.resolve(canDismiss(reason));
3368
+ }
3369
+ catch {
3370
+ return false;
3371
+ }
3372
+ }
3354
3373
 
3355
3374
  class DynamicDialogRef {
3356
3375
  overlayRef;
@@ -3363,6 +3382,8 @@ class DynamicDialogRef {
3363
3382
  _afterClosed = new Subject();
3364
3383
  panelEl;
3365
3384
  isClosing = false;
3385
+ _container;
3386
+ _opts;
3366
3387
  constructor(overlayRef) {
3367
3388
  this.overlayRef = overlayRef;
3368
3389
  this.afterOpened$ = this._afterOpened.asObservable();
@@ -3445,6 +3466,63 @@ class DynamicDialogRef {
3445
3466
  }
3446
3467
  this.overlayRef.dispose();
3447
3468
  }
3469
+ _attachForButtonUpdates(container, opts) {
3470
+ this._container = container;
3471
+ this._opts = opts;
3472
+ // garantir objetos
3473
+ this._opts.okBtnOptions ??= {};
3474
+ this._opts.cancelBtnOptions ??= {};
3475
+ }
3476
+ updateButtons(patch) {
3477
+ if (!this._opts)
3478
+ return;
3479
+ if (patch.ok)
3480
+ Object.assign(this._opts.okBtnOptions, patch.ok);
3481
+ if (patch.cancel)
3482
+ Object.assign(this._opts.cancelBtnOptions, patch.cancel);
3483
+ this._markForCheck();
3484
+ }
3485
+ // OK
3486
+ setOkLoading(state) {
3487
+ this._ensureBtn('ok').loading = state;
3488
+ // se você usa okLoading interno como overlay, mantenha em sincronia:
3489
+ this._container.okLoading = !!state;
3490
+ this._markForCheck();
3491
+ }
3492
+ setOkDisabled(state) {
3493
+ this._ensureBtn('ok').disabled = state;
3494
+ this._markForCheck();
3495
+ }
3496
+ setOkText(text) {
3497
+ this._ensureBtn('ok').text = text;
3498
+ this._markForCheck();
3499
+ }
3500
+ // Cancel
3501
+ setCancelLoading(state) {
3502
+ this._ensureBtn('cancel').loading = state;
3503
+ this._container.cancelLoading = !!state;
3504
+ this._markForCheck();
3505
+ }
3506
+ setCancelDisabled(state) {
3507
+ this._ensureBtn('cancel').disabled = state;
3508
+ this._markForCheck();
3509
+ }
3510
+ setCancelText(text) {
3511
+ this._ensureBtn('cancel').text = text;
3512
+ this._markForCheck();
3513
+ }
3514
+ _ensureBtn(which) {
3515
+ if (!this._opts)
3516
+ throw new Error('Dialog options not attached.');
3517
+ const key = which === 'ok' ? 'okBtnOptions' : 'cancelBtnOptions';
3518
+ this._opts[key] ??= {};
3519
+ return this._opts[key];
3520
+ }
3521
+ _markForCheck() {
3522
+ const cdr = this._container?.cdr;
3523
+ if (cdr?.markForCheck)
3524
+ cdr.markForCheck();
3525
+ }
3448
3526
  }
3449
3527
 
3450
3528
  class DynamicDialogComponent {
@@ -3472,10 +3550,11 @@ class DynamicDialogComponent {
3472
3550
  return;
3473
3551
  this.okLoading = true;
3474
3552
  try {
3475
- const res = await toMaybeBooleanPromise(this.dialogOptions?.onOk?.({ data: this.injectedData, ref: this.ref }));
3476
- if (res === false)
3553
+ const out = await toHandlerResult(this.dialogOptions?.onOk?.({ data: this.injectedData, ref: this.ref }));
3554
+ if (out === false)
3477
3555
  return;
3478
- await this.ref.closeAnimated(undefined, 'ok');
3556
+ const result = out === true || typeof out === 'undefined' ? undefined : out;
3557
+ await this.ref.closeAnimated(result, 'ok');
3479
3558
  }
3480
3559
  finally {
3481
3560
  this.okLoading = false;
@@ -3485,15 +3564,16 @@ class DynamicDialogComponent {
3485
3564
  async onCancel() {
3486
3565
  if (this.cancelLoading)
3487
3566
  return;
3488
- this.cancelLoading = true;
3567
+ // this.cancelLoading = true;
3489
3568
  try {
3490
- const res = await toMaybeBooleanPromise(this.dialogOptions?.onCancel?.({
3569
+ const out = await toHandlerResult(this.dialogOptions?.onCancel?.({
3491
3570
  data: this.injectedData,
3492
3571
  ref: this.ref,
3493
3572
  }));
3494
- if (res === false)
3495
- return;
3496
- await this.ref.closeAnimated(undefined, 'cancel');
3573
+ if (out === false)
3574
+ return; // NÃO fecha
3575
+ const result = out === true || typeof out === 'undefined' ? undefined : out;
3576
+ await this.ref.closeAnimated(result, 'cancel');
3497
3577
  }
3498
3578
  finally {
3499
3579
  this.cancelLoading = false;
@@ -3546,19 +3626,26 @@ class DynamicDialogService {
3546
3626
  classes.forEach((c) => overlayRef.addPanelClass(c));
3547
3627
  }
3548
3628
  const dialogRef = new DynamicDialogRef(overlayRef);
3629
+ const untilClosed$ = dialogRef.afterClosed$.pipe(take(1));
3549
3630
  if (opts.closeOnBackdrop) {
3550
3631
  overlayRef
3551
3632
  .backdropClick()
3552
- .pipe(take(1))
3553
- .subscribe(() => {
3633
+ .pipe(takeUntil(untilClosed$))
3634
+ .subscribe(async () => {
3635
+ if (!(await allow(opts.canDismiss, 'backdrop')))
3636
+ return;
3554
3637
  dialogRef.closeAnimated(undefined, 'backdrop');
3555
3638
  });
3556
3639
  }
3557
3640
  if (opts.closeOnEsc !== false) {
3558
3641
  overlayRef
3559
3642
  .keydownEvents()
3560
- .pipe(filter$1((e) => e.key === 'Escape' || e.key === 'Esc'), take(1))
3561
- .subscribe(() => dialogRef.closeAnimated(undefined, 'esc'));
3643
+ .pipe(filter$1((e) => e.key === 'Escape' || e.key === 'Esc'), takeUntil(untilClosed$))
3644
+ .subscribe(async () => {
3645
+ if (!(await allow(opts.canDismiss, 'esc')))
3646
+ return;
3647
+ dialogRef.closeAnimated(undefined, 'esc');
3648
+ });
3562
3649
  }
3563
3650
  // injeta o ref + dados no componente container
3564
3651
  const injector = Injector.create({
@@ -3572,6 +3659,7 @@ class DynamicDialogService {
3572
3659
  // anexa o container
3573
3660
  const containerPortal = new ComponentPortal(DynamicDialogComponent, null, injector);
3574
3661
  const containerRef = overlayRef.attach(containerPortal);
3662
+ dialogRef._attachForButtonUpdates(containerRef.instance, opts);
3575
3663
  const templateCtx = {
3576
3664
  $implicit: opts.data,
3577
3665
  data: opts.data,