toolbar.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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) {
  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.addEventListener("click", function (evt) {
  141. const target = evt.target;
  142. if (this.value === self.pageScaleValue && target.tagName.toUpperCase() === "OPTION") {
  143. this.blur();
  144. }
  145. });
  146. scaleSelect.oncontextmenu = _ui_utils.noContextMenuHandler;
  147. this.eventBus._on("localized", () => {
  148. this._wasLocalized = true;
  149. this._adjustScaleWidth();
  150. this._updateUIState(true);
  151. });
  152. }
  153. _updateUIState(resetNumPages = false) {
  154. if (!this._wasLocalized) {
  155. return;
  156. }
  157. const {
  158. pageNumber,
  159. pagesCount,
  160. pageScaleValue,
  161. pageScale,
  162. items
  163. } = this;
  164. if (resetNumPages) {
  165. if (this.hasPageLabels) {
  166. items.pageNumber.type = "text";
  167. } else {
  168. items.pageNumber.type = "number";
  169. this.l10n.get("of_pages", {
  170. pagesCount
  171. }).then(msg => {
  172. items.numPages.textContent = msg;
  173. });
  174. }
  175. items.pageNumber.max = pagesCount;
  176. }
  177. if (this.hasPageLabels) {
  178. items.pageNumber.value = this.pageLabel;
  179. this.l10n.get("page_of_pages", {
  180. pageNumber,
  181. pagesCount
  182. }).then(msg => {
  183. items.numPages.textContent = msg;
  184. });
  185. } else {
  186. items.pageNumber.value = pageNumber;
  187. }
  188. items.previous.disabled = pageNumber <= 1;
  189. items.next.disabled = pageNumber >= pagesCount;
  190. items.zoomOut.disabled = pageScale <= _ui_utils.MIN_SCALE;
  191. items.zoomIn.disabled = pageScale >= _ui_utils.MAX_SCALE;
  192. this.l10n.get("page_scale_percent", {
  193. scale: Math.round(pageScale * 10000) / 100
  194. }).then(msg => {
  195. let predefinedValueFound = false;
  196. for (const option of items.scaleSelect.options) {
  197. if (option.value !== pageScaleValue) {
  198. option.selected = false;
  199. continue;
  200. }
  201. option.selected = true;
  202. predefinedValueFound = true;
  203. }
  204. if (!predefinedValueFound) {
  205. items.customScaleOption.textContent = msg;
  206. items.customScaleOption.selected = true;
  207. }
  208. });
  209. }
  210. updateLoadingIndicatorState(loading = false) {
  211. const pageNumberInput = this.items.pageNumber;
  212. pageNumberInput.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
  213. }
  214. async _adjustScaleWidth() {
  215. const {
  216. items,
  217. l10n
  218. } = this;
  219. const predefinedValuesPromise = Promise.all([l10n.get("page_scale_auto"), l10n.get("page_scale_actual"), l10n.get("page_scale_fit"), l10n.get("page_scale_width")]);
  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. const {
  227. fontSize,
  228. fontFamily
  229. } = getComputedStyle(items.scaleSelect);
  230. ctx.font = `${fontSize} ${fontFamily}`;
  231. let maxWidth = 0;
  232. for (const predefinedValue of await predefinedValuesPromise) {
  233. const {
  234. width
  235. } = ctx.measureText(predefinedValue);
  236. if (width > maxWidth) {
  237. maxWidth = width;
  238. }
  239. }
  240. const overflow = SCALE_SELECT_WIDTH - SCALE_SELECT_CONTAINER_WIDTH;
  241. maxWidth += 2 * overflow;
  242. if (maxWidth > SCALE_SELECT_CONTAINER_WIDTH) {
  243. items.scaleSelect.style.width = `${maxWidth + overflow}px`;
  244. items.scaleSelectContainer.style.width = `${maxWidth}px`;
  245. }
  246. canvas.width = 0;
  247. canvas.height = 0;
  248. canvas = ctx = null;
  249. }
  250. }
  251. exports.Toolbar = Toolbar;