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

configurable scroll element #67

Merged
merged 2 commits into from
Aug 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@
ember install ember-router-scroll
```

### Options
You can specify the id of an element for which the scroll position is saved and set. Default is `window` for using the scroll position of the whole viewport. You can pass an options object in your applications `config/environment.js` file.
Copy link
Contributor

Choose a reason for hiding this comment

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

application's config/environment.js

Copy link
Contributor

Choose a reason for hiding this comment

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

@ktrhn can you put in an apostrophe in the word "application"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks!


```javascript
ENV['routerScroll'] = {
scrollElement: '#mainScrollElement'
};
```

### A small note

Like all good ember addons, this behavior was considered for core implementation. Good news, people like the idea. For now, the feature will live under the flag `ember-unique-location-history-state` until it's finally released in Ember 2.13. You can follow along for yourself [here](https://github.com/emberjs/ember.js/pull/14011/) and read up on the [RFC](https://github.com/emberjs/rfcs/pull/186#issuecomment-271416805) if you'd like as well.
Expand Down
13 changes: 12 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,24 @@ export default Mixin.create({
},

updateScrollPosition(transitions) {
const scrollElement = get(this, 'service.scrollElement');
let scrollPosition = get(this, 'service.position');

let preserveScrollPosition = transitions[transitions.length - 1]
.handler.controller.get('preserveScrollPosition');

if (!preserveScrollPosition) {
window.scrollTo(scrollPosition.x, scrollPosition.y);
if ('window' === scrollElement) {
window.scrollTo(scrollPosition.x, scrollPosition.y);

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove line.

} else if ('#' === scrollElement.charAt(0)) {
let element = document.getElementById(scrollElement.substring(1));

if (element) {
element.scrollLeft = scrollPosition.x;
element.scrollTop = scrollPosition.y;
}
}
}
}
});
38 changes: 36 additions & 2 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
/* eslint-disable */
import Ember from 'ember';
import Service from '@ember/service';
import { getWithDefault, computed, set, get } from '@ember/object';

const { getOwner, typeOf } = Ember;

export default Service.extend({
scrollElement: 'window',

init(...args) {
this._super(...args);
this._loadConfig();
set(this, 'scrollMap', {});
set(this, 'key', null);
},

update() {
const scrollElement = get(this, 'scrollElement');
const scrollMap = get(this, 'scrollMap');
const key = get(this, 'key');
let x;
let y;

if ('window' === scrollElement) {
x = window.scrollX;
y = window.scrollY;

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove line.

} else if ('#' === scrollElement.charAt(0)) {
let element = document.getElementById(scrollElement.substring(1));

if (element) {
x = element.scrollLeft;
y = element.scrollTop;
}
}

if (key) {
set(scrollMap, key, { x: window.scrollX, y: window.scrollY });
if (key && 'number' === typeOf(x) && 'number' === typeOf(y)) {
set(scrollMap, key, { x, y });
}
},

Expand All @@ -27,4 +49,16 @@ export default Service.extend({

return getWithDefault(scrollMap, key, { x: 0, y: 0 });
}).volatile(),

_loadConfig() {
const config = getOwner(this).resolveRegistration('config:environment');

if (config && config.routerScroll && config.routerScroll.scrollElement) {
const scrollElement = config.routerScroll.scrollElement;

if ('string' === typeOf(config.routerScroll.scrollElement)) {
set(this, 'scrollElement', config.routerScroll.scrollElement);
}
}
}
});