secondary_toolbar.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /* Copyright 2012 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { ScrollMode, SpreadMode } from "./ui_utils.js";
  16. import { CursorTool } from "./pdf_cursor_tools.js";
  17. import { PagesCountLimit } from "./pdf_viewer.js";
  18. /**
  19. * @typedef {Object} SecondaryToolbarOptions
  20. * @property {HTMLDivElement} toolbar - Container for the secondary toolbar.
  21. * @property {HTMLButtonElement} toggleButton - Button to toggle the visibility
  22. * of the secondary toolbar.
  23. * @property {HTMLButtonElement} presentationModeButton - Button for entering
  24. * presentation mode.
  25. * @property {HTMLButtonElement} openFileButton - Button to open a file.
  26. * @property {HTMLButtonElement} printButton - Button to print the document.
  27. * @property {HTMLButtonElement} downloadButton - Button to download the
  28. * document.
  29. * @property {HTMLAnchorElement} viewBookmarkButton - Button to obtain a
  30. * bookmark link to the current location in the document.
  31. * @property {HTMLButtonElement} firstPageButton - Button to go to the first
  32. * page in the document.
  33. * @property {HTMLButtonElement} lastPageButton - Button to go to the last page
  34. * in the document.
  35. * @property {HTMLButtonElement} pageRotateCwButton - Button to rotate the pages
  36. * clockwise.
  37. * @property {HTMLButtonElement} pageRotateCcwButton - Button to rotate the
  38. * pages counterclockwise.
  39. * @property {HTMLButtonElement} cursorSelectToolButton - Button to enable the
  40. * select tool.
  41. * @property {HTMLButtonElement} cursorHandToolButton - Button to enable the
  42. * hand tool.
  43. * @property {HTMLButtonElement} documentPropertiesButton - Button for opening
  44. * the document properties dialog.
  45. */
  46. class SecondaryToolbar {
  47. /**
  48. * @param {SecondaryToolbarOptions} options
  49. * @param {EventBus} eventBus
  50. */
  51. constructor(options, eventBus, externalServices) {
  52. this.toolbar = options.toolbar;
  53. this.toggleButton = options.toggleButton;
  54. this.buttons = [
  55. {
  56. element: options.presentationModeButton,
  57. eventName: "presentationmode",
  58. close: true,
  59. },
  60. { element: options.printButton, eventName: "print", close: true },
  61. { element: options.downloadButton, eventName: "download", close: true },
  62. { element: options.viewBookmarkButton, eventName: null, close: true },
  63. { element: options.firstPageButton, eventName: "firstpage", close: true },
  64. { element: options.lastPageButton, eventName: "lastpage", close: true },
  65. {
  66. element: options.pageRotateCwButton,
  67. eventName: "rotatecw",
  68. close: false,
  69. },
  70. {
  71. element: options.pageRotateCcwButton,
  72. eventName: "rotateccw",
  73. close: false,
  74. },
  75. {
  76. element: options.cursorSelectToolButton,
  77. eventName: "switchcursortool",
  78. eventDetails: { tool: CursorTool.SELECT },
  79. close: true,
  80. },
  81. {
  82. element: options.cursorHandToolButton,
  83. eventName: "switchcursortool",
  84. eventDetails: { tool: CursorTool.HAND },
  85. close: true,
  86. },
  87. {
  88. element: options.scrollPageButton,
  89. eventName: "switchscrollmode",
  90. eventDetails: { mode: ScrollMode.PAGE },
  91. close: true,
  92. },
  93. {
  94. element: options.scrollVerticalButton,
  95. eventName: "switchscrollmode",
  96. eventDetails: { mode: ScrollMode.VERTICAL },
  97. close: true,
  98. },
  99. {
  100. element: options.scrollHorizontalButton,
  101. eventName: "switchscrollmode",
  102. eventDetails: { mode: ScrollMode.HORIZONTAL },
  103. close: true,
  104. },
  105. {
  106. element: options.scrollWrappedButton,
  107. eventName: "switchscrollmode",
  108. eventDetails: { mode: ScrollMode.WRAPPED },
  109. close: true,
  110. },
  111. {
  112. element: options.spreadNoneButton,
  113. eventName: "switchspreadmode",
  114. eventDetails: { mode: SpreadMode.NONE },
  115. close: true,
  116. },
  117. {
  118. element: options.spreadOddButton,
  119. eventName: "switchspreadmode",
  120. eventDetails: { mode: SpreadMode.ODD },
  121. close: true,
  122. },
  123. {
  124. element: options.spreadEvenButton,
  125. eventName: "switchspreadmode",
  126. eventDetails: { mode: SpreadMode.EVEN },
  127. close: true,
  128. },
  129. {
  130. element: options.documentPropertiesButton,
  131. eventName: "documentproperties",
  132. close: true,
  133. },
  134. ];
  135. if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
  136. this.buttons.push({
  137. element: options.openFileButton,
  138. eventName: "openfile",
  139. close: true,
  140. });
  141. }
  142. this.items = {
  143. firstPage: options.firstPageButton,
  144. lastPage: options.lastPageButton,
  145. pageRotateCw: options.pageRotateCwButton,
  146. pageRotateCcw: options.pageRotateCcwButton,
  147. };
  148. this.eventBus = eventBus;
  149. this.externalServices = externalServices;
  150. this.opened = false;
  151. // Bind the event listeners for click, cursor tool, and scroll/spread mode
  152. // actions.
  153. this.#bindClickListeners();
  154. this.#bindCursorToolsListener(options);
  155. this.#bindScrollModeListener(options);
  156. this.#bindSpreadModeListener(options);
  157. this.reset();
  158. }
  159. /**
  160. * @type {boolean}
  161. */
  162. get isOpen() {
  163. return this.opened;
  164. }
  165. setPageNumber(pageNumber) {
  166. this.pageNumber = pageNumber;
  167. this.#updateUIState();
  168. }
  169. setPagesCount(pagesCount) {
  170. this.pagesCount = pagesCount;
  171. this.#updateUIState();
  172. }
  173. reset() {
  174. this.pageNumber = 0;
  175. this.pagesCount = 0;
  176. this.#updateUIState();
  177. // Reset the Scroll/Spread buttons too, since they're document specific.
  178. this.eventBus.dispatch("secondarytoolbarreset", { source: this });
  179. }
  180. #updateUIState() {
  181. this.items.firstPage.disabled = this.pageNumber <= 1;
  182. this.items.lastPage.disabled = this.pageNumber >= this.pagesCount;
  183. this.items.pageRotateCw.disabled = this.pagesCount === 0;
  184. this.items.pageRotateCcw.disabled = this.pagesCount === 0;
  185. }
  186. #bindClickListeners() {
  187. // Button to toggle the visibility of the secondary toolbar.
  188. this.toggleButton.addEventListener("click", this.toggle.bind(this));
  189. // All items within the secondary toolbar.
  190. for (const { element, eventName, close, eventDetails } of this.buttons) {
  191. element.addEventListener("click", evt => {
  192. if (eventName !== null) {
  193. const details = { source: this };
  194. for (const property in eventDetails) {
  195. details[property] = eventDetails[property];
  196. }
  197. this.eventBus.dispatch(eventName, details);
  198. }
  199. if (close) {
  200. this.close();
  201. }
  202. this.externalServices.reportTelemetry({
  203. type: "buttons",
  204. data: { id: element.id },
  205. });
  206. });
  207. }
  208. }
  209. #bindCursorToolsListener({ cursorSelectToolButton, cursorHandToolButton }) {
  210. this.eventBus._on("cursortoolchanged", function ({ tool }) {
  211. const isSelect = tool === CursorTool.SELECT,
  212. isHand = tool === CursorTool.HAND;
  213. cursorSelectToolButton.classList.toggle("toggled", isSelect);
  214. cursorHandToolButton.classList.toggle("toggled", isHand);
  215. cursorSelectToolButton.setAttribute("aria-checked", isSelect);
  216. cursorHandToolButton.setAttribute("aria-checked", isHand);
  217. });
  218. }
  219. #bindScrollModeListener({
  220. scrollPageButton,
  221. scrollVerticalButton,
  222. scrollHorizontalButton,
  223. scrollWrappedButton,
  224. spreadNoneButton,
  225. spreadOddButton,
  226. spreadEvenButton,
  227. }) {
  228. const scrollModeChanged = ({ mode }) => {
  229. const isPage = mode === ScrollMode.PAGE,
  230. isVertical = mode === ScrollMode.VERTICAL,
  231. isHorizontal = mode === ScrollMode.HORIZONTAL,
  232. isWrapped = mode === 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. // Permanently *disable* the Scroll buttons when PAGE-scrolling is being
  242. // enforced for *very* long/large documents; please see the `BaseViewer`.
  243. const forceScrollModePage =
  244. this.pagesCount > PagesCountLimit.FORCE_SCROLL_MODE_PAGE;
  245. scrollPageButton.disabled = forceScrollModePage;
  246. scrollVerticalButton.disabled = forceScrollModePage;
  247. scrollHorizontalButton.disabled = forceScrollModePage;
  248. scrollWrappedButton.disabled = forceScrollModePage;
  249. // Temporarily *disable* the Spread buttons when horizontal scrolling is
  250. // enabled, since the non-default Spread modes doesn't affect the layout.
  251. spreadNoneButton.disabled = isHorizontal;
  252. spreadOddButton.disabled = isHorizontal;
  253. spreadEvenButton.disabled = isHorizontal;
  254. };
  255. this.eventBus._on("scrollmodechanged", scrollModeChanged);
  256. this.eventBus._on("secondarytoolbarreset", evt => {
  257. if (evt.source === this) {
  258. scrollModeChanged({ mode: ScrollMode.VERTICAL });
  259. }
  260. });
  261. }
  262. #bindSpreadModeListener({
  263. spreadNoneButton,
  264. spreadOddButton,
  265. spreadEvenButton,
  266. }) {
  267. function spreadModeChanged({ mode }) {
  268. const isNone = mode === SpreadMode.NONE,
  269. isOdd = mode === SpreadMode.ODD,
  270. isEven = mode === SpreadMode.EVEN;
  271. spreadNoneButton.classList.toggle("toggled", isNone);
  272. spreadOddButton.classList.toggle("toggled", isOdd);
  273. spreadEvenButton.classList.toggle("toggled", isEven);
  274. spreadNoneButton.setAttribute("aria-checked", isNone);
  275. spreadOddButton.setAttribute("aria-checked", isOdd);
  276. spreadEvenButton.setAttribute("aria-checked", isEven);
  277. }
  278. this.eventBus._on("spreadmodechanged", spreadModeChanged);
  279. this.eventBus._on("secondarytoolbarreset", evt => {
  280. if (evt.source === this) {
  281. spreadModeChanged({ mode: SpreadMode.NONE });
  282. }
  283. });
  284. }
  285. open() {
  286. if (this.opened) {
  287. return;
  288. }
  289. this.opened = true;
  290. this.toggleButton.classList.add("toggled");
  291. this.toggleButton.setAttribute("aria-expanded", "true");
  292. this.toolbar.classList.remove("hidden");
  293. }
  294. close() {
  295. if (!this.opened) {
  296. return;
  297. }
  298. this.opened = false;
  299. this.toolbar.classList.add("hidden");
  300. this.toggleButton.classList.remove("toggled");
  301. this.toggleButton.setAttribute("aria-expanded", "false");
  302. }
  303. toggle() {
  304. if (this.opened) {
  305. this.close();
  306. } else {
  307. this.open();
  308. }
  309. }
  310. }
  311. export { SecondaryToolbar };