UNPKG

8.76 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2019 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.Toolbar = void 0;
28
29var _ui_utils = require("./ui_utils");
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
34
35function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
36
37var PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading';
38var SCALE_SELECT_CONTAINER_PADDING = 8;
39var SCALE_SELECT_PADDING = 22;
40
41var Toolbar =
42/*#__PURE__*/
43function () {
44 function Toolbar(options, eventBus) {
45 var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
46
47 _classCallCheck(this, Toolbar);
48
49 this.toolbar = options.container;
50 this.eventBus = eventBus;
51 this.l10n = l10n;
52 this.items = options;
53 this._wasLocalized = false;
54 this.reset();
55
56 this._bindListeners();
57 }
58
59 _createClass(Toolbar, [{
60 key: "setPageNumber",
61 value: function setPageNumber(pageNumber, pageLabel) {
62 this.pageNumber = pageNumber;
63 this.pageLabel = pageLabel;
64
65 this._updateUIState(false);
66 }
67 }, {
68 key: "setPagesCount",
69 value: function setPagesCount(pagesCount, hasPageLabels) {
70 this.pagesCount = pagesCount;
71 this.hasPageLabels = hasPageLabels;
72
73 this._updateUIState(true);
74 }
75 }, {
76 key: "setPageScale",
77 value: function setPageScale(pageScaleValue, pageScale) {
78 this.pageScaleValue = (pageScaleValue || pageScale).toString();
79 this.pageScale = pageScale;
80
81 this._updateUIState(false);
82 }
83 }, {
84 key: "reset",
85 value: function reset() {
86 this.pageNumber = 0;
87 this.pageLabel = null;
88 this.hasPageLabels = false;
89 this.pagesCount = 0;
90 this.pageScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
91 this.pageScale = _ui_utils.DEFAULT_SCALE;
92
93 this._updateUIState(true);
94 }
95 }, {
96 key: "_bindListeners",
97 value: function _bindListeners() {
98 var _this = this;
99
100 var eventBus = this.eventBus,
101 items = this.items;
102 var self = this;
103 items.previous.addEventListener('click', function () {
104 eventBus.dispatch('previouspage', {
105 source: self
106 });
107 });
108 items.next.addEventListener('click', function () {
109 eventBus.dispatch('nextpage', {
110 source: self
111 });
112 });
113 items.zoomIn.addEventListener('click', function () {
114 eventBus.dispatch('zoomin', {
115 source: self
116 });
117 });
118 items.zoomOut.addEventListener('click', function () {
119 eventBus.dispatch('zoomout', {
120 source: self
121 });
122 });
123 items.pageNumber.addEventListener('click', function () {
124 this.select();
125 });
126 items.pageNumber.addEventListener('change', function () {
127 eventBus.dispatch('pagenumberchanged', {
128 source: self,
129 value: this.value
130 });
131 });
132 items.scaleSelect.addEventListener('change', function () {
133 if (this.value === 'custom') {
134 return;
135 }
136
137 eventBus.dispatch('scalechanged', {
138 source: self,
139 value: this.value
140 });
141 });
142 items.presentationModeButton.addEventListener('click', function () {
143 eventBus.dispatch('presentationmode', {
144 source: self
145 });
146 });
147 items.openFile.addEventListener('click', function () {
148 eventBus.dispatch('openfile', {
149 source: self
150 });
151 });
152 items.print.addEventListener('click', function () {
153 eventBus.dispatch('print', {
154 source: self
155 });
156 });
157 items.download.addEventListener('click', function () {
158 eventBus.dispatch('download', {
159 source: self
160 });
161 });
162 items.scaleSelect.oncontextmenu = _ui_utils.noContextMenuHandler;
163 eventBus.on('localized', function () {
164 _this._localized();
165 });
166 }
167 }, {
168 key: "_localized",
169 value: function _localized() {
170 this._wasLocalized = true;
171
172 this._adjustScaleWidth();
173
174 this._updateUIState(true);
175 }
176 }, {
177 key: "_updateUIState",
178 value: function _updateUIState() {
179 var resetNumPages = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
180
181 if (!this._wasLocalized) {
182 return;
183 }
184
185 var pageNumber = this.pageNumber,
186 pagesCount = this.pagesCount,
187 pageScaleValue = this.pageScaleValue,
188 pageScale = this.pageScale,
189 items = this.items;
190
191 if (resetNumPages) {
192 if (this.hasPageLabels) {
193 items.pageNumber.type = 'text';
194 } else {
195 items.pageNumber.type = 'number';
196 this.l10n.get('of_pages', {
197 pagesCount: pagesCount
198 }, 'of {{pagesCount}}').then(function (msg) {
199 items.numPages.textContent = msg;
200 });
201 }
202
203 items.pageNumber.max = pagesCount;
204 }
205
206 if (this.hasPageLabels) {
207 items.pageNumber.value = this.pageLabel;
208 this.l10n.get('page_of_pages', {
209 pageNumber: pageNumber,
210 pagesCount: pagesCount
211 }, '({{pageNumber}} of {{pagesCount}})').then(function (msg) {
212 items.numPages.textContent = msg;
213 });
214 } else {
215 items.pageNumber.value = pageNumber;
216 }
217
218 items.previous.disabled = pageNumber <= 1;
219 items.next.disabled = pageNumber >= pagesCount;
220 items.zoomOut.disabled = pageScale <= _ui_utils.MIN_SCALE;
221 items.zoomIn.disabled = pageScale >= _ui_utils.MAX_SCALE;
222 var customScale = Math.round(pageScale * 10000) / 100;
223 this.l10n.get('page_scale_percent', {
224 scale: customScale
225 }, '{{scale}}%').then(function (msg) {
226 var options = items.scaleSelect.options;
227 var predefinedValueFound = false;
228
229 for (var i = 0, ii = options.length; i < ii; i++) {
230 var option = options[i];
231
232 if (option.value !== pageScaleValue) {
233 option.selected = false;
234 continue;
235 }
236
237 option.selected = true;
238 predefinedValueFound = true;
239 }
240
241 if (!predefinedValueFound) {
242 items.customScaleOption.textContent = msg;
243 items.customScaleOption.selected = true;
244 }
245 });
246 }
247 }, {
248 key: "updateLoadingIndicatorState",
249 value: function updateLoadingIndicatorState() {
250 var loading = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
251 var pageNumberInput = this.items.pageNumber;
252 pageNumberInput.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
253 }
254 }, {
255 key: "_adjustScaleWidth",
256 value: function _adjustScaleWidth() {
257 var container = this.items.scaleSelectContainer;
258 var select = this.items.scaleSelect;
259
260 _ui_utils.animationStarted.then(function () {
261 if (container.clientWidth === 0) {
262 container.setAttribute('style', 'display: inherit;');
263 }
264
265 if (container.clientWidth > 0) {
266 select.setAttribute('style', 'min-width: inherit;');
267 var width = select.clientWidth + SCALE_SELECT_CONTAINER_PADDING;
268 select.setAttribute('style', 'min-width: ' + (width + SCALE_SELECT_PADDING) + 'px;');
269 container.setAttribute('style', 'min-width: ' + width + 'px; ' + 'max-width: ' + width + 'px;');
270 }
271 });
272 }
273 }]);
274
275 return Toolbar;
276}();
277
278exports.Toolbar = Toolbar;
\No newline at end of file