toolbar.js 7.2 KB

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