2
0

pdf_sidebar.js 10 KB

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