2016-11-19 03:50:29 +09:00
|
|
|
/* Copyright 2016 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
import {
|
2017-05-04 10:05:53 +09:00
|
|
|
animationStarted, DEFAULT_SCALE, DEFAULT_SCALE_VALUE, MAX_SCALE,
|
|
|
|
MIN_SCALE, noContextMenuHandler, NullL10n
|
2017-04-15 00:32:36 +09:00
|
|
|
} from './ui_utils';
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
const PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading';
|
|
|
|
const SCALE_SELECT_CONTAINER_PADDING = 8;
|
|
|
|
const SCALE_SELECT_PADDING = 22;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} ToolbarOptions
|
|
|
|
* @property {HTMLDivElement} container - Container for the secondary toolbar.
|
|
|
|
* @property {HTMLSpanElement} numPages - Label that contains number of pages.
|
|
|
|
* @property {HTMLInputElement} pageNumber - Control for display and user input
|
|
|
|
* of the current page number.
|
|
|
|
* @property {HTMLSpanElement} scaleSelectContainer - Container where scale
|
|
|
|
* controls are placed. The width is adjusted on UI initialization.
|
|
|
|
* @property {HTMLSelectElement} scaleSelect - Scale selection control.
|
|
|
|
* @property {HTMLOptionElement} customScaleOption - The item used to display
|
|
|
|
* a non-predefined scale.
|
|
|
|
* @property {HTMLButtonElement} previous - Button to go to the previous page.
|
|
|
|
* @property {HTMLButtonElement} next - Button to go to the next page.
|
|
|
|
* @property {HTMLButtonElement} zoomIn - Button to zoom in the pages.
|
|
|
|
* @property {HTMLButtonElement} zoomOut - Button to zoom out the pages.
|
|
|
|
* @property {HTMLButtonElement} viewFind - Button to open find bar.
|
|
|
|
* @property {HTMLButtonElement} openFile - Button to open a new document.
|
|
|
|
* @property {HTMLButtonElement} presentationModeButton - Button to switch to
|
|
|
|
* presentation mode.
|
|
|
|
* @property {HTMLButtonElement} download - Button to download the document.
|
|
|
|
* @property {HTMLAElement} viewBookmark - Element to link current url of
|
|
|
|
* the page view.
|
|
|
|
*/
|
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
class Toolbar {
|
2016-11-19 03:50:29 +09:00
|
|
|
/**
|
|
|
|
* @param {ToolbarOptions} options
|
|
|
|
* @param {EventBus} eventBus
|
2017-05-04 10:05:53 +09:00
|
|
|
* @param {IL10n} l10n - Localization service.
|
2016-11-19 03:50:29 +09:00
|
|
|
*/
|
2018-10-05 17:17:37 +09:00
|
|
|
constructor(options, eventBus, l10n = NullL10n) {
|
2016-11-19 03:50:29 +09:00
|
|
|
this.toolbar = options.container;
|
|
|
|
this.eventBus = eventBus;
|
2017-05-04 10:05:53 +09:00
|
|
|
this.l10n = l10n;
|
2016-11-19 03:50:29 +09:00
|
|
|
this.items = options;
|
|
|
|
|
|
|
|
this._wasLocalized = false;
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
// Bind the event listeners for click and hand tool actions.
|
|
|
|
this._bindListeners();
|
|
|
|
}
|
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
setPageNumber(pageNumber, pageLabel) {
|
|
|
|
this.pageNumber = pageNumber;
|
|
|
|
this.pageLabel = pageLabel;
|
|
|
|
this._updateUIState(false);
|
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
setPagesCount(pagesCount, hasPageLabels) {
|
|
|
|
this.pagesCount = pagesCount;
|
|
|
|
this.hasPageLabels = hasPageLabels;
|
|
|
|
this._updateUIState(true);
|
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
setPageScale(pageScaleValue, pageScale) {
|
2018-10-05 19:13:48 +09:00
|
|
|
this.pageScaleValue = (pageScaleValue || pageScale).toString();
|
2017-06-18 06:07:17 +09:00
|
|
|
this.pageScale = pageScale;
|
|
|
|
this._updateUIState(false);
|
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
reset() {
|
|
|
|
this.pageNumber = 0;
|
|
|
|
this.pageLabel = null;
|
|
|
|
this.hasPageLabels = false;
|
|
|
|
this.pagesCount = 0;
|
|
|
|
this.pageScaleValue = DEFAULT_SCALE_VALUE;
|
|
|
|
this.pageScale = DEFAULT_SCALE;
|
|
|
|
this._updateUIState(true);
|
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
_bindListeners() {
|
2017-06-26 19:56:33 +09:00
|
|
|
let { eventBus, items, } = this;
|
2017-06-18 06:07:17 +09:00
|
|
|
let self = this;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.previous.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('previouspage', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.next.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('nextpage', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.zoomIn.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('zoomin', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.zoomOut.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('zoomout', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
items.pageNumber.addEventListener('click', function() {
|
|
|
|
this.select();
|
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.pageNumber.addEventListener('change', function() {
|
|
|
|
eventBus.dispatch('pagenumberchanged', {
|
|
|
|
source: self,
|
|
|
|
value: this.value,
|
2016-11-19 03:50:29 +09:00
|
|
|
});
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.scaleSelect.addEventListener('change', function() {
|
|
|
|
if (this.value === 'custom') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
eventBus.dispatch('scalechanged', {
|
|
|
|
source: self,
|
|
|
|
value: this.value,
|
2016-11-19 03:50:29 +09:00
|
|
|
});
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.presentationModeButton.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('presentationmode', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.openFile.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('openfile', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.print.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('print', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.download.addEventListener('click', function() {
|
2018-08-28 16:56:26 +09:00
|
|
|
eventBus.dispatch('download', { source: self, });
|
2017-06-18 06:07:17 +09:00
|
|
|
});
|
2017-05-04 10:05:53 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
// Suppress context menus for some controls.
|
|
|
|
items.scaleSelect.oncontextmenu = noContextMenuHandler;
|
|
|
|
|
|
|
|
eventBus.on('localized', () => {
|
|
|
|
this._localized();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
_localized() {
|
|
|
|
this._wasLocalized = true;
|
|
|
|
this._adjustScaleWidth();
|
|
|
|
this._updateUIState(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
_updateUIState(resetNumPages = false) {
|
|
|
|
if (!this._wasLocalized) {
|
|
|
|
// Don't update the UI state until we localize the toolbar.
|
|
|
|
return;
|
|
|
|
}
|
2018-10-05 19:13:48 +09:00
|
|
|
const { pageNumber, pagesCount, pageScaleValue, pageScale, items, } = this;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
if (resetNumPages) {
|
2016-11-19 03:50:29 +09:00
|
|
|
if (this.hasPageLabels) {
|
2017-06-18 06:07:17 +09:00
|
|
|
items.pageNumber.type = 'text';
|
|
|
|
} else {
|
|
|
|
items.pageNumber.type = 'number';
|
|
|
|
this.l10n.get('of_pages', { pagesCount, }, 'of {{pagesCount}}').
|
2017-05-04 10:05:53 +09:00
|
|
|
then((msg) => {
|
|
|
|
items.numPages.textContent = msg;
|
|
|
|
});
|
2016-11-19 03:50:29 +09:00
|
|
|
}
|
2017-06-18 06:07:17 +09:00
|
|
|
items.pageNumber.max = pagesCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.hasPageLabels) {
|
|
|
|
items.pageNumber.value = this.pageLabel;
|
|
|
|
this.l10n.get('page_of_pages', { pageNumber, pagesCount, },
|
2017-06-26 19:56:33 +09:00
|
|
|
'({{pageNumber}} of {{pagesCount}})').then((msg) => {
|
2017-06-18 06:07:17 +09:00
|
|
|
items.numPages.textContent = msg;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
items.pageNumber.value = pageNumber;
|
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
items.previous.disabled = (pageNumber <= 1);
|
|
|
|
items.next.disabled = (pageNumber >= pagesCount);
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2018-10-05 19:13:48 +09:00
|
|
|
items.zoomOut.disabled = (pageScale <= MIN_SCALE);
|
|
|
|
items.zoomIn.disabled = (pageScale >= MAX_SCALE);
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2018-10-05 19:13:48 +09:00
|
|
|
let customScale = Math.round(pageScale * 10000) / 100;
|
2017-06-26 19:56:33 +09:00
|
|
|
this.l10n.get('page_scale_percent', { scale: customScale, },
|
|
|
|
'{{scale}}%').then((msg) => {
|
|
|
|
let options = items.scaleSelect.options;
|
|
|
|
let predefinedValueFound = false;
|
|
|
|
for (let i = 0, ii = options.length; i < ii; i++) {
|
|
|
|
let option = options[i];
|
2018-10-05 19:13:48 +09:00
|
|
|
if (option.value !== pageScaleValue) {
|
2017-06-26 19:56:33 +09:00
|
|
|
option.selected = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
option.selected = true;
|
|
|
|
predefinedValueFound = true;
|
|
|
|
}
|
|
|
|
if (!predefinedValueFound) {
|
|
|
|
items.customScaleOption.textContent = msg;
|
|
|
|
items.customScaleOption.selected = true;
|
|
|
|
}
|
|
|
|
});
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
updateLoadingIndicatorState(loading = false) {
|
|
|
|
let pageNumberInput = this.items.pageNumber;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2018-10-12 22:29:45 +09:00
|
|
|
pageNumberInput.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
|
2017-06-18 06:07:17 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
_adjustScaleWidth() {
|
|
|
|
let container = this.items.scaleSelectContainer;
|
|
|
|
let select = this.items.scaleSelect;
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-06-18 06:07:17 +09:00
|
|
|
animationStarted.then(function() {
|
|
|
|
// Adjust the width of the zoom box to fit the content.
|
|
|
|
// Note: If the window is narrow enough that the zoom box is not
|
|
|
|
// visible, we temporarily show it to be able to adjust its width.
|
|
|
|
if (container.clientWidth === 0) {
|
|
|
|
container.setAttribute('style', 'display: inherit;');
|
|
|
|
}
|
|
|
|
if (container.clientWidth > 0) {
|
|
|
|
select.setAttribute('style', 'min-width: inherit;');
|
|
|
|
let width = select.clientWidth + SCALE_SELECT_CONTAINER_PADDING;
|
|
|
|
select.setAttribute('style', 'min-width: ' +
|
|
|
|
(width + SCALE_SELECT_PADDING) + 'px;');
|
|
|
|
container.setAttribute('style', 'min-width: ' + width + 'px; ' +
|
|
|
|
'max-width: ' + width + 'px;');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2016-11-19 03:50:29 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export {
|
|
|
|
Toolbar,
|
|
|
|
};
|