entity list reordering columns

This commit is contained in:
unknown 2018-11-16 17:14:30 -08:00
parent e9fb9b1f4d
commit a0f79f9881
2 changed files with 44 additions and 28 deletions

View file

@ -110,8 +110,8 @@ const COLUMNS = {
}; };
const COMPARE_ASCENDING = function(a, b) { const COMPARE_ASCENDING = function(a, b) {
let va = a[currentSortColumn]; let va = a[currentSortColumnID];
let vb = b[currentSortColumn]; let vb = b[currentSortColumnID];
if (va < vb) { if (va < vb) {
return -1; return -1;
@ -171,7 +171,7 @@ let entityList = null; // The ListView
*/ */
let entityListContextMenu = null; let entityListContextMenu = null;
let currentSortColumn = 'type'; let currentSortColumnID = 'type';
let currentSortOrder = ASCENDING_SORT; let currentSortOrder = ASCENDING_SORT;
let elSortOrders = {}; let elSortOrders = {};
let typeFilters = []; let typeFilters = [];
@ -182,6 +182,7 @@ let columnsByID = {};
let lastResizeEvent = null; let lastResizeEvent = null;
let resizeColumnIndex = 0; let resizeColumnIndex = 0;
let elTargetTh = null; let elTargetTh = null;
let elTargetSpan = null;
let targetColumnIndex = 0; let targetColumnIndex = 0;
let lastColumnSwapPosition = -1; let lastColumnSwapPosition = -1;
let initialThEvent = null; let initialThEvent = null;
@ -226,10 +227,6 @@ const PROFILE = !ENABLE_PROFILING ? PROFILE_NOOP : function(name, fn, args) {
console.log("PROFILE-Web " + profileIndent + "(" + name + ") End " + delta + "ms"); console.log("PROFILE-Web " + profileIndent + "(" + name + ") End " + delta + "ms");
}; };
debugPrint = function (message) {
console.log(message);
};
function loaded() { function loaded() {
openEventBridge(function() { openEventBridge(function() {
elEntityTable = document.getElementById("entity-table"); elEntityTable = document.getElementById("entity-table");
@ -320,10 +317,11 @@ function loaded() {
for (let columnID in COLUMNS) { for (let columnID in COLUMNS) {
let columnData = COLUMNS[columnID]; let columnData = COLUMNS[columnID];
let thID = "entity-" + columnID;
let elTh = document.createElement("th"); let elTh = document.createElement("th");
let thID = "entity-" + columnID;
elTh.setAttribute("id", thID); elTh.setAttribute("id", thID);
elTh.setAttribute("columnIndex", columnIndex); elTh.setAttribute("columnIndex", columnIndex);
elTh.setAttribute("columnID", columnID);
if (columnData.glyph) { if (columnData.glyph) {
let elGlyph = document.createElement("span"); let elGlyph = document.createElement("span");
elGlyph.className = "glyph"; elGlyph.className = "glyph";
@ -333,15 +331,19 @@ function loaded() {
elTh.innerText = columnData.columnHeader; elTh.innerText = columnData.columnHeader;
} }
elTh.onmousedown = function(event) { elTh.onmousedown = function(event) {
elTargetTh = event.target; if (event.target.nodeName === 'TH') {
elTargetTh = event.target;
targetColumnIndex = parseInt(elTargetTh.getAttribute("columnIndex"));
lastColumnSwapPosition = event.clientX;
} else if (event.target.nodeName === 'SPAN') {
elTargetSpan = event.target;
}
initialThEvent = event; initialThEvent = event;
targetColumnIndex = parseInt(elTargetTh.getAttribute("columnIndex"));
}; };
let elResizer = document.createElement("span"); let elResizer = document.createElement("span");
elResizer.className = "resizer"; elResizer.className = "resizer";
elResizer.innerHTML = "&nbsp;"; elResizer.innerHTML = "&nbsp;";
elResizer.setAttribute("columnIndex", columnIndex);
elResizer.onmousedown = onStartResize; elResizer.onmousedown = onStartResize;
elTh.appendChild(elResizer); elTh.appendChild(elResizer);
@ -699,13 +701,13 @@ function loaded() {
refreshNoEntitiesMessage(); refreshNoEntitiesMessage();
} }
function setSortColumn(column) { function setSortColumn(columnID) {
PROFILE("set-sort-column", function() { PROFILE("set-sort-column", function() {
if (currentSortColumn === column) { if (currentSortColumnID === columnID) {
currentSortOrder *= -1; currentSortOrder *= -1;
} else { } else {
elSortOrders[currentSortColumn].innerHTML = ""; elSortOrders[currentSortColumnID].innerHTML = "";
currentSortColumn = column; currentSortColumnID = columnID;
currentSortOrder = ASCENDING_SORT; currentSortOrder = ASCENDING_SORT;
} }
refreshSortOrder(); refreshSortOrder();
@ -714,7 +716,7 @@ function loaded() {
} }
function refreshSortOrder() { function refreshSortOrder() {
elSortOrders[currentSortColumn].innerHTML = currentSortOrder === ASCENDING_SORT ? ASCENDING_STRING : DESCENDING_STRING; elSortOrders[currentSortColumnID].innerHTML = currentSortOrder === ASCENDING_SORT ? ASCENDING_STRING : DESCENDING_STRING;
} }
function refreshEntities() { function refreshEntities() {
@ -1031,7 +1033,7 @@ function loaded() {
function onStartResize(event) { function onStartResize(event) {
lastResizeEvent = event; lastResizeEvent = event;
resizeColumnIndex = parseInt(this.getAttribute("columnIndex")); resizeColumnIndex = parseInt(this.parentNode.getAttribute("columnIndex"));
event.stopPropagation(); event.stopPropagation();
} }
@ -1089,14 +1091,18 @@ function loaded() {
for (let i = 0; i < visibleEntities.length; ++i) { for (let i = 0; i < visibleEntities.length; ++i) {
let elRow = visibleEntities[i].elRow; let elRow = visibleEntities[i].elRow;
let columnACell = elRow.childNodes[columnAIndex]; if (elRow) {
let columnBCell = elRow.childNodes[columnBIndex]; let columnACell = elRow.childNodes[columnAIndex];
elRow.removeChild(columnBCell); let columnBCell = elRow.childNodes[columnBIndex];
elRow.insertBefore(columnBCell, columnACell); elRow.removeChild(columnBCell);
elRow.insertBefore(columnBCell, columnACell);
}
} }
columns[columnAIndex] = columnB; columns[columnAIndex] = columnB;
columns[columnBIndex] = columnA; columns[columnBIndex] = columnA;
updateColumnWidths();
} }
document.onmousemove = function(event) { document.onmousemove = function(event) {
@ -1143,24 +1149,38 @@ function loaded() {
let prevColumnIndex = targetColumnIndex - 1; let prevColumnIndex = targetColumnIndex - 1;
let prevColumnTh = columns[prevColumnIndex].elTh; let prevColumnTh = columns[prevColumnIndex].elTh;
let prevColumnEndX = prevColumnTh.getBoundingClientRect().right; let prevColumnEndX = prevColumnTh.getBoundingClientRect().right;
if (event.clientX <= prevColumnEndX && event.clientX - lastColumnSwapPosition >= DELTA_X_COLUMN_SWAP_POSITION) { if (event.clientX <= prevColumnEndX && lastColumnSwapPosition - event.clientX >= DELTA_X_COLUMN_SWAP_POSITION) {
swapColumns(prevColumnIndex, targetColumnIndex); swapColumns(prevColumnIndex, targetColumnIndex);
targetColumnIndex = prevColumnIndex; targetColumnIndex = prevColumnIndex;
lastColumnSwapPosition = event.clientX;
} }
} }
} else if (elTargetSpan) {
let dxFromInitial = event.clientX - initialThEvent.clientX;
if (Math.abs(dxFromInitial) >= DELTA_X_MOVE_COLUMNS_THRESHOLD) {
elTargetTh = elTargetSpan.parentNode;
elTargetTh.className = "dragging";
targetColumnIndex = parseInt(elTargetTh.getAttribute("columnIndex"));
lastColumnSwapPosition = event.clientX;
elTargetSpan = null;
}
} }
} }
document.onmouseup = function(event) { document.onmouseup = function(event) {
if (elTargetTh) { if (elTargetTh) {
if (elTargetTh.className !== "dragging" && elTargetTh === event.target) { if (elTargetTh.className !== "dragging" && elTargetTh === event.target) {
let columnID = columns[targetColumnIndex].columnID; let columnID = elTargetTh.getAttribute("columnID");
setSortColumn(columnID); setSortColumn(columnID);
} }
elTargetTh.className = ""; elTargetTh.className = "";
} else if (elTargetSpan) {
let columnID = elTargetSpan.parentNode.getAttribute("columnID");
setSortColumn(columnID);
} }
lastResizeEvent = null; lastResizeEvent = null;
elTargetTh = null; elTargetTh = null;
elTargetSpan = null;
initialThEvent = null; initialThEvent = null;
} }

View file

@ -9,10 +9,6 @@
const SCROLL_ROWS = 2; // number of rows used as scrolling buffer, each time we pass this number of rows we scroll const SCROLL_ROWS = 2; // number of rows used as scrolling buffer, each time we pass this number of rows we scroll
const FIRST_ROW_INDEX = 2; // the first elRow element's index in the child nodes of the table body const FIRST_ROW_INDEX = 2; // the first elRow element's index in the child nodes of the table body
debugPrint = function (message) {
console.log(message);
};
function ListView(elTableBody, elTableScroll, elTableHeaderRow, createRowFunction, function ListView(elTableBody, elTableScroll, elTableHeaderRow, createRowFunction,
updateRowFunction, clearRowFunction, WINDOW_NONVARIABLE_HEIGHT) { updateRowFunction, clearRowFunction, WINDOW_NONVARIABLE_HEIGHT) {
this.elTableBody = elTableBody; this.elTableBody = elTableBody;