pdf_sidebar.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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.PDFSidebar = void 0;
  27. var _ui_utils = require("./ui_utils.js");
  28. const UI_NOTIFICATION_CLASS = "pdfSidebarNotification";
  29. class PDFSidebar {
  30. constructor({
  31. elements,
  32. pdfViewer,
  33. pdfThumbnailViewer,
  34. eventBus,
  35. l10n
  36. }) {
  37. this.isOpen = false;
  38. this.active = _ui_utils.SidebarView.THUMBS;
  39. this.isInitialViewSet = false;
  40. this.isInitialEventDispatched = false;
  41. this.onToggled = null;
  42. this.pdfViewer = pdfViewer;
  43. this.pdfThumbnailViewer = pdfThumbnailViewer;
  44. this.outerContainer = elements.outerContainer;
  45. this.sidebarContainer = elements.sidebarContainer;
  46. this.toggleButton = elements.toggleButton;
  47. this.thumbnailButton = elements.thumbnailButton;
  48. this.outlineButton = elements.outlineButton;
  49. this.attachmentsButton = elements.attachmentsButton;
  50. this.layersButton = elements.layersButton;
  51. this.thumbnailView = elements.thumbnailView;
  52. this.outlineView = elements.outlineView;
  53. this.attachmentsView = elements.attachmentsView;
  54. this.layersView = elements.layersView;
  55. this._outlineOptionsContainer = elements.outlineOptionsContainer;
  56. this._currentOutlineItemButton = elements.currentOutlineItemButton;
  57. this.eventBus = eventBus;
  58. this.l10n = l10n;
  59. this.#addEventListeners();
  60. }
  61. reset() {
  62. this.isInitialViewSet = false;
  63. this.isInitialEventDispatched = false;
  64. this.#hideUINotification(true);
  65. this.switchView(_ui_utils.SidebarView.THUMBS);
  66. this.outlineButton.disabled = false;
  67. this.attachmentsButton.disabled = false;
  68. this.layersButton.disabled = false;
  69. this._currentOutlineItemButton.disabled = true;
  70. }
  71. get visibleView() {
  72. return this.isOpen ? this.active : _ui_utils.SidebarView.NONE;
  73. }
  74. setInitialView(view = _ui_utils.SidebarView.NONE) {
  75. if (this.isInitialViewSet) {
  76. return;
  77. }
  78. this.isInitialViewSet = true;
  79. if (view === _ui_utils.SidebarView.NONE || view === _ui_utils.SidebarView.UNKNOWN) {
  80. this.#dispatchEvent();
  81. return;
  82. }
  83. this.switchView(view, true);
  84. if (!this.isInitialEventDispatched) {
  85. this.#dispatchEvent();
  86. }
  87. }
  88. switchView(view, forceOpen = false) {
  89. const isViewChanged = view !== this.active;
  90. let shouldForceRendering = false;
  91. switch (view) {
  92. case _ui_utils.SidebarView.NONE:
  93. if (this.isOpen) {
  94. this.close();
  95. }
  96. return;
  97. case _ui_utils.SidebarView.THUMBS:
  98. if (this.isOpen && isViewChanged) {
  99. shouldForceRendering = true;
  100. }
  101. break;
  102. case _ui_utils.SidebarView.OUTLINE:
  103. if (this.outlineButton.disabled) {
  104. return;
  105. }
  106. break;
  107. case _ui_utils.SidebarView.ATTACHMENTS:
  108. if (this.attachmentsButton.disabled) {
  109. return;
  110. }
  111. break;
  112. case _ui_utils.SidebarView.LAYERS:
  113. if (this.layersButton.disabled) {
  114. return;
  115. }
  116. break;
  117. default:
  118. console.error(`PDFSidebar.switchView: "${view}" is not a valid view.`);
  119. return;
  120. }
  121. this.active = view;
  122. const isThumbs = view === _ui_utils.SidebarView.THUMBS,
  123. isOutline = view === _ui_utils.SidebarView.OUTLINE,
  124. isAttachments = view === _ui_utils.SidebarView.ATTACHMENTS,
  125. isLayers = view === _ui_utils.SidebarView.LAYERS;
  126. this.thumbnailButton.classList.toggle("toggled", isThumbs);
  127. this.outlineButton.classList.toggle("toggled", isOutline);
  128. this.attachmentsButton.classList.toggle("toggled", isAttachments);
  129. this.layersButton.classList.toggle("toggled", isLayers);
  130. this.thumbnailButton.setAttribute("aria-checked", isThumbs);
  131. this.outlineButton.setAttribute("aria-checked", isOutline);
  132. this.attachmentsButton.setAttribute("aria-checked", isAttachments);
  133. this.layersButton.setAttribute("aria-checked", isLayers);
  134. this.thumbnailView.classList.toggle("hidden", !isThumbs);
  135. this.outlineView.classList.toggle("hidden", !isOutline);
  136. this.attachmentsView.classList.toggle("hidden", !isAttachments);
  137. this.layersView.classList.toggle("hidden", !isLayers);
  138. this._outlineOptionsContainer.classList.toggle("hidden", !isOutline);
  139. if (forceOpen && !this.isOpen) {
  140. this.open();
  141. return;
  142. }
  143. if (shouldForceRendering) {
  144. this.#updateThumbnailViewer();
  145. this.#forceRendering();
  146. }
  147. if (isViewChanged) {
  148. this.#dispatchEvent();
  149. }
  150. }
  151. open() {
  152. if (this.isOpen) {
  153. return;
  154. }
  155. this.isOpen = true;
  156. this.toggleButton.classList.add("toggled");
  157. this.toggleButton.setAttribute("aria-expanded", "true");
  158. this.outerContainer.classList.add("sidebarMoving", "sidebarOpen");
  159. if (this.active === _ui_utils.SidebarView.THUMBS) {
  160. this.#updateThumbnailViewer();
  161. }
  162. this.#forceRendering();
  163. this.#dispatchEvent();
  164. this.#hideUINotification();
  165. }
  166. close() {
  167. if (!this.isOpen) {
  168. return;
  169. }
  170. this.isOpen = false;
  171. this.toggleButton.classList.remove("toggled");
  172. this.toggleButton.setAttribute("aria-expanded", "false");
  173. this.outerContainer.classList.add("sidebarMoving");
  174. this.outerContainer.classList.remove("sidebarOpen");
  175. this.#forceRendering();
  176. this.#dispatchEvent();
  177. }
  178. toggle() {
  179. if (this.isOpen) {
  180. this.close();
  181. } else {
  182. this.open();
  183. }
  184. }
  185. #dispatchEvent() {
  186. if (this.isInitialViewSet && !this.isInitialEventDispatched) {
  187. this.isInitialEventDispatched = true;
  188. }
  189. this.eventBus.dispatch("sidebarviewchanged", {
  190. source: this,
  191. view: this.visibleView
  192. });
  193. }
  194. #forceRendering() {
  195. if (this.onToggled) {
  196. this.onToggled();
  197. } else {
  198. this.pdfViewer.forceRendering();
  199. this.pdfThumbnailViewer.forceRendering();
  200. }
  201. }
  202. #updateThumbnailViewer() {
  203. const {
  204. pdfViewer,
  205. pdfThumbnailViewer
  206. } = this;
  207. const pagesCount = pdfViewer.pagesCount;
  208. for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  209. const pageView = pdfViewer.getPageView(pageIndex);
  210. if (pageView?.renderingState === _ui_utils.RenderingStates.FINISHED) {
  211. const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
  212. thumbnailView.setImage(pageView);
  213. }
  214. }
  215. pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  216. }
  217. #showUINotification() {
  218. this.toggleButton.setAttribute("data-l10n-id", "toggle_sidebar_notification2");
  219. this.l10n.translate(this.toggleButton);
  220. if (!this.isOpen) {
  221. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  222. }
  223. }
  224. #hideUINotification(reset = false) {
  225. if (this.isOpen || reset) {
  226. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  227. }
  228. if (reset) {
  229. this.toggleButton.setAttribute("data-l10n-id", "toggle_sidebar");
  230. this.l10n.translate(this.toggleButton);
  231. }
  232. }
  233. #addEventListeners() {
  234. this.sidebarContainer.addEventListener("transitionend", evt => {
  235. if (evt.target === this.sidebarContainer) {
  236. this.outerContainer.classList.remove("sidebarMoving");
  237. }
  238. });
  239. this.toggleButton.addEventListener("click", () => {
  240. this.toggle();
  241. });
  242. this.thumbnailButton.addEventListener("click", () => {
  243. this.switchView(_ui_utils.SidebarView.THUMBS);
  244. });
  245. this.outlineButton.addEventListener("click", () => {
  246. this.switchView(_ui_utils.SidebarView.OUTLINE);
  247. });
  248. this.outlineButton.addEventListener("dblclick", () => {
  249. this.eventBus.dispatch("toggleoutlinetree", {
  250. source: this
  251. });
  252. });
  253. this.attachmentsButton.addEventListener("click", () => {
  254. this.switchView(_ui_utils.SidebarView.ATTACHMENTS);
  255. });
  256. this.layersButton.addEventListener("click", () => {
  257. this.switchView(_ui_utils.SidebarView.LAYERS);
  258. });
  259. this.layersButton.addEventListener("dblclick", () => {
  260. this.eventBus.dispatch("resetlayers", {
  261. source: this
  262. });
  263. });
  264. this._currentOutlineItemButton.addEventListener("click", () => {
  265. this.eventBus.dispatch("currentoutlineitem", {
  266. source: this
  267. });
  268. });
  269. const onTreeLoaded = (count, button, view) => {
  270. button.disabled = !count;
  271. if (count) {
  272. this.#showUINotification();
  273. } else if (this.active === view) {
  274. this.switchView(_ui_utils.SidebarView.THUMBS);
  275. }
  276. };
  277. this.eventBus._on("outlineloaded", evt => {
  278. onTreeLoaded(evt.outlineCount, this.outlineButton, _ui_utils.SidebarView.OUTLINE);
  279. evt.currentOutlineItemPromise.then(enabled => {
  280. if (!this.isInitialViewSet) {
  281. return;
  282. }
  283. this._currentOutlineItemButton.disabled = !enabled;
  284. });
  285. });
  286. this.eventBus._on("attachmentsloaded", evt => {
  287. onTreeLoaded(evt.attachmentsCount, this.attachmentsButton, _ui_utils.SidebarView.ATTACHMENTS);
  288. });
  289. this.eventBus._on("layersloaded", evt => {
  290. onTreeLoaded(evt.layersCount, this.layersButton, _ui_utils.SidebarView.LAYERS);
  291. });
  292. this.eventBus._on("presentationmodechanged", evt => {
  293. if (evt.state === _ui_utils.PresentationModeState.NORMAL && this.visibleView === _ui_utils.SidebarView.THUMBS) {
  294. this.#updateThumbnailViewer();
  295. }
  296. });
  297. }
  298. }
  299. exports.PDFSidebar = PDFSidebar;