2
0

pdf_sidebar.js 11 KB

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