From c56c05734b3b350b3c9e9e7555135223f6c37872 Mon Sep 17 00:00:00 2001 From: "Benny C. Wong" Date: Thu, 4 Aug 2016 13:52:52 -0700 Subject: [PATCH] add back missing change from 2 commits ago --- addon/index.js | 40 ++++++++++++++-------------------------- package.json | 2 +- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/addon/index.js b/addon/index.js index 37b94884..52b45282 100644 --- a/addon/index.js +++ b/addon/index.js @@ -1,42 +1,30 @@ 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; -}); +const { + get, + inject, + run: { next }, +} = Ember; export default Ember.Mixin.create({ + service: inject.service('router-scroll'), willTransition(...args) { this._super(...args); - scrollMap[windowLocation] = Ember.$(window).scrollTop(); + get(this, 'service').update(); }, didTransition(transitions, ...args) { this._super(transitions, ...args); + next(() => { + let scrollPosition = get(this, 'service.position'); - Ember.run.next(() => { - windowLocation = window.location.pathname; - const scrollPosition = scrollMap[windowLocation] ? scrollMap[windowLocation] : 0; - const preserveScrollPosition = transitions[transitions.length - 1] + let 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; + if (!preserveScrollPosition) { + window.scrollTo(scrollPosition.x, scrollPosition.y); + } }); - }, + } }); diff --git a/package.json b/package.json index 46975b57..cc927c00 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ember-router-scroll", - "version": "0.0.4", + "version": "0.0.5", "description": "Scroll to top with preserved browser history scroll position", "directories": { "doc": "doc",