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

Align with Ember's HistoryLocation router #38

Closed
wants to merge 1 commit into from
Closed
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
21 changes: 13 additions & 8 deletions addon/locations/router-scroll.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import Ember from 'ember';

function _uuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r, v;
r = Math.random() * 16 | 0;
v = c === 'x' ? r : r & 3 | 8;
return v.toString(16);
});
}

const {
get,
HistoryLocation,
} = Ember;

export default HistoryLocation.extend({
init(...args) {
this._super(...args);
this.stateCounter = 0;
},
pushState(path) {
const id = `${this.stateCounter++}`;
const state = { path, id };
const uuid = _uuid();
const state = { path, uuid };
get(this, 'history').pushState(state, null, path);
this._previousURL = this.getURL(); // eslint-disable-line no-underscore-dangle
},
replaceState(path) {
const id = `${this.stateCounter++}`;
const state = { path, id };
const uuid = _uuid();
const state = { path, uuid };
get(this, 'history').replaceState(state, null, path);
this._previousURL = this.getURL(); // eslint-disable-line no-underscore-dangle
},
Expand Down
8 changes: 4 additions & 4 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ export default Service.extend({
},

position: computed(function position() {
const scrollMap = get(this, 'scrollMap');
const stateId = get(window, 'history.state.id');
let scrollMap = get(this, 'scrollMap');
let stateUuid = get(window, 'history.state.uuid');

set(this, 'key', stateId);
const key = get(this, 'key') || '-1';
set(this, 'key', stateUuid);
let key = getWithDefault(this, 'key', '-1');

return getWithDefault(scrollMap, key, { x: 0, y: 0 });
}).volatile(),
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/services/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ test('updating will not set `scrollMap` to the current scroll position if `key`
assert.deepEqual(get(service, 'scrollMap'), { });
});

test('computing the position for an existing state id return the coords', function(assert) {
test('computing the position for an existing state uuid return the coords', function(assert) {
let service = this.subject();
let state = window.history.state;
window.history.replaceState({id: '123'}, null);
window.history.replaceState({uuid: '123'}, null);

let expected = { x: 1, y: 1 };
set(service, 'scrollMap.123', expected);
Expand All @@ -48,7 +48,7 @@ test('computing the position for an existing state id return the coords', functi
test('computing the position for a state without a cached scroll position returns default', function(assert) {
let service = this.subject();
let state = window.history.state;
window.history.replaceState({id: '123'}, null);
window.history.replaceState({uuid: '123'}, null);

let expected = { x: 0, y: 0 };
assert.deepEqual(get(service, 'position'), expected);
Expand Down