Merge pull request #2916 from Snuffleupagus/getVisibleElements

Rewrite and refactor getVisibleElements() to make it more generic
This commit is contained in:
Brendan Dahl 2013-03-19 11:35:53 -07:00
commit 921f3211a4

View File

@ -1592,55 +1592,52 @@ var PDFView = {
}, },
getVisiblePages: function pdfViewGetVisiblePages() { getVisiblePages: function pdfViewGetVisiblePages() {
return this.getVisibleElements(this.container, if (!this.isFullscreen) {
this.pages, true); return this.getVisibleElements(this.container, this.pages, true);
} else {
// The algorithm in getVisibleElements is broken in fullscreen mode.
var visible = [], page = this.page;
var currentPage = this.pages[page - 1];
visible.push({ id: currentPage.id, view: currentPage });
return { first: currentPage, last: currentPage, views: visible};
}
}, },
getVisibleThumbs: function pdfViewGetVisibleThumbs() { getVisibleThumbs: function pdfViewGetVisibleThumbs() {
return this.getVisibleElements(this.thumbnailContainer, return this.getVisibleElements(this.thumbnailContainer, this.thumbnails);
this.thumbnails);
}, },
// Generic helper to find out what elements are visible within a scroll pane. // Generic helper to find out what elements are visible within a scroll pane.
getVisibleElements: function pdfViewGetVisibleElements( getVisibleElements: function pdfViewGetVisibleElements(
scrollEl, views, sortByVisibility) { scrollEl, views, sortByVisibility) {
var currentHeight = 0, view; var top = scrollEl.scrollTop, bottom = top + scrollEl.clientHeight;
var top = scrollEl.scrollTop; var left = scrollEl.scrollLeft, right = left + scrollEl.clientWidth;
for (var i = 1, ii = views.length; i <= ii; ++i) { var visible = [], view;
view = views[i - 1]; var currentHeight, viewHeight, hiddenHeight, percentHeight;
var currentWidth, viewWidth;
for (var i = 0, ii = views.length; i < ii; ++i) {
view = views[i];
currentHeight = view.el.offsetTop + view.el.clientTop; currentHeight = view.el.offsetTop + view.el.clientTop;
if (currentHeight + view.el.clientHeight > top)
break;
currentHeight += view.el.clientHeight;
}
var visible = [];
// Algorithm broken in fullscreen mode
if (this.isFullscreen) {
var currentPage = this.pages[this.page - 1];
visible.push({
id: currentPage.id,
view: currentPage
});
return { first: currentPage, last: currentPage, views: visible};
}
var bottom = top + scrollEl.clientHeight;
var nextHeight, hidden, percent, viewHeight;
for (; i <= ii && currentHeight < bottom; ++i) {
view = views[i - 1];
viewHeight = view.el.clientHeight; viewHeight = view.el.clientHeight;
currentHeight = view.el.offsetTop + view.el.clientTop; if ((currentHeight + viewHeight) < top) {
nextHeight = currentHeight + viewHeight; continue;
hidden = Math.max(0, top - currentHeight) + }
Math.max(0, nextHeight - bottom); if (currentHeight > bottom) {
percent = Math.floor((viewHeight - hidden) * 100.0 / viewHeight); break;
}
currentWidth = view.el.offsetLeft + view.el.clientLeft;
viewWidth = view.el.clientWidth;
if ((currentWidth + viewWidth) < left || currentWidth > right) {
continue;
}
hiddenHeight = Math.max(0, top - currentHeight) +
Math.max(0, currentHeight + viewHeight - bottom);
percentHeight = ((viewHeight - hiddenHeight) * 100 / viewHeight) | 0;
visible.push({ id: view.id, y: currentHeight, visible.push({ id: view.id, y: currentHeight,
view: view, percent: percent }); view: view, percent: percentHeight });
currentHeight = nextHeight;
} }
var first = visible[0]; var first = visible[0];
@ -1649,13 +1646,12 @@ var PDFView = {
if (sortByVisibility) { if (sortByVisibility) {
visible.sort(function(a, b) { visible.sort(function(a, b) {
var pc = a.percent - b.percent; var pc = a.percent - b.percent;
if (Math.abs(pc) > 0.001) if (Math.abs(pc) > 0.001) {
return -pc; return -pc;
}
return a.id - b.id; // ensure stability return a.id - b.id; // ensure stability
}); });
} }
return {first: first, last: last, views: visible}; return {first: first, last: last, views: visible};
}, },