Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Much better algorithm for swipe with colspan
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Mar 19, 2019
1 parent e18a904 commit 402b536
Showing 1 changed file with 35 additions and 50 deletions.
85 changes: 35 additions & 50 deletions src/tables.swipetoggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@
}

function maintainWidths() {
var prefix = "#" + tableId + ".tablesaw-swipe ",
styles = [],
tableWidth = $table.width(),
hash = [],
newHash;
var prefix = "#" + tableId + ".tablesaw-swipe ";
var styles = [];
var tableWidth = $table.width();
var tableWidthNoPersistantColumns = tableWidth;
var hash = [];
var newHash;

// save persistent column widths (as long as they take up less than 75% of table width)
$headerCells.each(function(index) {
var width;
if (isPersistent(this)) {
width = this.offsetWidth;
tableWidthNoPersistantColumns -= width;

if (width < tableWidth * 0.75) {
hash.push(index + "-" + width);
Expand Down Expand Up @@ -148,6 +150,8 @@
.appendTo($head);
}
}

return tableWidthNoPersistantColumns;
}

function getNext() {
Expand All @@ -156,7 +160,6 @@
$headerCellsNoPersist.each(function(i) {
var $t = $(this);
var isHidden = $t.css("display") === "none" || $t.is("." + classes.hiddenCol);
var colspan = parseInt($t.attr("colspan") || 1, 10);

if (!isHidden && !checkFound) {
checkFound = true;
Expand Down Expand Up @@ -268,59 +271,41 @@
}
}

maintainWidths();

var showColumnIndex = pair[1];
var roomForColumnsWidth = maintainWidths();
var hideColumnIndex = pair[0];
var colspanCounter = parseInt(
$headerCellsNoPersist.eq(showColumnIndex).attr("colspan") || 1,
10
);
var columnToHide;

while (
colspanCounter > 0 &&
hideColumnIndex >= 0 &&
hideColumnIndex <= headerWidthsNoPersist.length &&
((isNavigateForward && hideColumnIndex < showColumnIndex) ||
(!isNavigateForward && hideColumnIndex > showColumnIndex))
) {
columnToHide = $headerCellsNoPersist.get(hideColumnIndex);

if (columnToHide) {
hideColumn(columnToHide);
tblsaw.updateColspanCells(classes.hiddenCol, columnToHide, false);
colspanCounter -= parseInt(
$headerCellsNoPersist.eq(hideColumnIndex).attr("colspan") || 1,
10
);
}

if (isNavigateForward) {
hideColumnIndex++;
} else {
hideColumnIndex--;
}
}
// Hide one column, show one or more
var columnToShow;
var columnToHide = $headerCellsNoPersist.get(hideColumnIndex);
var wasAtLeastOneColumnShown = false;

while (colspanCounter <= 0) {
var columnToShow = $headerCellsNoPersist.get(showColumnIndex);
showColumn(columnToShow);
tblsaw.updateColspanCells(classes.hiddenCol, columnToShow, true);
hideColumn(columnToHide);
tblsaw.updateColspanCells(classes.hiddenCol, columnToHide, true);

var columnIndex = hideColumnIndex;
while (columnIndex >= 0 && columnIndex <= headerWidthsNoPersist.length) {
if (isNavigateForward) {
showColumnIndex++;
columnIndex++;
} else {
showColumnIndex--;
columnIndex--;
}
roomForColumnsWidth -= headerWidthsNoPersist[columnIndex];
if (
roomForColumnsWidth > 0 &&
$headerCellsNoPersist.eq(columnIndex).is(".tablesaw-swipe-cellhidden")
) {
wasAtLeastOneColumnShown = true;
columnToShow = $headerCellsNoPersist.get(columnIndex);
showColumn(columnToShow);
tblsaw.updateColspanCells(classes.hiddenCol, columnToShow, false);
}

colspanCounter += parseInt(
$headerCellsNoPersist.eq(showColumnIndex).attr("colspan") || 1,
10
);
}

$table.trigger("tablesawcolumns");
if (!wasAtLeastOneColumnShown) {
navigate(isNavigateForward);
} else {
$table.trigger("tablesawcolumns");
}
}
}

Expand Down

0 comments on commit 402b536

Please sign in to comment.