[Tables] Maintain correct insertion order with duplicates. Fixes #1465

This commit is contained in:
Henry
2017-03-01 22:02:32 -08:00
parent 6fa5a31217
commit 06f4a955b5
5 changed files with 128 additions and 38 deletions

View File

@@ -425,6 +425,38 @@ define(
}
};
/**
* Finds the correct insertion point for a new row, which takes into
* account duplicates to make sure new rows are inserted in a way that
* maintains arrival order.
*
* @private
* @param {Array} searchArray
* @param {Object} searchElement Object to find the insertion point for
*/
MCTTableController.prototype.findInsertionPoint = function (searchArray, searchElement) {
//First, use a binary search to find the correct insertion point
var index = this.binarySearch(searchArray, searchElement, 0, searchArray.length - 1);
var testIndex = index;
//It's possible that the insertion point is a duplicate of the element to be inserted
var isDupe = function () {
return this.sortComparator(searchElement,
searchArray[testIndex][this.$scope.sortColumn].text) === 0;
}.bind(this);
// In the event of a duplicate, scan left or right (depending on
// sort order) to find an insertion point that maintains order received
while (testIndex >= 0 && testIndex < searchArray.length && isDupe()) {
if (this.$scope.sortDirection === 'asc') {
index = ++testIndex;
} else {
index = testIndex--;
}
}
return index;
};
/**
* @private
*/
@@ -439,9 +471,9 @@ define(
case -1:
return this.binarySearch(searchArray, searchElement, min,
sampleAt - 1);
case 0 :
case 0:
return sampleAt;
case 1 :
case 1:
return this.binarySearch(searchArray, searchElement,
sampleAt + 1, max);
}
@@ -458,7 +490,7 @@ define(
index = array.length;
} else {
//Sort is enabled, perform binary search to find insertion point
index = this.binarySearch(array, element[this.$scope.sortColumn].text, 0, array.length - 1);
index = this.findInsertionPoint(array, element[this.$scope.sortColumn].text);
}
if (index === -1) {
array.unshift(element);