2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-07 12:02:53 +08:00
discourse/app/assets/javascripts/discourse/mixins/scrolling.js

69 lines
No EOL
1.6 KiB
JavaScript

/**
This mixin adds support for being notified every time the browser window
is scrolled.
@class Scrolling
@extends Ember.Mixin
@namespace Discourse
@module Discourse
**/
Discourse.Scrolling = Em.Mixin.create({
/**
Begin watching for scroll events. By default they will be called at max every 100ms.
call with {debounce: N} for a diff time
@method bindScrolling
*/
bindScrolling: function(opts) {
opts = opts || {debounce: 100};
var scrollingMixin = this;
var onScrollMethod;
if (opts.debounce) {
onScrollMethod = Discourse.debounce(function() {
return scrollingMixin.scrolled();
}, 100);
} else {
onScrollMethod = function() {
return scrollingMixin.scrolled();
};
}
Discourse.ScrollingDOMMethods.bindOnScroll(onScrollMethod);
},
/**
Begin watching for scroll events. They will be called at max every 100ms.
@method unbindScrolling
*/
unbindScrolling: function() {
Discourse.ScrollingDOMMethods.unbindOnScroll();
}
});
/**
This object provides the DOM methods we need for our Mixin to bind to scrolling
methods in the browser. By removing them from the Mixin we can test them
easier.
@class ScrollingDOMMethods
@module Discourse
**/
Discourse.ScrollingDOMMethods = {
bindOnScroll: function(onScrollMethod) {
$(document).bind('touchmove.discourse', onScrollMethod);
$(window).bind('scroll.discourse', onScrollMethod);
},
unbindOnScroll: function() {
$(window).unbind('scroll.discourse');
$(document).unbind('touchmove.discourse');
}
};