2
0

secondary_toolbar.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 _base_viewer = require("./base_viewer.js");
  30. class SecondaryToolbar {
  31. constructor(options, eventBus) {
  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.opened = false;
  147. this.#bindClickListeners();
  148. this.#bindCursorToolsListener(options);
  149. this.#bindScrollModeListener(options);
  150. this.#bindSpreadModeListener(options);
  151. this.reset();
  152. }
  153. get isOpen() {
  154. return this.opened;
  155. }
  156. setPageNumber(pageNumber) {
  157. this.pageNumber = pageNumber;
  158. this.#updateUIState();
  159. }
  160. setPagesCount(pagesCount) {
  161. this.pagesCount = pagesCount;
  162. this.#updateUIState();
  163. }
  164. reset() {
  165. this.pageNumber = 0;
  166. this.pagesCount = 0;
  167. this.#updateUIState();
  168. this.eventBus.dispatch("secondarytoolbarreset", {
  169. source: this
  170. });
  171. }
  172. #updateUIState() {
  173. this.items.firstPage.disabled = this.pageNumber <= 1;
  174. this.items.lastPage.disabled = this.pageNumber >= this.pagesCount;
  175. this.items.pageRotateCw.disabled = this.pagesCount === 0;
  176. this.items.pageRotateCcw.disabled = this.pagesCount === 0;
  177. }
  178. #bindClickListeners() {
  179. this.toggleButton.addEventListener("click", this.toggle.bind(this));
  180. for (const {
  181. element,
  182. eventName,
  183. close,
  184. eventDetails
  185. } of this.buttons) {
  186. element.addEventListener("click", evt => {
  187. if (eventName !== null) {
  188. const details = {
  189. source: this
  190. };
  191. for (const property in eventDetails) {
  192. details[property] = eventDetails[property];
  193. }
  194. this.eventBus.dispatch(eventName, details);
  195. }
  196. if (close) {
  197. this.close();
  198. }
  199. });
  200. }
  201. }
  202. #bindCursorToolsListener({
  203. cursorSelectToolButton,
  204. cursorHandToolButton
  205. }) {
  206. this.eventBus._on("cursortoolchanged", function ({
  207. tool
  208. }) {
  209. const isSelect = tool === _pdf_cursor_tools.CursorTool.SELECT,
  210. isHand = tool === _pdf_cursor_tools.CursorTool.HAND;
  211. cursorSelectToolButton.classList.toggle("toggled", isSelect);
  212. cursorHandToolButton.classList.toggle("toggled", isHand);
  213. cursorSelectToolButton.setAttribute("aria-checked", isSelect);
  214. cursorHandToolButton.setAttribute("aria-checked", isHand);
  215. });
  216. }
  217. #bindScrollModeListener({
  218. scrollPageButton,
  219. scrollVerticalButton,
  220. scrollHorizontalButton,
  221. scrollWrappedButton,
  222. spreadNoneButton,
  223. spreadOddButton,
  224. spreadEvenButton
  225. }) {
  226. const scrollModeChanged = ({
  227. mode
  228. }) => {
  229. const isPage = mode === _ui_utils.ScrollMode.PAGE,
  230. isVertical = mode === _ui_utils.ScrollMode.VERTICAL,
  231. isHorizontal = mode === _ui_utils.ScrollMode.HORIZONTAL,
  232. isWrapped = mode === _ui_utils.ScrollMode.WRAPPED;
  233. scrollPageButton.classList.toggle("toggled", isPage);
  234. scrollVerticalButton.classList.toggle("toggled", isVertical);
  235. scrollHorizontalButton.classList.toggle("toggled", isHorizontal);
  236. scrollWrappedButton.classList.toggle("toggled", isWrapped);
  237. scrollPageButton.setAttribute("aria-checked", isPage);
  238. scrollVerticalButton.setAttribute("aria-checked", isVertical);
  239. scrollHorizontalButton.setAttribute("aria-checked", isHorizontal);
  240. scrollWrappedButton.setAttribute("aria-checked", isWrapped);
  241. const forceScrollModePage = this.pagesCount > _base_viewer.PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
  242. scrollPageButton.disabled = forceScrollModePage;
  243. scrollVerticalButton.disabled = forceScrollModePage;
  244. scrollHorizontalButton.disabled = forceScrollModePage;
  245. scrollWrappedButton.disabled = forceScrollModePage;
  246. spreadNoneButton.disabled = isHorizontal;
  247. spreadOddButton.disabled = isHorizontal;
  248. spreadEvenButton.disabled = isHorizontal;
  249. };
  250. this.eventBus._on("scrollmodechanged", scrollModeChanged);
  251. this.eventBus._on("secondarytoolbarreset", evt => {
  252. if (evt.source === this) {
  253. scrollModeChanged({
  254. mode: _ui_utils.ScrollMode.VERTICAL
  255. });
  256. }
  257. });
  258. }
  259. #bindSpreadModeListener({
  260. spreadNoneButton,
  261. spreadOddButton,
  262. spreadEvenButton
  263. }) {
  264. function spreadModeChanged({
  265. mode
  266. }) {
  267. const isNone = mode === _ui_utils.SpreadMode.NONE,
  268. isOdd = mode === _ui_utils.SpreadMode.ODD,
  269. isEven = mode === _ui_utils.SpreadMode.EVEN;
  270. spreadNoneButton.classList.toggle("toggled", isNone);
  271. spreadOddButton.classList.toggle("toggled", isOdd);
  272. spreadEvenButton.classList.toggle("toggled", isEven);
  273. spreadNoneButton.setAttribute("aria-checked", isNone);
  274. spreadOddButton.setAttribute("aria-checked", isOdd);
  275. spreadEvenButton.setAttribute("aria-checked", isEven);
  276. }
  277. this.eventBus._on("spreadmodechanged", spreadModeChanged);
  278. this.eventBus._on("secondarytoolbarreset", evt => {
  279. if (evt.source === this) {
  280. spreadModeChanged({
  281. mode: _ui_utils.SpreadMode.NONE
  282. });
  283. }
  284. });
  285. }
  286. open() {
  287. if (this.opened) {
  288. return;
  289. }
  290. this.opened = true;
  291. this.toggleButton.classList.add("toggled");
  292. this.toggleButton.setAttribute("aria-expanded", "true");
  293. this.toolbar.classList.remove("hidden");
  294. }
  295. close() {
  296. if (!this.opened) {
  297. return;
  298. }
  299. this.opened = false;
  300. this.toolbar.classList.add("hidden");
  301. this.toggleButton.classList.remove("toggled");
  302. this.toggleButton.setAttribute("aria-expanded", "false");
  303. }
  304. toggle() {
  305. if (this.opened) {
  306. this.close();
  307. } else {
  308. this.open();
  309. }
  310. }
  311. }
  312. exports.SecondaryToolbar = SecondaryToolbar;