Consolidate updating of various Toolbar state (e.g. page and scale) in one method in PDFViewerApplication

With the changes in PR 7289, we no longer dispatch a 'pagechanging' event on load. Since most PDF documents open on the first page, this means that the `previous` and `firstPage` buttons are no longer correctly disabled.
To avoid this, this patch moves the code that updates various UI toolbar state into one method, which is then called on document initialization and from the various existing event handling functions.
This commit is contained in:
Jonas Jenwald 2016-06-27 12:32:28 +02:00
parent e908b71309
commit dd0fe10a52
2 changed files with 76 additions and 51 deletions

View File

@ -832,11 +832,9 @@ var PDFViewerApplication = {
self.loadingBar.hide();
});
var pagesCount = pdfDocument.numPages;
var toolbarConfig = this.appConfig.toolbar;
toolbarConfig.numPages.textContent =
mozL10n.get('page_of', {pageCount: pagesCount}, 'of {{pageCount}}');
toolbarConfig.pageNumber.max = pagesCount;
this._updateUIToolbar({
resetNumPages: true,
});
var id = this.documentFingerprint = pdfDocument.fingerprint;
var store = this.store = new ViewHistory(id);
@ -1049,10 +1047,6 @@ var PDFViewerApplication = {
this.isInitialViewSet = true;
// When opening a new file, when one is already loaded in the viewer,
// ensure that the 'pageNumber' element displays the correct value.
this.appConfig.toolbar.pageNumber.value = this.pdfViewer.currentPageNumber;
this.pdfSidebar.setInitialView(this.preferenceSidebarViewOnLoad ||
(sidebarView | 0));
@ -1223,6 +1217,67 @@ var PDFViewerApplication = {
this.pdfPresentationMode.mouseScroll(delta);
},
/**
* @typedef UpdateUIToolbarParameters
* @property {number} pageNumber
* @property {string} scaleValue
* @property {scale} scale
* @property {boolean} resetNumPages
*/
/**
* @param {Object} UpdateUIToolbarParameters
* @private
*/
_updateUIToolbar: function (params) {
function selectScaleOption(value, scale) {
var options = toolbarConfig.scaleSelect.options;
var predefinedValueFound = false;
for (var i = 0, ii = options.length; i < ii; i++) {
var option = options[i];
if (option.value !== value) {
option.selected = false;
continue;
}
option.selected = true;
predefinedValueFound = true;
}
if (!predefinedValueFound) {
var customScale = Math.round(scale * 10000) / 100;
toolbarConfig.customScaleOption.textContent =
mozL10n.get('page_scale_percent', {scale: customScale}, '{{scale}}%');
toolbarConfig.customScaleOption.selected = true;
}
}
var pageNumber = params.pageNumber || this.pdfViewer.currentPageNumber;
var scaleValue = (params.scaleValue || params.scale ||
this.pdfViewer.currentScaleValue || DEFAULT_SCALE_VALUE).toString();
var scale = params.scale || this.pdfViewer.currentScale;
var resetNumPages = params.resetNumPages || false;
var toolbarConfig = this.appConfig.toolbar;
var pagesCount = this.pagesCount;
if (resetNumPages) {
toolbarConfig.numPages.textContent =
mozL10n.get('page_of', { pageCount: pagesCount }, 'of {{pageCount}}');
toolbarConfig.pageNumber.max = pagesCount;
}
toolbarConfig.pageNumber.value = pageNumber;
toolbarConfig.previous.disabled = (pageNumber <= 1);
toolbarConfig.next.disabled = (pageNumber >= pagesCount);
toolbarConfig.firstPage.disabled = (pageNumber <= 1);
toolbarConfig.lastPage.disabled = (pageNumber >= pagesCount);
toolbarConfig.zoomOut.disabled = (scale === MIN_SCALE);
toolbarConfig.zoomIn.disabled = (scale === MAX_SCALE);
selectScaleOption(scaleValue, scale);
},
bindEvents: function pdfViewBindEvents() {
var eventBus = this.eventBus;
@ -1824,21 +1879,6 @@ function webViewerFileInputChange(e) {
}
//#endif
function selectScaleOption(value) {
var options = PDFViewerApplication.appConfig.toolbar.scaleSelect.options;
var predefinedValueFound = false;
for (var i = 0, ii = options.length; i < ii; i++) {
var option = options[i];
if (option.value !== value) {
option.selected = false;
continue;
}
option.selected = true;
predefinedValueFound = true;
}
return predefinedValueFound;
}
window.addEventListener('localized', function localized(evt) {
PDFViewerApplication.eventBus.dispatch('localized');
});
@ -1924,20 +1964,11 @@ function webViewerFindFromUrlHash(e) {
}
function webViewerScaleChanging(e) {
var appConfig = PDFViewerApplication.appConfig;
appConfig.toolbar.zoomOut.disabled = (e.scale === MIN_SCALE);
appConfig.toolbar.zoomIn.disabled = (e.scale === MAX_SCALE);
PDFViewerApplication._updateUIToolbar({
scaleValue: e.presetValue,
scale: e.scale,
});
// Update the 'scaleSelect' DOM element.
var predefinedValueFound = selectScaleOption(e.presetValue ||
'' + e.scale);
if (!predefinedValueFound) {
var customScaleOption = appConfig.toolbar.customScaleOption;
var customScale = Math.round(e.scale * 10000) / 100;
customScaleOption.textContent =
mozL10n.get('page_scale_percent', { scale: customScale }, '{{scale}}%');
customScaleOption.selected = true;
}
if (!PDFViewerApplication.initialized) {
return;
}
@ -1946,20 +1977,14 @@ function webViewerScaleChanging(e) {
function webViewerPageChanging(e) {
var page = e.pageNumber;
if (e.previousPageNumber !== page) {
PDFViewerApplication.appConfig.toolbar.pageNumber.value = page;
if (PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
PDFViewerApplication.pdfThumbnailViewer.scrollThumbnailIntoView(page);
}
PDFViewerApplication._updateUIToolbar({
pageNumber: page,
});
if (e.previousPageNumber !== page &&
PDFViewerApplication.pdfSidebar.isThumbnailViewVisible) {
PDFViewerApplication.pdfThumbnailViewer.scrollThumbnailIntoView(page);
}
var numPages = PDFViewerApplication.pagesCount;
PDFViewerApplication.appConfig.toolbar.previous.disabled = (page <= 1);
PDFViewerApplication.appConfig.toolbar.next.disabled = (page >= numPages);
PDFViewerApplication.appConfig.toolbar.firstPage.disabled = (page <= 1);
PDFViewerApplication.appConfig.toolbar.lastPage.disabled = (page >= numPages);
// we need to update stats
if (pdfjsLib.PDFJS.pdfBug && Stats.enabled) {

View File

@ -231,7 +231,7 @@ var PDFViewer = (function pdfViewer() {
set currentScaleValue(val) {
if (!this.pdfDocument) {
this._currentScale = isNaN(val) ? UNKNOWN_SCALE : val;
this._currentScaleValue = val;
this._currentScaleValue = val.toString();
return;
}
this._setScale(val, false);
@ -418,7 +418,7 @@ var PDFViewer = (function pdfViewer() {
_setScaleUpdatePages: function pdfViewer_setScaleUpdatePages(
newScale, newValue, noScroll, preset) {
this._currentScaleValue = newValue;
this._currentScaleValue = newValue.toString();
if (isSameScale(this._currentScale, newScale)) {
if (preset) {