2
0

toolbar.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.Toolbar = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. const PAGE_NUMBER_LOADING_INDICATOR = "visiblePageIsLoading";
  29. const SCALE_SELECT_CONTAINER_WIDTH = 140;
  30. const SCALE_SELECT_WIDTH = 162;
  31. class Toolbar {
  32. constructor(options, eventBus, l10n = _ui_utils.NullL10n) {
  33. this.toolbar = options.container;
  34. this.eventBus = eventBus;
  35. this.l10n = l10n;
  36. this.buttons = [{
  37. element: options.previous,
  38. eventName: "previouspage"
  39. }, {
  40. element: options.next,
  41. eventName: "nextpage"
  42. }, {
  43. element: options.zoomIn,
  44. eventName: "zoomin"
  45. }, {
  46. element: options.zoomOut,
  47. eventName: "zoomout"
  48. }, {
  49. element: options.openFile,
  50. eventName: "openfile"
  51. }, {
  52. element: options.print,
  53. eventName: "print"
  54. }, {
  55. element: options.presentationModeButton,
  56. eventName: "presentationmode"
  57. }, {
  58. element: options.download,
  59. eventName: "download"
  60. }, {
  61. element: options.viewBookmark,
  62. eventName: null
  63. }];
  64. this.items = {
  65. numPages: options.numPages,
  66. pageNumber: options.pageNumber,
  67. scaleSelectContainer: options.scaleSelectContainer,
  68. scaleSelect: options.scaleSelect,
  69. customScaleOption: options.customScaleOption,
  70. previous: options.previous,
  71. next: options.next,
  72. zoomIn: options.zoomIn,
  73. zoomOut: options.zoomOut
  74. };
  75. this._wasLocalized = false;
  76. this.reset();
  77. this._bindListeners();
  78. }
  79. setPageNumber(pageNumber, pageLabel) {
  80. this.pageNumber = pageNumber;
  81. this.pageLabel = pageLabel;
  82. this._updateUIState(false);
  83. }
  84. setPagesCount(pagesCount, hasPageLabels) {
  85. this.pagesCount = pagesCount;
  86. this.hasPageLabels = hasPageLabels;
  87. this._updateUIState(true);
  88. }
  89. setPageScale(pageScaleValue, pageScale) {
  90. this.pageScaleValue = (pageScaleValue || pageScale).toString();
  91. this.pageScale = pageScale;
  92. this._updateUIState(false);
  93. }
  94. reset() {
  95. this.pageNumber = 0;
  96. this.pageLabel = null;
  97. this.hasPageLabels = false;
  98. this.pagesCount = 0;
  99. this.pageScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  100. this.pageScale = _ui_utils.DEFAULT_SCALE;
  101. this._updateUIState(true);
  102. this.updateLoadingIndicatorState();
  103. }
  104. _bindListeners() {
  105. const {
  106. pageNumber,
  107. scaleSelect
  108. } = this.items;
  109. const self = this;
  110. for (const {
  111. element,
  112. eventName
  113. } of this.buttons) {
  114. element.addEventListener("click", evt => {
  115. if (eventName !== null) {
  116. this.eventBus.dispatch(eventName, {
  117. source: this
  118. });
  119. }
  120. });
  121. }
  122. pageNumber.addEventListener("click", function () {
  123. this.select();
  124. });
  125. pageNumber.addEventListener("change", function () {
  126. self.eventBus.dispatch("pagenumberchanged", {
  127. source: self,
  128. value: this.value
  129. });
  130. });
  131. scaleSelect.addEventListener("change", function () {
  132. if (this.value === "custom") {
  133. return;
  134. }
  135. self.eventBus.dispatch("scalechanged", {
  136. source: self,
  137. value: this.value
  138. });
  139. });
  140. scaleSelect.oncontextmenu = _ui_utils.noContextMenuHandler;
  141. this.eventBus._on("localized", () => {
  142. this._wasLocalized = true;
  143. this._adjustScaleWidth();
  144. this._updateUIState(true);
  145. });
  146. }
  147. _updateUIState(resetNumPages = false) {
  148. if (!this._wasLocalized) {
  149. return;
  150. }
  151. const {
  152. pageNumber,
  153. pagesCount,
  154. pageScaleValue,
  155. pageScale,
  156. items
  157. } = this;
  158. if (resetNumPages) {
  159. if (this.hasPageLabels) {
  160. items.pageNumber.type = "text";
  161. } else {
  162. items.pageNumber.type = "number";
  163. this.l10n.get("of_pages", {
  164. pagesCount
  165. }, "of {{pagesCount}}").then(msg => {
  166. items.numPages.textContent = msg;
  167. });
  168. }
  169. items.pageNumber.max = pagesCount;
  170. }
  171. if (this.hasPageLabels) {
  172. items.pageNumber.value = this.pageLabel;
  173. this.l10n.get("page_of_pages", {
  174. pageNumber,
  175. pagesCount
  176. }, "({{pageNumber}} of {{pagesCount}})").then(msg => {
  177. items.numPages.textContent = msg;
  178. });
  179. } else {
  180. items.pageNumber.value = pageNumber;
  181. }
  182. items.previous.disabled = pageNumber <= 1;
  183. items.next.disabled = pageNumber >= pagesCount;
  184. items.zoomOut.disabled = pageScale <= _ui_utils.MIN_SCALE;
  185. items.zoomIn.disabled = pageScale >= _ui_utils.MAX_SCALE;
  186. const customScale = Math.round(pageScale * 10000) / 100;
  187. this.l10n.get("page_scale_percent", {
  188. scale: customScale
  189. }, "{{scale}}%").then(msg => {
  190. let predefinedValueFound = false;
  191. for (const option of items.scaleSelect.options) {
  192. if (option.value !== pageScaleValue) {
  193. option.selected = false;
  194. continue;
  195. }
  196. option.selected = true;
  197. predefinedValueFound = true;
  198. }
  199. if (!predefinedValueFound) {
  200. items.customScaleOption.textContent = msg;
  201. items.customScaleOption.selected = true;
  202. }
  203. });
  204. }
  205. updateLoadingIndicatorState(loading = false) {
  206. const pageNumberInput = this.items.pageNumber;
  207. pageNumberInput.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
  208. }
  209. async _adjustScaleWidth() {
  210. const {
  211. items,
  212. l10n
  213. } = this;
  214. const predefinedValuesPromise = Promise.all([l10n.get("page_scale_auto", null, "Automatic Zoom"), l10n.get("page_scale_actual", null, "Actual Size"), l10n.get("page_scale_fit", null, "Page Fit"), l10n.get("page_scale_width", null, "Page Width")]);
  215. let canvas = document.createElement("canvas");
  216. canvas.mozOpaque = true;
  217. let ctx = canvas.getContext("2d", {
  218. alpha: false
  219. });
  220. await _ui_utils.animationStarted;
  221. const {
  222. fontSize,
  223. fontFamily
  224. } = getComputedStyle(items.scaleSelect);
  225. ctx.font = `${fontSize} ${fontFamily}`;
  226. let maxWidth = 0;
  227. for (const predefinedValue of await predefinedValuesPromise) {
  228. const {
  229. width
  230. } = ctx.measureText(predefinedValue);
  231. if (width > maxWidth) {
  232. maxWidth = width;
  233. }
  234. }
  235. const overflow = SCALE_SELECT_WIDTH - SCALE_SELECT_CONTAINER_WIDTH;
  236. maxWidth += 2 * overflow;
  237. if (maxWidth > SCALE_SELECT_CONTAINER_WIDTH) {
  238. items.scaleSelect.style.width = `${maxWidth + overflow}px`;
  239. items.scaleSelectContainer.style.width = `${maxWidth}px`;
  240. }
  241. canvas.width = 0;
  242. canvas.height = 0;
  243. canvas = ctx = null;
  244. }
  245. }
  246. exports.Toolbar = Toolbar;