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

Fastboot compatibility #20

Merged
merged 5 commits into from
Aug 16, 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
14 changes: 11 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
---
sudo: required
dist: trusty
language: node_js
node_js:
- "4"

sudo: false

cache:
directories:
- node_modules
- bower_components

env:
- EMBER_TRY_SCENARIO=default
Expand All @@ -22,9 +23,16 @@ matrix:
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
- export CHROME_BIN=google-chrome
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- sudo apt-get update
- sudo apt-get install -y libappindicator1 fonts-liberation
- wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
- sudo dpkg -i google-chrome*.deb
- npm config set spin false
- npm install -g bower
- npm install phantomjs-prebuilt
- bower --version

install:
- npm install
Expand Down
28 changes: 20 additions & 8 deletions addon/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Ember from 'ember';
import getOwner from 'ember-getowner-polyfill';

const {
computed,
get,
inject,
run: { next },
Expand All @@ -9,22 +11,32 @@ const {
export default Ember.Mixin.create({
service: inject.service('router-scroll'),

isFastBoot: computed(function() {
const fastboot = getOwner(this).lookup('service:fastboot');
return fastboot ? fastboot.get('isFastBoot') : false;
}),

willTransition(...args) {
this._super(...args);
get(this, 'service').update();
},

didTransition(transitions, ...args) {
this._super(transitions, ...args);
next(() => {
let scrollPosition = get(this, 'service.position');

let preserveScrollPosition = transitions[transitions.length - 1]
.handler.controller.get('preserveScrollPosition');
if (get(this, 'isFastBoot')) { return; }

next(() => this.updateScrollPosition(transitions));
},

updateScrollPosition(transitions) {
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 (!preserveScrollPosition) {
window.scrollTo(scrollPosition.x, scrollPosition.y);
}
}
});
16 changes: 8 additions & 8 deletions addon/locations/router-scroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Ember from 'ember';

const {
get,
HistoryLocation
HistoryLocation,
} = Ember;

export default HistoryLocation.extend({
Expand All @@ -11,15 +11,15 @@ export default HistoryLocation.extend({
this.stateCounter = 0;
},
pushState(path) {
let id = `${this.stateCounter++}`;
let state = { path, id };
const id = `${this.stateCounter++}`;
const state = { path, id };
get(this, 'history').pushState(state, null, path);
this._previousURL = this.getURL();
this._previousURL = this.getURL(); // eslint-disable-line no-underscore-dangle
},
replaceState(path) {
let id = `${this.stateCounter++}`;
let state = { path, id };
const id = `${this.stateCounter++}`;
const state = { path, id };
get(this, 'history').replaceState(state, null, path);
this._previousURL = this.getURL();
}
this._previousURL = this.getURL(); // eslint-disable-line no-underscore-dangle
},
});
19 changes: 11 additions & 8 deletions addon/services/router-scroll.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* global window */
import Ember from 'ember';

const {
get,
set,
computed,
getWithDefault,
Service
Service,
} = Ember;

export default Service.extend({
Expand All @@ -14,21 +15,23 @@ export default Service.extend({
set(this, 'scrollMap', {});
set(this, 'key', null);
},

update() {
let scrollMap = get(this, 'scrollMap');
let key = get(this, 'key');
const scrollMap = get(this, 'scrollMap');
const key = get(this, 'key');

if (key) {
set(scrollMap, key, { x: window.scrollX, y: window.scrollY });
}
},
position: computed(function() {
let scrollMap = get(this, 'scrollMap');
let stateId = get(window, 'history.state.id');

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

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

return getWithDefault(scrollMap, key, { x: 0, y: 0 });
}).volatile()
}).volatile(),
});
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"scripts": {
"build": "ember build",
"eslint": "eslint . --ignore-path .eslintignore",
"eslint": "eslint app/**/*.js addon/**/*.js tests/**/*.js --quiet --ignore-path .eslintignore",
"start": "ember server",
"test": "ember try:each"
},
Expand Down Expand Up @@ -42,13 +42,15 @@
"ember-data": "^2.6.0",
"ember-disable-prototype-extensions": "^1.1.0",
"ember-export-application-global": "^1.0.5",
"ember-getowner-polyfill": "1.0.1",
"ember-load-initializers": "^0.5.1",
"ember-resolver": "^2.0.3",
"ember-welcome-page": "^1.0.1",
"eslint": "^2.4.0",
"eslint-config-airbnb": "^6.1.0",
"eslint-plugin-dollarshaveclub": "^1.0.0",
"loader.js": "^4.0.1"
"loader.js": "^4.0.1",
"pre-git": "^3.10.0"
},
"keywords": [
"ember-addon",
Expand Down
31 changes: 29 additions & 2 deletions testem.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,40 @@
/*jshint node:true*/
module.exports = {
"framework": "qunit",
"test_page": "tests/index.html?hidepassed",
"test_page": "tests/index.html?hidepassed&nolint",
"disable_watching": true,
"launch_in_ci": [
"PhantomJS"
],
"launch_in_dev": [
"PhantomJS",
"Chrome"
]
],
launch_in_ci: [
'Chrome Custom',
],
launch_in_dev: [
'Chrome Custom',
],
launchers: {
'Chrome Custom': {
exe: [
'google-chrome',
'~/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
'/Applications/Google Chrome.app/Contents/MacOS/Google Chrome',
],
args: [
'--user-data-dir=/tmp/testem.chrome.custom',
'--no-default-browser-check',
'--no-first-run',
'--ignore-certificate-errors',
'--test-type',
'--disable-extensions',
'--disable-web-security',
'--disable-renderer-backgrounding',
'--disable-background-timer-throttling',
],
protocol: 'browser',
},
},
};
3 changes: 3 additions & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<script src="testem.js" integrity=""></script>
<script src="assets/vendor.js"></script>
<script src="assets/test-support.js"></script>
<script>
QUnit.config.testTimeout = 5000;
</script>
<script src="assets/dummy.js"></script>
<script src="assets/tests.js"></script>
<script src="assets/test-loader.js"></script>
Expand Down
43 changes: 38 additions & 5 deletions tests/unit/mixins/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,44 @@ import Ember from 'ember';
import RouterScroll from 'ember-router-scroll';
import { module, test } from 'qunit';

module('Unit | Mixin | router scroll');
module('mixin:router-scroll');

// Replace this with your real tests.
test('it works', (assert) => {
test('when the application is FastBooted', (assert) => {
assert.expect(1);

const done = assert.async();
const RouterScrollObject = Ember.Object.extend(RouterScroll);
const subject = RouterScrollObject.create();
assert.ok(subject);
const subject = RouterScrollObject.create({
isFastBoot: true,
updateScrollPosition() {
assert.notOk(true, 'it should not call updateScrollPosition.');
done();
},
});

Ember.run(() => {
subject.didTransition();
Ember.run.next(() => {
assert.ok(true, 'it should not call updateScrollPosition.');
done();
});
});
});

test('when the application is not FastBooted', (assert) => {
assert.expect(1);

const done = assert.async();
const RouterScrollObject = Ember.Object.extend(RouterScroll);
const subject = RouterScrollObject.create({
isFastBoot: false,
updateScrollPosition() {
assert.ok(true, 'it should call updateScrollPosition.');
done();
},
});

Ember.run(() => {
subject.didTransition();
});
});
2 changes: 1 addition & 1 deletion tests/unit/services/router-scroll-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
set
} = Ember;

moduleFor('service:router-scroll', 'Unit | Service | router scroll', {
moduleFor('service:router-scroll', {
// Specify the other units that are required for this test.
// needs: ['service:foo']
});
Expand Down