@spartan-ng/brain 1.10.0-beta.2 → 1.10.0-beta.4

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.
@@ -631,6 +631,30 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "21.2.16", ngImpo
631
631
  }]
632
632
  }], ctorParameters: () => [] });
633
633
 
634
+ /**
635
+ * Enabled day in `month` closest to `preferred`'s day-of-month.
636
+ * Undefined when every day in the month is disabled.
637
+ * An earlier day wins a distance tie.
638
+ */
639
+ function nearestEnabledDateInMonth(month, preferred, adapter, isDisabled) {
640
+ const preferredDay = adapter.getDate(preferred);
641
+ let cursor = adapter.startOfMonth(month);
642
+ const end = adapter.endOfMonth(month);
643
+ let best;
644
+ let bestDistance = Number.POSITIVE_INFINITY;
645
+ for (let step = 0; step < 31 && !adapter.isAfter(cursor, end); step++) {
646
+ if (!isDisabled(cursor)) {
647
+ const distance = Math.abs(adapter.getDate(cursor) - preferredDay);
648
+ if (distance < bestDistance) {
649
+ best = cursor;
650
+ bestDistance = distance;
651
+ }
652
+ }
653
+ cursor = adapter.add(cursor, { days: 1 });
654
+ }
655
+ return best;
656
+ }
657
+
634
658
  class BrnCalendarNextButton {
635
659
  /** Access the calendar */
636
660
  _calendar = injectBrnCalendar();
@@ -638,27 +662,26 @@ class BrnCalendarNextButton {
638
662
  _dateAdapter = injectDateAdapter();
639
663
  /** Access the calendar i18n */
640
664
  _i18n = injectBrnCalendarI18n();
665
+ /**
666
+ * An enabled day in the next month, closest to the focused day number.
667
+ * Undefined when the whole month is outside min/max or rejected by `dateDisabled`.
668
+ */
669
+ _enabledTarget = computed(() => {
670
+ const targetDate = this.nextMonthDate();
671
+ return nearestEnabledDateInMonth(targetDate, targetDate, this._dateAdapter, (date) => this._calendar.isDateDisabled(date));
672
+ }, ...(ngDevMode ? [{ debugName: "_enabledTarget" }] : /* istanbul ignore next */ []));
641
673
  /** Whether navigating to the next month is disabled. */
642
- _disabled = computed(() => {
643
- const { targetDate, possibleDate } = this.nextMonthTarget();
644
- const effective = this._dateAdapter.isSameMonth(possibleDate, targetDate) ? possibleDate : targetDate;
645
- return this._calendar.isDateDisabled(effective);
646
- }, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
674
+ _disabled = computed(() => this._enabledTarget() === undefined, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
647
675
  /** Focus the next month */
648
676
  focusNextMonth() {
649
- if (this._disabled()) {
677
+ const date = this._enabledTarget();
678
+ if (date === undefined) {
650
679
  return;
651
680
  }
652
- const { targetDate, possibleDate } = this.nextMonthTarget();
653
- if (this._dateAdapter.isSameMonth(possibleDate, targetDate)) {
654
- // if this date is within the same month, then focus it
655
- this._calendar.setFocusedDate(possibleDate);
656
- return;
657
- }
658
- this._calendar.setFocusedDate(targetDate);
681
+ this._calendar.setFocusedDate(date);
659
682
  }
660
- /** @internal The target date in the next month and its constrained counterpart. */
661
- nextMonthTarget() {
683
+ /** The same day number in the next month, clamped to that month's length. */
684
+ nextMonthDate() {
662
685
  const focusedDate = this._calendar.focusedDate();
663
686
  const date = this._dateAdapter.getDate(focusedDate);
664
687
  // go to start of month first, then add 1 month to avoid day overflow
@@ -666,15 +689,10 @@ class BrnCalendarNextButton {
666
689
  nextMonthTarget = this._dateAdapter.add(nextMonthTarget, { months: 1 });
667
690
  const lastDay = this._dateAdapter.endOfMonth(nextMonthTarget);
668
691
  // if we are on a date that does not exist in the next month, clamp to the last day of the month.
669
- let targetDate;
670
692
  if (date > this._dateAdapter.getDate(lastDay)) {
671
- targetDate = lastDay;
672
- }
673
- else {
674
- targetDate = this._dateAdapter.set(nextMonthTarget, { day: date });
693
+ return lastDay;
675
694
  }
676
- // if the date is disabled, but there are available dates in the month, focus the constrained date.
677
- return { targetDate, possibleDate: this._calendar.constrainDate(targetDate) };
695
+ return this._dateAdapter.set(nextMonthTarget, { day: date });
678
696
  }
679
697
  /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnCalendarNextButton, deps: [], target: i0.ɵɵFactoryTarget.Directive });
680
698
  /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.16", type: BrnCalendarNextButton, isStandalone: true, selector: "button[brnCalendarNextButton]", host: { attributes: { "type": "button", "data-slot": "calendar-next-button" }, listeners: { "click": "focusNextMonth()" }, properties: { "attr.aria-label": "_i18n.config().labelNext()", "attr.aria-disabled": "_disabled() ? true : null", "disabled": "_disabled()" } }, exportAs: ["brnCalendarNextButton"], ngImport: i0 });
@@ -702,27 +720,26 @@ class BrnCalendarPreviousButton {
702
720
  _dateAdapter = injectDateAdapter();
703
721
  /** Access the calendar i18n */
704
722
  _i18n = injectBrnCalendarI18n();
723
+ /**
724
+ * An enabled day in the previous month, closest to the focused day number.
725
+ * Undefined when the whole month is outside min/max or rejected by `dateDisabled`.
726
+ */
727
+ _enabledTarget = computed(() => {
728
+ const targetDate = this.previousMonthDate();
729
+ return nearestEnabledDateInMonth(targetDate, targetDate, this._dateAdapter, (date) => this._calendar.isDateDisabled(date));
730
+ }, ...(ngDevMode ? [{ debugName: "_enabledTarget" }] : /* istanbul ignore next */ []));
705
731
  /** Whether navigating to the previous month is disabled. */
706
- _disabled = computed(() => {
707
- const { targetDate, possibleDate } = this.previousMonthTarget();
708
- const effective = this._dateAdapter.isSameMonth(possibleDate, targetDate) ? possibleDate : targetDate;
709
- return this._calendar.isDateDisabled(effective);
710
- }, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
732
+ _disabled = computed(() => this._enabledTarget() === undefined, ...(ngDevMode ? [{ debugName: "_disabled" }] : /* istanbul ignore next */ []));
711
733
  /** Focus the previous month */
712
734
  focusPreviousMonth() {
713
- if (this._disabled()) {
735
+ const date = this._enabledTarget();
736
+ if (date === undefined) {
714
737
  return;
715
738
  }
716
- const { targetDate, possibleDate } = this.previousMonthTarget();
717
- if (this._dateAdapter.isSameMonth(possibleDate, targetDate)) {
718
- // if this date is within the same month, then focus it
719
- this._calendar.setFocusedDate(possibleDate);
720
- return;
721
- }
722
- this._calendar.setFocusedDate(targetDate);
739
+ this._calendar.setFocusedDate(date);
723
740
  }
724
- /** @internal The target date in the previous month and its constrained counterpart. */
725
- previousMonthTarget() {
741
+ /** The same day number in the previous month, clamped to that month's length. */
742
+ previousMonthDate() {
726
743
  const focusedDate = this._calendar.focusedDate();
727
744
  const date = this._dateAdapter.getDate(focusedDate);
728
745
  // go to start of month first, then subtract 1 month to avoid day overflow
@@ -730,15 +747,10 @@ class BrnCalendarPreviousButton {
730
747
  previousMonthTarget = this._dateAdapter.subtract(previousMonthTarget, { months: 1 });
731
748
  const lastDay = this._dateAdapter.endOfMonth(previousMonthTarget);
732
749
  // if we are on a date that does not exist in the previous month, clamp to the last day of the month.
733
- let targetDate;
734
750
  if (date > this._dateAdapter.getDate(lastDay)) {
735
- targetDate = lastDay;
736
- }
737
- else {
738
- targetDate = this._dateAdapter.set(previousMonthTarget, { day: date });
751
+ return lastDay;
739
752
  }
740
- // if the date is disabled, but there are available dates in the month, focus the constrained date.
741
- return { targetDate, possibleDate: this._calendar.constrainDate(targetDate) };
753
+ return this._dateAdapter.set(previousMonthTarget, { day: date });
742
754
  }
743
755
  /** @nocollapse */ static ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "21.2.16", ngImport: i0, type: BrnCalendarPreviousButton, deps: [], target: i0.ɵɵFactoryTarget.Directive });
744
756
  /** @nocollapse */ static ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "21.2.16", type: BrnCalendarPreviousButton, isStandalone: true, selector: "button[brnCalendarPreviousButton]", host: { attributes: { "type": "button", "data-slot": "calendar-previous-button" }, listeners: { "click": "focusPreviousMonth()" }, properties: { "attr.aria-label": "_i18n.config().labelPrevious()", "attr.aria-disabled": "_disabled() ? true : null", "disabled": "_disabled()" } }, exportAs: ["brnCalendarPreviousButton"], ngImport: i0 });