pdf_sidebar.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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.l10n.get("toggle_sidebar_notification2.title").then(msg => {
  219. this.toggleButton.title = msg;
  220. });
  221. if (!this.isOpen) {
  222. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  223. }
  224. }
  225. #hideUINotification(reset = false) {
  226. if (this.isOpen || reset) {
  227. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  228. }
  229. if (reset) {
  230. this.l10n.get("toggle_sidebar.title").then(msg => {
  231. this.toggleButton.title = msg;
  232. });
  233. }
  234. }
  235. #addEventListeners() {
  236. this.sidebarContainer.addEventListener("transitionend", evt => {
  237. if (evt.target === this.sidebarContainer) {
  238. this.outerContainer.classList.remove("sidebarMoving");
  239. }
  240. });
  241. this.toggleButton.addEventListener("click", () => {
  242. this.toggle();
  243. });
  244. this.thumbnailButton.addEventListener("click", () => {
  245. this.switchView(_ui_utils.SidebarView.THUMBS);
  246. });
  247. this.outlineButton.addEventListener("click", () => {
  248. this.switchView(_ui_utils.SidebarView.OUTLINE);
  249. });
  250. this.outlineButton.addEventListener("dblclick", () => {
  251. this.eventBus.dispatch("toggleoutlinetree", {
  252. source: this
  253. });
  254. });
  255. this.attachmentsButton.addEventListener("click", () => {
  256. this.switchView(_ui_utils.SidebarView.ATTACHMENTS);
  257. });
  258. this.layersButton.addEventListener("click", () => {
  259. this.switchView(_ui_utils.SidebarView.LAYERS);
  260. });
  261. this.layersButton.addEventListener("dblclick", () => {
  262. this.eventBus.dispatch("resetlayers", {
  263. source: this
  264. });
  265. });
  266. this._currentOutlineItemButton.addEventListener("click", () => {
  267. this.eventBus.dispatch("currentoutlineitem", {
  268. source: this
  269. });
  270. });
  271. const onTreeLoaded = (count, button, view) => {
  272. button.disabled = !count;
  273. if (count) {
  274. this.#showUINotification();
  275. } else if (this.active === view) {
  276. this.switchView(_ui_utils.SidebarView.THUMBS);
  277. }
  278. };
  279. this.eventBus._on("outlineloaded", evt => {
  280. onTreeLoaded(evt.outlineCount, this.outlineButton, _ui_utils.SidebarView.OUTLINE);
  281. evt.currentOutlineItemPromise.then(enabled => {
  282. if (!this.isInitialViewSet) {
  283. return;
  284. }
  285. this._currentOutlineItemButton.disabled = !enabled;
  286. });
  287. });
  288. this.eventBus._on("attachmentsloaded", evt => {
  289. onTreeLoaded(evt.attachmentsCount, this.attachmentsButton, _ui_utils.SidebarView.ATTACHMENTS);
  290. });
  291. this.eventBus._on("layersloaded", evt => {
  292. onTreeLoaded(evt.layersCount, this.layersButton, _ui_utils.SidebarView.LAYERS);
  293. });
  294. this.eventBus._on("presentationmodechanged", evt => {
  295. if (evt.state === _ui_utils.PresentationModeState.NORMAL && this.visibleView === _ui_utils.SidebarView.THUMBS) {
  296. this.#updateThumbnailViewer();
  297. }
  298. });
  299. }
  300. }
  301. exports.PDFSidebar = PDFSidebar;