Skip to content

Commit 143392a

Browse files
committed
Refactor <NavStack> to add an extension point for the logic of identifying the current root component
1 parent 17de6fd commit 143392a

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

addon/components/nav-stack/component.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { setTransform } from 'ember-nav-stack/utils/animation';
1414
import { inject as service } from '@ember/service';
1515
import { bool, mapBy, reads } from 'macro-decorators';
1616
import { guidFor } from '@ember/object/internals';
17+
import { extractComponentKey } from 'ember-nav-stack/utils/component'
1718

1819
function currentTransitionPercentage(fromValue, toValue, currentValue) {
1920
if (fromValue === undefined || fromValue === toValue) {
@@ -155,7 +156,7 @@ export default class NavStack extends Component {
155156
let stackItems = this.stackItems || [];
156157
let stackDepth = stackItems.length;
157158
let rootComponentRef = stackItems[0] && stackItems[0].component;
158-
let rootComponentIdentifier = getComponentIdentifier(rootComponentRef);
159+
let rootComponentKey = this.args.extractComponentKey ? this.args.extractComponentKey(rootComponentRef) : extractComponentKey(rootComponentRef);
159160

160161
let layer = this.args.layer;
161162
if (isInitialRender) {
@@ -171,7 +172,7 @@ export default class NavStack extends Component {
171172
this.element.style.display = 'none';
172173
this.schedule(this.slideDown);
173174

174-
} else if (rootComponentIdentifier !== this._rootComponentIdentifier) {
175+
} else if (rootComponentKey !== this._rootComponentKey) {
175176
this.schedule(this.cut);
176177

177178
} else if (stackDepth < this._stackDepth) {
@@ -185,7 +186,7 @@ export default class NavStack extends Component {
185186
}
186187

187188
this._stackDepth = stackDepth;
188-
this._rootComponentIdentifier = rootComponentIdentifier;
189+
this._rootComponentKey = rootComponentKey;
189190
}
190191

191192
schedule(method) {
@@ -574,25 +575,3 @@ export default class NavStack extends Component {
574575
return x;
575576
}
576577
}
577-
578-
function getComponentIdentifier(componentRef) {
579-
if (!componentRef) {
580-
return 'none';
581-
}
582-
let result = componentRef.name || componentRef.inner.name;
583-
if (componentRef.args.named.model) {
584-
let model = componentRef.args.named.model.value();
585-
if (model) {
586-
result += `:${get(model, 'id')}`;
587-
}
588-
} else if (componentRef.args.named.has && componentRef.args.named.has('model')) {
589-
let model = componentRef.args.named.get('model').value();
590-
if (model) {
591-
let modelId = get(model, 'id');
592-
if (modelId) {
593-
result += `:${modelId}`;
594-
}
595-
}
596-
}
597-
return result;
598-
}

addon/utils/component.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { get } from '@ember/object';
2+
3+
export function extractComponentKey(componentRef) {
4+
if (!componentRef) {
5+
return 'none';
6+
}
7+
let result = componentRef.name || componentRef.inner.name;
8+
if (componentRef.args.named.model) {
9+
let model = componentRef.args.named.model.value();
10+
if (model) {
11+
result += `:${get(model, 'id')}`;
12+
}
13+
} else if (componentRef.args.named.has && componentRef.args.named.has('model')) {
14+
let model = componentRef.args.named.get('model').value();
15+
if (model) {
16+
let modelId = get(model, 'id');
17+
if (modelId) {
18+
result += `:${modelId}`;
19+
}
20+
}
21+
}
22+
return result;
23+
}

0 commit comments

Comments
 (0)