From 178bbbef5baf537f16d1e1e7726ec400674eaf0c Mon Sep 17 00:00:00 2001 From: Jason Farmer Date: Thu, 21 Jul 2016 11:23:49 -0400 Subject: [PATCH 1/2] 001 - initial commit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 31d372bc..bb987ce5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Ember-router-scroll -This README outlines the details of collaborating on this Ember addon. +ember-router-scroll ## Installation From 690a5812a8ff44e46d9ca31b735b41f8d2dd6bfa Mon Sep 17 00:00:00 2001 From: Jason Farmer Date: Thu, 21 Jul 2016 12:50:19 -0400 Subject: [PATCH 2/2] 001 - router-scroll mixin added --- addon/mixins/router-scroll.js | 42 +++++++++++++++++++++++++ app/mixins/ember-router-scroll.js | 1 + tests/unit/mixins/router-scroll-test.js | 12 +++++++ 3 files changed, 55 insertions(+) create mode 100644 addon/mixins/router-scroll.js create mode 100644 app/mixins/ember-router-scroll.js create mode 100644 tests/unit/mixins/router-scroll-test.js diff --git a/addon/mixins/router-scroll.js b/addon/mixins/router-scroll.js new file mode 100644 index 00000000..37b94884 --- /dev/null +++ b/addon/mixins/router-scroll.js @@ -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; + }); + }, +}); diff --git a/app/mixins/ember-router-scroll.js b/app/mixins/ember-router-scroll.js new file mode 100644 index 00000000..90ff5d84 --- /dev/null +++ b/app/mixins/ember-router-scroll.js @@ -0,0 +1 @@ +export { default } from 'ember-router-scroll/mixins/router-scroll'; diff --git a/tests/unit/mixins/router-scroll-test.js b/tests/unit/mixins/router-scroll-test.js new file mode 100644 index 00000000..2da34785 --- /dev/null +++ b/tests/unit/mixins/router-scroll-test.js @@ -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); +});