Skip to content

Commit

Permalink
Merge branch 'scroll_work'
Browse files Browse the repository at this point in the history
  • Loading branch information
SamSaffron committed Mar 25, 2013
2 parents 988a712 + ae2cfa3 commit 4cc1b88
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 148 deletions.
89 changes: 85 additions & 4 deletions app/assets/javascripts/discourse/components/eyeline.js
Expand Up @@ -14,7 +14,91 @@
**/
Discourse.Eyeline = function Eyeline(selector) {
this.selector = selector;
}
};


/**
Call this to analyze the positions of all the nodes in a set
returns: a hash with top, bottom and onScreen items
{top: , bottom:, onScreen:}
**/
Discourse.Eyeline.analyze = function(rows) {
var current, goingUp, i, increment, offset,
winHeight, winOffset, detected, onScreen,
bottom, top, outerHeight;

if (rows.length === 0) return;

i = parseInt(rows.length / 2, 10);
increment = parseInt(rows.length / 4, 10);
goingUp = undefined;
winOffset = window.pageYOffset || $('html').scrollTop();
winHeight = window.innerHeight || $(window).height();

while (true) {
if (i === 0 || (i >= rows.length - 1)) {
break;
}
current = $(rows[i]);
offset = current.offset();

if (offset.top - winHeight < winOffset) {
if (offset.top + current.outerHeight() - window.innerHeight > winOffset) {
break;
} else {
i = i + increment;
if (goingUp !== undefined && increment === 1 && !goingUp) {
break;
}
goingUp = true;
}
} else {
i = i - increment;
if (goingUp !== undefined && increment === 1 && goingUp) {
break;
}
goingUp = false;
}
if (increment > 1) {
increment = parseInt(increment / 2, 10);
goingUp = undefined;
}
if (increment === 0) {
increment = 1;
goingUp = undefined;
}
}

onScreen = [];
bottom = i;
// quick analysis of whats on screen
while(true) {
if(i < 0) { break;}

current = $(rows[i]);
offset = current.offset();
outerHeight = current.outerHeight();

// on screen
if(offset.top > winOffset && offset.top + outerHeight < winOffset + winHeight) {
onScreen.unshift(i);
} else {

if(offset.top < winOffset) {
top = i;
break;
} else {
// bottom
}
}
i -=1;
}

return({top: top, bottom: bottom, onScreen: onScreen});

};


/**
Call this whenever you want to consider what is being seen by the browser
Expand All @@ -25,9 +109,6 @@ Discourse.Eyeline.prototype.update = function() {
var $elements, atBottom, bottomOffset, docViewBottom, docViewTop, documentHeight, foundElement, windowHeight,
_this = this;

// before anything ... let us not do anything if we have no focus
if (!Discourse.get('hasFocus')) { return; }

docViewTop = $(window).scrollTop();
windowHeight = $(window).height();
docViewBottom = docViewTop + windowHeight;
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/discourse/components/screen_track.js
Expand Up @@ -138,6 +138,9 @@ Discourse.ScreenTrack = Ember.Object.extend({
this.topicTime += diff;
docViewTop = $(window).scrollTop() + $('header').height();
docViewBottom = docViewTop + $(window).height();

// TODO: Eyeline has a smarter more accurate function here

return Object.keys(this.timings, function(id) {
var $element, elemBottom, elemTop, timing;
$element = $(id);
Expand Down
15 changes: 12 additions & 3 deletions app/assets/javascripts/discourse/mixins/scrolling.js
Expand Up @@ -10,14 +10,23 @@
Discourse.Scrolling = Em.Mixin.create({

/**
Begin watching for scroll events. They will be called at max every 100ms.
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() {
bindScrolling: function(opts) {
var onScroll,
_this = this;
onScroll = Discourse.debounce(function() { return _this.scrolled(); }, 100);

opts = opts || {debounce: 100};

if (opts.debounce) {
onScroll = Discourse.debounce(function() { return _this.scrolled(); }, 100);
} else {
onScroll = function(){ return _this.scrolled(); };
}

$(document).bind('touchmove.discourse', onScroll);
$(window).bind('scroll.discourse', onScroll);
},
Expand Down

0 comments on commit 4cc1b88

Please sign in to comment.