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 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: 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 application's `config/environment.js` file.

```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
12 changes: 11 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,23 @@ 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);
} else if ('#' === scrollElement.charAt(0)) {
let element = document.getElementById(scrollElement.substring(1));

if (element) {
element.scrollLeft = scrollPosition.x;
element.scrollTop = scrollPosition.y;
}
}
}
}
});
37 changes: 35 additions & 2 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
/* 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;
} 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 +48,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);
}
}
}
});