Moves scrollPageIntoView to the PDFViewer.

This commit is contained in:
Yury Delendik 2014-09-30 07:13:46 -05:00
parent f5d416cfdf
commit 374b94381d
4 changed files with 113 additions and 112 deletions

View File

@ -14,9 +14,8 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
/* globals RenderingStates, PDFJS, mozL10n, CustomStyle, /* globals RenderingStates, PDFJS, mozL10n, CustomStyle, getOutputScale, Stats,
SCROLLBAR_PADDING, CSS_UNITS, UNKNOWN_SCALE, DEFAULT_SCALE, CSS_UNITS */
getOutputScale, scrollIntoView, Stats, PresentationModeState */
'use strict'; 'use strict';
@ -358,96 +357,6 @@ var PageView = function pageView(container, id, scale, defaultViewport,
return this.viewport.convertToPdfPoint(x, y); return this.viewport.convertToPdfPoint(x, y);
}; };
this.scrollIntoView = function pageViewScrollIntoView(dest) {
if (this.viewer.presentationModeState ===
PresentationModeState.FULLSCREEN) {
if (this.linkService.page !== this.id) {
// Avoid breaking getVisiblePages in presentation mode.
this.linkService.page = this.id;
return;
}
dest = null;
// Fixes the case when PDF has different page sizes.
this.viewer._setScale(this.viewer.currentScaleValue, true);
}
if (!dest) {
scrollIntoView(div);
return;
}
var x = 0, y = 0;
var width = 0, height = 0, widthScale, heightScale;
var changeOrientation = (this.rotation % 180 === 0 ? false : true);
var pageWidth = (changeOrientation ? this.height : this.width) /
this.scale / CSS_UNITS;
var pageHeight = (changeOrientation ? this.width : this.height) /
this.scale / CSS_UNITS;
var scale = 0;
switch (dest[1].name) {
case 'XYZ':
x = dest[2];
y = dest[3];
scale = dest[4];
// If x and/or y coordinates are not supplied, default to
// _top_ left of the page (not the obvious bottom left,
// since aligning the bottom of the intended page with the
// top of the window is rarely helpful).
x = x !== null ? x : 0;
y = y !== null ? y : pageHeight;
break;
case 'Fit':
case 'FitB':
scale = 'page-fit';
break;
case 'FitH':
case 'FitBH':
y = dest[2];
scale = 'page-width';
break;
case 'FitV':
case 'FitBV':
x = dest[2];
width = pageWidth;
height = pageHeight;
scale = 'page-height';
break;
case 'FitR':
x = dest[2];
y = dest[3];
width = dest[4] - x;
height = dest[5] - y;
var viewerContainer = this.viewer.container;
widthScale = (viewerContainer.clientWidth - SCROLLBAR_PADDING) /
width / CSS_UNITS;
heightScale = (viewerContainer.clientHeight - SCROLLBAR_PADDING) /
height / CSS_UNITS;
scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
break;
default:
return;
}
if (scale && scale !== this.viewer.currentScale) {
this.viewer.currentScaleValue = scale;
} else if (this.viewer.currentScale === UNKNOWN_SCALE) {
this.viewer.currentScaleValue = DEFAULT_SCALE;
}
if (scale === 'page-fit' && !dest[4]) {
scrollIntoView(div);
return;
}
var boundingRect = [
this.viewport.convertToViewportPoint(x, y),
this.viewport.convertToViewportPoint(x + width, y + height)
];
var left = Math.min(boundingRect[0][0], boundingRect[1][0]);
var top = Math.min(boundingRect[0][1], boundingRect[1][1]);
scrollIntoView(div, { left: left, top: top });
};
this.draw = function pageviewDraw(callback) { this.draw = function pageviewDraw(callback) {
var pdfPage = this.pdfPage; var pdfPage = this.pdfPage;

View File

@ -202,7 +202,7 @@ var PDFFindController = (function PDFFindControllerClosure() {
// If the page is selected, scroll the page into view, which triggers // If the page is selected, scroll the page into view, which triggers
// rendering the page, which adds the textLayer. Once the textLayer is // rendering the page, which adds the textLayer. Once the textLayer is
// build, it will scroll onto the selected match. // build, it will scroll onto the selected match.
page.scrollIntoView(); this.pdfViewer.scrollPageIntoView(index + 1);
} }
if (page.textLayer) { if (page.textLayer) {

View File

@ -16,8 +16,8 @@
*/ */
/*globals watchScroll, Cache, DEFAULT_CACHE_SIZE, PageView, UNKNOWN_SCALE, /*globals watchScroll, Cache, DEFAULT_CACHE_SIZE, PageView, UNKNOWN_SCALE,
SCROLLBAR_PADDING, VERTICAL_PADDING, MAX_AUTO_SCALE, CSS_UNITS, SCROLLBAR_PADDING, VERTICAL_PADDING, MAX_AUTO_SCALE, CSS_UNITS,
getVisibleElements, RenderingStates, Promise, DEFAULT_SCALE, scrollIntoView, getVisibleElements, RenderingStates,
PDFJS, TextLayerBuilder, PDFRenderingQueue */ PDFJS, Promise, TextLayerBuilder, PDFRenderingQueue */
'use strict'; 'use strict';
@ -322,7 +322,7 @@ var PDFViewer = (function pdfViewer() {
dest = [null, { name: 'XYZ' }, this.location.left, dest = [null, { name: 'XYZ' }, this.location.left,
this.location.top, null]; this.location.top, null];
} }
this.pages[page - 1].scrollIntoView(dest); this.scrollPageIntoView(page, dest);
} }
var event = document.createEvent('UIEvents'); var event = document.createEvent('UIEvents');
@ -383,6 +383,106 @@ var PDFViewer = (function pdfViewer() {
} }
}, },
/**
* Scrolls page into view.
* @param {number} pageNumber
* @param {Array} dest - (optional) original PDF destination array:
* <page-ref> </XYZ|FitXXX> <args..>
*/
scrollPageIntoView: function PDFViewer_scrollPageIntoView(pageNumber,
dest) {
var pageView = this.pages[pageNumber - 1];
var pageViewDiv = pageView.el;
if (this.presentationModeState ===
PresentationModeState.FULLSCREEN) {
if (this.linkService.page !== pageView.id) {
// Avoid breaking getVisiblePages in presentation mode.
this.linkService.page = pageView.id;
return;
}
dest = null;
// Fixes the case when PDF has different page sizes.
this._setScale(this.currentScaleValue, true);
}
if (!dest) {
scrollIntoView(pageViewDiv);
return;
}
var x = 0, y = 0;
var width = 0, height = 0, widthScale, heightScale;
var changeOrientation = (pageView.rotation % 180 === 0 ? false : true);
var pageWidth = (changeOrientation ? pageView.height : pageView.width) /
pageView.scale / CSS_UNITS;
var pageHeight = (changeOrientation ? pageView.width : pageView.height) /
pageView.scale / CSS_UNITS;
var scale = 0;
switch (dest[1].name) {
case 'XYZ':
x = dest[2];
y = dest[3];
scale = dest[4];
// If x and/or y coordinates are not supplied, default to
// _top_ left of the page (not the obvious bottom left,
// since aligning the bottom of the intended page with the
// top of the window is rarely helpful).
x = x !== null ? x : 0;
y = y !== null ? y : pageHeight;
break;
case 'Fit':
case 'FitB':
scale = 'page-fit';
break;
case 'FitH':
case 'FitBH':
y = dest[2];
scale = 'page-width';
break;
case 'FitV':
case 'FitBV':
x = dest[2];
width = pageWidth;
height = pageHeight;
scale = 'page-height';
break;
case 'FitR':
x = dest[2];
y = dest[3];
width = dest[4] - x;
height = dest[5] - y;
var viewerContainer = this.container;
widthScale = (viewerContainer.clientWidth - SCROLLBAR_PADDING) /
width / CSS_UNITS;
heightScale = (viewerContainer.clientHeight - SCROLLBAR_PADDING) /
height / CSS_UNITS;
scale = Math.min(Math.abs(widthScale), Math.abs(heightScale));
break;
default:
return;
}
if (scale && scale !== this.currentScale) {
this.currentScaleValue = scale;
} else if (this.currentScale === UNKNOWN_SCALE) {
this.currentScaleValue = DEFAULT_SCALE;
}
if (scale === 'page-fit' && !dest[4]) {
scrollIntoView(pageViewDiv);
return;
}
var boundingRect = [
pageView.viewport.convertToViewportPoint(x, y),
pageView.viewport.convertToViewportPoint(x + width, y + height)
];
var left = Math.min(boundingRect[0][0], boundingRect[1][0]);
var top = Math.min(boundingRect[0][1], boundingRect[1][1]);
scrollIntoView(pageViewDiv, { left: left, top: top });
},
_updateLocation: function (firstPage) { _updateLocation: function (firstPage) {
var currentScale = this._currentScale; var currentScale = this._currentScale;
var currentScaleValue = this._currentScaleValue; var currentScaleValue = this._currentScaleValue;

View File

@ -253,10 +253,6 @@ var PDFViewerApplication = {
}); });
}, },
getPageView: function pdfViewGetPageView(index) {
return this.pdfViewer.pages[index];
},
zoomIn: function pdfViewZoomIn(ticks) { zoomIn: function pdfViewZoomIn(ticks) {
var newScale = this.pdfViewer.currentScale; var newScale = this.pdfViewer.currentScale;
do { do {
@ -663,8 +659,7 @@ var PDFViewerApplication = {
if (pageNumber > self.pagesCount) { if (pageNumber > self.pagesCount) {
pageNumber = self.pagesCount; pageNumber = self.pagesCount;
} }
var currentPage = self.getPageView(pageNumber - 1); self.pdfViewer.scrollPageIntoView(pageNumber, dest);
currentPage.scrollIntoView(dest);
// Update the browsing history. // Update the browsing history.
PDFHistory.push({ dest: dest, hash: destString, page: pageNumber }); PDFHistory.push({ dest: dest, hash: destString, page: pageNumber });
@ -1190,8 +1185,7 @@ var PDFViewerApplication = {
zoomArg]; zoomArg];
} }
if (dest) { if (dest) {
var currentPage = this.getPageView((pageNumber || this.page) - 1); this.pdfViewer.scrollPageIntoView(pageNumber || this.page, dest);
currentPage.scrollIntoView(dest);
} else if (pageNumber) { } else if (pageNumber) {
this.page = pageNumber; // simple page this.page = pageNumber; // simple page
} }
@ -1297,7 +1291,7 @@ var PDFViewerApplication = {
alertNotReady = true; alertNotReady = true;
} else { } else {
for (i = 0, ii = this.pagesCount; i < ii; ++i) { for (i = 0, ii = this.pagesCount; i < ii; ++i) {
if (!this.getPageView(i).pdfPage) { if (!this.pdfViewer.getPageView(i).pdfPage) {
alertNotReady = true; alertNotReady = true;
break; break;
} }
@ -1316,7 +1310,7 @@ var PDFViewerApplication = {
var body = document.querySelector('body'); var body = document.querySelector('body');
body.setAttribute('data-mozPrintCallback', true); body.setAttribute('data-mozPrintCallback', true);
for (i = 0, ii = this.pagesCount; i < ii; ++i) { for (i = 0, ii = this.pagesCount; i < ii; ++i) {
this.getPageView(i).beforePrint(); this.pdfViewer.getPageView(i).beforePrint();
} }
//#if (FIREFOX || MOZCENTRAL) //#if (FIREFOX || MOZCENTRAL)
@ -1343,7 +1337,7 @@ var PDFViewerApplication = {
}, },
rotatePages: function pdfViewRotatePages(delta) { rotatePages: function pdfViewRotatePages(delta) {
var currentPage = this.getPageView(this.page - 1); var pageNumber = this.page;
this.pageRotation = (this.pageRotation + 360 + delta) % 360; this.pageRotation = (this.pageRotation + 360 + delta) % 360;
this.pdfViewer.pagesRotation = this.pageRotation; this.pdfViewer.pagesRotation = this.pageRotation;
@ -1351,9 +1345,7 @@ var PDFViewerApplication = {
this.forceRendering(); this.forceRendering();
if (currentPage) { this.pdfViewer.scrollPageIntoView(pageNumber);
currentPage.scrollIntoView();
}
}, },
/** /**
@ -1949,7 +1941,7 @@ window.addEventListener('pagechange', function pagechange(evt) {
if (this.loading && page === 1) { if (this.loading && page === 1) {
return; return;
} }
PDFViewerApplication.getPageView(page - 1).scrollIntoView(); PDFViewerApplication.pdfViewer.scrollPageIntoView(page);
}, true); }, true);
function handleMouseWheel(evt) { function handleMouseWheel(evt) {