2
0

toolbar.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. var _pdf = require("../pdf");
  29. const PAGE_NUMBER_LOADING_INDICATOR = "visiblePageIsLoading";
  30. class Toolbar {
  31. #wasLocalized = false;
  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.print,
  50. eventName: "print"
  51. }, {
  52. element: options.presentationModeButton,
  53. eventName: "presentationmode"
  54. }, {
  55. element: options.download,
  56. eventName: "download"
  57. }, {
  58. element: options.viewBookmark,
  59. eventName: null
  60. }, {
  61. element: options.editorFreeTextButton,
  62. eventName: "switchannotationeditormode",
  63. eventDetails: {
  64. get mode() {
  65. const {
  66. classList
  67. } = options.editorFreeTextButton;
  68. return classList.contains("toggled") ? _pdf.AnnotationEditorType.NONE : _pdf.AnnotationEditorType.FREETEXT;
  69. }
  70. }
  71. }, {
  72. element: options.editorInkButton,
  73. eventName: "switchannotationeditormode",
  74. eventDetails: {
  75. get mode() {
  76. const {
  77. classList
  78. } = options.editorInkButton;
  79. return classList.contains("toggled") ? _pdf.AnnotationEditorType.NONE : _pdf.AnnotationEditorType.INK;
  80. }
  81. }
  82. }];
  83. this.buttons.push({
  84. element: options.openFile,
  85. eventName: "openfile"
  86. });
  87. this.items = {
  88. numPages: options.numPages,
  89. pageNumber: options.pageNumber,
  90. scaleSelect: options.scaleSelect,
  91. customScaleOption: options.customScaleOption,
  92. previous: options.previous,
  93. next: options.next,
  94. zoomIn: options.zoomIn,
  95. zoomOut: options.zoomOut
  96. };
  97. this.#bindListeners(options);
  98. this.reset();
  99. }
  100. setPageNumber(pageNumber, pageLabel) {
  101. this.pageNumber = pageNumber;
  102. this.pageLabel = pageLabel;
  103. this.#updateUIState(false);
  104. }
  105. setPagesCount(pagesCount, hasPageLabels) {
  106. this.pagesCount = pagesCount;
  107. this.hasPageLabels = hasPageLabels;
  108. this.#updateUIState(true);
  109. }
  110. setPageScale(pageScaleValue, pageScale) {
  111. this.pageScaleValue = (pageScaleValue || pageScale).toString();
  112. this.pageScale = pageScale;
  113. this.#updateUIState(false);
  114. }
  115. reset() {
  116. this.pageNumber = 0;
  117. this.pageLabel = null;
  118. this.hasPageLabels = false;
  119. this.pagesCount = 0;
  120. this.pageScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  121. this.pageScale = _ui_utils.DEFAULT_SCALE;
  122. this.#updateUIState(true);
  123. this.updateLoadingIndicatorState();
  124. this.eventBus.dispatch("toolbarreset", {
  125. source: this
  126. });
  127. }
  128. #bindListeners(options) {
  129. const {
  130. pageNumber,
  131. scaleSelect
  132. } = this.items;
  133. const self = this;
  134. for (const {
  135. element,
  136. eventName,
  137. eventDetails
  138. } of this.buttons) {
  139. element.addEventListener("click", evt => {
  140. if (eventName !== null) {
  141. const details = {
  142. source: this
  143. };
  144. if (eventDetails) {
  145. for (const property in eventDetails) {
  146. details[property] = eventDetails[property];
  147. }
  148. }
  149. this.eventBus.dispatch(eventName, details);
  150. }
  151. });
  152. }
  153. pageNumber.addEventListener("click", function () {
  154. this.select();
  155. });
  156. pageNumber.addEventListener("change", function () {
  157. self.eventBus.dispatch("pagenumberchanged", {
  158. source: self,
  159. value: this.value
  160. });
  161. });
  162. scaleSelect.addEventListener("change", function () {
  163. if (this.value === "custom") {
  164. return;
  165. }
  166. self.eventBus.dispatch("scalechanged", {
  167. source: self,
  168. value: this.value
  169. });
  170. });
  171. scaleSelect.addEventListener("click", function (evt) {
  172. const target = evt.target;
  173. if (this.value === self.pageScaleValue && target.tagName.toUpperCase() === "OPTION") {
  174. this.blur();
  175. }
  176. });
  177. scaleSelect.oncontextmenu = _ui_utils.noContextMenuHandler;
  178. this.eventBus._on("localized", () => {
  179. this.#wasLocalized = true;
  180. this.#adjustScaleWidth();
  181. this.#updateUIState(true);
  182. });
  183. this.#bindEditorToolsListener(options);
  184. }
  185. #bindEditorToolsListener({
  186. editorFreeTextButton,
  187. editorFreeTextParamsToolbar,
  188. editorInkButton,
  189. editorInkParamsToolbar
  190. }) {
  191. const editorModeChanged = (evt, disableButtons = false) => {
  192. const editorButtons = [{
  193. mode: _pdf.AnnotationEditorType.FREETEXT,
  194. button: editorFreeTextButton,
  195. toolbar: editorFreeTextParamsToolbar
  196. }, {
  197. mode: _pdf.AnnotationEditorType.INK,
  198. button: editorInkButton,
  199. toolbar: editorInkParamsToolbar
  200. }];
  201. for (const {
  202. mode,
  203. button,
  204. toolbar
  205. } of editorButtons) {
  206. const checked = mode === evt.mode;
  207. button.classList.toggle("toggled", checked);
  208. button.setAttribute("aria-checked", checked);
  209. button.disabled = disableButtons;
  210. toolbar?.classList.toggle("hidden", !checked);
  211. }
  212. };
  213. this.eventBus._on("annotationeditormodechanged", editorModeChanged);
  214. this.eventBus._on("toolbarreset", evt => {
  215. if (evt.source === this) {
  216. editorModeChanged({
  217. mode: _pdf.AnnotationEditorType.NONE
  218. }, true);
  219. }
  220. });
  221. }
  222. #updateUIState(resetNumPages = false) {
  223. if (!this.#wasLocalized) {
  224. return;
  225. }
  226. const {
  227. pageNumber,
  228. pagesCount,
  229. pageScaleValue,
  230. pageScale,
  231. items
  232. } = this;
  233. if (resetNumPages) {
  234. if (this.hasPageLabels) {
  235. items.pageNumber.type = "text";
  236. } else {
  237. items.pageNumber.type = "number";
  238. this.l10n.get("of_pages", {
  239. pagesCount
  240. }).then(msg => {
  241. items.numPages.textContent = msg;
  242. });
  243. }
  244. items.pageNumber.max = pagesCount;
  245. }
  246. if (this.hasPageLabels) {
  247. items.pageNumber.value = this.pageLabel;
  248. this.l10n.get("page_of_pages", {
  249. pageNumber,
  250. pagesCount
  251. }).then(msg => {
  252. items.numPages.textContent = msg;
  253. });
  254. } else {
  255. items.pageNumber.value = pageNumber;
  256. }
  257. items.previous.disabled = pageNumber <= 1;
  258. items.next.disabled = pageNumber >= pagesCount;
  259. items.zoomOut.disabled = pageScale <= _ui_utils.MIN_SCALE;
  260. items.zoomIn.disabled = pageScale >= _ui_utils.MAX_SCALE;
  261. this.l10n.get("page_scale_percent", {
  262. scale: Math.round(pageScale * 10000) / 100
  263. }).then(msg => {
  264. let predefinedValueFound = false;
  265. for (const option of items.scaleSelect.options) {
  266. if (option.value !== pageScaleValue) {
  267. option.selected = false;
  268. continue;
  269. }
  270. option.selected = true;
  271. predefinedValueFound = true;
  272. }
  273. if (!predefinedValueFound) {
  274. items.customScaleOption.textContent = msg;
  275. items.customScaleOption.selected = true;
  276. }
  277. });
  278. }
  279. updateLoadingIndicatorState(loading = false) {
  280. const {
  281. pageNumber
  282. } = this.items;
  283. pageNumber.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
  284. }
  285. async #adjustScaleWidth() {
  286. const {
  287. items,
  288. l10n
  289. } = this;
  290. const predefinedValuesPromise = Promise.all([l10n.get("page_scale_auto"), l10n.get("page_scale_actual"), l10n.get("page_scale_fit"), l10n.get("page_scale_width")]);
  291. await _ui_utils.animationStarted;
  292. const style = getComputedStyle(items.scaleSelect),
  293. scaleSelectContainerWidth = parseInt(style.getPropertyValue("--scale-select-container-width"), 10),
  294. scaleSelectOverflow = parseInt(style.getPropertyValue("--scale-select-overflow"), 10);
  295. const canvas = document.createElement("canvas");
  296. const ctx = canvas.getContext("2d", {
  297. alpha: false
  298. });
  299. ctx.font = `${style.fontSize} ${style.fontFamily}`;
  300. let maxWidth = 0;
  301. for (const predefinedValue of await predefinedValuesPromise) {
  302. const {
  303. width
  304. } = ctx.measureText(predefinedValue);
  305. if (width > maxWidth) {
  306. maxWidth = width;
  307. }
  308. }
  309. maxWidth += 2 * scaleSelectOverflow;
  310. if (maxWidth > scaleSelectContainerWidth) {
  311. _ui_utils.docStyle.setProperty("--scale-select-container-width", `${maxWidth}px`);
  312. }
  313. canvas.width = 0;
  314. canvas.height = 0;
  315. }
  316. }
  317. exports.Toolbar = Toolbar;