Skip to content

Commit

Permalink
Merge pull request #5361 from yurydelendik/movescrollinto
Browse files Browse the repository at this point in the history
Moves scrollPageIntoView to the PDFViewer.
  • Loading branch information
Snuffleupagus committed Sep 30, 2014
2 parents f5d416c + 374b943 commit 7059358
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 112 deletions.
95 changes: 2 additions & 93 deletions web/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals RenderingStates, PDFJS, mozL10n, CustomStyle,
SCROLLBAR_PADDING, CSS_UNITS, UNKNOWN_SCALE, DEFAULT_SCALE,
getOutputScale, scrollIntoView, Stats, PresentationModeState */
/* globals RenderingStates, PDFJS, mozL10n, CustomStyle, getOutputScale, Stats,
CSS_UNITS */

'use strict';

Expand Down Expand Up @@ -358,96 +357,6 @@ var PageView = function pageView(container, id, scale, defaultViewport,
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) {
var pdfPage = this.pdfPage;

Expand Down
2 changes: 1 addition & 1 deletion web/pdf_find_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ var PDFFindController = (function PDFFindControllerClosure() {
// If the page is selected, scroll the page into view, which triggers
// rendering the page, which adds the textLayer. Once the textLayer is
// build, it will scroll onto the selected match.
page.scrollIntoView();
this.pdfViewer.scrollPageIntoView(index + 1);
}

if (page.textLayer) {
Expand Down
106 changes: 103 additions & 3 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
/*globals watchScroll, Cache, DEFAULT_CACHE_SIZE, PageView, UNKNOWN_SCALE,
SCROLLBAR_PADDING, VERTICAL_PADDING, MAX_AUTO_SCALE, CSS_UNITS,
getVisibleElements, RenderingStates, Promise,
PDFJS, TextLayerBuilder, PDFRenderingQueue */
DEFAULT_SCALE, scrollIntoView, getVisibleElements, RenderingStates,
PDFJS, Promise, TextLayerBuilder, PDFRenderingQueue */

'use strict';

Expand Down Expand Up @@ -322,7 +322,7 @@ var PDFViewer = (function pdfViewer() {
dest = [null, { name: 'XYZ' }, this.location.left,
this.location.top, null];
}
this.pages[page - 1].scrollIntoView(dest);
this.scrollPageIntoView(page, dest);
}

var event = document.createEvent('UIEvents');
Expand Down Expand Up @@ -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) {
var currentScale = this._currentScale;
var currentScaleValue = this._currentScaleValue;
Expand Down
22 changes: 7 additions & 15 deletions web/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,6 @@ var PDFViewerApplication = {
});
},

getPageView: function pdfViewGetPageView(index) {
return this.pdfViewer.pages[index];
},

zoomIn: function pdfViewZoomIn(ticks) {
var newScale = this.pdfViewer.currentScale;
do {
Expand Down Expand Up @@ -663,8 +659,7 @@ var PDFViewerApplication = {
if (pageNumber > self.pagesCount) {
pageNumber = self.pagesCount;
}
var currentPage = self.getPageView(pageNumber - 1);
currentPage.scrollIntoView(dest);
self.pdfViewer.scrollPageIntoView(pageNumber, dest);

// Update the browsing history.
PDFHistory.push({ dest: dest, hash: destString, page: pageNumber });
Expand Down Expand Up @@ -1190,8 +1185,7 @@ var PDFViewerApplication = {
zoomArg];
}
if (dest) {
var currentPage = this.getPageView((pageNumber || this.page) - 1);
currentPage.scrollIntoView(dest);
this.pdfViewer.scrollPageIntoView(pageNumber || this.page, dest);
} else if (pageNumber) {
this.page = pageNumber; // simple page
}
Expand Down Expand Up @@ -1297,7 +1291,7 @@ var PDFViewerApplication = {
alertNotReady = true;
} else {
for (i = 0, ii = this.pagesCount; i < ii; ++i) {
if (!this.getPageView(i).pdfPage) {
if (!this.pdfViewer.getPageView(i).pdfPage) {
alertNotReady = true;
break;
}
Expand All @@ -1316,7 +1310,7 @@ var PDFViewerApplication = {
var body = document.querySelector('body');
body.setAttribute('data-mozPrintCallback', true);
for (i = 0, ii = this.pagesCount; i < ii; ++i) {
this.getPageView(i).beforePrint();
this.pdfViewer.getPageView(i).beforePrint();
}

//#if (FIREFOX || MOZCENTRAL)
Expand All @@ -1343,17 +1337,15 @@ var PDFViewerApplication = {
},

rotatePages: function pdfViewRotatePages(delta) {
var currentPage = this.getPageView(this.page - 1);
var pageNumber = this.page;

this.pageRotation = (this.pageRotation + 360 + delta) % 360;
this.pdfViewer.pagesRotation = this.pageRotation;
this.pdfThumbnailViewer.pagesRotation = this.pageRotation;

this.forceRendering();

if (currentPage) {
currentPage.scrollIntoView();
}
this.pdfViewer.scrollPageIntoView(pageNumber);
},

/**
Expand Down Expand Up @@ -1949,7 +1941,7 @@ window.addEventListener('pagechange', function pagechange(evt) {
if (this.loading && page === 1) {
return;
}
PDFViewerApplication.getPageView(page - 1).scrollIntoView();
PDFViewerApplication.pdfViewer.scrollPageIntoView(page);
}, true);

function handleMouseWheel(evt) {
Expand Down

0 comments on commit 7059358

Please sign in to comment.