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

ERS creation #1

Merged
merged 2 commits into from
Jul 21, 2016
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Ember-router-scroll

This README outlines the details of collaborating on this Ember addon.
ember-router-scroll

## Installation

Expand Down
42 changes: 42 additions & 0 deletions addon/mixins/router-scroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Ember from 'ember';

// ember-router-scroll variables
let popStateEvent = false;
const scrollMap = {};
let windowLocation = '';

// ember-router-scroll event handler
window.addEventListener('popstate', () => {
popStateEvent = true;
});

export default Ember.Mixin.create({

willTransition(...args) {
this._super(...args);
scrollMap[windowLocation] = Ember.$(window).scrollTop();
},

didTransition(transitions, ...args) {
this._super(transitions, ...args);

Ember.run.next(() => {
windowLocation = window.location.pathname;
const scrollPosition = scrollMap[windowLocation] ? scrollMap[windowLocation] : 0;
const preserveScrollPosition = transitions[transitions.length - 1]
.handler.controller.get('preserveScrollPosition');
if (popStateEvent) {
// If the back or forward browser button is pressed, set scroll top to
// the position it had when the page was last seen.
Ember.$(window).scrollTop(scrollPosition);
} else {
// This is an initial or direct page visit, so begin at page top.
if (preserveScrollPosition) {return;}
Ember.$(window).scrollTop(0);
}

// Reset popstate event status.
popStateEvent = false;
});
},
});
1 change: 1 addition & 0 deletions app/mixins/ember-router-scroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from 'ember-router-scroll/mixins/router-scroll';
12 changes: 12 additions & 0 deletions tests/unit/mixins/router-scroll-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Ember from 'ember';
import RouterScrollMixin from 'ember-router-scroll/mixins/router-scroll';
import { module, test } from 'qunit';

module('Unit | Mixin | router scroll');

// Replace this with your real tests.
test('it works', function(assert) {
let RouterScrollObject = Ember.Object.extend(RouterScrollMixin);
let subject = RouterScrollObject.create();
assert.ok(subject);
});