2
0

pdf_sidebar.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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.thumbnailView = elements.thumbnailView;
  61. this.outlineView = elements.outlineView;
  62. this.attachmentsView = elements.attachmentsView;
  63. this.eventBus = eventBus;
  64. this.l10n = l10n;
  65. this._disableNotification = disableNotification;
  66. this._addEventListeners();
  67. }
  68. reset() {
  69. this.isInitialViewSet = false;
  70. this._hideUINotification(null);
  71. this.switchView(SidebarView.THUMBS);
  72. this.outlineButton.disabled = false;
  73. this.attachmentsButton.disabled = false;
  74. }
  75. get visibleView() {
  76. return this.isOpen ? this.active : SidebarView.NONE;
  77. }
  78. get isThumbnailViewVisible() {
  79. return this.isOpen && this.active === SidebarView.THUMBS;
  80. }
  81. get isOutlineViewVisible() {
  82. return this.isOpen && this.active === SidebarView.OUTLINE;
  83. }
  84. get isAttachmentsViewVisible() {
  85. return this.isOpen && this.active === SidebarView.ATTACHMENTS;
  86. }
  87. setInitialView(view = SidebarView.NONE) {
  88. if (this.isInitialViewSet) {
  89. return;
  90. }
  91. this.isInitialViewSet = true;
  92. if (view === SidebarView.NONE || view === SidebarView.UNKNOWN) {
  93. this._dispatchEvent();
  94. return;
  95. }
  96. if (!this._switchView(view, true)) {
  97. this._dispatchEvent();
  98. }
  99. }
  100. switchView(view, forceOpen = false) {
  101. this._switchView(view, forceOpen);
  102. }
  103. _switchView(view, forceOpen = false) {
  104. const isViewChanged = view !== this.active;
  105. let shouldForceRendering = false;
  106. switch (view) {
  107. case SidebarView.NONE:
  108. if (this.isOpen) {
  109. this.close();
  110. return true;
  111. }
  112. return false;
  113. case SidebarView.THUMBS:
  114. if (this.isOpen && isViewChanged) {
  115. shouldForceRendering = true;
  116. }
  117. break;
  118. case SidebarView.OUTLINE:
  119. if (this.outlineButton.disabled) {
  120. return false;
  121. }
  122. break;
  123. case SidebarView.ATTACHMENTS:
  124. if (this.attachmentsButton.disabled) {
  125. return false;
  126. }
  127. break;
  128. default:
  129. console.error(`PDFSidebar._switchView: "${view}" is not a valid view.`);
  130. return false;
  131. }
  132. this.active = view;
  133. this.thumbnailButton.classList.toggle("toggled", view === SidebarView.THUMBS);
  134. this.outlineButton.classList.toggle("toggled", view === SidebarView.OUTLINE);
  135. this.attachmentsButton.classList.toggle("toggled", view === SidebarView.ATTACHMENTS);
  136. this.thumbnailView.classList.toggle("hidden", view !== SidebarView.THUMBS);
  137. this.outlineView.classList.toggle("hidden", view !== SidebarView.OUTLINE);
  138. this.attachmentsView.classList.toggle("hidden", view !== SidebarView.ATTACHMENTS);
  139. if (forceOpen && !this.isOpen) {
  140. this.open();
  141. return true;
  142. }
  143. if (shouldForceRendering) {
  144. this._updateThumbnailViewer();
  145. this._forceRendering();
  146. }
  147. if (isViewChanged) {
  148. this._dispatchEvent();
  149. }
  150. this._hideUINotification(this.active);
  151. return isViewChanged;
  152. }
  153. open() {
  154. if (this.isOpen) {
  155. return;
  156. }
  157. this.isOpen = true;
  158. this.toggleButton.classList.add("toggled");
  159. this.outerContainer.classList.add("sidebarMoving", "sidebarOpen");
  160. if (this.active === SidebarView.THUMBS) {
  161. this._updateThumbnailViewer();
  162. }
  163. this._forceRendering();
  164. this._dispatchEvent();
  165. this._hideUINotification(this.active);
  166. }
  167. close() {
  168. if (!this.isOpen) {
  169. return;
  170. }
  171. this.isOpen = false;
  172. this.toggleButton.classList.remove("toggled");
  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. this.eventBus.dispatch("sidebarviewchanged", {
  187. source: this,
  188. view: this.visibleView
  189. });
  190. }
  191. _forceRendering() {
  192. if (this.onToggled) {
  193. this.onToggled();
  194. } else {
  195. this.pdfViewer.forceRendering();
  196. this.pdfThumbnailViewer.forceRendering();
  197. }
  198. }
  199. _updateThumbnailViewer() {
  200. const {
  201. pdfViewer,
  202. pdfThumbnailViewer
  203. } = this;
  204. const pagesCount = pdfViewer.pagesCount;
  205. for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  206. const pageView = pdfViewer.getPageView(pageIndex);
  207. if (pageView && pageView.renderingState === _pdf_rendering_queue.RenderingStates.FINISHED) {
  208. const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
  209. thumbnailView.setImage(pageView);
  210. }
  211. }
  212. pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  213. }
  214. _showUINotification(view) {
  215. if (this._disableNotification) {
  216. return;
  217. }
  218. this.l10n.get("toggle_sidebar_notification.title", null, "Toggle Sidebar (document contains outline/attachments)").then(msg => {
  219. this.toggleButton.title = msg;
  220. });
  221. if (!this.isOpen) {
  222. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  223. } else if (view === this.active) {
  224. return;
  225. }
  226. switch (view) {
  227. case SidebarView.OUTLINE:
  228. this.outlineButton.classList.add(UI_NOTIFICATION_CLASS);
  229. break;
  230. case SidebarView.ATTACHMENTS:
  231. this.attachmentsButton.classList.add(UI_NOTIFICATION_CLASS);
  232. break;
  233. }
  234. }
  235. _hideUINotification(view) {
  236. if (this._disableNotification) {
  237. return;
  238. }
  239. const removeNotification = sidebarView => {
  240. switch (sidebarView) {
  241. case SidebarView.OUTLINE:
  242. this.outlineButton.classList.remove(UI_NOTIFICATION_CLASS);
  243. break;
  244. case SidebarView.ATTACHMENTS:
  245. this.attachmentsButton.classList.remove(UI_NOTIFICATION_CLASS);
  246. break;
  247. }
  248. };
  249. if (!this.isOpen && view !== null) {
  250. return;
  251. }
  252. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  253. if (view !== null) {
  254. removeNotification(view);
  255. return;
  256. }
  257. for (view in SidebarView) {
  258. removeNotification(SidebarView[view]);
  259. }
  260. this.l10n.get("toggle_sidebar.title", null, "Toggle Sidebar").then(msg => {
  261. this.toggleButton.title = msg;
  262. });
  263. }
  264. _addEventListeners() {
  265. this.viewerContainer.addEventListener("transitionend", evt => {
  266. if (evt.target === this.viewerContainer) {
  267. this.outerContainer.classList.remove("sidebarMoving");
  268. }
  269. });
  270. this.toggleButton.addEventListener("click", () => {
  271. this.toggle();
  272. });
  273. this.thumbnailButton.addEventListener("click", () => {
  274. this.switchView(SidebarView.THUMBS);
  275. });
  276. this.outlineButton.addEventListener("click", () => {
  277. this.switchView(SidebarView.OUTLINE);
  278. });
  279. this.outlineButton.addEventListener("dblclick", () => {
  280. this.eventBus.dispatch("toggleoutlinetree", {
  281. source: this
  282. });
  283. });
  284. this.attachmentsButton.addEventListener("click", () => {
  285. this.switchView(SidebarView.ATTACHMENTS);
  286. });
  287. this.eventBus._on("outlineloaded", evt => {
  288. const outlineCount = evt.outlineCount;
  289. this.outlineButton.disabled = !outlineCount;
  290. if (outlineCount) {
  291. this._showUINotification(SidebarView.OUTLINE);
  292. } else if (this.active === SidebarView.OUTLINE) {
  293. this.switchView(SidebarView.THUMBS);
  294. }
  295. });
  296. this.eventBus._on("attachmentsloaded", evt => {
  297. if (evt.attachmentsCount) {
  298. this.attachmentsButton.disabled = false;
  299. this._showUINotification(SidebarView.ATTACHMENTS);
  300. return;
  301. }
  302. Promise.resolve().then(() => {
  303. if (this.attachmentsView.hasChildNodes()) {
  304. return;
  305. }
  306. this.attachmentsButton.disabled = true;
  307. if (this.active === SidebarView.ATTACHMENTS) {
  308. this.switchView(SidebarView.THUMBS);
  309. }
  310. });
  311. });
  312. this.eventBus._on("presentationmodechanged", evt => {
  313. if (!evt.active && !evt.switchInProgress && this.isThumbnailViewVisible) {
  314. this._updateThumbnailViewer();
  315. }
  316. });
  317. }
  318. }
  319. exports.PDFSidebar = PDFSidebar;