pdf_sidebar.js 10 KB

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