@maxax/hooks 1.0.0

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/dist/index.mjs ADDED
@@ -0,0 +1,846 @@
1
+ // src/use-boolean.ts
2
+ import { ref } from "vue";
3
+ function useBoolean(initValue = false) {
4
+ const bool = ref(initValue);
5
+ function setBool(value) {
6
+ bool.value = value;
7
+ }
8
+ function setTrue() {
9
+ setBool(true);
10
+ }
11
+ function setFalse() {
12
+ setBool(false);
13
+ }
14
+ function toggle() {
15
+ setBool(!bool.value);
16
+ }
17
+ return {
18
+ bool,
19
+ setBool,
20
+ setTrue,
21
+ setFalse,
22
+ toggle
23
+ };
24
+ }
25
+
26
+ // src/use-breakpoint.ts
27
+ import { computed, ref as ref3, unref as unref2 } from "vue";
28
+
29
+ // src/enum/breakpointEnum.ts
30
+ var SizeEnum = /* @__PURE__ */ ((SizeEnum2) => {
31
+ SizeEnum2["XS"] = "XS";
32
+ SizeEnum2["SM"] = "SM";
33
+ SizeEnum2["MD"] = "MD";
34
+ SizeEnum2["LG"] = "LG";
35
+ SizeEnum2["XL"] = "XL";
36
+ SizeEnum2["XXL"] = "XXL";
37
+ return SizeEnum2;
38
+ })(SizeEnum || {});
39
+ var ScreenEnum = /* @__PURE__ */ ((ScreenEnum2) => {
40
+ ScreenEnum2[ScreenEnum2["XS"] = 320] = "XS";
41
+ ScreenEnum2[ScreenEnum2["SM"] = 640] = "SM";
42
+ ScreenEnum2[ScreenEnum2["MD"] = 768] = "MD";
43
+ ScreenEnum2[ScreenEnum2["LG"] = 960] = "LG";
44
+ ScreenEnum2[ScreenEnum2["XL"] = 1280] = "XL";
45
+ ScreenEnum2[ScreenEnum2["XXL"] = 1536] = "XXL";
46
+ return ScreenEnum2;
47
+ })(ScreenEnum || {});
48
+ var screenMap = /* @__PURE__ */ new Map();
49
+ screenMap.set("XS" /* XS */, 320 /* XS */);
50
+ screenMap.set("SM" /* SM */, 640 /* SM */);
51
+ screenMap.set("MD" /* MD */, 768 /* MD */);
52
+ screenMap.set("LG" /* LG */, 960 /* LG */);
53
+ screenMap.set("XL" /* XL */, 1280 /* XL */);
54
+ screenMap.set("XXL" /* XXL */, 1536 /* XXL */);
55
+
56
+ // src/use-event-listener.ts
57
+ import { ref as ref2, unref, watch } from "vue";
58
+ import { useDebounceFn, useThrottleFn } from "@vueuse/core";
59
+ function useEventListener({ el = window, name, listener, options, autoRemove = true, isDebounce = true, wait = 80 }) {
60
+ let remove = () => {
61
+ };
62
+ const isAddRef = ref2(false);
63
+ if (el) {
64
+ const element = ref2(el);
65
+ const handler = isDebounce ? useDebounceFn(listener, wait) : useThrottleFn(listener, wait);
66
+ const realHandler = wait ? handler : listener;
67
+ const removeEventListener = (e) => {
68
+ isAddRef.value = true;
69
+ e.removeEventListener(name, realHandler, options);
70
+ };
71
+ const addEventListener = (e) => e.addEventListener(name, realHandler, options);
72
+ const removeWatch = watch(
73
+ element,
74
+ (v, _ov, cleanUp) => {
75
+ if (v) {
76
+ !unref(isAddRef) && addEventListener(v);
77
+ cleanUp(() => {
78
+ autoRemove && removeEventListener(v);
79
+ });
80
+ }
81
+ },
82
+ { immediate: true }
83
+ );
84
+ remove = () => {
85
+ removeEventListener(element.value);
86
+ removeWatch();
87
+ };
88
+ }
89
+ return { removeEvent: remove };
90
+ }
91
+
92
+ // src/use-breakpoint.ts
93
+ var globalScreenRef;
94
+ var globalWidthRef;
95
+ var globalRealWidthRef;
96
+ function useBreakpoint() {
97
+ return {
98
+ screenRef: computed(() => unref2(globalScreenRef)),
99
+ widthRef: globalWidthRef,
100
+ screenEnum: ScreenEnum,
101
+ realWidthRef: globalRealWidthRef
102
+ };
103
+ }
104
+ function createBreakpointListen(fn) {
105
+ const screenRef = ref3("XL" /* XL */);
106
+ const realWidthRef = ref3(window.innerWidth);
107
+ function getWindowWidth() {
108
+ const width = document.body.clientWidth;
109
+ const xs = screenMap.get("XS" /* XS */);
110
+ const sm = screenMap.get("SM" /* SM */);
111
+ const md = screenMap.get("MD" /* MD */);
112
+ const lg = screenMap.get("LG" /* LG */);
113
+ const xl = screenMap.get("XL" /* XL */);
114
+ if (width < xs) {
115
+ screenRef.value = "XS" /* XS */;
116
+ } else if (width < sm) {
117
+ screenRef.value = "SM" /* SM */;
118
+ } else if (width < md) {
119
+ screenRef.value = "MD" /* MD */;
120
+ } else if (width < lg) {
121
+ screenRef.value = "LG" /* LG */;
122
+ } else if (width < xl) {
123
+ screenRef.value = "XL" /* XL */;
124
+ } else {
125
+ screenRef.value = "XXL" /* XXL */;
126
+ }
127
+ realWidthRef.value = width;
128
+ }
129
+ useEventListener({
130
+ el: window,
131
+ name: "resize",
132
+ listener: () => {
133
+ getWindowWidth();
134
+ resizeFn();
135
+ }
136
+ // Wait: 100,
137
+ });
138
+ getWindowWidth();
139
+ globalScreenRef = computed(() => unref2(screenRef));
140
+ globalWidthRef = computed(() => screenMap.get(unref2(screenRef)));
141
+ globalRealWidthRef = computed(() => unref2(realWidthRef));
142
+ function resizeFn() {
143
+ fn == null ? void 0 : fn({
144
+ screen: globalScreenRef,
145
+ width: globalWidthRef,
146
+ realWidth: globalRealWidthRef,
147
+ screenEnum: ScreenEnum,
148
+ screenMap,
149
+ sizeEnum: SizeEnum
150
+ });
151
+ }
152
+ resizeFn();
153
+ return {
154
+ screenRef: globalScreenRef,
155
+ screenEnum: ScreenEnum,
156
+ widthRef: globalWidthRef,
157
+ realWidthRef: globalRealWidthRef
158
+ };
159
+ }
160
+
161
+ // src/use-browser-location.ts
162
+ import { onMounted, onUnmounted, ref as ref4 } from "vue";
163
+ var isBrowser = typeof document !== "undefined" && typeof window !== "undefined";
164
+ function useBrowserLocation(customWindow = isBrowser ? window : null) {
165
+ const getWindowLocation = () => {
166
+ const { hash, host, hostname, href, origin, pathname, port, protocol, search } = (customWindow == null ? void 0 : customWindow.location) || {};
167
+ return {
168
+ hash,
169
+ host,
170
+ hostname,
171
+ href,
172
+ origin,
173
+ pathname,
174
+ port,
175
+ protocol,
176
+ search
177
+ };
178
+ };
179
+ const locationState = ref4(getWindowLocation());
180
+ const updateLocation = () => {
181
+ locationState.value = getWindowLocation();
182
+ };
183
+ onMounted(() => {
184
+ if (customWindow) {
185
+ customWindow.addEventListener("popstate", updateLocation);
186
+ customWindow.addEventListener("hashchange", updateLocation);
187
+ }
188
+ });
189
+ onUnmounted(() => {
190
+ if (customWindow) {
191
+ customWindow.removeEventListener("popstate", updateLocation);
192
+ customWindow.removeEventListener("hashchange", updateLocation);
193
+ }
194
+ });
195
+ return locationState;
196
+ }
197
+
198
+ // src/use-collection.ts
199
+ import { getCurrentInstance, inject, onBeforeUnmount, onMounted as onMounted2, watch as watch2 } from "vue";
200
+ function useInjectionInstanceCollection(injectionName, collectionKey, registerKeyRef) {
201
+ var _a;
202
+ const injection = inject(injectionName, null);
203
+ if (injection === null) return;
204
+ const vm = (_a = getCurrentInstance()) == null ? void 0 : _a.proxy;
205
+ watch2(registerKeyRef, registerInstance);
206
+ registerInstance(registerKeyRef.value);
207
+ onBeforeUnmount(() => {
208
+ registerInstance(void 0, registerKeyRef.value);
209
+ });
210
+ function registerInstance(key, oldKey) {
211
+ if (!injection) return;
212
+ const collection = injection[collectionKey];
213
+ if (oldKey !== void 0) removeInstance(collection, oldKey);
214
+ if (key !== void 0) addInstance(collection, key);
215
+ }
216
+ function removeInstance(collection, key) {
217
+ if (!collection[key]) collection[key] = [];
218
+ collection[key].splice(
219
+ collection[key].findIndex((instance) => instance === vm),
220
+ 1
221
+ );
222
+ }
223
+ function addInstance(collection, key) {
224
+ if (!collection[key]) collection[key] = [];
225
+ if (!~collection[key].findIndex((instance) => instance === vm)) {
226
+ collection[key].push(vm);
227
+ }
228
+ }
229
+ }
230
+ function useInjectionCollection(injectionName, collectionKey, valueRef) {
231
+ const injection = inject(injectionName, null);
232
+ if (injection === null) return;
233
+ if (!(collectionKey in injection)) {
234
+ injection[collectionKey] = [];
235
+ }
236
+ injection[collectionKey].push(valueRef.value);
237
+ watch2(valueRef, (value, prevValue) => {
238
+ const collectionArray = injection[collectionKey];
239
+ const index = collectionArray.findIndex((collectionValue) => collectionValue === prevValue);
240
+ if (~index) collectionArray.splice(index, 1);
241
+ collectionArray.push(value);
242
+ });
243
+ onBeforeUnmount(() => {
244
+ const collectionArray = injection[collectionKey];
245
+ const index = collectionArray.findIndex((collectionValue) => collectionValue === valueRef.value);
246
+ if (~index) collectionArray.splice(index, 1);
247
+ });
248
+ }
249
+ function useInjectionElementCollection(injectionName, collectionKey, getElement) {
250
+ const injection = inject(injectionName, null);
251
+ if (injection === null) return;
252
+ if (!(collectionKey in injection)) {
253
+ injection[collectionKey] = [];
254
+ }
255
+ onMounted2(() => {
256
+ const el = getElement();
257
+ if (!el) return;
258
+ injection[collectionKey].push(el);
259
+ });
260
+ onBeforeUnmount(() => {
261
+ const collectionArray = injection[collectionKey];
262
+ const element = getElement();
263
+ const index = collectionArray.findIndex((collectionElement) => collectionElement === element);
264
+ if (~index) collectionArray.splice(index, 1);
265
+ });
266
+ }
267
+
268
+ // src/use-context.ts
269
+ import { inject as inject2, provide } from "vue";
270
+ function useContext(contextName, fn) {
271
+ const { useProvide, useInject: useStore } = createContext(contextName);
272
+ function setupStore(...args) {
273
+ const context = fn(...args);
274
+ return useProvide(context);
275
+ }
276
+ return {
277
+ /** Setup store in the parent component */
278
+ setupStore,
279
+ /** Use store in the child component */
280
+ useStore
281
+ };
282
+ }
283
+ function createContext(contextName) {
284
+ const injectKey = Symbol(contextName);
285
+ function useProvide(context) {
286
+ provide(injectKey, context);
287
+ return context;
288
+ }
289
+ function useInject() {
290
+ return inject2(injectKey);
291
+ }
292
+ return {
293
+ useProvide,
294
+ useInject
295
+ };
296
+ }
297
+
298
+ // src/use-count-down.ts
299
+ import { computed as computed2, onScopeDispose, ref as ref5 } from "vue";
300
+ import { useRafFn } from "@vueuse/core";
301
+ function useCountDown(seconds) {
302
+ const FPS_PER_SECOND = 60;
303
+ const fps = ref5(0);
304
+ const count = computed2(() => Math.ceil(fps.value / FPS_PER_SECOND));
305
+ const isCounting = computed2(() => fps.value > 0);
306
+ const { pause, resume } = useRafFn(
307
+ () => {
308
+ if (fps.value > 0) {
309
+ fps.value -= 1;
310
+ } else {
311
+ pause();
312
+ }
313
+ },
314
+ { immediate: false }
315
+ );
316
+ function start(updateSeconds = seconds) {
317
+ fps.value = FPS_PER_SECOND * updateSeconds;
318
+ resume();
319
+ }
320
+ function stop() {
321
+ fps.value = 0;
322
+ pause();
323
+ }
324
+ onScopeDispose(() => {
325
+ pause();
326
+ });
327
+ return {
328
+ count,
329
+ isCounting,
330
+ start,
331
+ stop
332
+ };
333
+ }
334
+
335
+ // src/use-deferred-true.ts
336
+ import { ref as ref6, watch as watch3 } from "vue";
337
+ function useDeferredTrue(valueRef, delay, shouldDelayRef) {
338
+ if (!delay) return valueRef;
339
+ const delayedRef = ref6(valueRef.value);
340
+ let timerId = null;
341
+ watch3(valueRef, (value) => {
342
+ if (timerId !== null) window.clearTimeout(timerId);
343
+ if (value === true) {
344
+ if (shouldDelayRef && !shouldDelayRef.value) {
345
+ delayedRef.value = true;
346
+ } else {
347
+ timerId = window.setTimeout(() => {
348
+ delayedRef.value = true;
349
+ }, delay);
350
+ }
351
+ } else {
352
+ delayedRef.value = false;
353
+ }
354
+ });
355
+ return delayedRef;
356
+ }
357
+
358
+ // src/use-is-composing.ts
359
+ import { onBeforeMount, onBeforeUnmount as onBeforeUnmount2, ref as ref7 } from "vue";
360
+ var isComposingRef = ref7(false);
361
+ function compositionStartHandler() {
362
+ isComposingRef.value = true;
363
+ }
364
+ function compositionEndHandler() {
365
+ isComposingRef.value = false;
366
+ }
367
+ var mountedCount = 0;
368
+ function useIsComposing() {
369
+ if (document && window) {
370
+ onBeforeMount(() => {
371
+ if (!mountedCount) {
372
+ window.addEventListener("compositionstart", compositionStartHandler);
373
+ window.addEventListener("compositionend", compositionEndHandler);
374
+ }
375
+ mountedCount++;
376
+ });
377
+ onBeforeUnmount2(() => {
378
+ if (mountedCount <= 1) {
379
+ window.removeEventListener("compositionstart", compositionStartHandler);
380
+ window.removeEventListener("compositionend", compositionEndHandler);
381
+ mountedCount = 0;
382
+ } else {
383
+ mountedCount--;
384
+ }
385
+ });
386
+ }
387
+ return isComposingRef;
388
+ }
389
+
390
+ // src/use-layout-style.ts
391
+ import { computed as computed3, onMounted as onMounted3, onUnmounted as onUnmounted2, ref as ref8 } from "vue";
392
+ import { useCssVar, useDebounceFn as useDebounceFn2 } from "@vueuse/core";
393
+ import { getElementVisibleRect } from "@maxax/utils";
394
+
395
+ // src/const/index.ts
396
+ var CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT = "--max-content-height";
397
+ var CSS_VARIABLE_LAYOUT_CONTENT_WIDTH = "--max-content-width";
398
+ var CSS_VARIABLE_LAYOUT_HEADER_HEIGHT = "--max-header-height";
399
+ var CSS_VARIABLE_LAYOUT_FOOTER_HEIGHT = "--max-footer-height";
400
+
401
+ // src/use-layout-style.ts
402
+ function useLayoutContentStyle(diffHeight = 0) {
403
+ let resizeObserver = null;
404
+ const contentElement = ref8(null);
405
+ const visibleDomRect = ref8(null);
406
+ const contentHeight = useCssVar(CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT);
407
+ const contentWidth = useCssVar(CSS_VARIABLE_LAYOUT_CONTENT_WIDTH);
408
+ const overlayStyle = computed3(() => {
409
+ var _a;
410
+ const { height, left, top, width } = (_a = visibleDomRect.value) != null ? _a : {};
411
+ return {
412
+ height: `${height}px`,
413
+ left: `${left}px`,
414
+ position: "fixed",
415
+ top: `${top}px`,
416
+ width: `${width}px`,
417
+ zIndex: 150
418
+ };
419
+ });
420
+ const debouncedCalcHeight = useDebounceFn2((_entries) => {
421
+ visibleDomRect.value = getElementVisibleRect(contentElement.value);
422
+ contentHeight.value = `${visibleDomRect.value.height - diffHeight}px`;
423
+ contentWidth.value = `${visibleDomRect.value.width}px`;
424
+ }, 16);
425
+ onMounted3(() => {
426
+ if (contentElement.value && !resizeObserver) {
427
+ resizeObserver = new ResizeObserver(debouncedCalcHeight);
428
+ resizeObserver.observe(contentElement.value);
429
+ }
430
+ });
431
+ onUnmounted2(() => {
432
+ resizeObserver == null ? void 0 : resizeObserver.disconnect();
433
+ resizeObserver = null;
434
+ });
435
+ return { contentElement, overlayStyle, visibleDomRect };
436
+ }
437
+ function useLayoutHeaderStyle() {
438
+ const headerHeight = useCssVar(CSS_VARIABLE_LAYOUT_HEADER_HEIGHT);
439
+ return {
440
+ getLayoutHeaderHeight: () => {
441
+ return Number.parseInt(`${headerHeight.value}`, 10);
442
+ },
443
+ setLayoutHeaderHeight: (height) => {
444
+ headerHeight.value = `${height}px`;
445
+ }
446
+ };
447
+ }
448
+ function useLayoutFooterStyle() {
449
+ const footerHeight = useCssVar(CSS_VARIABLE_LAYOUT_FOOTER_HEIGHT);
450
+ return {
451
+ getLayoutFooterHeight: () => {
452
+ return Number.parseInt(`${footerHeight.value}`, 10);
453
+ },
454
+ setLayoutFooterHeight: (height) => {
455
+ footerHeight.value = `${height}px`;
456
+ }
457
+ };
458
+ }
459
+
460
+ // src/use-loading.ts
461
+ function useLoading(initValue = false) {
462
+ const { bool: loading, setTrue: startLoading, setFalse: endLoading } = useBoolean(initValue);
463
+ return {
464
+ loading,
465
+ startLoading,
466
+ endLoading
467
+ };
468
+ }
469
+
470
+ // src/use-local-page.ts
471
+ import { computed as computed4, ref as ref9, unref as unref3 } from "vue";
472
+ function pagination(list, pageNo, pageSize) {
473
+ const offset = (pageNo - 1) * Number(pageSize);
474
+ const ret = offset + Number(pageSize) >= list.length ? list.slice(offset, list.length) : list.slice(offset, offset + Number(pageSize));
475
+ return ret;
476
+ }
477
+ function useLocalPage(list, pageSize) {
478
+ const currentPage = ref9(1);
479
+ const pageSizeRef = ref9(pageSize);
480
+ const listRef = ref9(list);
481
+ const getPaginationList = computed4(() => {
482
+ return pagination(unref3(listRef), unref3(currentPage), unref3(pageSizeRef));
483
+ });
484
+ const getTotal = computed4(() => {
485
+ return unref3(listRef).length;
486
+ });
487
+ function setCurrentPage(page) {
488
+ currentPage.value = page;
489
+ }
490
+ function setPageSize(size) {
491
+ pageSizeRef.value = size;
492
+ }
493
+ function setPagination(page) {
494
+ setCurrentPage(page.currentPage);
495
+ setPageSize(page.pageSize);
496
+ }
497
+ function getPageList() {
498
+ return pagination(unref3(listRef), unref3(currentPage), unref3(pageSizeRef));
499
+ }
500
+ return { setCurrentPage, getTotal, setPageSize, getPaginationList, setPagination, getPageList };
501
+ }
502
+
503
+ // src/use-lock-html-scroll.ts
504
+ import { onBeforeUnmount as onBeforeUnmount3, onMounted as onMounted4, ref as ref10, watch as watch4 } from "vue";
505
+ var lockCount = 0;
506
+ var originalMarginRight = "";
507
+ var originalOverflow = "";
508
+ var originalOverflowX = "";
509
+ var originalOverflowY = "";
510
+ var lockHtmlScrollRightCompensationRef = ref10("0px");
511
+ function useLockHtmlScroll(lockRef) {
512
+ if (typeof document === "undefined") return;
513
+ const el = document.documentElement;
514
+ let watchStopHandle;
515
+ let activated = false;
516
+ const unlock = () => {
517
+ el.style.marginRight = originalMarginRight;
518
+ el.style.overflow = originalOverflow;
519
+ el.style.overflowX = originalOverflowX;
520
+ el.style.overflowY = originalOverflowY;
521
+ lockHtmlScrollRightCompensationRef.value = "0px";
522
+ };
523
+ onMounted4(() => {
524
+ watchStopHandle = watch4(
525
+ lockRef,
526
+ (value) => {
527
+ if (value) {
528
+ if (!lockCount) {
529
+ const scrollbarWidth = window.innerWidth - el.offsetWidth;
530
+ if (scrollbarWidth > 0) {
531
+ originalMarginRight = el.style.marginRight;
532
+ el.style.marginRight = `${scrollbarWidth}px`;
533
+ lockHtmlScrollRightCompensationRef.value = `${scrollbarWidth}px`;
534
+ }
535
+ originalOverflow = el.style.overflow;
536
+ originalOverflowX = el.style.overflowX;
537
+ originalOverflowY = el.style.overflowY;
538
+ el.style.overflow = "hidden";
539
+ el.style.overflowX = "hidden";
540
+ el.style.overflowY = "hidden";
541
+ }
542
+ activated = true;
543
+ lockCount++;
544
+ } else {
545
+ lockCount--;
546
+ if (!lockCount) {
547
+ unlock();
548
+ }
549
+ activated = false;
550
+ }
551
+ },
552
+ {
553
+ immediate: true
554
+ }
555
+ );
556
+ });
557
+ onBeforeUnmount3(() => {
558
+ watchStopHandle == null ? void 0 : watchStopHandle();
559
+ if (activated) {
560
+ lockCount--;
561
+ if (!lockCount) {
562
+ unlock();
563
+ }
564
+ activated = false;
565
+ }
566
+ });
567
+ }
568
+
569
+ // src/use-memo.ts
570
+ import { computed as computed5, ref as ref11, watch as watch5 } from "vue";
571
+ function useMemo(getterOrOptions) {
572
+ const computedValueRef = computed5(getterOrOptions);
573
+ const valueRef = ref11(computedValueRef.value);
574
+ watch5(computedValueRef, (value) => {
575
+ valueRef.value = value;
576
+ });
577
+ if (typeof getterOrOptions === "function") {
578
+ return valueRef;
579
+ }
580
+ return {
581
+ __v_isRef: true,
582
+ get value() {
583
+ return valueRef.value;
584
+ },
585
+ set value(v) {
586
+ ;
587
+ getterOrOptions.set(v);
588
+ }
589
+ };
590
+ }
591
+
592
+ // src/use-merged-state.ts
593
+ import { computed as computed6, watch as watch6 } from "vue";
594
+ function useMergedState(controlledStateRef, uncontrolledStateRef) {
595
+ watch6(controlledStateRef, (value) => {
596
+ if (value !== void 0) {
597
+ uncontrolledStateRef.value = value;
598
+ }
599
+ });
600
+ return computed6(() => {
601
+ if (controlledStateRef.value === void 0) {
602
+ return uncontrolledStateRef.value;
603
+ }
604
+ return controlledStateRef.value;
605
+ });
606
+ }
607
+
608
+ // src/use-script.ts
609
+ import { onMounted as onMounted5, onUnmounted as onUnmounted3, ref as ref12 } from "vue";
610
+ function useScript(opts) {
611
+ const isLoading = ref12(false);
612
+ const error = ref12(false);
613
+ const success = ref12(false);
614
+ let script;
615
+ const promise = new Promise((resolve, reject) => {
616
+ onMounted5(() => {
617
+ script = document.createElement("script");
618
+ script.type = "text/javascript";
619
+ script.onload = () => {
620
+ isLoading.value = false;
621
+ success.value = true;
622
+ error.value = false;
623
+ resolve("");
624
+ };
625
+ script.onerror = (err) => {
626
+ isLoading.value = false;
627
+ success.value = false;
628
+ error.value = true;
629
+ reject(err);
630
+ };
631
+ script.src = opts.src;
632
+ document.head.appendChild(script);
633
+ });
634
+ });
635
+ onUnmounted3(() => {
636
+ script && script.remove();
637
+ });
638
+ return {
639
+ isLoading,
640
+ error,
641
+ success,
642
+ toPromise: () => promise
643
+ };
644
+ }
645
+
646
+ // src/use-scroll.ts
647
+ import { onMounted as onMounted6, onUnmounted as onUnmounted4, ref as ref13, watch as watch7 } from "vue";
648
+ import { useThrottleFn as useThrottleFn2 } from "@vueuse/core";
649
+ import { isObject, isWindow } from "@maxax/utils";
650
+ function useScroll(refEl, options) {
651
+ const refX = ref13(0);
652
+ const refY = ref13(0);
653
+ let handler = () => {
654
+ if (isWindow(refEl.value)) {
655
+ refX.value = refEl.value.scrollX;
656
+ refY.value = refEl.value.scrollY;
657
+ } else if (refEl.value) {
658
+ refX.value = refEl.value.scrollLeft;
659
+ refY.value = refEl.value.scrollTop;
660
+ }
661
+ };
662
+ if (isObject(options)) {
663
+ let wait = 0;
664
+ if (options && options.wait && options.wait > 0) {
665
+ wait = options.wait;
666
+ Reflect.deleteProperty(options, "wait");
667
+ }
668
+ handler = useThrottleFn2(handler, wait);
669
+ }
670
+ let stopWatch;
671
+ onMounted6(() => {
672
+ stopWatch = watch7(
673
+ refEl,
674
+ (el, prevEl, onCleanup) => {
675
+ if (el) {
676
+ el.addEventListener("scroll", handler);
677
+ } else if (prevEl) {
678
+ prevEl.removeEventListener("scroll", handler);
679
+ }
680
+ onCleanup(() => {
681
+ refX.value = 0;
682
+ refY.value = 0;
683
+ el && el.removeEventListener("scroll", handler);
684
+ });
685
+ },
686
+ { immediate: true }
687
+ );
688
+ });
689
+ onUnmounted4(() => {
690
+ refEl.value && refEl.value.removeEventListener("scroll", handler);
691
+ });
692
+ function stop() {
693
+ stopWatch && stopWatch();
694
+ }
695
+ return { refX, refY, stop };
696
+ }
697
+
698
+ // src/use-signal.ts
699
+ import { computed as computed7, ref as ref14, shallowRef, triggerRef } from "vue";
700
+ function useSignal(initialValue, options) {
701
+ const { useRef } = options || {};
702
+ const state = useRef ? ref14(initialValue) : shallowRef(initialValue);
703
+ return createSignal(state);
704
+ }
705
+ function useComputed(getterOrOptions, debugOptions) {
706
+ const isGetter = typeof getterOrOptions === "function";
707
+ const computedValue = computed7(getterOrOptions, debugOptions);
708
+ if (isGetter) {
709
+ return () => computedValue.value;
710
+ }
711
+ return createSignal(computedValue);
712
+ }
713
+ function createSignal(state) {
714
+ const signal = () => state.value;
715
+ signal.set = (value) => {
716
+ state.value = value;
717
+ };
718
+ signal.update = (updater) => {
719
+ state.value = updater(state.value);
720
+ };
721
+ signal.mutate = (mutator) => {
722
+ mutator(state.value);
723
+ triggerRef(state);
724
+ };
725
+ signal.getRef = () => state;
726
+ return signal;
727
+ }
728
+
729
+ // src/use-svg-icon-render.ts
730
+ import { h } from "vue";
731
+ function useSvgIconRender(SvgIcon) {
732
+ const SvgIconVNode = (config) => {
733
+ const { color, fontSize, icon, localIcon } = config;
734
+ const style = {};
735
+ if (color) {
736
+ style.color = color;
737
+ }
738
+ if (fontSize) {
739
+ style.fontSize = `${fontSize}px`;
740
+ }
741
+ if (!icon && !localIcon) {
742
+ return void 0;
743
+ }
744
+ return () => h(SvgIcon, { icon, localIcon, style });
745
+ };
746
+ return {
747
+ SvgIconVNode
748
+ };
749
+ }
750
+
751
+ // src/use-table.ts
752
+ import { computed as computed8, reactive, ref as ref15 } from "vue";
753
+ import { jsonClone } from "@maxax/utils";
754
+ function useHookTable(config) {
755
+ const { loading, startLoading, endLoading } = useLoading();
756
+ const { bool: empty, setBool: setEmpty } = useBoolean();
757
+ const { apiFn, apiParams, transformer, immediate = true, getColumnChecks, getColumns } = config;
758
+ const searchParams = reactive(jsonClone({ ...apiParams }));
759
+ const allColumns = ref15(config.columns());
760
+ const data = ref15([]);
761
+ const columnChecks = ref15(getColumnChecks(config.columns()));
762
+ const columns = computed8(() => getColumns(allColumns.value, columnChecks.value));
763
+ function reloadColumns() {
764
+ allColumns.value = config.columns();
765
+ const checkMap = new Map(columnChecks.value.map((col) => [col.key, col.checked]));
766
+ const defaultChecks = getColumnChecks(allColumns.value);
767
+ columnChecks.value = defaultChecks.map((col) => {
768
+ var _a;
769
+ return {
770
+ ...col,
771
+ checked: (_a = checkMap.get(col.key)) != null ? _a : col.checked
772
+ };
773
+ });
774
+ }
775
+ async function getData() {
776
+ var _a;
777
+ startLoading();
778
+ const formattedParams = formatSearchParams(searchParams);
779
+ const response = await apiFn(formattedParams);
780
+ const transformed = transformer(response);
781
+ data.value = transformed.data;
782
+ setEmpty(transformed.data.length === 0);
783
+ await ((_a = config.onFetched) == null ? void 0 : _a.call(config, transformed));
784
+ endLoading();
785
+ }
786
+ function formatSearchParams(params) {
787
+ const formattedParams = {};
788
+ Object.entries(params).forEach(([key, value]) => {
789
+ if (value !== null && value !== void 0) {
790
+ formattedParams[key] = value;
791
+ }
792
+ });
793
+ return formattedParams;
794
+ }
795
+ function updateSearchParams(params) {
796
+ Object.assign(searchParams, params);
797
+ }
798
+ function resetSearchParams() {
799
+ Object.assign(searchParams, jsonClone(apiParams));
800
+ }
801
+ if (immediate) {
802
+ getData();
803
+ }
804
+ return {
805
+ loading,
806
+ empty,
807
+ data,
808
+ columns,
809
+ columnChecks,
810
+ reloadColumns,
811
+ getData,
812
+ searchParams,
813
+ updateSearchParams,
814
+ resetSearchParams
815
+ };
816
+ }
817
+ export {
818
+ createBreakpointListen,
819
+ lockHtmlScrollRightCompensationRef,
820
+ useBoolean,
821
+ useBreakpoint,
822
+ useBrowserLocation,
823
+ useComputed,
824
+ useContext,
825
+ useCountDown,
826
+ useDeferredTrue,
827
+ useEventListener,
828
+ useHookTable,
829
+ useInjectionCollection,
830
+ useInjectionElementCollection,
831
+ useInjectionInstanceCollection,
832
+ useIsComposing,
833
+ useLayoutContentStyle,
834
+ useLayoutFooterStyle,
835
+ useLayoutHeaderStyle,
836
+ useLoading,
837
+ useLocalPage,
838
+ useLockHtmlScroll,
839
+ useMemo,
840
+ useMergedState,
841
+ useScript,
842
+ useScroll,
843
+ useSignal,
844
+ useSvgIconRender
845
+ };
846
+ //# sourceMappingURL=index.mjs.map