Skip to content

Commit

Permalink
Revert "[bugfix] Recompute row meta index when previous prepend cause…
Browse files Browse the repository at this point in the history
…s shift (Addepar#623)"

This reverts commit 5efba8c.
  • Loading branch information
cyk committed Feb 1, 2019
1 parent 322d9c8 commit bd8730f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 52 deletions.
43 changes: 10 additions & 33 deletions addon/-private/collapse-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export class TableRowMeta extends EmberObject {
*/
_cellMetaCache = new Map();
_isCollapsed = false;
_lastKnownIndex = null;

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

@computed('_lastKnownIndex', '_prevSiblingMeta.index')
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 @@ -125,18 +112,18 @@ export class TableRowMeta extends EmberObject {
@computed('_tree.length')
get next() {
let tree = get(this, '_tree');
if (get(this, '_lastKnownIndex') + 1 >= get(tree, 'length')) {
if (get(this, 'index') + 1 >= get(tree, 'length')) {
return null;
}
return tree.objectAt(get(this, '_lastKnownIndex') + 1);
return tree.objectAt(get(this, 'index') + 1);
}

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

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

// 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);
}
// 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);

return result;
}
Expand Down
19 changes: 0 additions & 19 deletions tests/unit/-private/collapse-tree-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,25 +173,6 @@ 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

0 comments on commit bd8730f

Please sign in to comment.