pdf_sidebar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 = _ui_utils.NullL10n
  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.outerContainer.classList.add("sidebarMoving", "sidebarOpen");
  165. if (this.active === _ui_utils.SidebarView.THUMBS) {
  166. this._updateThumbnailViewer();
  167. }
  168. this._forceRendering();
  169. this._dispatchEvent();
  170. this._hideUINotification();
  171. }
  172. close() {
  173. if (!this.isOpen) {
  174. return;
  175. }
  176. this.isOpen = false;
  177. this.toggleButton.classList.remove("toggled");
  178. this.outerContainer.classList.add("sidebarMoving");
  179. this.outerContainer.classList.remove("sidebarOpen");
  180. this._forceRendering();
  181. this._dispatchEvent();
  182. }
  183. toggle() {
  184. if (this.isOpen) {
  185. this.close();
  186. } else {
  187. this.open();
  188. }
  189. }
  190. _dispatchEvent() {
  191. this.eventBus.dispatch("sidebarviewchanged", {
  192. source: this,
  193. view: this.visibleView
  194. });
  195. }
  196. _forceRendering() {
  197. if (this.onToggled) {
  198. this.onToggled();
  199. } else {
  200. this.pdfViewer.forceRendering();
  201. this.pdfThumbnailViewer.forceRendering();
  202. }
  203. }
  204. _updateThumbnailViewer() {
  205. const {
  206. pdfViewer,
  207. pdfThumbnailViewer
  208. } = this;
  209. const pagesCount = pdfViewer.pagesCount;
  210. for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  211. const pageView = pdfViewer.getPageView(pageIndex);
  212. if (pageView && pageView.renderingState === _pdf_rendering_queue.RenderingStates.FINISHED) {
  213. const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
  214. thumbnailView.setImage(pageView);
  215. }
  216. }
  217. pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  218. }
  219. _showUINotification() {
  220. this.l10n.get("toggle_sidebar_notification2.title", null, "Toggle Sidebar (document contains outline/attachments/layers)").then(msg => {
  221. this.toggleButton.title = msg;
  222. });
  223. if (!this.isOpen) {
  224. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  225. }
  226. }
  227. _hideUINotification(reset = false) {
  228. if (this.isOpen || reset) {
  229. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  230. }
  231. if (reset) {
  232. this.l10n.get("toggle_sidebar.title", null, "Toggle Sidebar").then(msg => {
  233. this.toggleButton.title = msg;
  234. });
  235. }
  236. }
  237. _addEventListeners() {
  238. this.viewerContainer.addEventListener("transitionend", evt => {
  239. if (evt.target === this.viewerContainer) {
  240. this.outerContainer.classList.remove("sidebarMoving");
  241. }
  242. });
  243. this.toggleButton.addEventListener("click", () => {
  244. this.toggle();
  245. });
  246. this.thumbnailButton.addEventListener("click", () => {
  247. this.switchView(_ui_utils.SidebarView.THUMBS);
  248. });
  249. this.outlineButton.addEventListener("click", () => {
  250. this.switchView(_ui_utils.SidebarView.OUTLINE);
  251. });
  252. this.outlineButton.addEventListener("dblclick", () => {
  253. this.eventBus.dispatch("toggleoutlinetree", {
  254. source: this
  255. });
  256. });
  257. this.attachmentsButton.addEventListener("click", () => {
  258. this.switchView(_ui_utils.SidebarView.ATTACHMENTS);
  259. });
  260. this.layersButton.addEventListener("click", () => {
  261. this.switchView(_ui_utils.SidebarView.LAYERS);
  262. });
  263. this.layersButton.addEventListener("dblclick", () => {
  264. this.eventBus.dispatch("resetlayers", {
  265. source: this
  266. });
  267. });
  268. this._currentOutlineItemButton.addEventListener("click", () => {
  269. this.eventBus.dispatch("currentoutlineitem", {
  270. source: this
  271. });
  272. });
  273. const onTreeLoaded = (count, button, view) => {
  274. button.disabled = !count;
  275. if (count) {
  276. this._showUINotification();
  277. } else if (this.active === view) {
  278. this.switchView(_ui_utils.SidebarView.THUMBS);
  279. }
  280. };
  281. this.eventBus._on("outlineloaded", evt => {
  282. onTreeLoaded(evt.outlineCount, this.outlineButton, _ui_utils.SidebarView.OUTLINE);
  283. if (evt.enableCurrentOutlineItemButton) {
  284. this.pdfViewer.pagesPromise.then(() => {
  285. this._currentOutlineItemButton.disabled = !this.isInitialViewSet;
  286. });
  287. }
  288. });
  289. this.eventBus._on("attachmentsloaded", evt => {
  290. onTreeLoaded(evt.attachmentsCount, this.attachmentsButton, _ui_utils.SidebarView.ATTACHMENTS);
  291. });
  292. this.eventBus._on("layersloaded", evt => {
  293. onTreeLoaded(evt.layersCount, this.layersButton, _ui_utils.SidebarView.LAYERS);
  294. });
  295. this.eventBus._on("presentationmodechanged", evt => {
  296. if (evt.state === _ui_utils.PresentationModeState.NORMAL && this.isThumbnailViewVisible) {
  297. this._updateThumbnailViewer();
  298. }
  299. });
  300. }
  301. }
  302. exports.PDFSidebar = PDFSidebar;