Skip to content

Commit a844775

Browse files
committed
Refactor column resizing logic in lightPivotTable.js by introducing a resize handle for improved user interaction and updating class name handling for better styling consistency.
1 parent 252a1aa commit a844775

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/app/lib/lightPivotTable.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3748,7 +3748,7 @@ PivotView.prototype.renderRawData = function (data) {
37483748

37493749
CLICK_EVENT = this.controller.CONFIG["triggerEvent"] || "click",
37503750
ATTACH_TOTALS = info.SUMMARY_SHOWN && this.controller.CONFIG["attachTotals"] ? 1 : 0,
3751-
COLUMN_RESIZE_ON = !!this.controller.CONFIG.columnResizing,
3751+
COLUMN_RESIZE_ON = !!this.controller.CONFIG["columnResizing"],
37523752
LISTING = info.leftHeaderColumnsNumber === 0,
37533753
SEARCH_ENABLED = LISTING && this.controller.CONFIG["enableSearch"],
37543754
LISTING_SELECT_ENABLED = this.controller.CONFIG["enableListingSelect"],
@@ -3848,10 +3848,7 @@ PivotView.prototype.renderRawData = function (data) {
38483848
};
38493849

38503850
var bindResize = function (element, column) {
3851-
3852-
var baseWidth = 0,
3853-
baseX = 0;
3854-
3851+
var baseWidth = 0, baseX = 0;
38553852
var moveListener = function (e) {
38563853
e.cancelBubble = true;
38573854
e.preventDefault();
@@ -3863,7 +3860,6 @@ PivotView.prototype.renderRawData = function (data) {
38633860
_.restoreScrollPosition();
38643861
}
38653862
};
3866-
38673863
var upListener = function (e) {
38683864
e.cancelBubble = true;
38693865
e.preventDefault();
@@ -3875,7 +3871,6 @@ PivotView.prototype.renderRawData = function (data) {
38753871
document.removeEventListener("mousemove", moveListener);
38763872
document.removeEventListener("mouseup", upListener);
38773873
};
3878-
38793874
element.addEventListener("mousedown", function (e) {
38803875
if ((e.target || e.srcElement) !== element) return;
38813876
e.cancelBubble = true;
@@ -3885,7 +3880,41 @@ PivotView.prototype.renderRawData = function (data) {
38853880
document.addEventListener("mousemove", moveListener);
38863881
document.addEventListener("mouseup", upListener);
38873882
});
3883+
};
38883884

3885+
// Resize via handle on RIGHT edge: dragging handle resizes the column to its left
3886+
var bindResizeWithHandle = function (handleElement, thElement, column) {
3887+
var baseWidth = 0, baseX = 0;
3888+
var moveListener = function (e) {
3889+
e.cancelBubble = true;
3890+
e.preventDefault();
3891+
thElement.style.width = thElement.style.minWidth =
3892+
baseWidth - baseX + e.pageX + "px";
3893+
if (RESIZE_ANIMATION) {
3894+
_.saveScrollPosition();
3895+
_.recalculateSizes(container);
3896+
_.restoreScrollPosition();
3897+
}
3898+
};
3899+
var upListener = function (e) {
3900+
e.cancelBubble = true;
3901+
e.preventDefault();
3902+
thElement.style.width = thElement.style.minWidth =
3903+
(_.FIXED_COLUMN_SIZES[column] = baseWidth - baseX + e.pageX) + "px";
3904+
_.saveScrollPosition();
3905+
_.recalculateSizes(container);
3906+
_.restoreScrollPosition();
3907+
document.removeEventListener("mousemove", moveListener);
3908+
document.removeEventListener("mouseup", upListener);
3909+
};
3910+
handleElement.addEventListener("mousedown", function (e) {
3911+
e.cancelBubble = true;
3912+
e.preventDefault();
3913+
baseWidth = thElement.offsetWidth;
3914+
baseX = e.pageX;
3915+
document.addEventListener("mousemove", moveListener);
3916+
document.addEventListener("mouseup", upListener);
3917+
});
38893918
};
38903919

38913920
var bindResizeLeftColumn = function (handleElement, thElement, columnIndex, allColumnThs) {
@@ -3901,11 +3930,9 @@ PivotView.prototype.renderRawData = function (data) {
39013930
e.cancelBubble = true;
39023931
e.preventDefault();
39033932
setColumnWidth(baseWidth - baseX + e.pageX);
3904-
if (RESIZE_ANIMATION) {
3905-
_.saveScrollPosition();
3906-
_.recalculateSizes(container);
3907-
_.restoreScrollPosition();
3908-
}
3933+
_.saveScrollPosition();
3934+
_.recalculateSizes(container);
3935+
_.restoreScrollPosition();
39093936
};
39103937
var upListener = function (e) {
39113938
e.cancelBubble = true;
@@ -4024,7 +4051,6 @@ PivotView.prototype.renderRawData = function (data) {
40244051
th.style.whiteSpace = "normal";
40254052
th.style.wordWrap = "normal";
40264053
}
4027-
if (rawData[y][x].className) th.className = rawData[y][x].className;
40284054
if (rawData[y][x].group) renderedGroups[rawData[y][x].group] = {
40294055
x: x,
40304056
y: y,
@@ -4036,6 +4062,7 @@ PivotView.prototype.renderRawData = function (data) {
40364062
rowProps[y].format || columnProps[x].format
40374063
);
40384064
}
4065+
if (rawData[y][x].className) th.className = ((th.className || "").replace(/\blpt-sort(?:Asc|Desc)\b/g, "").trim() + " " + rawData[y][x].className).trim();
40394066

40404067
// add listeners
40414068
if (vertical && x === xTo - 1) {
@@ -4092,7 +4119,12 @@ PivotView.prototype.renderRawData = function (data) {
40924119
// Do not bind resize to the first data column (next to left header) —
40934120
// this boundary is resized only via the left header handle
40944121
if (COLUMN_RESIZE_ON && (info.leftHeaderColumnsNumber === 0 || x !== info.leftHeaderColumnsNumber)) {
4095-
bindResize(th, x);
4122+
th.style.position = "relative";
4123+
var colResizeHandle = document.createElement("div");
4124+
colResizeHandle.className = "lpt-resizeHandle lpt-resizableColumn";
4125+
colResizeHandle.style.cssText = "position:absolute;right:0;top:0;bottom:0;width:6px;cursor:col-resize;z-index:1;";
4126+
th.appendChild(colResizeHandle);
4127+
bindResizeWithHandle(colResizeHandle, th, x);
40964128
th.className += " lpt-resizableColumn";
40974129
}
40984130
primaryColumns.push(th);

0 commit comments

Comments
 (0)