toolbar.js 7.3 KB

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