scrollspy.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { B as H, W as x, d as T, M as C, N as B, K as d, a6 as D, t as O, m as k, a7 as S, _ as p, a as z, G as b, Z as G, P as M, E as N, r as W, v as Z } from "./base-component-BMXjNJAi.mjs";
  2. import { y as $ } from "./index-CnhA8i0D.mjs";
  3. import { a as f } from "./activeClass-iqaD75Su.mjs";
  4. import { i as q } from "./isDisabled-BG5MoQVt.mjs";
  5. const F = "scrollspy", I = "ScrollSpy", K = '[data-bs-spy="scroll"]', P = "[href]", Q = {
  6. offset: 10,
  7. target: void 0
  8. }, j = (o) => O(o, I), J = (o) => new U(o), _ = k(`activate.bs.${F}`), R = (o) => {
  9. const {
  10. target: t,
  11. _itemsLength: r,
  12. _observables: e
  13. } = o, s = S("A", t), n = T(t);
  14. !s.length || r === e.size || (e.clear(), Array.from(s).forEach((l) => {
  15. const a = d(l, "href")?.slice(1), i = a?.length ? n.getElementById(a) : null;
  16. i && !q(l) && o._observables.set(i, l);
  17. }), o._itemsLength = o._observables.size);
  18. }, E = (o) => {
  19. Array.from(S("A", o)).forEach(
  20. (t) => {
  21. p(t, f) && z(t, f);
  22. }
  23. );
  24. }, y = (o, t) => {
  25. const { target: r, element: e } = o;
  26. E(r), o._activeItem = t, b(t, f);
  27. let s = t;
  28. for (; s !== r; )
  29. if (s = s.parentElement, ["nav", "dropdown-menu", "list-group"].some(
  30. (n) => p(s, n)
  31. )) {
  32. const n = s.previousElementSibling;
  33. n && !p(n, f) && b(n, f);
  34. }
  35. _.relatedTarget = t, G(e, _);
  36. }, g = (o, t) => {
  37. const { scrollTarget: r, element: e, options: s } = o;
  38. return (r !== e ? M(t).top + r.scrollTop : t.offsetTop) - (s.offset || 10);
  39. };
  40. class U extends H {
  41. static selector = K;
  42. static init = J;
  43. static getInstance = j;
  44. constructor(t, r) {
  45. super(t, r);
  46. const { element: e, options: s } = this, n = x(
  47. s.target,
  48. T(e)
  49. );
  50. n && (this.target = n, this.scrollTarget = e.clientHeight < e.scrollHeight ? e : C(e), this._observables = /* @__PURE__ */ new Map(), this.refresh(), this._observer = new $(() => {
  51. requestAnimationFrame(() => this.refresh());
  52. }, {
  53. root: this.scrollTarget
  54. }), this._toggleEventListeners(!0));
  55. }
  56. get name() {
  57. return I;
  58. }
  59. get defaults() {
  60. return Q;
  61. }
  62. refresh = () => {
  63. const { target: t, scrollTarget: r } = this;
  64. if (!t || t.offsetHeight === 0) return;
  65. R(this);
  66. const { _itemsLength: e, _observables: s, _activeItem: n } = this;
  67. if (!e) return;
  68. const l = s.entries().toArray(), { scrollTop: a, scrollHeight: i, clientHeight: A } = r;
  69. if (a >= i - A) {
  70. const c = l[e - 1]?.[1];
  71. n !== c && y(this, c);
  72. return;
  73. }
  74. const h = l[0]?.[0] ? g(this, l[0][0]) : null;
  75. if (h !== null && a < h && h > 0) {
  76. this._activeItem = null, E(t);
  77. return;
  78. }
  79. for (let c = 0; c < e; c += 1) {
  80. const [w, m] = l[c], L = g(this, w), u = l[c + 1]?.[0], v = u ? g(this, u) : null;
  81. if (n !== m && a >= L && (v === null || a < v)) {
  82. y(this, m);
  83. break;
  84. }
  85. }
  86. };
  87. _scrollTo = (t) => {
  88. const r = B(t.target, P), e = r && d(r, "href")?.slice(1), s = e && D(e, this.target);
  89. s && (this.scrollTarget.scrollTo({
  90. top: s.offsetTop,
  91. behavior: "smooth"
  92. }), t.preventDefault());
  93. };
  94. _toggleEventListeners = (t) => {
  95. const { target: r, _observables: e, _observer: s, _scrollTo: n } = this;
  96. (t ? N : W)(r, Z, n), t ? e?.forEach((a, i) => s.observe(i)) : s.disconnect();
  97. };
  98. dispose() {
  99. this._toggleEventListeners(), super.dispose();
  100. }
  101. }
  102. export {
  103. U as default
  104. };
  105. //# sourceMappingURL=scrollspy.mjs.map