secondary_toolbar.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.SecondaryToolbar = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. var _pdf_cursor_tools = require("./pdf_cursor_tools.js");
  29. var _pdf_viewer = require("./pdf_viewer.js");
  30. class SecondaryToolbar {
  31. constructor(options, eventBus, externalServices) {
  32. this.toolbar = options.toolbar;
  33. this.toggleButton = options.toggleButton;
  34. this.buttons = [{
  35. element: options.presentationModeButton,
  36. eventName: "presentationmode",
  37. close: true
  38. }, {
  39. element: options.printButton,
  40. eventName: "print",
  41. close: true
  42. }, {
  43. element: options.downloadButton,
  44. eventName: "download",
  45. close: true
  46. }, {
  47. element: options.viewBookmarkButton,
  48. eventName: null,
  49. close: true
  50. }, {
  51. element: options.firstPageButton,
  52. eventName: "firstpage",
  53. close: true
  54. }, {
  55. element: options.lastPageButton,
  56. eventName: "lastpage",
  57. close: true
  58. }, {
  59. element: options.pageRotateCwButton,
  60. eventName: "rotatecw",
  61. close: false
  62. }, {
  63. element: options.pageRotateCcwButton,
  64. eventName: "rotateccw",
  65. close: false
  66. }, {
  67. element: options.cursorSelectToolButton,
  68. eventName: "switchcursortool",
  69. eventDetails: {
  70. tool: _pdf_cursor_tools.CursorTool.SELECT
  71. },
  72. close: true
  73. }, {
  74. element: options.cursorHandToolButton,
  75. eventName: "switchcursortool",
  76. eventDetails: {
  77. tool: _pdf_cursor_tools.CursorTool.HAND
  78. },
  79. close: true
  80. }, {
  81. element: options.scrollPageButton,
  82. eventName: "switchscrollmode",
  83. eventDetails: {
  84. mode: _ui_utils.ScrollMode.PAGE
  85. },
  86. close: true
  87. }, {
  88. element: options.scrollVerticalButton,
  89. eventName: "switchscrollmode",
  90. eventDetails: {
  91. mode: _ui_utils.ScrollMode.VERTICAL
  92. },
  93. close: true
  94. }, {
  95. element: options.scrollHorizontalButton,
  96. eventName: "switchscrollmode",
  97. eventDetails: {
  98. mode: _ui_utils.ScrollMode.HORIZONTAL
  99. },
  100. close: true
  101. }, {
  102. element: options.scrollWrappedButton,
  103. eventName: "switchscrollmode",
  104. eventDetails: {
  105. mode: _ui_utils.ScrollMode.WRAPPED
  106. },
  107. close: true
  108. }, {
  109. element: options.spreadNoneButton,
  110. eventName: "switchspreadmode",
  111. eventDetails: {
  112. mode: _ui_utils.SpreadMode.NONE
  113. },
  114. close: true
  115. }, {
  116. element: options.spreadOddButton,
  117. eventName: "switchspreadmode",
  118. eventDetails: {
  119. mode: _ui_utils.SpreadMode.ODD
  120. },
  121. close: true
  122. }, {
  123. element: options.spreadEvenButton,
  124. eventName: "switchspreadmode",
  125. eventDetails: {
  126. mode: _ui_utils.SpreadMode.EVEN
  127. },
  128. close: true
  129. }, {
  130. element: options.documentPropertiesButton,
  131. eventName: "documentproperties",
  132. close: true
  133. }];
  134. this.buttons.push({
  135. element: options.openFileButton,
  136. eventName: "openfile",
  137. close: true
  138. });
  139. this.items = {
  140. firstPage: options.firstPageButton,
  141. lastPage: options.lastPageButton,
  142. pageRotateCw: options.pageRotateCwButton,
  143. pageRotateCcw: options.pageRotateCcwButton
  144. };
  145. this.eventBus = eventBus;
  146. this.externalServices = externalServices;
  147. this.opened = false;
  148. this.#bindClickListeners();
  149. this.#bindCursorToolsListener(options);
  150. this.#bindScrollModeListener(options);
  151. this.#bindSpreadModeListener(options);
  152. this.reset();
  153. }
  154. get isOpen() {
  155. return this.opened;
  156. }
  157. setPageNumber(pageNumber) {
  158. this.pageNumber = pageNumber;
  159. this.#updateUIState();
  160. }
  161. setPagesCount(pagesCount) {
  162. this.pagesCount = pagesCount;
  163. this.#updateUIState();
  164. }
  165. reset() {
  166. this.pageNumber = 0;
  167. this.pagesCount = 0;
  168. this.#updateUIState();
  169. this.eventBus.dispatch("secondarytoolbarreset", {
  170. source: this
  171. });
  172. }
  173. #updateUIState() {
  174. this.items.firstPage.disabled = this.pageNumber <= 1;
  175. this.items.lastPage.disabled = this.pageNumber >= this.pagesCount;
  176. this.items.pageRotateCw.disabled = this.pagesCount === 0;
  177. this.items.pageRotateCcw.disabled = this.pagesCount === 0;
  178. }
  179. #bindClickListeners() {
  180. this.toggleButton.addEventListener("click", this.toggle.bind(this));
  181. for (const {
  182. element,
  183. eventName,
  184. close,
  185. eventDetails
  186. } of this.buttons) {
  187. element.addEventListener("click", evt => {
  188. if (eventName !== null) {
  189. const details = {
  190. source: this
  191. };
  192. for (const property in eventDetails) {
  193. details[property] = eventDetails[property];
  194. }
  195. this.eventBus.dispatch(eventName, details);
  196. }
  197. if (close) {
  198. this.close();
  199. }
  200. this.externalServices.reportTelemetry({
  201. type: "buttons",
  202. data: {
  203. id: element.id
  204. }
  205. });
  206. });
  207. }
  208. }
  209. #bindCursorToolsListener({
  210. cursorSelectToolButton,
  211. cursorHandToolButton
  212. }) {
  213. this.eventBus._on("cursortoolchanged", function ({
  214. tool
  215. }) {
  216. const isSelect = tool === _pdf_cursor_tools.CursorTool.SELECT,
  217. isHand = tool === _pdf_cursor_tools.CursorTool.HAND;
  218. cursorSelectToolButton.classList.toggle("toggled", isSelect);
  219. cursorHandToolButton.classList.toggle("toggled", isHand);
  220. cursorSelectToolButton.setAttribute("aria-checked", isSelect);
  221. cursorHandToolButton.setAttribute("aria-checked", isHand);
  222. });
  223. }
  224. #bindScrollModeListener({
  225. scrollPageButton,
  226. scrollVerticalButton,
  227. scrollHorizontalButton,
  228. scrollWrappedButton,
  229. spreadNoneButton,
  230. spreadOddButton,
  231. spreadEvenButton
  232. }) {
  233. const scrollModeChanged = ({
  234. mode
  235. }) => {
  236. const isPage = mode === _ui_utils.ScrollMode.PAGE,
  237. isVertical = mode === _ui_utils.ScrollMode.VERTICAL,
  238. isHorizontal = mode === _ui_utils.ScrollMode.HORIZONTAL,
  239. isWrapped = mode === _ui_utils.ScrollMode.WRAPPED;
  240. scrollPageButton.classList.toggle("toggled", isPage);
  241. scrollVerticalButton.classList.toggle("toggled", isVertical);
  242. scrollHorizontalButton.classList.toggle("toggled", isHorizontal);
  243. scrollWrappedButton.classList.toggle("toggled", isWrapped);
  244. scrollPageButton.setAttribute("aria-checked", isPage);
  245. scrollVerticalButton.setAttribute("aria-checked", isVertical);
  246. scrollHorizontalButton.setAttribute("aria-checked", isHorizontal);
  247. scrollWrappedButton.setAttribute("aria-checked", isWrapped);
  248. const forceScrollModePage = this.pagesCount > _pdf_viewer.PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
  249. scrollPageButton.disabled = forceScrollModePage;
  250. scrollVerticalButton.disabled = forceScrollModePage;
  251. scrollHorizontalButton.disabled = forceScrollModePage;
  252. scrollWrappedButton.disabled = forceScrollModePage;
  253. spreadNoneButton.disabled = isHorizontal;
  254. spreadOddButton.disabled = isHorizontal;
  255. spreadEvenButton.disabled = isHorizontal;
  256. };
  257. this.eventBus._on("scrollmodechanged", scrollModeChanged);
  258. this.eventBus._on("secondarytoolbarreset", evt => {
  259. if (evt.source === this) {
  260. scrollModeChanged({
  261. mode: _ui_utils.ScrollMode.VERTICAL
  262. });
  263. }
  264. });
  265. }
  266. #bindSpreadModeListener({
  267. spreadNoneButton,
  268. spreadOddButton,
  269. spreadEvenButton
  270. }) {
  271. function spreadModeChanged({
  272. mode
  273. }) {
  274. const isNone = mode === _ui_utils.SpreadMode.NONE,
  275. isOdd = mode === _ui_utils.SpreadMode.ODD,
  276. isEven = mode === _ui_utils.SpreadMode.EVEN;
  277. spreadNoneButton.classList.toggle("toggled", isNone);
  278. spreadOddButton.classList.toggle("toggled", isOdd);
  279. spreadEvenButton.classList.toggle("toggled", isEven);
  280. spreadNoneButton.setAttribute("aria-checked", isNone);
  281. spreadOddButton.setAttribute("aria-checked", isOdd);
  282. spreadEvenButton.setAttribute("aria-checked", isEven);
  283. }
  284. this.eventBus._on("spreadmodechanged", spreadModeChanged);
  285. this.eventBus._on("secondarytoolbarreset", evt => {
  286. if (evt.source === this) {
  287. spreadModeChanged({
  288. mode: _ui_utils.SpreadMode.NONE
  289. });
  290. }
  291. });
  292. }
  293. open() {
  294. if (this.opened) {
  295. return;
  296. }
  297. this.opened = true;
  298. this.toggleButton.classList.add("toggled");
  299. this.toggleButton.setAttribute("aria-expanded", "true");
  300. this.toolbar.classList.remove("hidden");
  301. }
  302. close() {
  303. if (!this.opened) {
  304. return;
  305. }
  306. this.opened = false;
  307. this.toolbar.classList.add("hidden");
  308. this.toggleButton.classList.remove("toggled");
  309. this.toggleButton.setAttribute("aria-expanded", "false");
  310. }
  311. toggle() {
  312. if (this.opened) {
  313. this.close();
  314. } else {
  315. this.open();
  316. }
  317. }
  318. }
  319. exports.SecondaryToolbar = SecondaryToolbar;