stk-table-vue 0.0.1 → 0.0.2

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.
@@ -1,1263 +1,1269 @@
1
- import { onMounted, onBeforeUnmount, watch, ref, computed, defineComponent, shallowRef, toRaw, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, withDirectives, createElementVNode, vShow, Fragment, renderList, createCommentVNode, createBlock, resolveDynamicComponent, renderSlot, toDisplayString, createTextVNode } from "vue";
2
- import { interpolateRgb } from "d3-interpolate";
3
- function useAutoResize({ tableContainer, initVirtualScroll, scrollTo, props, debounceMs }) {
4
- let resizeObserver = null;
5
- onMounted(() => {
6
- initResizeObserver();
7
- });
8
- onBeforeUnmount(() => {
9
- removeResizeObserver();
10
- });
11
- function initResizeObserver() {
12
- if (window.ResizeObserver) {
13
- if (!tableContainer.value) {
14
- const watchDom = watch(
15
- () => tableContainer,
16
- () => {
17
- initResizeObserver();
18
- watchDom();
19
- }
20
- );
21
- return;
22
- }
23
- resizeObserver = new ResizeObserver(resizeCallback);
24
- resizeObserver.observe(tableContainer.value);
25
- } else {
26
- window.addEventListener("resize", resizeCallback);
27
- }
28
- }
29
- function removeResizeObserver() {
30
- if (resizeObserver) {
31
- resizeObserver.disconnect();
32
- resizeObserver = null;
33
- } else {
34
- window.removeEventListener("resize", resizeCallback);
35
- }
36
- }
37
- let debounceTime = 0;
38
- function resizeCallback() {
39
- if (debounceTime) {
40
- window.clearTimeout(debounceTime);
41
- }
42
- debounceTime = window.setTimeout(() => {
43
- if (props.autoResize) {
44
- initVirtualScroll();
45
- if (typeof props.autoResize === "function") {
46
- props.autoResize();
47
- }
48
- }
49
- debounceTime = 0;
50
- }, debounceMs);
51
- }
52
- }
53
- const Default_Col_Width = "100";
54
- const Default_Table_Height = 100;
55
- const Default_Table_Width = 200;
56
- const Highlight_Color = {
57
- light: { from: "#71a2fd", to: "#fff" },
58
- dark: { from: "#1e4c99", to: "#181c21" }
59
- };
60
- const Highlight_Duration = 2e3;
61
- const Highlight_Color_Change_Freq = 100;
62
- let _chromeVersion = 0;
63
- try {
64
- const userAgent = navigator.userAgent.match(/chrome\/\d+/i);
65
- if (userAgent) {
66
- _chromeVersion = +userAgent[0].split("/")[1];
67
- }
68
- } catch (e) {
69
- console.error("Cannot get Chrome version", e);
70
- }
71
- const Is_Legacy_Mode = _chromeVersion < 56;
72
- function useColResize({
73
- tableContainer,
74
- tableHeaderLast,
75
- colResizeIndicator,
76
- props,
77
- emits,
78
- colKeyGen
79
- }) {
80
- const isColResizing = ref(false);
81
- let colResizeState = {
82
- currentCol: null,
83
- currentColIndex: 0,
84
- lastCol: null,
85
- startX: 0,
86
- startOffsetTableX: 0
87
- };
88
- onMounted(() => {
89
- initColResizeEvent();
90
- });
91
- onBeforeUnmount(() => {
92
- clearColResizeEvent();
93
- });
94
- function initColResizeEvent() {
95
- window.addEventListener("mousemove", onThResizeMouseMove);
96
- window.addEventListener("mouseup", onThResizeMouseUp);
97
- }
98
- function clearColResizeEvent() {
99
- window.removeEventListener("mousemove", onThResizeMouseMove);
100
- window.removeEventListener("mouseup", onThResizeMouseUp);
101
- }
102
- function onThResizeMouseDown(e, col, isPrev = false) {
103
- if (!tableContainer.value)
104
- return;
105
- e.stopPropagation();
106
- e.preventDefault();
107
- const { clientX } = e;
108
- const { scrollLeft, scrollTop } = tableContainer.value;
109
- const { left } = tableContainer.value.getBoundingClientRect();
110
- let colIndex = tableHeaderLast.value.findIndex((it) => colKeyGen(it) === colKeyGen(col));
111
- if (isPrev) {
112
- colIndex -= 1;
113
- col = tableHeaderLast.value[colIndex];
114
- }
115
- const offsetTableX = clientX - left + scrollLeft;
116
- isColResizing.value = true;
117
- Object.assign(colResizeState, {
118
- currentCol: col,
119
- currentColIndex: colIndex,
120
- lastCol: findLastChildCol(col),
121
- startX: clientX,
122
- startOffsetTableX: offsetTableX
123
- });
124
- if (colResizeIndicator.value) {
125
- const style = colResizeIndicator.value.style;
126
- style.display = "block";
127
- style.left = offsetTableX + "px";
128
- style.top = scrollTop + "px";
129
- }
130
- }
131
- function onThResizeMouseMove(e) {
132
- if (!isColResizing.value)
133
- return;
134
- e.stopPropagation();
135
- e.preventDefault();
136
- const { lastCol, startX, startOffsetTableX } = colResizeState;
137
- const { clientX } = e;
138
- let moveX = clientX - startX;
139
- const currentColWidth = parseInt((lastCol == null ? void 0 : lastCol.width) || Default_Col_Width);
140
- if (currentColWidth + moveX < props.colMinWidth) {
141
- moveX = -currentColWidth;
142
- }
143
- const offsetTableX = startOffsetTableX + moveX;
144
- if (!colResizeIndicator.value)
145
- return;
146
- colResizeIndicator.value.style.left = offsetTableX + "px";
147
- }
148
- function onThResizeMouseUp(e) {
149
- if (!isColResizing.value)
150
- return;
151
- const { startX, lastCol } = colResizeState;
152
- const { clientX } = e;
153
- const moveX = clientX - startX;
154
- let width = parseInt((lastCol == null ? void 0 : lastCol.width) || Default_Col_Width) + moveX;
155
- if (width < props.colMinWidth)
156
- width = props.colMinWidth;
157
- const curCol = tableHeaderLast.value.find((it) => colKeyGen(it) === colKeyGen(lastCol));
158
- if (!curCol)
159
- return;
160
- curCol.width = width + "px";
161
- emits("update:columns", [...props.columns]);
162
- if (colResizeIndicator.value) {
163
- const style = colResizeIndicator.value.style;
164
- style.display = "none";
165
- style.left = "0";
166
- style.top = "0";
167
- }
168
- isColResizing.value = false;
169
- colResizeState = {
170
- currentCol: null,
171
- currentColIndex: 0,
172
- lastCol: null,
173
- startX: 0,
174
- startOffsetTableX: 0
175
- };
176
- }
177
- function findLastChildCol(column) {
178
- var _a;
179
- if ((_a = column == null ? void 0 : column.children) == null ? void 0 : _a.length) {
180
- const lastChild = column.children.at(-1);
181
- return findLastChildCol(lastChild);
182
- }
183
- return column;
184
- }
185
- return {
186
- isColResizing,
187
- onThResizeMouseDown,
188
- onThResizeMouseMove,
189
- onThResizeMouseUp
190
- };
191
- }
192
- function useFixedStyle({ tableHeaderLast, virtualScroll, virtualScrollX, virtualX_on, virtualX_offsetRight }) {
193
- const fixedColumnsPositionStore = computed(() => {
194
- const store = {};
195
- const cols = [...tableHeaderLast.value];
196
- let left = 0;
197
- let rightStartIndex = 0;
198
- for (let i = 0; i < cols.length; i++) {
199
- const item = cols[i];
200
- if (item.fixed === "left") {
201
- store[item.dataIndex] = left;
202
- left += parseInt(item.width || Default_Col_Width);
203
- }
204
- if (!rightStartIndex && item.fixed === "right") {
205
- rightStartIndex = i;
206
- }
207
- }
208
- let right = 0;
209
- for (let i = cols.length - 1; i >= rightStartIndex; i--) {
210
- const item = cols[i];
211
- if (item.fixed === "right") {
212
- store[item.dataIndex] = right;
213
- right += parseInt(item.width || Default_Col_Width);
214
- }
215
- }
216
- return store;
217
- });
218
- function getFixedStyle(tagType, col) {
219
- const style = {};
220
- if (Is_Legacy_Mode) {
221
- if (tagType === 1) {
222
- style.position = "relative";
223
- style.top = virtualScroll.value.scrollTop + "px";
224
- }
225
- }
226
- const { fixed, dataIndex } = col;
227
- if (fixed === "left" || fixed === "right") {
228
- const isFixedLeft = fixed === "left";
229
- if (Is_Legacy_Mode) {
230
- style.position = "relative";
231
- if (isFixedLeft) {
232
- if (virtualX_on.value)
233
- style.left = virtualScrollX.value.scrollLeft - virtualScrollX.value.offsetLeft + "px";
234
- else
235
- style.left = virtualScrollX.value.scrollLeft + "px";
236
- } else {
237
- style.right = `${virtualX_offsetRight.value}px`;
238
- }
239
- if (tagType === 1) {
240
- style.top = virtualScroll.value.scrollTop + "px";
241
- style.zIndex = isFixedLeft ? "4" : "3";
242
- } else {
243
- style.zIndex = isFixedLeft ? "3" : "2";
244
- }
245
- } else {
246
- style.position = "sticky";
247
- if (isFixedLeft) {
248
- style.left = fixedColumnsPositionStore.value[dataIndex] + "px";
249
- } else {
250
- style.right = fixedColumnsPositionStore.value[dataIndex] + "px";
251
- }
252
- if (tagType === 1) {
253
- style.top = "0";
254
- style.zIndex = isFixedLeft ? "4" : "3";
255
- } else {
256
- style.zIndex = isFixedLeft ? "3" : "2";
257
- }
258
- }
259
- }
260
- return style;
261
- }
262
- return {
263
- getFixedStyle
264
- };
265
- }
266
- function useHighlight({ props, tableContainer, rowKeyGen }) {
267
- const highlightInter = computed(() => {
268
- return interpolateRgb(Highlight_Color[props.theme].from, Highlight_Color[props.theme].to);
269
- });
270
- const highlightDimRows = /* @__PURE__ */ new Set();
271
- const highlightDimRowsTimeout = /* @__PURE__ */ new Map();
272
- const highlightDimCellsTimeout = /* @__PURE__ */ new Map();
273
- let calcHighlightDimLoop = false;
274
- function calcHighlightLoop() {
275
- if (calcHighlightDimLoop)
276
- return;
277
- calcHighlightDimLoop = true;
278
- const recursion = () => {
279
- window.setTimeout(() => {
280
- const nowTs = Date.now();
281
- const needDeleteRows = [];
282
- highlightDimRows.forEach((row) => {
283
- const progress = (nowTs - row._bgc_progress_ms) / Highlight_Duration;
284
- if (0 < progress && progress < 1) {
285
- row._bgc = highlightInter.value(progress);
286
- } else {
287
- row._bgc = "";
288
- needDeleteRows.push(row);
289
- }
290
- });
291
- needDeleteRows.forEach((row) => highlightDimRows.delete(row));
292
- if (highlightDimRows.size > 0) {
293
- recursion();
294
- } else {
295
- calcHighlightDimLoop = false;
296
- }
297
- }, Highlight_Color_Change_Freq);
298
- };
299
- recursion();
300
- }
301
- function setHighlightDimCell(rowKeyValue, dataIndex) {
302
- var _a;
303
- const cellEl = (_a = tableContainer.value) == null ? void 0 : _a.querySelector(`[data-row-key="${rowKeyValue}"]>[data-index="${dataIndex}"]`);
304
- if (!cellEl)
305
- return;
306
- if (cellEl.classList.contains("highlight-cell")) {
307
- cellEl.classList.remove("highlight-cell");
308
- void cellEl.offsetHeight;
309
- }
310
- cellEl.classList.add("highlight-cell");
311
- window.clearTimeout(highlightDimCellsTimeout.get(rowKeyValue));
312
- highlightDimCellsTimeout.set(
313
- rowKeyValue,
314
- window.setTimeout(() => {
315
- cellEl.classList.remove("highlight-cell");
316
- highlightDimCellsTimeout.delete(rowKeyValue);
317
- }, Highlight_Duration)
318
- );
319
- }
320
- function setHighlightDimRow(rowKeyValues) {
321
- var _a, _b;
322
- if (!Array.isArray(rowKeyValues))
323
- rowKeyValues = [rowKeyValues];
324
- if (props.virtual) {
325
- const nowTs = Date.now();
326
- for (let i = 0; i < rowKeyValues.length; i++) {
327
- const rowKeyValue = rowKeyValues[i];
328
- const row = props.dataSource.find((it) => rowKeyGen(it) === rowKeyValue);
329
- if (!row)
330
- continue;
331
- row._bgc_progress_ms = nowTs;
332
- highlightDimRows.add(row);
333
- }
334
- calcHighlightLoop();
335
- } else {
336
- let needRepaint = false;
337
- const rowElTemp = [];
338
- for (let i = 0; i < rowKeyValues.length; i++) {
339
- const rowKeyValue = rowKeyValues[i];
340
- const rowEl = (_a = tableContainer.value) == null ? void 0 : _a.querySelector(`[data-row-key="${rowKeyValue}"]`);
341
- if (!rowEl)
342
- continue;
343
- if (rowEl.classList.contains("highlight-row")) {
344
- rowEl.classList.remove("highlight-row");
345
- needRepaint = true;
346
- }
347
- rowElTemp.push(rowEl);
348
- window.clearTimeout(highlightDimRowsTimeout.get(rowKeyValue));
349
- highlightDimRowsTimeout.set(
350
- rowKeyValue,
351
- window.setTimeout(() => {
352
- rowEl.classList.remove("highlight-row");
353
- highlightDimRowsTimeout.delete(rowKeyValue);
354
- }, Highlight_Duration)
355
- );
356
- }
357
- if (needRepaint) {
358
- void ((_b = tableContainer.value) == null ? void 0 : _b.offsetWidth);
359
- }
360
- rowElTemp.forEach((el) => el.classList.add("highlight-row"));
361
- }
362
- }
363
- return {
364
- setHighlightDimRow,
365
- setHighlightDimCell
366
- };
367
- }
368
- const ARROW_CODES = ["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"];
369
- function useKeyboardArrowScroll(targetElement, { scrollTo, virtualScroll, virtualScrollX }) {
370
- let isMouseOver = false;
371
- onMounted(() => {
372
- var _a, _b, _c;
373
- window.addEventListener("keydown", handleKeydown);
374
- (_a = targetElement.value) == null ? void 0 : _a.addEventListener("mouseenter", handleMouseEnter);
375
- (_b = targetElement.value) == null ? void 0 : _b.addEventListener("mouseleave", handleMouseLeave);
376
- (_c = targetElement.value) == null ? void 0 : _c.addEventListener("mousedown", handleMouseDown);
377
- });
378
- onBeforeUnmount(() => {
379
- var _a, _b, _c;
380
- window.removeEventListener("keydown", handleKeydown);
381
- (_a = targetElement.value) == null ? void 0 : _a.removeEventListener("mouseenter", handleMouseEnter);
382
- (_b = targetElement.value) == null ? void 0 : _b.removeEventListener("mouseleave", handleMouseLeave);
383
- (_c = targetElement.value) == null ? void 0 : _c.removeEventListener("mousedown", handleMouseDown);
384
- });
385
- function handleKeydown(e) {
386
- if (!ARROW_CODES.includes(e.code))
387
- return;
388
- if (!isMouseOver)
389
- return;
390
- e.preventDefault();
391
- const { scrollTop, rowHeight } = virtualScroll.value;
392
- const { scrollLeft } = virtualScrollX.value;
393
- if (e.code === ARROW_CODES[0]) {
394
- scrollTo(scrollTop - rowHeight, null);
395
- } else if (e.code === ARROW_CODES[1]) {
396
- scrollTo(null, scrollLeft + rowHeight);
397
- } else if (e.code === ARROW_CODES[2]) {
398
- scrollTo(scrollTop + rowHeight, null);
399
- } else if (e.code === ARROW_CODES[3]) {
400
- scrollTo(null, scrollLeft - rowHeight);
401
- }
402
- }
403
- function handleMouseEnter() {
404
- isMouseOver = true;
405
- }
406
- function handleMouseLeave() {
407
- isMouseOver = false;
408
- }
409
- function handleMouseDown() {
410
- if (!isMouseOver)
411
- isMouseOver = true;
412
- }
413
- }
414
- function useThDrag({ emits }) {
415
- let dragStartKey = void 0;
416
- function onThDragStart(e) {
417
- dragStartKey = e.target.dataset.colKey;
418
- emits("th-drag-start", dragStartKey);
419
- }
420
- function onThDragOver(e) {
421
- e.preventDefault();
422
- }
423
- function onThDrop(e) {
424
- let th = e.target;
425
- while (th) {
426
- if (th.tagName === "TH")
427
- break;
428
- th = th.parentNode;
429
- }
430
- if (dragStartKey !== th.dataset.colKey) {
431
- emits("col-order-change", dragStartKey, th.dataset.colKey);
432
- }
433
- emits("th-drop", th.dataset.colKey);
434
- }
435
- return {
436
- onThDragStart,
437
- onThDragOver,
438
- onThDrop
439
- };
440
- }
441
- function getCalcWidth(col) {
442
- return parseInt(col.minWidth || col.width || Default_Col_Width);
443
- }
444
- function useVirtualScroll({ tableContainer, props, dataSourceCopy, tableHeaderLast }) {
445
- const virtualScroll = ref({
446
- containerHeight: 0,
447
- rowHeight: props.rowHeight,
448
- pageSize: 10,
449
- startIndex: 0,
450
- endIndex: 0,
451
- offsetTop: 0,
452
- scrollTop: 0
453
- });
454
- const virtualScrollX = ref({
455
- containerWidth: 0,
456
- startIndex: 0,
457
- endIndex: 0,
458
- offsetLeft: 0,
459
- scrollLeft: 0
460
- });
461
- const virtual_on = computed(() => {
462
- return props.virtual && dataSourceCopy.value.length > virtualScroll.value.pageSize * 2;
463
- });
464
- const virtual_dataSourcePart = computed(() => {
465
- if (!virtual_on.value)
466
- return dataSourceCopy.value;
467
- const { startIndex, endIndex } = virtualScroll.value;
468
- return dataSourceCopy.value.slice(startIndex, endIndex);
469
- });
470
- const virtual_offsetBottom = computed(() => {
471
- if (!virtual_on.value)
472
- return 0;
473
- const { startIndex, rowHeight } = virtualScroll.value;
474
- return (dataSourceCopy.value.length - startIndex - virtual_dataSourcePart.value.length) * rowHeight;
475
- });
476
- const virtualX_on = computed(() => {
477
- return props.virtualX && tableHeaderLast.value.reduce((sum, col) => sum += getCalcWidth(col), 0) > virtualScrollX.value.containerWidth * 1.5;
478
- });
479
- const virtualX_columnPart = computed(() => {
480
- if (virtualX_on.value) {
481
- const leftCols = [];
482
- const rightCols = [];
483
- const { startIndex, endIndex } = virtualScrollX.value;
484
- for (let i = 0; i < startIndex; i++) {
485
- const col = tableHeaderLast.value[i];
486
- if (col.fixed === "left")
487
- leftCols.push(col);
488
- }
489
- for (let i = endIndex; i < tableHeaderLast.value.length; i++) {
490
- const col = tableHeaderLast.value[i];
491
- if (col.fixed === "right")
492
- rightCols.push(col);
493
- }
494
- const mainColumns = tableHeaderLast.value.slice(startIndex, endIndex);
495
- return leftCols.concat(mainColumns).concat(rightCols);
496
- }
497
- return tableHeaderLast.value;
498
- });
499
- const virtualX_offsetRight = computed(() => {
500
- if (!virtualX_on.value)
501
- return 0;
502
- let width = 0;
503
- for (let i = virtualScrollX.value.endIndex; i < tableHeaderLast.value.length; i++) {
504
- const col = tableHeaderLast.value[i];
505
- if (col.fixed !== "right") {
506
- width += getCalcWidth(col);
507
- }
508
- }
509
- return width;
510
- });
511
- function initVirtualScrollY(height) {
512
- if (!virtual_on.value)
513
- return;
514
- const { offsetHeight, scrollTop } = tableContainer.value || {};
515
- const { rowHeight } = virtualScroll.value;
516
- let containerHeight;
517
- if (typeof height === "number") {
518
- containerHeight = height;
519
- } else {
520
- containerHeight = offsetHeight || Default_Table_Height;
521
- }
522
- Object.assign(virtualScroll.value, {
523
- containerHeight,
524
- pageSize: Math.ceil(containerHeight / rowHeight) + 1
525
- // 这里最终+1,因为headless=true无头时,需要上下各预渲染一行。
526
- });
527
- updateVirtualScrollY(scrollTop);
528
- }
529
- function initVirtualScrollX() {
530
- if (!props.virtualX)
531
- return;
532
- const { offsetWidth, scrollLeft } = tableContainer.value || {};
533
- virtualScrollX.value.containerWidth = offsetWidth || Default_Table_Width;
534
- updateVirtualScrollX(scrollLeft);
535
- }
536
- function initVirtualScroll(height) {
537
- initVirtualScrollY(height);
538
- initVirtualScrollX();
539
- }
540
- function updateVirtualScrollY(sTop = 0) {
541
- const { rowHeight, pageSize } = virtualScroll.value;
542
- const startIndex = Math.floor(sTop / rowHeight);
543
- let endIndex = startIndex + pageSize;
544
- if (endIndex > dataSourceCopy.value.length) {
545
- endIndex = dataSourceCopy.value.length;
546
- }
547
- Object.assign(virtualScroll.value, {
548
- startIndex,
549
- offsetTop: startIndex * rowHeight,
550
- // startIndex之前的高度
551
- endIndex
552
- });
553
- }
554
- function updateVirtualScrollX(sLeft = 0) {
555
- var _a;
556
- const headerLength = (_a = tableHeaderLast.value) == null ? void 0 : _a.length;
557
- if (!headerLength)
558
- return;
559
- let startIndex = 0;
560
- let offsetLeft = 0;
561
- let colWidthSum = 0;
562
- for (let colIndex = 0; colIndex < headerLength; colIndex++) {
563
- startIndex++;
564
- const col = tableHeaderLast.value[colIndex];
565
- if (col.fixed === "left")
566
- continue;
567
- const colWidth = getCalcWidth(col);
568
- colWidthSum += colWidth;
569
- if (colWidthSum >= sLeft) {
570
- offsetLeft = colWidthSum - colWidth;
571
- startIndex--;
572
- break;
573
- }
574
- }
575
- colWidthSum = 0;
576
- let endIndex = headerLength;
577
- for (let colIndex = startIndex; colIndex < headerLength; colIndex++) {
578
- const col = tableHeaderLast.value[colIndex];
579
- colWidthSum += getCalcWidth(col);
580
- if (colWidthSum >= virtualScrollX.value.containerWidth) {
581
- endIndex = colIndex + 1;
582
- break;
583
- }
584
- }
585
- if (endIndex > headerLength) {
586
- endIndex = headerLength;
587
- }
588
- Object.assign(virtualScrollX.value, { startIndex, endIndex, offsetLeft });
589
- }
590
- return {
591
- virtualScroll,
592
- virtualScrollX,
593
- virtual_on,
594
- virtual_dataSourcePart,
595
- virtual_offsetBottom,
596
- virtualX_on,
597
- virtualX_columnPart,
598
- virtualX_offsetRight,
599
- initVirtualScroll,
600
- initVirtualScrollY,
601
- initVirtualScrollX,
602
- updateVirtualScrollY,
603
- updateVirtualScrollX
604
- };
605
- }
606
- function insertToOrderedArray(sortState, newItem, targetArray) {
607
- const { dataIndex, order } = sortState;
608
- let { sortType } = sortState;
609
- if (!sortType)
610
- sortType = typeof newItem[dataIndex];
611
- const data = [...targetArray];
612
- if (!order) {
613
- data.unshift(newItem);
614
- return data;
615
- }
616
- let sIndex = 0;
617
- let eIndex = data.length - 1;
618
- const targetVal = newItem[dataIndex];
619
- while (sIndex <= eIndex) {
620
- const midIndex = Math.floor((sIndex + eIndex) / 2);
621
- const midVal = data[midIndex][dataIndex];
622
- const compareRes = strCompare(midVal, targetVal, sortType);
623
- if (compareRes === 0) {
624
- sIndex = midIndex;
625
- break;
626
- } else if (compareRes === -1) {
627
- if (order === "asc")
628
- sIndex = midIndex + 1;
629
- else
630
- eIndex = midIndex - 1;
631
- } else {
632
- if (order === "asc")
633
- eIndex = midIndex - 1;
634
- else
635
- sIndex = midIndex + 1;
636
- }
637
- }
638
- data.splice(sIndex, 0, newItem);
639
- return data;
640
- }
641
- function strCompare(a, b, type) {
642
- if (type === "number") {
643
- if (+a > +b)
644
- return 1;
645
- else if (+a === +b)
646
- return 0;
647
- else
648
- return -1;
649
- } else {
650
- return String(a).localeCompare(b);
651
- }
652
- }
653
- function tableSort(sortOption, order, dataSource) {
654
- if (!(dataSource == null ? void 0 : dataSource.length))
655
- return dataSource || [];
656
- let targetDataSource = [...dataSource];
657
- if (typeof sortOption.sorter === "function") {
658
- const customSorterData = sortOption.sorter(targetDataSource, { order, column: sortOption });
659
- if (customSorterData)
660
- targetDataSource = customSorterData;
661
- } else if (order) {
662
- const sortField = sortOption.sortField || sortOption.dataIndex;
663
- let { sortType } = sortOption;
664
- if (!sortType)
665
- sortType = typeof dataSource[0][sortField];
666
- if (sortType === "number") {
667
- const nanArr = [];
668
- const numArr = [];
669
- for (let i = 0; i < targetDataSource.length; i++) {
670
- const row = targetDataSource[i];
671
- if (row[sortField] === null || row[sortField] === "" || typeof row[sortField] === "boolean" || Number.isNaN(+row[sortField])) {
672
- nanArr.push(row);
673
- } else {
674
- numArr.push(row);
675
- }
676
- }
677
- if (order === "asc") {
678
- numArr.sort((a, b) => +a[sortField] - +b[sortField]);
679
- targetDataSource = [...nanArr, ...numArr];
680
- } else {
681
- numArr.sort((a, b) => +b[sortField] - +a[sortField]);
682
- targetDataSource = [...numArr, ...nanArr];
683
- }
684
- } else {
685
- if (order === "asc") {
686
- targetDataSource.sort((a, b) => String(a[sortField]).localeCompare(b[sortField]));
687
- } else {
688
- targetDataSource.sort((a, b) => String(a[sortField]).localeCompare(b[sortField]) * -1);
689
- }
690
- }
691
- }
692
- return targetDataSource;
693
- }
694
- function howDeepTheColumn(arr, level = 1) {
695
- const levels = [level];
696
- arr.forEach((item) => {
697
- var _a;
698
- if ((_a = item.children) == null ? void 0 : _a.length) {
699
- levels.push(howDeepTheColumn(item.children, level + 1));
700
- }
701
- });
702
- return Math.max(...levels);
703
- }
704
- const _hoisted_1 = { key: 0 };
705
- const _hoisted_2 = ["data-col-key", "draggable", "rowspan", "colspan", "title", "onClick"];
706
- const _hoisted_3 = { class: "table-header-cell-wrapper" };
707
- const _hoisted_4 = { class: "table-header-title" };
708
- const _hoisted_5 = {
709
- key: 2,
710
- class: "table-header-sorter"
711
- };
712
- const _hoisted_6 = /* @__PURE__ */ createElementVNode("svg", {
713
- xmlns: "http://www.w3.org/2000/svg",
714
- width: "16px",
715
- height: "16px",
716
- viewBox: "0 0 16 16"
717
- }, [
718
- /* @__PURE__ */ createElementVNode("g", { id: "sort-btn" }, [
719
- /* @__PURE__ */ createElementVNode("polygon", {
720
- id: "arrow-up",
721
- fill: "#757699",
722
- points: "8 2 4.8 6 11.2 6"
723
- }),
724
- /* @__PURE__ */ createElementVNode("polygon", {
725
- id: "arrow-down",
726
- transform: "translate(8, 12) rotate(-180) translate(-8, -12) ",
727
- points: "8 10 4.8 14 11.2 14"
728
- })
729
- ])
730
- ], -1);
731
- const _hoisted_7 = [
732
- _hoisted_6
733
- ];
734
- const _hoisted_8 = ["onMousedown"];
735
- const _hoisted_9 = ["onMousedown"];
736
- const _hoisted_10 = {
737
- key: 0,
738
- class: "virtual-x-left",
739
- style: { "padding": "0" }
740
- };
741
- const _hoisted_11 = ["data-row-key", "onClick", "onDblclick", "onContextmenu", "onMouseover"];
742
- const _hoisted_12 = {
743
- key: 0,
744
- class: "virtual-x-left",
745
- style: { "padding": "0" }
746
- };
747
- const _hoisted_13 = ["data-index", "onClick"];
748
- const _hoisted_14 = ["title"];
749
- const _sfc_main = /* @__PURE__ */ defineComponent({
750
- __name: "StkTable",
751
- props: {
752
- width: { default: "" },
753
- minWidth: { default: "" },
754
- maxWidth: { default: "" },
755
- stripe: { type: Boolean, default: false },
756
- fixedMode: { type: Boolean, default: false },
757
- headless: { type: Boolean, default: false },
758
- theme: { default: "light" },
759
- rowHeight: { default: 28 },
760
- virtual: { type: Boolean, default: false },
761
- virtualX: { type: Boolean, default: false },
762
- columns: { default: () => [] },
763
- dataSource: { default: () => [] },
764
- rowKey: { type: [String, Function], default: "" },
765
- colKey: { type: [String, Function], default: "dataIndex" },
766
- emptyCellText: { default: "--" },
767
- noDataFull: { type: Boolean, default: false },
768
- showNoData: { type: Boolean, default: true },
769
- sortRemote: { type: Boolean, default: false },
770
- showHeaderOverflow: { type: Boolean, default: false },
771
- showOverflow: { type: Boolean, default: false },
772
- showTrHoverClass: { type: Boolean, default: false },
773
- headerDrag: { type: Boolean, default: false },
774
- rowClassName: { type: Function, default: () => "" },
775
- colResizable: { type: Boolean, default: false },
776
- colMinWidth: { default: 10 },
777
- bordered: { type: [Boolean, String], default: true },
778
- autoResize: { type: [Boolean, Function], default: true }
779
- },
780
- emits: ["sort-change", "row-click", "current-change", "row-dblclick", "header-row-menu", "row-menu", "cell-click", "header-cell-click", "scroll", "scroll-x", "col-order-change", "th-drag-start", "th-drop", "update:columns"],
781
- setup(__props, { expose: __expose, emit: __emit }) {
782
- const props = __props;
783
- const emits = __emit;
784
- const tableContainer = ref();
785
- const colResizeIndicator = ref();
786
- const currentItem = ref(null);
787
- const currentHover = ref(null);
788
- let sortCol = ref();
789
- let sortOrderIndex = ref(0);
790
- const sortSwitchOrder = [null, "desc", "asc"];
791
- const tableHeaders = ref([]);
792
- const tableHeaderLast = ref([]);
793
- const dataSourceCopy = shallowRef([...props.dataSource]);
794
- const rowKeyGenStore = /* @__PURE__ */ new WeakMap();
795
- const { isColResizing, onThResizeMouseDown } = useColResize({
796
- props,
797
- emits,
798
- colKeyGen,
799
- colResizeIndicator,
800
- tableContainer,
801
- tableHeaderLast
802
- });
803
- const { onThDragStart, onThDragOver, onThDrop } = useThDrag({ emits });
804
- const {
805
- virtualScroll,
806
- virtualScrollX,
807
- virtual_on,
808
- virtual_dataSourcePart,
809
- virtual_offsetBottom,
810
- virtualX_on,
811
- virtualX_columnPart,
812
- virtualX_offsetRight,
813
- initVirtualScroll,
814
- initVirtualScrollY,
815
- initVirtualScrollX,
816
- updateVirtualScrollY,
817
- updateVirtualScrollX
818
- } = useVirtualScroll({ tableContainer, props, dataSourceCopy, tableHeaderLast });
819
- const { getFixedStyle } = useFixedStyle({
820
- tableHeaderLast,
821
- virtualScroll,
822
- virtualScrollX,
823
- virtualX_on,
824
- virtualX_offsetRight
825
- });
826
- const { setHighlightDimCell, setHighlightDimRow } = useHighlight({ props, tableContainer, rowKeyGen });
827
- if (props.autoResize) {
828
- useAutoResize({ tableContainer, initVirtualScroll, scrollTo, props, debounceMs: 500 });
829
- }
830
- useKeyboardArrowScroll(tableContainer, {
831
- scrollTo,
832
- virtualScroll,
833
- virtualScrollX
834
- });
835
- watch(
836
- () => props.columns,
837
- () => {
838
- dealColumns();
839
- initVirtualScrollX();
840
- }
841
- );
842
- dealColumns();
843
- watch(
844
- () => props.dataSource,
845
- (val) => {
846
- if (!val) {
847
- console.warn("invalid dataSource");
848
- return;
849
- }
850
- let needInitVirtualScrollY = false;
851
- if (dataSourceCopy.value.length !== val.length) {
852
- needInitVirtualScrollY = true;
853
- }
854
- dataSourceCopy.value = [...val];
855
- if (needInitVirtualScrollY)
856
- initVirtualScrollY();
857
- if (sortCol.value) {
858
- const column = tableHeaderLast.value.find((it) => it.dataIndex === sortCol.value);
859
- onColumnSort(column, false);
860
- }
861
- },
862
- {
863
- deep: false
864
- }
865
- );
866
- onMounted(() => {
867
- initVirtualScroll();
868
- });
869
- function dealColumns() {
870
- tableHeaders.value = [];
871
- tableHeaderLast.value = [];
872
- const copyColumn = props.columns;
873
- const deep = howDeepTheColumn(copyColumn);
874
- const tmpHeaderLast = [];
875
- if (deep > 1 && props.virtualX) {
876
- console.error("多级表头不支持横向虚拟滚动");
877
- }
878
- (function flat(arr, depth = 0) {
879
- if (!tableHeaders.value[depth]) {
880
- tableHeaders.value[depth] = [];
881
- }
882
- arr.forEach((col) => {
883
- var _a;
884
- col.rowSpan = col.children ? 1 : deep - depth;
885
- col.colSpan = (_a = col.children) == null ? void 0 : _a.length;
886
- if (col.rowSpan === 1)
887
- delete col.rowSpan;
888
- if (col.colSpan === 1)
889
- delete col.colSpan;
890
- tableHeaders.value[depth].push(col);
891
- if (!props.virtualX && col.children) {
892
- flat(col.children, depth + 1);
893
- } else {
894
- tmpHeaderLast.push(col);
895
- }
896
- });
897
- })(copyColumn);
898
- tableHeaderLast.value = tmpHeaderLast;
899
- }
900
- function rowKeyGen(row) {
901
- let key = rowKeyGenStore.get(row);
902
- if (!key) {
903
- key = typeof props.rowKey === "function" ? props.rowKey(row) : row[props.rowKey];
904
- rowKeyGenStore.set(row, key);
905
- }
906
- return key;
907
- }
908
- function colKeyGen(col) {
909
- return typeof props.colKey === "function" ? props.colKey(col) : col[props.colKey];
910
- }
911
- function getColWidthStyle(col) {
912
- const style = {
913
- width: col.width,
914
- minWidth: col.minWidth,
915
- maxWidth: col.maxWidth
916
- };
917
- if (props.colResizable) {
918
- style.minWidth = col.width;
919
- style.maxWidth = col.width;
920
- } else {
921
- style.minWidth = col.minWidth === void 0 ? col.width : col.minWidth;
922
- style.maxWidth = col.maxWidth === void 0 ? col.width : col.maxWidth;
923
- }
924
- return style;
925
- }
926
- function getCellStyle(tagType, col) {
927
- const style = {
928
- ...getColWidthStyle(col),
929
- ...getFixedStyle(tagType, col)
930
- };
931
- if (tagType === 1) {
932
- style.textAlign = col.headerAlign;
933
- } else if (tagType === 2) {
934
- style.textAlign = col.align;
935
- }
936
- return style;
937
- }
938
- function onColumnSort(col, click = true, options = {}) {
939
- if (!(col == null ? void 0 : col.sorter))
940
- return;
941
- options = { force: false, emit: false, ...options };
942
- if (sortCol.value !== col.dataIndex) {
943
- sortCol.value = col.dataIndex;
944
- sortOrderIndex.value = 0;
945
- }
946
- if (click)
947
- sortOrderIndex.value++;
948
- sortOrderIndex.value = sortOrderIndex.value % 3;
949
- const order = sortSwitchOrder[sortOrderIndex.value];
950
- if (!props.sortRemote || options.force) {
951
- dataSourceCopy.value = tableSort(col, order, props.dataSource);
952
- }
953
- if (click || options.emit) {
954
- emits("sort-change", col, order, toRaw(dataSourceCopy.value));
955
- }
956
- }
957
- function onRowClick(e, row) {
958
- emits("row-click", e, row);
959
- if (currentItem.value === row)
960
- return;
961
- currentItem.value = row;
962
- emits("current-change", e, row);
963
- }
964
- function onRowDblclick(e, row) {
965
- emits("row-dblclick", e, row);
966
- }
967
- function onHeaderMenu(e) {
968
- emits("header-row-menu", e);
969
- }
970
- function onRowMenu(e, row) {
971
- emits("row-menu", e, row);
972
- }
973
- function onCellClick(e, row, col) {
974
- emits("cell-click", e, row, col);
975
- }
976
- function onHeaderCellClick(e, col) {
977
- emits("header-cell-click", e, col);
978
- }
979
- function onTableWheel(e) {
980
- if (isColResizing.value) {
981
- e.preventDefault();
982
- e.stopPropagation();
983
- return;
984
- }
985
- }
986
- function onTableScroll(e) {
987
- if (!(e == null ? void 0 : e.target))
988
- return;
989
- const { scrollTop, scrollLeft } = e.target;
990
- const isYScroll = scrollTop !== virtualScroll.value.scrollTop;
991
- const isXScroll = scrollLeft !== virtualScrollX.value.scrollLeft;
992
- if (isYScroll) {
993
- virtualScroll.value.scrollTop = scrollTop;
994
- }
995
- if (virtual_on.value) {
996
- updateVirtualScrollY(scrollTop);
997
- }
998
- if (isXScroll) {
999
- virtualScrollX.value.scrollLeft = scrollLeft;
1000
- }
1001
- if (virtualX_on.value) {
1002
- updateVirtualScrollX(scrollLeft);
1003
- }
1004
- const data = {
1005
- startIndex: virtualScroll.value.startIndex,
1006
- endIndex: virtualScroll.value.endIndex
1007
- };
1008
- if (isYScroll) {
1009
- emits("scroll", e, data);
1010
- }
1011
- if (isXScroll) {
1012
- emits("scroll-x", e);
1013
- }
1014
- }
1015
- function onTrMouseOver(_e, row) {
1016
- if (props.showTrHoverClass) {
1017
- currentHover.value = rowKeyGen(row);
1018
- }
1019
- }
1020
- function setCurrentRow(rowKey, option = { silent: false }) {
1021
- if (!dataSourceCopy.value.length)
1022
- return;
1023
- currentItem.value = dataSourceCopy.value.find((it) => rowKeyGen(it) === rowKey);
1024
- if (!option.silent) {
1025
- emits("current-change", null, currentItem.value);
1026
- }
1027
- }
1028
- function setSorter(dataIndex, order, option = {}) {
1029
- var _a;
1030
- const newOption = { silent: true, sortOption: null, sort: true, ...option };
1031
- sortCol.value = dataIndex;
1032
- sortOrderIndex.value = sortSwitchOrder.findIndex((it) => it === order);
1033
- if (newOption.sort && ((_a = dataSourceCopy.value) == null ? void 0 : _a.length)) {
1034
- const column = newOption.sortOption || tableHeaderLast.value.find((it) => it.dataIndex === sortCol.value);
1035
- if (column)
1036
- onColumnSort(column, false, { force: true, emit: !newOption.silent });
1037
- else
1038
- console.warn("Can not find column by dataIndex:", sortCol.value);
1039
- }
1040
- return dataSourceCopy.value;
1041
- }
1042
- function resetSorter() {
1043
- sortCol.value = null;
1044
- sortOrderIndex.value = 0;
1045
- dataSourceCopy.value = [...props.dataSource];
1046
- }
1047
- function scrollTo(top = 0, left = 0) {
1048
- if (!tableContainer.value)
1049
- return;
1050
- if (top !== null)
1051
- tableContainer.value.scrollTop = top;
1052
- if (left !== null)
1053
- tableContainer.value.scrollLeft = left;
1054
- }
1055
- function getTableData() {
1056
- return toRaw(dataSourceCopy.value);
1057
- }
1058
- __expose({
1059
- initVirtualScroll,
1060
- initVirtualScrollX,
1061
- initVirtualScrollY,
1062
- setCurrentRow,
1063
- setHighlightDimCell,
1064
- setHighlightDimRow,
1065
- sortCol,
1066
- setSorter,
1067
- resetSorter,
1068
- scrollTo,
1069
- getTableData
1070
- });
1071
- return (_ctx, _cache) => {
1072
- return openBlock(), createElementBlock("div", {
1073
- ref_key: "tableContainer",
1074
- ref: tableContainer,
1075
- class: normalizeClass(["stk-table", {
1076
- virtual: _ctx.virtual,
1077
- "virtual-x": _ctx.virtualX,
1078
- dark: _ctx.theme === "dark",
1079
- headless: _ctx.headless,
1080
- "is-col-resizing": unref(isColResizing),
1081
- "col-resizable": props.colResizable,
1082
- border: props.bordered,
1083
- "border-h": props.bordered === "h",
1084
- "border-v": props.bordered === "v",
1085
- "border-body-v": props.bordered === "body-v",
1086
- stripe: props.stripe
1087
- }]),
1088
- style: normalizeStyle(_ctx.virtual && { "--row-height": unref(virtualScroll).rowHeight + "px" }),
1089
- onScroll: onTableScroll,
1090
- onWheel: onTableWheel
1091
- }, [
1092
- withDirectives(createElementVNode("div", {
1093
- ref_key: "colResizeIndicator",
1094
- ref: colResizeIndicator,
1095
- class: "column-resize-indicator"
1096
- }, null, 512), [
1097
- [vShow, _ctx.colResizable]
1098
- ]),
1099
- createElementVNode("table", {
1100
- class: normalizeClass(["stk-table-main", {
1101
- "fixed-mode": props.fixedMode
1102
- }]),
1103
- style: normalizeStyle({ width: _ctx.width, minWidth: _ctx.minWidth, maxWidth: _ctx.maxWidth })
1104
- }, [
1105
- !_ctx.headless ? (openBlock(), createElementBlock("thead", _hoisted_1, [
1106
- (openBlock(true), createElementBlock(Fragment, null, renderList(tableHeaders.value, (row, rowIndex) => {
1107
- return openBlock(), createElementBlock("tr", {
1108
- key: rowIndex,
1109
- onContextmenu: _cache[3] || (_cache[3] = (e) => onHeaderMenu(e))
1110
- }, [
1111
- unref(virtualX_on) ? (openBlock(), createElementBlock("th", {
1112
- key: 0,
1113
- class: "virtual-x-left",
1114
- style: normalizeStyle({
1115
- minWidth: unref(virtualScrollX).offsetLeft + "px",
1116
- width: unref(virtualScrollX).offsetLeft + "px"
1117
- })
1118
- }, null, 4)) : createCommentVNode("", true),
1119
- (openBlock(true), createElementBlock(Fragment, null, renderList(unref(virtualX_on) && rowIndex === tableHeaders.value.length - 1 ? unref(virtualX_columnPart) : row, (col, colIndex) => {
1120
- return openBlock(), createElementBlock("th", {
1121
- key: col.dataIndex,
1122
- "data-col-key": colKeyGen(col),
1123
- draggable: _ctx.headerDrag ? "true" : "false",
1124
- rowspan: unref(virtualX_on) ? 1 : col.rowSpan,
1125
- colspan: col.colSpan,
1126
- style: normalizeStyle(getCellStyle(1, col)),
1127
- title: col.title,
1128
- class: normalizeClass([
1129
- col.sorter ? "sortable" : "",
1130
- col.dataIndex === unref(sortCol) && unref(sortOrderIndex) !== 0 && "sorter-" + sortSwitchOrder[unref(sortOrderIndex)],
1131
- _ctx.showHeaderOverflow ? "text-overflow" : "",
1132
- col.headerClassName,
1133
- col.fixed ? "fixed-cell" : "",
1134
- col.fixed ? "fixed-cell--" + col.fixed : ""
1135
- ]),
1136
- onClick: (e) => {
1137
- onColumnSort(col);
1138
- onHeaderCellClick(e, col);
1139
- },
1140
- onDragstart: _cache[0] || (_cache[0] = //@ts-ignore
1141
- (...args) => unref(onThDragStart) && unref(onThDragStart)(...args)),
1142
- onDrop: _cache[1] || (_cache[1] = //@ts-ignore
1143
- (...args) => unref(onThDrop) && unref(onThDrop)(...args)),
1144
- onDragover: _cache[2] || (_cache[2] = //@ts-ignore
1145
- (...args) => unref(onThDragOver) && unref(onThDragOver)(...args))
1146
- }, [
1147
- createElementVNode("div", _hoisted_3, [
1148
- col.customHeaderCell ? (openBlock(), createBlock(resolveDynamicComponent(col.customHeaderCell), {
1149
- key: 0,
1150
- col
1151
- }, null, 8, ["col"])) : renderSlot(_ctx.$slots, "tableHeader", {
1152
- key: 1,
1153
- column: col
1154
- }, () => [
1155
- createElementVNode("span", _hoisted_4, toDisplayString(col.title), 1)
1156
- ]),
1157
- col.sorter ? (openBlock(), createElementBlock("span", _hoisted_5, _hoisted_7)) : createCommentVNode("", true),
1158
- _ctx.colResizable && colIndex > 0 ? (openBlock(), createElementBlock("div", {
1159
- key: 3,
1160
- class: "table-header-resizer left",
1161
- onMousedown: (e) => unref(onThResizeMouseDown)(e, col, true)
1162
- }, null, 40, _hoisted_8)) : createCommentVNode("", true),
1163
- _ctx.colResizable ? (openBlock(), createElementBlock("div", {
1164
- key: 4,
1165
- class: "table-header-resizer right",
1166
- onMousedown: (e) => unref(onThResizeMouseDown)(e, col)
1167
- }, null, 40, _hoisted_9)) : createCommentVNode("", true)
1168
- ])
1169
- ], 46, _hoisted_2);
1170
- }), 128)),
1171
- unref(virtualX_on) ? (openBlock(), createElementBlock("th", {
1172
- key: 1,
1173
- class: "virtual-x-right",
1174
- style: normalizeStyle({
1175
- minWidth: unref(virtualX_offsetRight) + "px",
1176
- width: unref(virtualX_offsetRight) + "px"
1177
- })
1178
- }, null, 4)) : createCommentVNode("", true)
1179
- ], 32);
1180
- }), 128))
1181
- ])) : createCommentVNode("", true),
1182
- createElementVNode("tbody", null, [
1183
- unref(virtual_on) ? (openBlock(), createElementBlock("tr", {
1184
- key: 0,
1185
- style: normalizeStyle({ height: `${unref(virtualScroll).offsetTop}px` }),
1186
- class: "padding-top-tr"
1187
- }, [
1188
- unref(virtualX_on) && _ctx.fixedMode && _ctx.headless ? (openBlock(), createElementBlock("td", _hoisted_10)) : createCommentVNode("", true),
1189
- _ctx.fixedMode && _ctx.headless ? (openBlock(true), createElementBlock(Fragment, { key: 1 }, renderList(unref(virtualX_columnPart), (col) => {
1190
- return openBlock(), createElementBlock("td", {
1191
- key: col.dataIndex,
1192
- style: normalizeStyle(getCellStyle(2, col))
1193
- }, null, 4);
1194
- }), 128)) : createCommentVNode("", true)
1195
- ], 4)) : createCommentVNode("", true),
1196
- (openBlock(true), createElementBlock(Fragment, null, renderList(unref(virtual_dataSourcePart), (row, i) => {
1197
- return openBlock(), createElementBlock("tr", {
1198
- key: _ctx.rowKey ? rowKeyGen(row) : i,
1199
- "data-row-key": _ctx.rowKey ? rowKeyGen(row) : i,
1200
- class: normalizeClass({
1201
- active: _ctx.rowKey ? rowKeyGen(row) === (currentItem.value && rowKeyGen(currentItem.value)) : row === currentItem.value,
1202
- hover: _ctx.rowKey ? rowKeyGen(row) === currentHover.value : row === currentHover.value,
1203
- [_ctx.rowClassName(row, i)]: true
1204
- }),
1205
- style: normalizeStyle({
1206
- backgroundColor: row._bgc
1207
- }),
1208
- onClick: (e) => onRowClick(e, row),
1209
- onDblclick: (e) => onRowDblclick(e, row),
1210
- onContextmenu: (e) => onRowMenu(e, row),
1211
- onMouseover: (e) => onTrMouseOver(e, row)
1212
- }, [
1213
- unref(virtualX_on) ? (openBlock(), createElementBlock("td", _hoisted_12)) : createCommentVNode("", true),
1214
- (openBlock(true), createElementBlock(Fragment, null, renderList(unref(virtualX_columnPart), (col) => {
1215
- return openBlock(), createElementBlock("td", {
1216
- key: col.dataIndex,
1217
- "data-index": col.dataIndex,
1218
- class: normalizeClass([
1219
- col.className,
1220
- _ctx.showOverflow ? "text-overflow" : "",
1221
- col.fixed ? "fixed-cell" : "",
1222
- col.fixed ? "fixed-cell--" + col.fixed : ""
1223
- ]),
1224
- style: normalizeStyle(getCellStyle(2, col)),
1225
- onClick: (e) => onCellClick(e, row, col)
1226
- }, [
1227
- col.customCell ? (openBlock(), createBlock(resolveDynamicComponent(col.customCell), {
1228
- key: 0,
1229
- col,
1230
- row,
1231
- "cell-value": row[col.dataIndex]
1232
- }, null, 8, ["col", "row", "cell-value"])) : (openBlock(), createElementBlock("div", {
1233
- key: 1,
1234
- class: "table-cell-wrapper",
1235
- title: row[col.dataIndex]
1236
- }, toDisplayString(row[col.dataIndex] ?? _ctx.emptyCellText), 9, _hoisted_14))
1237
- ], 14, _hoisted_13);
1238
- }), 128))
1239
- ], 46, _hoisted_11);
1240
- }), 128)),
1241
- unref(virtual_on) ? (openBlock(), createElementBlock("tr", {
1242
- key: 1,
1243
- style: normalizeStyle({ height: `${unref(virtual_offsetBottom)}px` })
1244
- }, null, 4)) : createCommentVNode("", true)
1245
- ])
1246
- ], 6),
1247
- (!dataSourceCopy.value || !dataSourceCopy.value.length) && _ctx.showNoData ? (openBlock(), createElementBlock("div", {
1248
- key: 0,
1249
- class: normalizeClass(["stk-table-no-data", { "no-data-full": _ctx.noDataFull }])
1250
- }, [
1251
- renderSlot(_ctx.$slots, "empty", {}, () => [
1252
- createTextVNode("暂无数据")
1253
- ])
1254
- ], 2)) : createCommentVNode("", true)
1255
- ], 38);
1256
- };
1257
- }
1258
- });
1259
- export {
1260
- _sfc_main as StkTable,
1261
- insertToOrderedArray,
1262
- tableSort
1263
- };
1
+ import { onMounted, onBeforeUnmount, watch, ref, computed, defineComponent, shallowRef, toRaw, openBlock, createElementBlock, normalizeClass, unref, normalizeStyle, withDirectives, createElementVNode, vShow, Fragment, renderList, createCommentVNode, createBlock, resolveDynamicComponent, renderSlot, toDisplayString, createTextVNode } from "vue";
2
+ import { interpolateRgb } from "d3-interpolate";
3
+ function useAutoResize({ tableContainer, initVirtualScroll, scrollTo, props, debounceMs }) {
4
+ let resizeObserver = null;
5
+ onMounted(() => {
6
+ initResizeObserver();
7
+ });
8
+ onBeforeUnmount(() => {
9
+ removeResizeObserver();
10
+ });
11
+ function initResizeObserver() {
12
+ if (window.ResizeObserver) {
13
+ if (!tableContainer.value) {
14
+ const watchDom = watch(
15
+ () => tableContainer,
16
+ () => {
17
+ initResizeObserver();
18
+ watchDom();
19
+ }
20
+ );
21
+ return;
22
+ }
23
+ resizeObserver = new ResizeObserver(resizeCallback);
24
+ resizeObserver.observe(tableContainer.value);
25
+ } else {
26
+ window.addEventListener("resize", resizeCallback);
27
+ }
28
+ }
29
+ function removeResizeObserver() {
30
+ if (resizeObserver) {
31
+ resizeObserver.disconnect();
32
+ resizeObserver = null;
33
+ } else {
34
+ window.removeEventListener("resize", resizeCallback);
35
+ }
36
+ }
37
+ let debounceTime = 0;
38
+ function resizeCallback() {
39
+ if (debounceTime) {
40
+ window.clearTimeout(debounceTime);
41
+ }
42
+ debounceTime = window.setTimeout(() => {
43
+ if (props.autoResize) {
44
+ initVirtualScroll();
45
+ if (typeof props.autoResize === "function") {
46
+ props.autoResize();
47
+ }
48
+ }
49
+ debounceTime = 0;
50
+ }, debounceMs);
51
+ }
52
+ }
53
+ const Default_Col_Width = "100";
54
+ const Default_Table_Height = 100;
55
+ const Default_Table_Width = 200;
56
+ const Highlight_Color = {
57
+ light: { from: "#71a2fd", to: "#fff" },
58
+ dark: { from: "#1e4c99", to: "#181c21" }
59
+ };
60
+ const Highlight_Duration = 2e3;
61
+ const Highlight_Color_Change_Freq = 100;
62
+ let _chromeVersion = 0;
63
+ try {
64
+ const userAgent = navigator.userAgent.match(/chrome\/\d+/i);
65
+ if (userAgent) {
66
+ _chromeVersion = +userAgent[0].split("/")[1];
67
+ }
68
+ } catch (e) {
69
+ console.error("Cannot get Chrome version", e);
70
+ }
71
+ const Is_Legacy_Mode = _chromeVersion < 56;
72
+ function useColResize({
73
+ tableContainer,
74
+ tableHeaderLast,
75
+ colResizeIndicator,
76
+ props,
77
+ emits,
78
+ colKeyGen
79
+ }) {
80
+ const isColResizing = ref(false);
81
+ let colResizeState = {
82
+ currentCol: null,
83
+ currentColIndex: 0,
84
+ lastCol: null,
85
+ startX: 0,
86
+ startOffsetTableX: 0
87
+ };
88
+ onMounted(() => {
89
+ initColResizeEvent();
90
+ });
91
+ onBeforeUnmount(() => {
92
+ clearColResizeEvent();
93
+ });
94
+ function initColResizeEvent() {
95
+ window.addEventListener("mousemove", onThResizeMouseMove);
96
+ window.addEventListener("mouseup", onThResizeMouseUp);
97
+ }
98
+ function clearColResizeEvent() {
99
+ window.removeEventListener("mousemove", onThResizeMouseMove);
100
+ window.removeEventListener("mouseup", onThResizeMouseUp);
101
+ }
102
+ function onThResizeMouseDown(e, col, isPrev = false) {
103
+ if (!tableContainer.value)
104
+ return;
105
+ e.stopPropagation();
106
+ e.preventDefault();
107
+ const { clientX } = e;
108
+ const { scrollLeft, scrollTop } = tableContainer.value;
109
+ const { left } = tableContainer.value.getBoundingClientRect();
110
+ let colIndex = tableHeaderLast.value.findIndex((it) => colKeyGen(it) === colKeyGen(col));
111
+ if (isPrev) {
112
+ colIndex -= 1;
113
+ col = tableHeaderLast.value[colIndex];
114
+ }
115
+ const offsetTableX = clientX - left + scrollLeft;
116
+ isColResizing.value = true;
117
+ Object.assign(colResizeState, {
118
+ currentCol: col,
119
+ currentColIndex: colIndex,
120
+ lastCol: findLastChildCol(col),
121
+ startX: clientX,
122
+ startOffsetTableX: offsetTableX
123
+ });
124
+ if (colResizeIndicator.value) {
125
+ const style = colResizeIndicator.value.style;
126
+ style.display = "block";
127
+ style.left = offsetTableX + "px";
128
+ style.top = scrollTop + "px";
129
+ }
130
+ }
131
+ function onThResizeMouseMove(e) {
132
+ if (!isColResizing.value)
133
+ return;
134
+ e.stopPropagation();
135
+ e.preventDefault();
136
+ const { lastCol, startX, startOffsetTableX } = colResizeState;
137
+ const { clientX } = e;
138
+ let moveX = clientX - startX;
139
+ const currentColWidth = parseInt((lastCol == null ? void 0 : lastCol.width) || Default_Col_Width);
140
+ if (currentColWidth + moveX < props.colMinWidth) {
141
+ moveX = -currentColWidth;
142
+ }
143
+ const offsetTableX = startOffsetTableX + moveX;
144
+ if (!colResizeIndicator.value)
145
+ return;
146
+ colResizeIndicator.value.style.left = offsetTableX + "px";
147
+ }
148
+ function onThResizeMouseUp(e) {
149
+ if (!isColResizing.value)
150
+ return;
151
+ const { startX, lastCol } = colResizeState;
152
+ const { clientX } = e;
153
+ const moveX = clientX - startX;
154
+ let width = parseInt((lastCol == null ? void 0 : lastCol.width) || Default_Col_Width) + moveX;
155
+ if (width < props.colMinWidth)
156
+ width = props.colMinWidth;
157
+ const curCol = tableHeaderLast.value.find((it) => colKeyGen(it) === colKeyGen(lastCol));
158
+ if (!curCol)
159
+ return;
160
+ curCol.width = width + "px";
161
+ emits("update:columns", [...props.columns]);
162
+ if (colResizeIndicator.value) {
163
+ const style = colResizeIndicator.value.style;
164
+ style.display = "none";
165
+ style.left = "0";
166
+ style.top = "0";
167
+ }
168
+ isColResizing.value = false;
169
+ colResizeState = {
170
+ currentCol: null,
171
+ currentColIndex: 0,
172
+ lastCol: null,
173
+ startX: 0,
174
+ startOffsetTableX: 0
175
+ };
176
+ }
177
+ function findLastChildCol(column) {
178
+ var _a;
179
+ if ((_a = column == null ? void 0 : column.children) == null ? void 0 : _a.length) {
180
+ const lastChild = column.children.at(-1);
181
+ return findLastChildCol(lastChild);
182
+ }
183
+ return column;
184
+ }
185
+ return {
186
+ isColResizing,
187
+ onThResizeMouseDown,
188
+ onThResizeMouseMove,
189
+ onThResizeMouseUp
190
+ };
191
+ }
192
+ function useFixedStyle({ props, tableHeaderLast, virtualScroll, virtualScrollX, virtualX_on, virtualX_offsetRight }) {
193
+ const fixedColumnsPositionStore = computed(() => {
194
+ const store = {};
195
+ const cols = [...tableHeaderLast.value];
196
+ let left = 0;
197
+ let rightStartIndex = 0;
198
+ for (let i = 0; i < cols.length; i++) {
199
+ const item = cols[i];
200
+ if (item.fixed === "left") {
201
+ store[item.dataIndex] = left;
202
+ left += parseInt(item.width || Default_Col_Width);
203
+ }
204
+ if (!rightStartIndex && item.fixed === "right") {
205
+ rightStartIndex = i;
206
+ }
207
+ }
208
+ let right = 0;
209
+ for (let i = cols.length - 1; i >= rightStartIndex; i--) {
210
+ const item = cols[i];
211
+ if (item.fixed === "right") {
212
+ store[item.dataIndex] = right;
213
+ right += parseInt(item.width || Default_Col_Width);
214
+ }
215
+ }
216
+ return store;
217
+ });
218
+ function getFixedStyle(tagType, col, depth = 0) {
219
+ const { fixed, dataIndex } = col;
220
+ const isFixedLeft = fixed === "left";
221
+ const style = {};
222
+ if (Is_Legacy_Mode) {
223
+ style.position = "relative";
224
+ } else {
225
+ style.position = "sticky";
226
+ }
227
+ if (tagType === 1) {
228
+ if (Is_Legacy_Mode) {
229
+ style.top = virtualScroll.value.scrollTop + depth * props.rowHeight + "px";
230
+ } else {
231
+ style.top = depth * props.rowHeight + "px";
232
+ }
233
+ style.zIndex = isFixedLeft ? "5" : "4";
234
+ } else {
235
+ style.zIndex = isFixedLeft ? "3" : "2";
236
+ }
237
+ if (fixed === "left" || fixed === "right") {
238
+ if (Is_Legacy_Mode) {
239
+ if (isFixedLeft) {
240
+ if (virtualX_on.value)
241
+ style.left = virtualScrollX.value.scrollLeft - virtualScrollX.value.offsetLeft + "px";
242
+ else
243
+ style.left = virtualScrollX.value.scrollLeft + "px";
244
+ } else {
245
+ style.right = `${virtualX_offsetRight.value}px`;
246
+ }
247
+ } else {
248
+ if (isFixedLeft) {
249
+ style.left = fixedColumnsPositionStore.value[dataIndex] + "px";
250
+ } else {
251
+ style.right = fixedColumnsPositionStore.value[dataIndex] + "px";
252
+ }
253
+ }
254
+ }
255
+ return style;
256
+ }
257
+ return {
258
+ getFixedStyle
259
+ };
260
+ }
261
+ function useHighlight({ props, tableContainer, rowKeyGen }) {
262
+ const highlightInter = computed(() => {
263
+ return interpolateRgb(Highlight_Color[props.theme].from, Highlight_Color[props.theme].to);
264
+ });
265
+ const highlightDimRows = /* @__PURE__ */ new Set();
266
+ const highlightDimRowsTimeout = /* @__PURE__ */ new Map();
267
+ const highlightDimCellsTimeout = /* @__PURE__ */ new Map();
268
+ let calcHighlightDimLoop = false;
269
+ function calcHighlightLoop() {
270
+ if (calcHighlightDimLoop)
271
+ return;
272
+ calcHighlightDimLoop = true;
273
+ const recursion = () => {
274
+ window.setTimeout(() => {
275
+ const nowTs = Date.now();
276
+ const needDeleteRows = [];
277
+ highlightDimRows.forEach((row) => {
278
+ const progress = (nowTs - row._bgc_progress_ms) / Highlight_Duration;
279
+ if (0 < progress && progress < 1) {
280
+ row._bgc = highlightInter.value(progress);
281
+ } else {
282
+ row._bgc = "";
283
+ needDeleteRows.push(row);
284
+ }
285
+ });
286
+ needDeleteRows.forEach((row) => highlightDimRows.delete(row));
287
+ if (highlightDimRows.size > 0) {
288
+ recursion();
289
+ } else {
290
+ calcHighlightDimLoop = false;
291
+ }
292
+ }, Highlight_Color_Change_Freq);
293
+ };
294
+ recursion();
295
+ }
296
+ function setHighlightDimCell(rowKeyValue, dataIndex) {
297
+ var _a;
298
+ const cellEl = (_a = tableContainer.value) == null ? void 0 : _a.querySelector(`[data-row-key="${rowKeyValue}"]>[data-index="${dataIndex}"]`);
299
+ if (!cellEl)
300
+ return;
301
+ if (cellEl.classList.contains("highlight-cell")) {
302
+ cellEl.classList.remove("highlight-cell");
303
+ void cellEl.offsetHeight;
304
+ }
305
+ cellEl.classList.add("highlight-cell");
306
+ window.clearTimeout(highlightDimCellsTimeout.get(rowKeyValue));
307
+ highlightDimCellsTimeout.set(
308
+ rowKeyValue,
309
+ window.setTimeout(() => {
310
+ cellEl.classList.remove("highlight-cell");
311
+ highlightDimCellsTimeout.delete(rowKeyValue);
312
+ }, Highlight_Duration)
313
+ );
314
+ }
315
+ function setHighlightDimRow(rowKeyValues) {
316
+ var _a, _b;
317
+ if (!Array.isArray(rowKeyValues))
318
+ rowKeyValues = [rowKeyValues];
319
+ if (props.virtual) {
320
+ const nowTs = Date.now();
321
+ for (let i = 0; i < rowKeyValues.length; i++) {
322
+ const rowKeyValue = rowKeyValues[i];
323
+ const row = props.dataSource.find((it) => rowKeyGen(it) === rowKeyValue);
324
+ if (!row)
325
+ continue;
326
+ row._bgc_progress_ms = nowTs;
327
+ highlightDimRows.add(row);
328
+ }
329
+ calcHighlightLoop();
330
+ } else {
331
+ let needRepaint = false;
332
+ const rowElTemp = [];
333
+ for (let i = 0; i < rowKeyValues.length; i++) {
334
+ const rowKeyValue = rowKeyValues[i];
335
+ const rowEl = (_a = tableContainer.value) == null ? void 0 : _a.querySelector(`[data-row-key="${rowKeyValue}"]`);
336
+ if (!rowEl)
337
+ continue;
338
+ if (rowEl.classList.contains("highlight-row")) {
339
+ rowEl.classList.remove("highlight-row");
340
+ needRepaint = true;
341
+ }
342
+ rowElTemp.push(rowEl);
343
+ window.clearTimeout(highlightDimRowsTimeout.get(rowKeyValue));
344
+ highlightDimRowsTimeout.set(
345
+ rowKeyValue,
346
+ window.setTimeout(() => {
347
+ rowEl.classList.remove("highlight-row");
348
+ highlightDimRowsTimeout.delete(rowKeyValue);
349
+ }, Highlight_Duration)
350
+ );
351
+ }
352
+ if (needRepaint) {
353
+ void ((_b = tableContainer.value) == null ? void 0 : _b.offsetWidth);
354
+ }
355
+ rowElTemp.forEach((el) => el.classList.add("highlight-row"));
356
+ }
357
+ }
358
+ return {
359
+ setHighlightDimRow,
360
+ setHighlightDimCell
361
+ };
362
+ }
363
+ const ARROW_CODES = ["ArrowUp", "ArrowRight", "ArrowDown", "ArrowLeft"];
364
+ function useKeyboardArrowScroll(targetElement, { scrollTo, virtualScroll, virtualScrollX }) {
365
+ let isMouseOver = false;
366
+ onMounted(() => {
367
+ var _a, _b, _c;
368
+ window.addEventListener("keydown", handleKeydown);
369
+ (_a = targetElement.value) == null ? void 0 : _a.addEventListener("mouseenter", handleMouseEnter);
370
+ (_b = targetElement.value) == null ? void 0 : _b.addEventListener("mouseleave", handleMouseLeave);
371
+ (_c = targetElement.value) == null ? void 0 : _c.addEventListener("mousedown", handleMouseDown);
372
+ });
373
+ onBeforeUnmount(() => {
374
+ var _a, _b, _c;
375
+ window.removeEventListener("keydown", handleKeydown);
376
+ (_a = targetElement.value) == null ? void 0 : _a.removeEventListener("mouseenter", handleMouseEnter);
377
+ (_b = targetElement.value) == null ? void 0 : _b.removeEventListener("mouseleave", handleMouseLeave);
378
+ (_c = targetElement.value) == null ? void 0 : _c.removeEventListener("mousedown", handleMouseDown);
379
+ });
380
+ function handleKeydown(e) {
381
+ if (!ARROW_CODES.includes(e.code))
382
+ return;
383
+ if (!isMouseOver)
384
+ return;
385
+ e.preventDefault();
386
+ const { scrollTop, rowHeight } = virtualScroll.value;
387
+ const { scrollLeft } = virtualScrollX.value;
388
+ if (e.code === ARROW_CODES[0]) {
389
+ scrollTo(scrollTop - rowHeight, null);
390
+ } else if (e.code === ARROW_CODES[1]) {
391
+ scrollTo(null, scrollLeft + rowHeight);
392
+ } else if (e.code === ARROW_CODES[2]) {
393
+ scrollTo(scrollTop + rowHeight, null);
394
+ } else if (e.code === ARROW_CODES[3]) {
395
+ scrollTo(null, scrollLeft - rowHeight);
396
+ }
397
+ }
398
+ function handleMouseEnter() {
399
+ isMouseOver = true;
400
+ }
401
+ function handleMouseLeave() {
402
+ isMouseOver = false;
403
+ }
404
+ function handleMouseDown() {
405
+ if (!isMouseOver)
406
+ isMouseOver = true;
407
+ }
408
+ }
409
+ function useThDrag({ emits }) {
410
+ let dragStartKey = void 0;
411
+ function onThDragStart(e) {
412
+ dragStartKey = e.target.dataset.colKey;
413
+ emits("th-drag-start", dragStartKey);
414
+ }
415
+ function onThDragOver(e) {
416
+ e.preventDefault();
417
+ }
418
+ function onThDrop(e) {
419
+ let th = e.target;
420
+ while (th) {
421
+ if (th.tagName === "TH")
422
+ break;
423
+ th = th.parentNode;
424
+ }
425
+ if (dragStartKey !== th.dataset.colKey) {
426
+ emits("col-order-change", dragStartKey, th.dataset.colKey);
427
+ }
428
+ emits("th-drop", th.dataset.colKey);
429
+ }
430
+ return {
431
+ onThDragStart,
432
+ onThDragOver,
433
+ onThDrop
434
+ };
435
+ }
436
+ function getCalcWidth(col) {
437
+ return parseInt(col.minWidth || col.width || Default_Col_Width);
438
+ }
439
+ function useVirtualScroll({ tableContainer, props, dataSourceCopy, tableHeaderLast }) {
440
+ const virtualScroll = ref({
441
+ containerHeight: 0,
442
+ rowHeight: props.rowHeight,
443
+ pageSize: 10,
444
+ startIndex: 0,
445
+ endIndex: 0,
446
+ offsetTop: 0,
447
+ scrollTop: 0
448
+ });
449
+ const virtualScrollX = ref({
450
+ containerWidth: 0,
451
+ startIndex: 0,
452
+ endIndex: 0,
453
+ offsetLeft: 0,
454
+ scrollLeft: 0
455
+ });
456
+ const virtual_on = computed(() => {
457
+ return props.virtual && dataSourceCopy.value.length > virtualScroll.value.pageSize * 2;
458
+ });
459
+ const virtual_dataSourcePart = computed(() => {
460
+ if (!virtual_on.value)
461
+ return dataSourceCopy.value;
462
+ const { startIndex, endIndex } = virtualScroll.value;
463
+ return dataSourceCopy.value.slice(startIndex, endIndex);
464
+ });
465
+ const virtual_offsetBottom = computed(() => {
466
+ if (!virtual_on.value)
467
+ return 0;
468
+ const { startIndex, rowHeight } = virtualScroll.value;
469
+ return (dataSourceCopy.value.length - startIndex - virtual_dataSourcePart.value.length) * rowHeight;
470
+ });
471
+ const virtualX_on = computed(() => {
472
+ return props.virtualX && tableHeaderLast.value.reduce((sum, col) => sum += getCalcWidth(col), 0) > virtualScrollX.value.containerWidth * 1.5;
473
+ });
474
+ const virtualX_columnPart = computed(() => {
475
+ if (virtualX_on.value) {
476
+ const leftCols = [];
477
+ const rightCols = [];
478
+ const { startIndex, endIndex } = virtualScrollX.value;
479
+ for (let i = 0; i < startIndex; i++) {
480
+ const col = tableHeaderLast.value[i];
481
+ if (col.fixed === "left")
482
+ leftCols.push(col);
483
+ }
484
+ for (let i = endIndex; i < tableHeaderLast.value.length; i++) {
485
+ const col = tableHeaderLast.value[i];
486
+ if (col.fixed === "right")
487
+ rightCols.push(col);
488
+ }
489
+ const mainColumns = tableHeaderLast.value.slice(startIndex, endIndex);
490
+ return leftCols.concat(mainColumns).concat(rightCols);
491
+ }
492
+ return tableHeaderLast.value;
493
+ });
494
+ const virtualX_offsetRight = computed(() => {
495
+ if (!virtualX_on.value)
496
+ return 0;
497
+ let width = 0;
498
+ for (let i = virtualScrollX.value.endIndex; i < tableHeaderLast.value.length; i++) {
499
+ const col = tableHeaderLast.value[i];
500
+ if (col.fixed !== "right") {
501
+ width += getCalcWidth(col);
502
+ }
503
+ }
504
+ return width;
505
+ });
506
+ function initVirtualScrollY(height) {
507
+ if (!virtual_on.value)
508
+ return;
509
+ const { offsetHeight, scrollTop } = tableContainer.value || {};
510
+ const { rowHeight } = virtualScroll.value;
511
+ let containerHeight;
512
+ if (typeof height === "number") {
513
+ containerHeight = height;
514
+ } else {
515
+ containerHeight = offsetHeight || Default_Table_Height;
516
+ }
517
+ Object.assign(virtualScroll.value, {
518
+ containerHeight,
519
+ pageSize: Math.ceil(containerHeight / rowHeight) + 1
520
+ // 这里最终+1,因为headless=true无头时,需要上下各预渲染一行。
521
+ });
522
+ updateVirtualScrollY(scrollTop);
523
+ }
524
+ function initVirtualScrollX() {
525
+ if (!props.virtualX)
526
+ return;
527
+ const { offsetWidth, scrollLeft } = tableContainer.value || {};
528
+ virtualScrollX.value.containerWidth = offsetWidth || Default_Table_Width;
529
+ updateVirtualScrollX(scrollLeft);
530
+ }
531
+ function initVirtualScroll(height) {
532
+ initVirtualScrollY(height);
533
+ initVirtualScrollX();
534
+ }
535
+ function updateVirtualScrollY(sTop = 0) {
536
+ const { rowHeight, pageSize } = virtualScroll.value;
537
+ const startIndex = Math.floor(sTop / rowHeight);
538
+ let endIndex = startIndex + pageSize;
539
+ if (endIndex > dataSourceCopy.value.length) {
540
+ endIndex = dataSourceCopy.value.length;
541
+ }
542
+ Object.assign(virtualScroll.value, {
543
+ startIndex,
544
+ offsetTop: startIndex * rowHeight,
545
+ // startIndex之前的高度
546
+ endIndex
547
+ });
548
+ }
549
+ function updateVirtualScrollX(sLeft = 0) {
550
+ var _a;
551
+ const headerLength = (_a = tableHeaderLast.value) == null ? void 0 : _a.length;
552
+ if (!headerLength)
553
+ return;
554
+ let startIndex = 0;
555
+ let offsetLeft = 0;
556
+ let colWidthSum = 0;
557
+ for (let colIndex = 0; colIndex < headerLength; colIndex++) {
558
+ startIndex++;
559
+ const col = tableHeaderLast.value[colIndex];
560
+ if (col.fixed === "left")
561
+ continue;
562
+ const colWidth = getCalcWidth(col);
563
+ colWidthSum += colWidth;
564
+ if (colWidthSum >= sLeft) {
565
+ offsetLeft = colWidthSum - colWidth;
566
+ startIndex--;
567
+ break;
568
+ }
569
+ }
570
+ colWidthSum = 0;
571
+ let endIndex = headerLength;
572
+ for (let colIndex = startIndex; colIndex < headerLength; colIndex++) {
573
+ const col = tableHeaderLast.value[colIndex];
574
+ colWidthSum += getCalcWidth(col);
575
+ if (colWidthSum >= virtualScrollX.value.containerWidth) {
576
+ endIndex = colIndex + 1;
577
+ break;
578
+ }
579
+ }
580
+ if (endIndex > headerLength) {
581
+ endIndex = headerLength;
582
+ }
583
+ Object.assign(virtualScrollX.value, { startIndex, endIndex, offsetLeft });
584
+ }
585
+ return {
586
+ virtualScroll,
587
+ virtualScrollX,
588
+ virtual_on,
589
+ virtual_dataSourcePart,
590
+ virtual_offsetBottom,
591
+ virtualX_on,
592
+ virtualX_columnPart,
593
+ virtualX_offsetRight,
594
+ initVirtualScroll,
595
+ initVirtualScrollY,
596
+ initVirtualScrollX,
597
+ updateVirtualScrollY,
598
+ updateVirtualScrollX
599
+ };
600
+ }
601
+ function insertToOrderedArray(sortState, newItem, targetArray) {
602
+ const { dataIndex, order } = sortState;
603
+ let { sortType } = sortState;
604
+ if (!sortType)
605
+ sortType = typeof newItem[dataIndex];
606
+ const data = [...targetArray];
607
+ if (!order) {
608
+ data.unshift(newItem);
609
+ return data;
610
+ }
611
+ let sIndex = 0;
612
+ let eIndex = data.length - 1;
613
+ const targetVal = newItem[dataIndex];
614
+ while (sIndex <= eIndex) {
615
+ const midIndex = Math.floor((sIndex + eIndex) / 2);
616
+ const midVal = data[midIndex][dataIndex];
617
+ const compareRes = strCompare(midVal, targetVal, sortType);
618
+ if (compareRes === 0) {
619
+ sIndex = midIndex;
620
+ break;
621
+ } else if (compareRes === -1) {
622
+ if (order === "asc")
623
+ sIndex = midIndex + 1;
624
+ else
625
+ eIndex = midIndex - 1;
626
+ } else {
627
+ if (order === "asc")
628
+ eIndex = midIndex - 1;
629
+ else
630
+ sIndex = midIndex + 1;
631
+ }
632
+ }
633
+ data.splice(sIndex, 0, newItem);
634
+ return data;
635
+ }
636
+ function strCompare(a, b, type) {
637
+ if (type === "number") {
638
+ if (+a > +b)
639
+ return 1;
640
+ else if (+a === +b)
641
+ return 0;
642
+ else
643
+ return -1;
644
+ } else {
645
+ return String(a).localeCompare(b);
646
+ }
647
+ }
648
+ function tableSort(sortOption, order, dataSource) {
649
+ if (!(dataSource == null ? void 0 : dataSource.length))
650
+ return dataSource || [];
651
+ let targetDataSource = [...dataSource];
652
+ if (typeof sortOption.sorter === "function") {
653
+ const customSorterData = sortOption.sorter(targetDataSource, { order, column: sortOption });
654
+ if (customSorterData)
655
+ targetDataSource = customSorterData;
656
+ } else if (order) {
657
+ const sortField = sortOption.sortField || sortOption.dataIndex;
658
+ let { sortType } = sortOption;
659
+ if (!sortType)
660
+ sortType = typeof dataSource[0][sortField];
661
+ if (sortType === "number") {
662
+ const nanArr = [];
663
+ const numArr = [];
664
+ for (let i = 0; i < targetDataSource.length; i++) {
665
+ const row = targetDataSource[i];
666
+ if (row[sortField] === null || row[sortField] === "" || typeof row[sortField] === "boolean" || Number.isNaN(+row[sortField])) {
667
+ nanArr.push(row);
668
+ } else {
669
+ numArr.push(row);
670
+ }
671
+ }
672
+ if (order === "asc") {
673
+ numArr.sort((a, b) => +a[sortField] - +b[sortField]);
674
+ targetDataSource = [...nanArr, ...numArr];
675
+ } else {
676
+ numArr.sort((a, b) => +b[sortField] - +a[sortField]);
677
+ targetDataSource = [...numArr, ...nanArr];
678
+ }
679
+ } else {
680
+ if (order === "asc") {
681
+ targetDataSource.sort((a, b) => String(a[sortField]).localeCompare(b[sortField]));
682
+ } else {
683
+ targetDataSource.sort((a, b) => String(a[sortField]).localeCompare(b[sortField]) * -1);
684
+ }
685
+ }
686
+ }
687
+ return targetDataSource;
688
+ }
689
+ function howDeepTheHeader(arr, level = 1) {
690
+ const levels = [level];
691
+ arr.forEach((item) => {
692
+ var _a;
693
+ if ((_a = item.children) == null ? void 0 : _a.length) {
694
+ levels.push(howDeepTheHeader(item.children, level + 1));
695
+ }
696
+ });
697
+ return Math.max(...levels);
698
+ }
699
+ const _hoisted_1 = { key: 0 };
700
+ const _hoisted_2 = ["data-col-key", "draggable", "rowspan", "colspan", "title", "onClick"];
701
+ const _hoisted_3 = { class: "table-header-cell-wrapper" };
702
+ const _hoisted_4 = { class: "table-header-title" };
703
+ const _hoisted_5 = {
704
+ key: 2,
705
+ class: "table-header-sorter"
706
+ };
707
+ const _hoisted_6 = /* @__PURE__ */ createElementVNode("svg", {
708
+ xmlns: "http://www.w3.org/2000/svg",
709
+ width: "16px",
710
+ height: "16px",
711
+ viewBox: "0 0 16 16"
712
+ }, [
713
+ /* @__PURE__ */ createElementVNode("g", { id: "sort-btn" }, [
714
+ /* @__PURE__ */ createElementVNode("polygon", {
715
+ id: "arrow-up",
716
+ fill: "#757699",
717
+ points: "8 2 4.8 6 11.2 6"
718
+ }),
719
+ /* @__PURE__ */ createElementVNode("polygon", {
720
+ id: "arrow-down",
721
+ transform: "translate(8, 12) rotate(-180) translate(-8, -12) ",
722
+ points: "8 10 4.8 14 11.2 14"
723
+ })
724
+ ])
725
+ ], -1);
726
+ const _hoisted_7 = [
727
+ _hoisted_6
728
+ ];
729
+ const _hoisted_8 = ["onMousedown"];
730
+ const _hoisted_9 = ["onMousedown"];
731
+ const _hoisted_10 = {
732
+ key: 0,
733
+ class: "virtual-x-left",
734
+ style: { "padding": "0" }
735
+ };
736
+ const _hoisted_11 = ["data-row-key", "onClick", "onDblclick", "onContextmenu", "onMouseover"];
737
+ const _hoisted_12 = {
738
+ key: 0,
739
+ class: "virtual-x-left",
740
+ style: { "padding": "0" }
741
+ };
742
+ const _hoisted_13 = ["data-index", "onClick"];
743
+ const _hoisted_14 = ["title"];
744
+ const _sfc_main = /* @__PURE__ */ defineComponent({
745
+ __name: "StkTable",
746
+ props: {
747
+ width: { default: "" },
748
+ minWidth: { default: "" },
749
+ maxWidth: { default: "" },
750
+ stripe: { type: Boolean, default: false },
751
+ fixedMode: { type: Boolean, default: false },
752
+ headless: { type: Boolean, default: false },
753
+ theme: { default: "light" },
754
+ rowHeight: { default: 28 },
755
+ virtual: { type: Boolean, default: false },
756
+ virtualX: { type: Boolean, default: false },
757
+ columns: { default: () => [] },
758
+ dataSource: { default: () => [] },
759
+ rowKey: { type: [String, Function], default: "" },
760
+ colKey: { type: [String, Function], default: "dataIndex" },
761
+ emptyCellText: { default: "--" },
762
+ noDataFull: { type: Boolean, default: false },
763
+ showNoData: { type: Boolean, default: true },
764
+ sortRemote: { type: Boolean, default: false },
765
+ showHeaderOverflow: { type: Boolean, default: false },
766
+ showOverflow: { type: Boolean, default: false },
767
+ showTrHoverClass: { type: Boolean, default: false },
768
+ headerDrag: { type: Boolean, default: false },
769
+ rowClassName: { type: Function, default: () => "" },
770
+ colResizable: { type: Boolean, default: false },
771
+ colMinWidth: { default: 10 },
772
+ bordered: { type: [Boolean, String], default: true },
773
+ autoResize: { type: [Boolean, Function], default: true }
774
+ },
775
+ emits: ["sort-change", "row-click", "current-change", "row-dblclick", "header-row-menu", "row-menu", "cell-click", "header-cell-click", "scroll", "scroll-x", "col-order-change", "th-drag-start", "th-drop", "update:columns"],
776
+ setup(__props, { expose: __expose, emit: __emit }) {
777
+ const props = __props;
778
+ const emits = __emit;
779
+ const tableContainer = ref();
780
+ const colResizeIndicator = ref();
781
+ const currentItem = ref(null);
782
+ const currentHover = ref(null);
783
+ let sortCol = ref();
784
+ let sortOrderIndex = ref(0);
785
+ const sortSwitchOrder = [null, "desc", "asc"];
786
+ const tableHeaders = ref([]);
787
+ const tableHeaderLast = ref([]);
788
+ const dataSourceCopy = shallowRef([...props.dataSource]);
789
+ const rowKeyGenStore = /* @__PURE__ */ new WeakMap();
790
+ const { isColResizing, onThResizeMouseDown } = useColResize({
791
+ props,
792
+ emits,
793
+ colKeyGen,
794
+ colResizeIndicator,
795
+ tableContainer,
796
+ tableHeaderLast
797
+ });
798
+ const { onThDragStart, onThDragOver, onThDrop } = useThDrag({ emits });
799
+ const {
800
+ virtualScroll,
801
+ virtualScrollX,
802
+ virtual_on,
803
+ virtual_dataSourcePart,
804
+ virtual_offsetBottom,
805
+ virtualX_on,
806
+ virtualX_columnPart,
807
+ virtualX_offsetRight,
808
+ initVirtualScroll,
809
+ initVirtualScrollY,
810
+ initVirtualScrollX,
811
+ updateVirtualScrollY,
812
+ updateVirtualScrollX
813
+ } = useVirtualScroll({ tableContainer, props, dataSourceCopy, tableHeaderLast });
814
+ const { getFixedStyle } = useFixedStyle({
815
+ props,
816
+ tableHeaderLast,
817
+ virtualScroll,
818
+ virtualScrollX,
819
+ virtualX_on,
820
+ virtualX_offsetRight
821
+ });
822
+ const { setHighlightDimCell, setHighlightDimRow } = useHighlight({ props, tableContainer, rowKeyGen });
823
+ if (props.autoResize) {
824
+ useAutoResize({ tableContainer, initVirtualScroll, scrollTo, props, debounceMs: 500 });
825
+ }
826
+ useKeyboardArrowScroll(tableContainer, {
827
+ scrollTo,
828
+ virtualScroll,
829
+ virtualScrollX
830
+ });
831
+ watch(
832
+ () => props.columns,
833
+ () => {
834
+ dealColumns();
835
+ initVirtualScrollX();
836
+ }
837
+ );
838
+ dealColumns();
839
+ watch(
840
+ () => props.dataSource,
841
+ (val) => {
842
+ if (!val) {
843
+ console.warn("invalid dataSource");
844
+ return;
845
+ }
846
+ let needInitVirtualScrollY = false;
847
+ if (dataSourceCopy.value.length !== val.length) {
848
+ needInitVirtualScrollY = true;
849
+ }
850
+ dataSourceCopy.value = [...val];
851
+ if (needInitVirtualScrollY)
852
+ initVirtualScrollY();
853
+ if (sortCol.value) {
854
+ const column = tableHeaderLast.value.find((it) => it.dataIndex === sortCol.value);
855
+ onColumnSort(column, false);
856
+ }
857
+ },
858
+ {
859
+ deep: false
860
+ }
861
+ );
862
+ onMounted(() => {
863
+ initVirtualScroll();
864
+ });
865
+ function dealColumns() {
866
+ tableHeaders.value = [];
867
+ tableHeaderLast.value = [];
868
+ const copyColumn = props.columns;
869
+ const deep = howDeepTheHeader(copyColumn);
870
+ const tempHeaderLast = [];
871
+ if (deep > 1 && props.virtualX) {
872
+ console.error("多级表头不支持横向虚拟滚动");
873
+ }
874
+ function flat(arr, depth = 0) {
875
+ if (!tableHeaders.value[depth]) {
876
+ tableHeaders.value[depth] = [];
877
+ }
878
+ let allChildrenLen = 0;
879
+ arr.forEach((col) => {
880
+ let colChildrenLen = 1;
881
+ if (col.children) {
882
+ colChildrenLen = flat(
883
+ col.children,
884
+ depth + 1
885
+ /* , col.fixed */
886
+ );
887
+ } else {
888
+ tempHeaderLast.push(col);
889
+ }
890
+ tableHeaders.value[depth].push(col);
891
+ const rowSpan = col.children ? 1 : deep - depth;
892
+ const colSpan = colChildrenLen;
893
+ if (rowSpan !== 1) {
894
+ col.rowSpan = rowSpan;
895
+ }
896
+ if (colSpan !== 1) {
897
+ col.colSpan = colSpan;
898
+ }
899
+ allChildrenLen += colChildrenLen;
900
+ });
901
+ return allChildrenLen;
902
+ }
903
+ flat(copyColumn);
904
+ tableHeaderLast.value = tempHeaderLast;
905
+ }
906
+ function rowKeyGen(row) {
907
+ let key = rowKeyGenStore.get(row);
908
+ if (!key) {
909
+ key = typeof props.rowKey === "function" ? props.rowKey(row) : row[props.rowKey];
910
+ rowKeyGenStore.set(row, key);
911
+ }
912
+ return key;
913
+ }
914
+ function colKeyGen(col) {
915
+ return typeof props.colKey === "function" ? props.colKey(col) : col[props.colKey];
916
+ }
917
+ function getColWidthStyle(col) {
918
+ const style = {
919
+ width: col.width,
920
+ minWidth: col.minWidth,
921
+ maxWidth: col.maxWidth
922
+ };
923
+ if (props.colResizable) {
924
+ style.minWidth = col.width;
925
+ style.maxWidth = col.width;
926
+ } else {
927
+ style.minWidth = col.minWidth === void 0 ? col.width : col.minWidth;
928
+ style.maxWidth = col.maxWidth === void 0 ? col.width : col.maxWidth;
929
+ }
930
+ return style;
931
+ }
932
+ function getCellStyle(tagType, col, depth) {
933
+ const style = {
934
+ ...getColWidthStyle(col),
935
+ ...getFixedStyle(tagType, col, depth)
936
+ };
937
+ if (tagType === 1) {
938
+ style.textAlign = col.headerAlign;
939
+ } else if (tagType === 2) {
940
+ style.textAlign = col.align;
941
+ }
942
+ return style;
943
+ }
944
+ function onColumnSort(col, click = true, options = {}) {
945
+ if (!(col == null ? void 0 : col.sorter))
946
+ return;
947
+ options = { force: false, emit: false, ...options };
948
+ if (sortCol.value !== col.dataIndex) {
949
+ sortCol.value = col.dataIndex;
950
+ sortOrderIndex.value = 0;
951
+ }
952
+ if (click)
953
+ sortOrderIndex.value++;
954
+ sortOrderIndex.value = sortOrderIndex.value % 3;
955
+ const order = sortSwitchOrder[sortOrderIndex.value];
956
+ if (!props.sortRemote || options.force) {
957
+ dataSourceCopy.value = tableSort(col, order, props.dataSource);
958
+ }
959
+ if (click || options.emit) {
960
+ emits("sort-change", col, order, toRaw(dataSourceCopy.value));
961
+ }
962
+ }
963
+ function onRowClick(e, row) {
964
+ emits("row-click", e, row);
965
+ if (currentItem.value === row)
966
+ return;
967
+ currentItem.value = row;
968
+ emits("current-change", e, row);
969
+ }
970
+ function onRowDblclick(e, row) {
971
+ emits("row-dblclick", e, row);
972
+ }
973
+ function onHeaderMenu(e) {
974
+ emits("header-row-menu", e);
975
+ }
976
+ function onRowMenu(e, row) {
977
+ emits("row-menu", e, row);
978
+ }
979
+ function onCellClick(e, row, col) {
980
+ emits("cell-click", e, row, col);
981
+ }
982
+ function onHeaderCellClick(e, col) {
983
+ emits("header-cell-click", e, col);
984
+ }
985
+ function onTableWheel(e) {
986
+ if (isColResizing.value) {
987
+ e.preventDefault();
988
+ e.stopPropagation();
989
+ return;
990
+ }
991
+ }
992
+ function onTableScroll(e) {
993
+ if (!(e == null ? void 0 : e.target))
994
+ return;
995
+ const { scrollTop, scrollLeft } = e.target;
996
+ const isYScroll = scrollTop !== virtualScroll.value.scrollTop;
997
+ const isXScroll = scrollLeft !== virtualScrollX.value.scrollLeft;
998
+ if (isYScroll) {
999
+ virtualScroll.value.scrollTop = scrollTop;
1000
+ }
1001
+ if (virtual_on.value) {
1002
+ updateVirtualScrollY(scrollTop);
1003
+ }
1004
+ if (isXScroll) {
1005
+ virtualScrollX.value.scrollLeft = scrollLeft;
1006
+ }
1007
+ if (virtualX_on.value) {
1008
+ updateVirtualScrollX(scrollLeft);
1009
+ }
1010
+ const data = {
1011
+ startIndex: virtualScroll.value.startIndex,
1012
+ endIndex: virtualScroll.value.endIndex
1013
+ };
1014
+ if (isYScroll) {
1015
+ emits("scroll", e, data);
1016
+ }
1017
+ if (isXScroll) {
1018
+ emits("scroll-x", e);
1019
+ }
1020
+ }
1021
+ function onTrMouseOver(_e, row) {
1022
+ if (props.showTrHoverClass) {
1023
+ currentHover.value = rowKeyGen(row);
1024
+ }
1025
+ }
1026
+ function setCurrentRow(rowKey, option = { silent: false }) {
1027
+ if (!dataSourceCopy.value.length)
1028
+ return;
1029
+ currentItem.value = dataSourceCopy.value.find((it) => rowKeyGen(it) === rowKey);
1030
+ if (!option.silent) {
1031
+ emits("current-change", null, currentItem.value);
1032
+ }
1033
+ }
1034
+ function setSorter(dataIndex, order, option = {}) {
1035
+ var _a;
1036
+ const newOption = { silent: true, sortOption: null, sort: true, ...option };
1037
+ sortCol.value = dataIndex;
1038
+ sortOrderIndex.value = sortSwitchOrder.findIndex((it) => it === order);
1039
+ if (newOption.sort && ((_a = dataSourceCopy.value) == null ? void 0 : _a.length)) {
1040
+ const column = newOption.sortOption || tableHeaderLast.value.find((it) => it.dataIndex === sortCol.value);
1041
+ if (column)
1042
+ onColumnSort(column, false, { force: true, emit: !newOption.silent });
1043
+ else
1044
+ console.warn("Can not find column by dataIndex:", sortCol.value);
1045
+ }
1046
+ return dataSourceCopy.value;
1047
+ }
1048
+ function resetSorter() {
1049
+ sortCol.value = null;
1050
+ sortOrderIndex.value = 0;
1051
+ dataSourceCopy.value = [...props.dataSource];
1052
+ }
1053
+ function scrollTo(top = 0, left = 0) {
1054
+ if (!tableContainer.value)
1055
+ return;
1056
+ if (top !== null)
1057
+ tableContainer.value.scrollTop = top;
1058
+ if (left !== null)
1059
+ tableContainer.value.scrollLeft = left;
1060
+ }
1061
+ function getTableData() {
1062
+ return toRaw(dataSourceCopy.value);
1063
+ }
1064
+ __expose({
1065
+ initVirtualScroll,
1066
+ initVirtualScrollX,
1067
+ initVirtualScrollY,
1068
+ setCurrentRow,
1069
+ setHighlightDimCell,
1070
+ setHighlightDimRow,
1071
+ sortCol,
1072
+ setSorter,
1073
+ resetSorter,
1074
+ scrollTo,
1075
+ getTableData
1076
+ });
1077
+ return (_ctx, _cache) => {
1078
+ return openBlock(), createElementBlock("div", {
1079
+ ref_key: "tableContainer",
1080
+ ref: tableContainer,
1081
+ class: normalizeClass(["stk-table", {
1082
+ virtual: _ctx.virtual,
1083
+ "virtual-x": _ctx.virtualX,
1084
+ dark: _ctx.theme === "dark",
1085
+ headless: _ctx.headless,
1086
+ "is-col-resizing": unref(isColResizing),
1087
+ "col-resizable": props.colResizable,
1088
+ border: props.bordered,
1089
+ "border-h": props.bordered === "h",
1090
+ "border-v": props.bordered === "v",
1091
+ "border-body-v": props.bordered === "body-v",
1092
+ stripe: props.stripe
1093
+ }]),
1094
+ style: normalizeStyle(_ctx.virtual && { "--row-height": unref(virtualScroll).rowHeight + "px" }),
1095
+ onScroll: onTableScroll,
1096
+ onWheel: onTableWheel
1097
+ }, [
1098
+ withDirectives(createElementVNode("div", {
1099
+ ref_key: "colResizeIndicator",
1100
+ ref: colResizeIndicator,
1101
+ class: "column-resize-indicator"
1102
+ }, null, 512), [
1103
+ [vShow, _ctx.colResizable]
1104
+ ]),
1105
+ createElementVNode("table", {
1106
+ class: normalizeClass(["stk-table-main", {
1107
+ "fixed-mode": props.fixedMode
1108
+ }]),
1109
+ style: normalizeStyle({ width: _ctx.width, minWidth: _ctx.minWidth, maxWidth: _ctx.maxWidth })
1110
+ }, [
1111
+ !_ctx.headless ? (openBlock(), createElementBlock("thead", _hoisted_1, [
1112
+ (openBlock(true), createElementBlock(Fragment, null, renderList(tableHeaders.value, (row, rowIndex) => {
1113
+ return openBlock(), createElementBlock("tr", {
1114
+ key: rowIndex,
1115
+ onContextmenu: _cache[3] || (_cache[3] = (e) => onHeaderMenu(e))
1116
+ }, [
1117
+ unref(virtualX_on) ? (openBlock(), createElementBlock("th", {
1118
+ key: 0,
1119
+ class: "virtual-x-left",
1120
+ style: normalizeStyle({
1121
+ minWidth: unref(virtualScrollX).offsetLeft + "px",
1122
+ width: unref(virtualScrollX).offsetLeft + "px"
1123
+ })
1124
+ }, null, 4)) : createCommentVNode("", true),
1125
+ (openBlock(true), createElementBlock(Fragment, null, renderList(unref(virtualX_on) && rowIndex === tableHeaders.value.length - 1 ? unref(virtualX_columnPart) : row, (col, colIndex) => {
1126
+ return openBlock(), createElementBlock("th", {
1127
+ key: col.dataIndex,
1128
+ "data-col-key": colKeyGen(col),
1129
+ draggable: _ctx.headerDrag ? "true" : "false",
1130
+ rowspan: unref(virtualX_on) ? 1 : col.rowSpan,
1131
+ colspan: col.colSpan,
1132
+ style: normalizeStyle(getCellStyle(1, col, rowIndex)),
1133
+ title: col.title,
1134
+ class: normalizeClass([
1135
+ col.sorter ? "sortable" : "",
1136
+ col.dataIndex === unref(sortCol) && unref(sortOrderIndex) !== 0 && "sorter-" + sortSwitchOrder[unref(sortOrderIndex)],
1137
+ _ctx.showHeaderOverflow ? "text-overflow" : "",
1138
+ col.headerClassName,
1139
+ col.fixed ? "fixed-cell" : "",
1140
+ col.fixed ? "fixed-cell--" + col.fixed : ""
1141
+ ]),
1142
+ onClick: (e) => {
1143
+ onColumnSort(col);
1144
+ onHeaderCellClick(e, col);
1145
+ },
1146
+ onDragstart: _cache[0] || (_cache[0] = //@ts-ignore
1147
+ (...args) => unref(onThDragStart) && unref(onThDragStart)(...args)),
1148
+ onDrop: _cache[1] || (_cache[1] = //@ts-ignore
1149
+ (...args) => unref(onThDrop) && unref(onThDrop)(...args)),
1150
+ onDragover: _cache[2] || (_cache[2] = //@ts-ignore
1151
+ (...args) => unref(onThDragOver) && unref(onThDragOver)(...args))
1152
+ }, [
1153
+ createElementVNode("div", _hoisted_3, [
1154
+ col.customHeaderCell ? (openBlock(), createBlock(resolveDynamicComponent(col.customHeaderCell), {
1155
+ key: 0,
1156
+ col
1157
+ }, null, 8, ["col"])) : renderSlot(_ctx.$slots, "tableHeader", {
1158
+ key: 1,
1159
+ column: col
1160
+ }, () => [
1161
+ createElementVNode("span", _hoisted_4, toDisplayString(col.title), 1)
1162
+ ]),
1163
+ col.sorter ? (openBlock(), createElementBlock("span", _hoisted_5, _hoisted_7)) : createCommentVNode("", true),
1164
+ _ctx.colResizable && colIndex > 0 ? (openBlock(), createElementBlock("div", {
1165
+ key: 3,
1166
+ class: "table-header-resizer left",
1167
+ onMousedown: (e) => unref(onThResizeMouseDown)(e, col, true)
1168
+ }, null, 40, _hoisted_8)) : createCommentVNode("", true),
1169
+ _ctx.colResizable ? (openBlock(), createElementBlock("div", {
1170
+ key: 4,
1171
+ class: "table-header-resizer right",
1172
+ onMousedown: (e) => unref(onThResizeMouseDown)(e, col)
1173
+ }, null, 40, _hoisted_9)) : createCommentVNode("", true)
1174
+ ])
1175
+ ], 46, _hoisted_2);
1176
+ }), 128)),
1177
+ unref(virtualX_on) ? (openBlock(), createElementBlock("th", {
1178
+ key: 1,
1179
+ class: "virtual-x-right",
1180
+ style: normalizeStyle({
1181
+ minWidth: unref(virtualX_offsetRight) + "px",
1182
+ width: unref(virtualX_offsetRight) + "px"
1183
+ })
1184
+ }, null, 4)) : createCommentVNode("", true)
1185
+ ], 32);
1186
+ }), 128))
1187
+ ])) : createCommentVNode("", true),
1188
+ createElementVNode("tbody", null, [
1189
+ unref(virtual_on) ? (openBlock(), createElementBlock("tr", {
1190
+ key: 0,
1191
+ style: normalizeStyle({ height: `${unref(virtualScroll).offsetTop}px` }),
1192
+ class: "padding-top-tr"
1193
+ }, [
1194
+ unref(virtualX_on) && _ctx.fixedMode && _ctx.headless ? (openBlock(), createElementBlock("td", _hoisted_10)) : createCommentVNode("", true),
1195
+ _ctx.fixedMode && _ctx.headless ? (openBlock(true), createElementBlock(Fragment, { key: 1 }, renderList(unref(virtualX_columnPart), (col) => {
1196
+ return openBlock(), createElementBlock("td", {
1197
+ key: col.dataIndex,
1198
+ style: normalizeStyle(getCellStyle(2, col))
1199
+ }, null, 4);
1200
+ }), 128)) : createCommentVNode("", true)
1201
+ ], 4)) : createCommentVNode("", true),
1202
+ (openBlock(true), createElementBlock(Fragment, null, renderList(unref(virtual_dataSourcePart), (row, i) => {
1203
+ return openBlock(), createElementBlock("tr", {
1204
+ key: _ctx.rowKey ? rowKeyGen(row) : i,
1205
+ "data-row-key": _ctx.rowKey ? rowKeyGen(row) : i,
1206
+ class: normalizeClass({
1207
+ active: _ctx.rowKey ? rowKeyGen(row) === (currentItem.value && rowKeyGen(currentItem.value)) : row === currentItem.value,
1208
+ hover: _ctx.rowKey ? rowKeyGen(row) === currentHover.value : row === currentHover.value,
1209
+ [_ctx.rowClassName(row, i)]: true
1210
+ }),
1211
+ style: normalizeStyle({
1212
+ backgroundColor: row._bgc
1213
+ }),
1214
+ onClick: (e) => onRowClick(e, row),
1215
+ onDblclick: (e) => onRowDblclick(e, row),
1216
+ onContextmenu: (e) => onRowMenu(e, row),
1217
+ onMouseover: (e) => onTrMouseOver(e, row)
1218
+ }, [
1219
+ unref(virtualX_on) ? (openBlock(), createElementBlock("td", _hoisted_12)) : createCommentVNode("", true),
1220
+ (openBlock(true), createElementBlock(Fragment, null, renderList(unref(virtualX_columnPart), (col) => {
1221
+ return openBlock(), createElementBlock("td", {
1222
+ key: col.dataIndex,
1223
+ "data-index": col.dataIndex,
1224
+ class: normalizeClass([
1225
+ col.className,
1226
+ _ctx.showOverflow ? "text-overflow" : "",
1227
+ col.fixed ? "fixed-cell" : "",
1228
+ col.fixed ? "fixed-cell--" + col.fixed : ""
1229
+ ]),
1230
+ style: normalizeStyle(getCellStyle(2, col)),
1231
+ onClick: (e) => onCellClick(e, row, col)
1232
+ }, [
1233
+ col.customCell ? (openBlock(), createBlock(resolveDynamicComponent(col.customCell), {
1234
+ key: 0,
1235
+ col,
1236
+ row,
1237
+ "cell-value": row[col.dataIndex]
1238
+ }, null, 8, ["col", "row", "cell-value"])) : (openBlock(), createElementBlock("div", {
1239
+ key: 1,
1240
+ class: "table-cell-wrapper",
1241
+ title: row[col.dataIndex]
1242
+ }, toDisplayString(row[col.dataIndex] ?? _ctx.emptyCellText), 9, _hoisted_14))
1243
+ ], 14, _hoisted_13);
1244
+ }), 128))
1245
+ ], 46, _hoisted_11);
1246
+ }), 128)),
1247
+ unref(virtual_on) ? (openBlock(), createElementBlock("tr", {
1248
+ key: 1,
1249
+ style: normalizeStyle({ height: `${unref(virtual_offsetBottom)}px` })
1250
+ }, null, 4)) : createCommentVNode("", true)
1251
+ ])
1252
+ ], 6),
1253
+ (!dataSourceCopy.value || !dataSourceCopy.value.length) && _ctx.showNoData ? (openBlock(), createElementBlock("div", {
1254
+ key: 0,
1255
+ class: normalizeClass(["stk-table-no-data", { "no-data-full": _ctx.noDataFull }])
1256
+ }, [
1257
+ renderSlot(_ctx.$slots, "empty", {}, () => [
1258
+ createTextVNode("暂无数据")
1259
+ ])
1260
+ ], 2)) : createCommentVNode("", true)
1261
+ ], 38);
1262
+ };
1263
+ }
1264
+ });
1265
+ export {
1266
+ _sfc_main as StkTable,
1267
+ insertToOrderedArray,
1268
+ tableSort
1269
+ };