@hairy/vue-lib 1.47.0 → 1.49.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.js DELETED
@@ -1,376 +0,0 @@
1
- // src/components/c-field.ts
2
- import { defineComponent, h } from "vue-demi";
3
- var Field = defineComponent({
4
- name: "Field",
5
- props: {
6
- is: {
7
- type: [String, Number, Object],
8
- default: ""
9
- }
10
- },
11
- setup(props) {
12
- return () => {
13
- if (typeof props.is === "string" || typeof props.is === "number")
14
- return props.is;
15
- return props.is ? h(props.is) : null;
16
- };
17
- }
18
- });
19
-
20
- // src/components/collapse-transition.ts
21
- import CssRender from "css-render";
22
- import { defineComponent as defineComponent2, h as h2, Transition } from "vue-demi";
23
- var { c } = CssRender();
24
- var CollapseTransition = defineComponent2({
25
- name: "CollapseTransition",
26
- setup(_, { slots }) {
27
- const on = {
28
- onBeforeEnter(el) {
29
- el.classList.add("collapse-transition");
30
- if (!el.dataset)
31
- el.dataset = {};
32
- el.dataset.oldPaddingTop = el.style.paddingTop;
33
- el.dataset.oldPaddingBottom = el.style.paddingBottom;
34
- el.style.height = "0";
35
- el.style.paddingTop = "0";
36
- el.style.paddingBottom = "0";
37
- },
38
- onEnter(el) {
39
- el.dataset.oldOverflow = el.style.overflow;
40
- if (el.scrollHeight !== 0) {
41
- el.style.height = `${el.scrollHeight}px`;
42
- el.style.paddingTop = el.dataset.oldPaddingTop || "0";
43
- el.style.paddingBottom = el.dataset.oldPaddingBottom || "0";
44
- } else {
45
- el.style.height = "";
46
- el.style.paddingTop = el.dataset.oldPaddingTop || "0";
47
- el.style.paddingBottom = el.dataset.oldPaddingBottom || "0";
48
- }
49
- el.style.overflow = "hidden";
50
- },
51
- onAfterEnter(el) {
52
- el.classList.remove("collapse-transition");
53
- el.style.height = "";
54
- el.style.overflow = el.dataset.oldOverflow || "0";
55
- },
56
- onBeforeLeave(el) {
57
- if (!el.dataset)
58
- el.dataset = {};
59
- el.dataset.oldPaddingTop = el.style.paddingTop;
60
- el.dataset.oldPaddingBottom = el.style.paddingBottom;
61
- el.dataset.oldOverflow = el.style.overflow;
62
- el.style.height = `${el.scrollHeight}px`;
63
- el.style.overflow = "hidden";
64
- },
65
- onLeave(el) {
66
- if (el.scrollHeight !== 0) {
67
- el.classList.add("collapse-transition");
68
- el.style.transitionProperty = "height";
69
- el.style.height = "0";
70
- el.style.paddingTop = "0";
71
- el.style.paddingBottom = "0";
72
- }
73
- },
74
- onAfterLeave(el) {
75
- el.classList.remove("collapse-transition");
76
- el.style.height = "";
77
- el.style.overflow = el.dataset.oldOverflow || "0";
78
- el.style.paddingTop = el.dataset.oldPaddingTop || "0";
79
- el.style.paddingBottom = el.dataset.oldPaddingBottom || "0";
80
- }
81
- };
82
- style.mount();
83
- return () => h2(Transition, on, slots);
84
- }
85
- });
86
- var style = c(".collapse-transition", {
87
- transition: "0.2s height ease-in-out, 0.2s padding-top ease-in-out,0.2s padding-bottom ease-in-out"
88
- });
89
-
90
- // src/hooks/syncElementScroll/index.ts
91
- import { useEventListener } from "@vueuse/core";
92
- import debounce from "lodash/debounce";
93
- import throttle from "lodash/throttle";
94
- import { reactive } from "vue";
95
- function syncElementSyncScroll(fromTarget, toTarget, options = {}) {
96
- const { left = true, top = true, wait, immediate = true } = options;
97
- const debounceScrollLocks = reactive({ from: false, to: false });
98
- const onChangeLockScrollListener = debounce(
99
- (type) => {
100
- debounceScrollLocks[type] = true;
101
- },
102
- 20,
103
- { leading: true, trailing: false }
104
- );
105
- const offChangeLockScrollListener = debounce(
106
- (type) => {
107
- debounceScrollLocks[type] = false;
108
- },
109
- 20,
110
- { leading: false, trailing: true }
111
- );
112
- const syncScroll = (type) => {
113
- const positive = type;
114
- const opposite = type === "from" ? "to" : "from";
115
- const elements = reactive({
116
- from: fromTarget,
117
- to: toTarget
118
- });
119
- if (debounceScrollLocks[type])
120
- return;
121
- onChangeLockScrollListener(opposite);
122
- const aElement = elements[positive];
123
- const bElement = elements[opposite];
124
- const options2 = {};
125
- if (left)
126
- options2.left = aElement.scrollLeft;
127
- if (top)
128
- options2.top = aElement.scrollTop;
129
- if (!aElement || !bElement)
130
- return;
131
- bElement?.scroll(options2);
132
- offChangeLockScrollListener(opposite);
133
- };
134
- const syncFromTo = () => syncScroll("from");
135
- const syncToFrom = () => syncScroll("to");
136
- let toStopHandle;
137
- let formStopHandle;
138
- const stop = () => {
139
- toStopHandle?.();
140
- formStopHandle?.();
141
- };
142
- const start = () => {
143
- toStopHandle = useEventListener(fromTarget, "scroll", throttle(syncFromTo, wait));
144
- formStopHandle = useEventListener(toTarget, "scroll", throttle(syncToFrom, wait));
145
- };
146
- if (immediate)
147
- start();
148
- return { stop, start, syncFromTo, syncToFrom };
149
- }
150
-
151
- // src/hooks/syncElementSize/index.ts
152
- import { unit } from "@hairy/utils";
153
- import { unrefElement, useElementSize } from "@vueuse/core";
154
- import { unref, watch } from "vue";
155
- function syncElementSize(fromTarget, toTarget, options = {}) {
156
- const { width: isOnWidth = true, height: isOnHeight = false } = options;
157
- const defaultSize = { width: "", height: "" };
158
- const fromSize = useElementSize(fromTarget);
159
- let widthStopHandle;
160
- let heightStopHandle;
161
- const sync = (type) => {
162
- const sources = [fromSize[type], toTarget];
163
- const callback = () => {
164
- const element = unrefElement(toTarget);
165
- if (!element)
166
- return void 0;
167
- element.style[type] = unit(fromSize[type].value);
168
- };
169
- return watch(sources, callback, { immediate: true, ...options });
170
- };
171
- const stop = () => {
172
- widthStopHandle?.();
173
- heightStopHandle?.();
174
- const element = unrefElement(toTarget);
175
- element.style.width = defaultSize.width;
176
- element.style.height = defaultSize.height;
177
- };
178
- const start = () => {
179
- if (isOnWidth)
180
- widthStopHandle = sync("width");
181
- if (isOnHeight)
182
- heightStopHandle = sync("height");
183
- };
184
- watch(
185
- () => unref(toTarget),
186
- () => {
187
- const element = unrefElement(toTarget);
188
- defaultSize.width = element.style.width;
189
- defaultSize.height = element.style.height;
190
- }
191
- );
192
- start();
193
- return { start, stop };
194
- }
195
-
196
- // src/hooks/useChecked/index.ts
197
- import { computed, unref as unref2 } from "vue";
198
- function useChecked(target, checked = true, unchecked = false) {
199
- return computed({
200
- get: () => target.value === unref2(checked),
201
- set: (_value) => {
202
- target.value = _value ? unref2(checked) : unref2(unchecked);
203
- }
204
- });
205
- }
206
-
207
- // src/hooks/usePaginationServer/index.ts
208
- import { useAsyncState, useOffsetPagination } from "@vueuse/core";
209
- import debounce2 from "lodash/debounce";
210
- import { nextTick, reactive as reactive2, ref, watch as watch2 } from "vue";
211
- function useServerPagination(options) {
212
- const total = ref(Infinity);
213
- const pagination = useOffsetPagination({ total, ...options });
214
- const resolveOptions = reactive2({
215
- currentPage: pagination.currentPage,
216
- currentPageSize: pagination.currentPageSize,
217
- total
218
- });
219
- const paginationRef = reactive2({
220
- currentPage: pagination.currentPage,
221
- currentPageSize: pagination.currentPageSize,
222
- pageCount: pagination.pageCount,
223
- isFirstPage: pagination.isFirstPage,
224
- isLastPage: pagination.isLastPage,
225
- total
226
- });
227
- const state = ref([]);
228
- const {
229
- execute: _execute,
230
- isLoading: loading,
231
- error
232
- } = useAsyncState(async () => {
233
- const resolved = await options.resolve(resolveOptions);
234
- return resolved;
235
- }, []);
236
- const execute = debounce2(async () => {
237
- state.value = await _execute();
238
- }, 100);
239
- nextTick(() => {
240
- watch2([pagination.currentPageSize, ...options.sources || []], execute, { immediate: true, ...options });
241
- watch2(pagination.currentPage, (newValue, oldValue) => newValue !== oldValue && execute());
242
- });
243
- return {
244
- state,
245
- loading,
246
- error,
247
- execute,
248
- pagination: paginationRef,
249
- next: pagination.next,
250
- prev: pagination.prev
251
- };
252
- }
253
-
254
- // src/hooks/useSelectedMultiple/index.ts
255
- import { computed as computed2, unref as unref3 } from "vue";
256
-
257
- // src/hooks/utils/extendSelected.ts
258
- import { extendRef } from "@vueuse/core";
259
- import { ref as ref2, watch as watch3 } from "vue";
260
- function extendSelected(array, fieldName) {
261
- watch3(
262
- array,
263
- (items) => {
264
- for (const item of items) {
265
- if (typeof item[fieldName] === "undefined")
266
- extendRef(item, { [fieldName]: ref2(false) });
267
- }
268
- },
269
- { immediate: true, flush: "sync" }
270
- );
271
- }
272
-
273
- // src/hooks/useSelectedMultiple/index.ts
274
- function useSelectedMultiple(array, options = {}) {
275
- const { fieldName = "select", disabled } = options;
276
- extendSelected(array, fieldName);
277
- const selected = computed2(() => unref3(array).filter((item) => item[fieldName]));
278
- const isSelectedAll = computed2({
279
- get: () => !selected.value?.length && !unref3(array).some((item) => !item[fieldName]),
280
- set: (value) => unref3(array).forEach((item, index) => {
281
- if (!disabled?.(item, index))
282
- item[fieldName] = value;
283
- })
284
- });
285
- const isSelected = computed2(() => !!unref3(array).some((item) => item[fieldName]));
286
- const isIndeterminate = computed2(() => {
287
- const selectCount = selected.value.length;
288
- return selectCount > 0 && selectCount < unref3(array).length;
289
- });
290
- return { selected, isSelectedAll, isSelected, isIndeterminate };
291
- }
292
-
293
- // src/hooks/useSelectedSingle/index.ts
294
- import { computed as computed3, ref as ref3, unref as unref4, watch as watch4 } from "vue";
295
- function useSelectedSingle(array, options = {}) {
296
- const fieldName = options.fieldName ?? "select";
297
- const required = options.required ?? false;
298
- extendSelected(array, fieldName);
299
- const SELECTED_SINGLE_KEY = "selected_single_key";
300
- const isLocked = ref3(false);
301
- const isSelected = computed3(() => !!unref4(array).some((item) => item.select));
302
- if (required) {
303
- const recover = (bool) => {
304
- const index = required === true ? 0 : required;
305
- if (!bool && unref4(array).length > 0)
306
- unref4(array)[index][fieldName] = true;
307
- };
308
- watch4(isSelected, recover, { flush: "sync", immediate: true });
309
- }
310
- const itemChange = (neglect) => {
311
- if (isLocked.value)
312
- return;
313
- isLocked.value = true;
314
- const _array = unref4(array);
315
- const _value = !_array[neglect][fieldName];
316
- if (required && !_value)
317
- _array[neglect][fieldName] = true;
318
- _array.forEach((target, index) => {
319
- if (neglect === index)
320
- return void 0;
321
- if (!target[fieldName])
322
- return void 0;
323
- target[fieldName] = false;
324
- });
325
- isLocked.value = false;
326
- };
327
- const targetEffect = (target, index) => {
328
- if (!target[SELECTED_SINGLE_KEY]) {
329
- return watch4(
330
- () => target[fieldName],
331
- () => itemChange(index),
332
- { flush: "sync" }
333
- );
334
- }
335
- Object.defineProperty(target, SELECTED_SINGLE_KEY, { value: true });
336
- };
337
- watch4(
338
- array,
339
- () => {
340
- unref4(array).forEach(targetEffect);
341
- },
342
- { immediate: true, flush: "sync" }
343
- );
344
- const selected = computed3(() => unref4(array).find((v) => v[fieldName]));
345
- return { selected, isSelected };
346
- }
347
-
348
- // src/utils/index.ts
349
- import { computed as computed4, h as h3, render } from "vue-demi";
350
- function ehr(component, tag) {
351
- const container = document.createElement(tag || "dev");
352
- render(h3(component), container);
353
- return container;
354
- }
355
- function propertyToRef(data, prop) {
356
- return computed4({
357
- get() {
358
- return data[prop];
359
- },
360
- set(v) {
361
- data[prop] = v;
362
- }
363
- });
364
- }
365
- export {
366
- CollapseTransition,
367
- Field,
368
- ehr,
369
- propertyToRef,
370
- syncElementSize,
371
- syncElementSyncScroll,
372
- useChecked,
373
- useSelectedMultiple,
374
- useSelectedSingle,
375
- useServerPagination
376
- };