Last active
August 29, 2015 14:10
-
-
Save poteto/d88d19dbd23c04b7f9e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Mixin.create({ | |
scrollTimeout: 100, | |
boundingClientRect: 0, | |
windowHeight: 0, | |
windowWidth: 0, | |
enteredViewport: Ember.computed('boundingClientRect', 'windowHeight', 'windowWidth', function() { | |
var rect, windowHeight, windowWidth; | |
rect = this.get('boundingClientRect'); | |
windowHeight = this.get('windowHeight'); | |
windowWidth = this.get('windowWidth'); | |
return ( | |
rect.top >= 0 && | |
rect.left >= 0 && | |
rect.bottom <= windowHeight && | |
rect.right <= windowWidth | |
); | |
}), | |
exitedViewport: Ember.computed.not('enteredViewport'), | |
_updateBoundingClientRect: function() { | |
var el; | |
el = this.$()[0]; | |
this.set('boundingClientRect', el.getBoundingClientRect()); | |
}, | |
_setup: (function() { | |
return Ember.run.scheduleOnce('afterRender', this, function() { | |
this._updateBoundingClientRect(); | |
this.set('windowHeight', window.innerHeight || document.documentElement.clientHeight); | |
this.set('windowWidth', window.innerWidth || document.documentElement.clientWidth); | |
}); | |
}).on('didInsertElement'), | |
_scrollHandler: function() { | |
return Ember.run.debounce(this, '_updateBoundingClientRect', this.get('scrollTimeout')); | |
}, | |
_bindScroll: (function() { | |
var scrollHandler; | |
scrollHandler = this._scrollHandler.bind(this); | |
Ember.$(document).on('touchmove.scrollable', scrollHandler); | |
Ember.$(window).on('scroll.scrollable', scrollHandler); | |
}).on('didInsertElement'), | |
_unbindScroll: (function() { | |
Ember.$(window).off('.scrollable'); | |
Ember.$(document).off('.scrollable'); | |
}).on('willDestroyElement') | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment