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

Correct preserveScrollPosition calculation in nested controllers #320

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install Node
uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 14.x
cache: yarn
- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand All @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 14.x
cache: yarn
- name: Install Dependencies
run: yarn install --no-lockfile
Expand All @@ -54,6 +54,9 @@ jobs:
- ember-lts-3.16
- ember-lts-3.20
- ember-lts-3.24
- ember-lts-3.28
- ember-lts-4.4
- ember-lts-4.8
- ember-release
- ember-beta
- ember-canary
Expand All @@ -67,7 +70,7 @@ jobs:
- name: Install Node
uses: actions/setup-node@v2
with:
node-version: 12.x
node-version: 14.x
cache: yarn
- name: Install Dependencies
run: yarn install --frozen-lockfile
Expand Down
15 changes: 10 additions & 5 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,18 @@ class RouterScroll extends Service {
this.unsetFirstLoad();
}

let scrollPosition = this.position;
let scrollPosition = this.position || { x: 0, y: 0 };

// If `preserveScrollPosition` was not set on the controller, attempt fallback to `preserveScrollPosition` which was set on the router service.
let preserveScrollPosition =
(transition.router.currentRouteInfos || []).some(
(routeInfo) => routeInfo.route.controller.preserveScrollPosition
) || this.preserveScrollPosition;
let preserveScrollPosition = false;
(transition.router.currentRouteInfos || []).forEach((routeInfo) => {
let thisPreserveScrollPosition =
routeInfo.route.controller.preserveScrollPosition;
preserveScrollPosition =
typeof thisPreserveScrollPosition === 'boolean' &&
thisPreserveScrollPosition;
});
preserveScrollPosition ||= this.preserveScrollPosition;

if (!preserveScrollPosition) {
const { scrollElement, targetElement, currentURL } = this;
Expand Down
24 changes: 24 additions & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ module.exports = async function () {
},
},
},
{
name: 'ember-lts-3.28',
npm: {
devDependencies: {
'ember-source': '~3.28.0',
},
},
},
{
name: 'ember-lts-4.4',
npm: {
devDependencies: {
'ember-source': '~4.4.0',
},
},
},
{
name: 'ember-lts-4.8',
npm: {
devDependencies: {
'ember-source': '~4.8.0',
},
},
},
{
name: 'ember-release',
npm: {
Expand Down
37 changes: 34 additions & 3 deletions tests/unit/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ module('router-scroll', function (hooks) {
window.scrollTo = scrollTo;
});

function getTransitionsMock(URL, isPreserveScroll) {
function getTransitionsMock(URL, isPreserveScroll = false, nested = false) {
Object.defineProperty(subject, 'currentURL', {
value: URL || 'Hello/World',
});

const transition = {
handler: {
controller: {
preserveScrollPosition: isPreserveScroll || false,
preserveScrollPosition: nested || isPreserveScroll,
},
},
router: {
currentRouteInfos: [
{
route: {
controller: {
preserveScrollPosition: isPreserveScroll || false,
preserveScrollPosition: !nested && isPreserveScroll,
},
},
},
Expand Down Expand Up @@ -177,6 +177,37 @@ module('router-scroll', function (hooks) {
await settled();
});

test('Update Scroll Position: Can override preserveScrollPosition', async function (assert) {
assert.expect(1);
const done = assert.async();

window.scrollTo = () => {
assert.ok(true, 'Scroll To should be called');
done();
};

this.owner.register(
'service:fastboot',
class extends EmberObject {
isFastBoot = false;
}
);
const routerScrollService = this.owner.lookup('service:router-scroll');
Object.defineProperty(routerScrollService, 'position', {
get position() {
return { x: 0, y: 0 };
},
});
routerScrollService.scrollElement = 'window';

subject = this.owner.lookup('service:router');
subject.trigger(
'routeDidChange',
getTransitionsMock('Hello/World', false, true)
);
await settled();
});

test('Update Scroll Position: Can preserve position using routerService', async function (assert) {
assert.expect(0);

Expand Down