Skip to content

Commit

Permalink
Merge branch 'release/1.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
poteto committed Apr 29, 2015
2 parents 2c894e6 + 59cbc37 commit 4fac70f
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 76 deletions.
36 changes: 23 additions & 13 deletions addon/mixins/in-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import canUseRAF from 'ember-in-viewport/utils/can-use-raf';
import isInViewport from 'ember-in-viewport/utils/is-in-viewport';
import checkScrollDirection from 'ember-in-viewport/utils/check-scroll-direction';

const get = Ember.get;
const set = Ember.set;

const {
get: get,
set: set,
setProperties,
computed,
merge,
Expand Down Expand Up @@ -42,22 +43,24 @@ export default Ember.Mixin.create({

_setInitialState: on('init', function() {
const options = merge({
viewportUseRAF : canUseRAF(),
viewportEntered : false,
viewportListeners : defaultListeners
viewportUseRAF: canUseRAF(),
viewportEntered: false,
viewportListeners: defaultListeners
}, this._buildOptions());

setProperties(this, options);
}),

_buildOptions(defaultOptions = []) {
_buildOptions(defaultOptions = {}) {
if (this.container) {
return merge(defaultOptions, this.container.lookup('config:in-viewport'));
}
},

_setupElement: on('didInsertElement', function() {
if (!canUseDOM) { return; }
if (!canUseDOM) {
return;
}

this._setInitialViewport(window);
this._addObserverIfNotSpying();
Expand Down Expand Up @@ -88,7 +91,9 @@ export default Ember.Mixin.create({

const element = get(this, 'element');

if (!element) { return; }
if (!element) {
return;
}

const elementId = get(this, 'elementId');
const viewportUseRAF = get(this, 'viewportUseRAF');
Expand Down Expand Up @@ -118,8 +123,8 @@ export default Ember.Mixin.create({
const lastDirectionForEl = lastDirection[elementId];
const lastPositionForEl = lastPosition[elementId];
const newPosition = {
top : $contextEl.scrollTop(),
left : $contextEl.scrollLeft()
top: $contextEl.scrollTop(),
left: $contextEl.scrollLeft()
};

const scrollDirection = checkScrollDirection(lastPositionForEl, newPosition, sensitivity);
Expand All @@ -139,12 +144,17 @@ export default Ember.Mixin.create({
const didLeave = viewportEntered && !hasEnteredViewport;
let triggeredEventName = '';

if (didEnter) { triggeredEventName = 'didEnterViewport'; }
if (didLeave) { triggeredEventName = 'didExitViewport'; }
if (didEnter) {
triggeredEventName = 'didEnterViewport';
}

this.trigger(triggeredEventName);
if (didLeave) {
triggeredEventName = 'didExitViewport';
}

set(this, 'viewportEntered', hasEnteredViewport);

this.trigger(triggeredEventName);
},

_unbindIfEntered() {
Expand Down
11 changes: 6 additions & 5 deletions addon/utils/can-use-raf.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ function checkRAF(window, rAF, cAF) {
let vendors = [ 'ms', 'moz', 'webkit', 'o' ];

for (x = 0; x < vendors.length && !window[rAF]; ++x) {
window[rAF] = window[vendors[x] + 'RequestAnimationFrame'];
window[cAF] = window[vendors[x] + 'CancelAnimationFrame'] ||
window[vendors[x] + 'CancelRequestAnimationFrame'];
window[rAF] = window[`${vendors[x]}RequestAnimationFrame`];
window[cAF] = window[`${vendors[x]}CancelAnimationFrame`] ||
window[`${vendors[x]}CancelRequestAnimationFrame`];
}

if (window[rAF] && window[cAF]) {
Expand All @@ -25,9 +25,10 @@ function checkRAF(window, rAF, cAF) {
}
}


export default function canUseRAF() {
if (!canUseDOM) { return false; }
if (!canUseDOM) {
return false;
}

return checkRAF(window, 'requestAnimationFrame', 'cancelAnimationFrame');
}
28 changes: 21 additions & 7 deletions addon/utils/check-scroll-direction.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,34 @@ import Ember from 'ember';
const { floor } = Math;

export default function checkScrollDirection(lastPosition = null, newPosition = {}, sensitivity = 1) {
if (!lastPosition) { return 'none'; }
if (!lastPosition) {
return 'none';
}

Ember.assert('sensitivity cannot be 0', sensitivity);

const { top, left } = newPosition;
const { top: lastTop, left: lastLeft } = lastPosition;

const delta = {
top : floor((top - lastTop) / sensitivity) * sensitivity,
left : floor((left - lastLeft) / sensitivity) * sensitivity
top: floor((top - lastTop) / sensitivity) * sensitivity,
left: floor((left - lastLeft) / sensitivity) * sensitivity
};

if (delta.top > 0) { return 'down'; }
if (delta.top < 0) { return 'up'; }
if (delta.left > 0) { return 'right'; }
if (delta.left < 0) { return 'left'; }
if (delta.top > 0) {
return 'down';
}

if (delta.top < 0) {
return 'up';
}

if (delta.left > 0) {
return 'right';
}

if (delta.left < 0) {
return 'left';
}

}
16 changes: 8 additions & 8 deletions addon/utils/is-in-viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import Ember from 'ember';
const { merge } = Ember;

const defaultTolerance = {
top : 0,
left : 0,
bottom : 0,
right : 0
top: 0,
left: 0,
bottom: 0,
right: 0
};

export default function isInViewport(boundingClientRect = {}, height = 0, width = 0, tolerance = defaultTolerance) {
const { top, left, bottom, right } = boundingClientRect;
const tolerances = merge(defaultTolerance, tolerance);
const {
top : topTolerance,
left : leftTolerance,
bottom : bottomTolerance,
right : rightTolerance
top: topTolerance,
left: leftTolerance,
bottom: bottomTolerance,
right: rightTolerance
} = tolerances;

return (
Expand Down
2 changes: 1 addition & 1 deletion app/initializers/viewport-config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import config from '../config/environment';

export function initialize(_container, application) {
const { viewportConfig } = config;
const { viewportConfig = {} } = config;

application.register('config:in-viewport', viewportConfig, { instantiate: false });
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ember-in-viewport",
"version": "1.2.0",
"version": "1.2.1",
"description": "Detect if an Ember View or Component is in the viewport @ 60FPS",
"directories": {
"doc": "doc",
Expand Down Expand Up @@ -28,8 +28,9 @@
"ember-cli-inject-live-reload": "^1.3.0",
"ember-cli-qunit": "0.3.10",
"ember-cli-uglify": "1.0.1",
"ember-export-application-global": "^1.0.2",
"ember-disable-prototype-extensions": "^1.0.0",
"ember-export-application-global": "^1.0.2",
"ember-suave": "0.1.7",
"ember-try": "0.0.4"
},
"keywords": [
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/integration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
import startApp from '../helpers/start-app';
import { lookupComponent } from '../helpers/utils/lookup';

var application;
let application;
const { run } = Ember;

module('Acceptance: Integration', {
Expand Down
4 changes: 2 additions & 2 deletions tests/dummy/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import Resolver from 'ember/resolver';
import loadInitializers from 'ember/load-initializers';
import config from './config/environment';

var App;
let App;

Ember.MODEL_FACTORY_INJECTIONS = true;

App = Ember.Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver: Resolver
Resolver
});

loadInitializers(App, config.modulePrefix);
Expand Down
6 changes: 3 additions & 3 deletions tests/dummy/app/components/foo-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Ember from 'ember';
import InViewportMixin from 'ember-in-viewport';

export default Ember.Component.extend(InViewportMixin, {
classNames : [ 'fooBar' ],
classNameBindings : [ 'viewportEntered:active:inactive' ],
classNames: [ 'fooBar' ],
classNameBindings: [ 'viewportEntered:active:inactive' ],

viewportOptionsOverride: Ember.on('didInsertElement', function() {
Ember.set(this, 'viewportUseRAF', false);
}),
})
});
2 changes: 1 addition & 1 deletion tests/dummy/app/router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
const Router = Ember.Router.extend({
location: config.locationType
});

Expand Down
10 changes: 7 additions & 3 deletions tests/helpers/resolver.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import Resolver from 'ember/resolver';
import config from '../../config/environment';

var resolver = Resolver.create();
const resolver = Resolver.create();
const {
modulePrefix,
podModulePrefix
} = config;

resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
modulePrefix,
podModulePrefix
};

export default resolver;
4 changes: 2 additions & 2 deletions tests/helpers/start-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import Router from '../../router';
import config from '../../config/environment';

export default function startApp(attrs) {
var application;
let application;

var attributes = Ember.merge({}, config.APP);
let attributes = Ember.merge({}, config.APP);
attributes = Ember.merge(attributes, attrs); // use defaults, but you can override;

Ember.run(function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/initializers/viewport-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const { keys } = Ember;
let container, application;

module('ViewportConfigInitializer', {
beforeEach: function() {
beforeEach() {
Ember.run(function() {
application = Ember.Application.create();
container = application.__container__;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/mixins/in-viewport-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module('InViewportMixin');

// Replace this with your real tests.
test('it works', function(assert) {
var InViewportObject = Ember.Object.extend(InViewportMixin);
var subject = InViewportObject.create();
const InViewportObject = Ember.Object.extend(InViewportMixin);
const subject = InViewportObject.create();
assert.ok(subject);
});
18 changes: 9 additions & 9 deletions tests/unit/utils/check-scroll-direction-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ const { forEach } = Ember.EnumerableUtils;
module('checkScrollDirection', {
beforeEach() {
lastPosition = {
top : 300,
left : 150
top: 300,
left: 150
};
}
});

test('returns the right direction', function(assert) {
const movements = [
{ direction: 'down', position: { top: 400, left: 150 }},
{ direction: 'up', position: { top: 200, left: 150 }},
{ direction: 'right', position: { top: 300, left: 250 }},
{ direction: 'left', position: { top: 300, left: 100 }}
{ direction: 'down', position: { top: 400, left: 150 } },
{ direction: 'up', position: { top: 200, left: 150 } },
{ direction: 'right', position: { top: 300, left: 250 } },
{ direction: 'left', position: { top: 300, left: 100 } }
];

assert.expect(movements.length);
Expand All @@ -35,9 +35,9 @@ test('returns the right direction', function(assert) {

test('adjusts for sensitivity', function(assert) {
const movements = [
{ direction: undefined, position: { top: 399, left: 150 }},
{ direction: 'down', position: { top: 400, left: 150 }},
{ direction: 'down', position: { top: 500, left: 250 }}
{ direction: undefined, position: { top: 399, left: 150 } },
{ direction: 'down', position: { top: 400, left: 150 } },
{ direction: 'down', position: { top: 500, left: 250 } }
];

assert.expect(movements.length);
Expand Down
32 changes: 16 additions & 16 deletions tests/unit/utils/is-in-viewport-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,34 @@ let fakeRectNotInViewport, fakeRectInViewport, fakeWindow, fakeNoTolerance, fake
module('isInViewport', {
beforeEach() {
fakeRectNotInViewport = {
top : 450,
left : 150,
bottom : 550,
right : 1130
top: 450,
left: 150,
bottom: 550,
right: 1130
};

fakeRectInViewport = {
top : 300,
left : 150,
bottom : 400,
right : 1130
top: 300,
left: 150,
bottom: 400,
right: 1130
};

fakeWindow = {
innerHeight : 400,
innerWidth : 1280
innerHeight: 400,
innerWidth: 1280
};

fakeNoTolerance = {
top : 0,
left : 0,
bottom : 0,
right : 0
top: 0,
left: 0,
bottom: 0,
right: 0
};

fakeTolerance = {
top : 200,
bottom : 200
top: 200,
bottom: 200
};
}
});
Expand Down

0 comments on commit 4fac70f

Please sign in to comment.