Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix] Recompute row meta index when previous prepend causes shift #623

Merged
merged 5 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions addon/-private/collapse-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export class TableRowMeta extends EmberObject {
*/
_cellMetaCache = new Map();
_isCollapsed = false;
_lastKnownIndex = null;

@computed('_rowValue.isCollapsed')
get isCollapsed() {
Expand Down Expand Up @@ -95,6 +96,18 @@ export class TableRowMeta extends EmberObject {
return parentMeta ? get(parentMeta, 'depth') + 1 : 0;
}

@computed('_lastKnownIndex', '_prevSiblingMeta.index')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the initial value of _lastKnownIndex}

get index() {
let prevSiblingIndex = get(this, '_prevSiblingMeta.index');
let lastKnownIndex = get(this, '_lastKnownIndex');

if (lastKnownIndex === prevSiblingIndex) {
return lastKnownIndex + 1;
}

return lastKnownIndex;
}

@computed('_tree.length')
get first() {
if (get(this, '_tree.length') === 0) {
Expand All @@ -112,18 +125,18 @@ export class TableRowMeta extends EmberObject {
@computed('_tree.length')
get next() {
let tree = get(this, '_tree');
if (get(this, 'index') + 1 >= get(tree, 'length')) {
if (get(this, '_lastKnownIndex') + 1 >= get(tree, 'length')) {
return null;
}
return tree.objectAt(get(this, 'index') + 1);
return tree.objectAt(get(this, '_lastKnownIndex') + 1);
}

@computed('_tree.length')
get prev() {
if (get(this, 'index') === 0) {
if (get(this, '_lastKnownIndex') === 0) {
return null;
}
return get(this, '_tree').objectAt(get(this, 'index') - 1);
return get(this, '_tree').objectAt(get(this, '_lastKnownIndex') - 1);
}

toggleCollapse() {
Expand Down Expand Up @@ -692,18 +705,28 @@ export default class CollapseTree extends EmberObject.extend(EmberArray) {
@return {{ value: object, parents: Array<object> }}
*/
objectAt(index) {
if (index >= get(this, 'length') || index < 0) {
let length = get(this, 'length');
if (index >= length || index < 0) {
return undefined;
}

let root = get(this, 'root');
let rowMetaCache = this.get('rowMetaCache');

// We add a "fake" top level node to account for the root node
let normalizedIndex = index + 1;
let result = get(this, 'root').objectAt(normalizedIndex);
let meta = this.get('rowMetaCache').get(result);
let result = root.objectAt(normalizedIndex);
let meta = rowMetaCache.get(result);

// Set the perceived index on the meta. It should be safe to do this here, since
// the row will always be retrieved via `objectAt` before being used.
set(meta, 'index', index);
// Set the last known index on the meta and link the next siblings meta
// so that its index can recompute in case it conflicts from shifting
set(meta, '_lastKnownIndex', index);

if (index < length - 1) {
let nextSibling = root.objectAt(normalizedIndex + 1);
let nextMeta = rowMetaCache.get(nextSibling);
set(nextMeta, '_prevSiblingMeta', meta);
}

return result;
}
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/-private/collapse-tree-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,25 @@ module('Unit | Private | CollapseTree', function(hooks) {
}
});

test('rowMeta index is recomputed when row is added or removed', function(assert) {
let rows = generateTree([1, [2, 3, [4, 5], 6], 7]);
tree = CollapseTree.create({ rows, rowMetaCache, enableTree: true });

let nodes = tree.toArray();
nodes.forEach((node, i) => assert.equal(metaFor(node).get('index'), i));

rows.unshiftObject({ value: 0 });

let firstNode = run(() => tree.objectAt(0));
nodes = [firstNode].concat(nodes);
nodes.forEach((node, i) => assert.equal(metaFor(node).get('index'), i));

rows.pushObject({ value: 8 });

let lastNode = run(() => tree.objectAt(8));
nodes.concat(lastNode).forEach((node, i) => assert.equal(metaFor(node).get('index'), i));
});

test('can disable tree', function(assert) {
tree = CollapseTree.create({
rows: generateTree([0, [1, 2]]),
Expand Down