pdf_sidebar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. 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.viewerContainer = elements.viewerContainer;
  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. this.thumbnailButton.classList.toggle("toggled", view === _ui_utils.SidebarView.THUMBS);
  136. this.outlineButton.classList.toggle("toggled", view === _ui_utils.SidebarView.OUTLINE);
  137. this.attachmentsButton.classList.toggle("toggled", view === _ui_utils.SidebarView.ATTACHMENTS);
  138. this.layersButton.classList.toggle("toggled", view === _ui_utils.SidebarView.LAYERS);
  139. this.thumbnailView.classList.toggle("hidden", view !== _ui_utils.SidebarView.THUMBS);
  140. this.outlineView.classList.toggle("hidden", view !== _ui_utils.SidebarView.OUTLINE);
  141. this.attachmentsView.classList.toggle("hidden", view !== _ui_utils.SidebarView.ATTACHMENTS);
  142. this.layersView.classList.toggle("hidden", view !== _ui_utils.SidebarView.LAYERS);
  143. this._outlineOptionsContainer.classList.toggle("hidden", view !== _ui_utils.SidebarView.OUTLINE);
  144. if (forceOpen && !this.isOpen) {
  145. this.open();
  146. return true;
  147. }
  148. if (shouldForceRendering) {
  149. this._updateThumbnailViewer();
  150. this._forceRendering();
  151. }
  152. if (isViewChanged) {
  153. this._dispatchEvent();
  154. }
  155. return isViewChanged;
  156. }
  157. open() {
  158. if (this.isOpen) {
  159. return;
  160. }
  161. this.isOpen = true;
  162. this.toggleButton.classList.add("toggled");
  163. this.toggleButton.setAttribute("aria-expanded", "true");
  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.toggleButton.setAttribute("aria-expanded", "false");
  179. this.outerContainer.classList.add("sidebarMoving");
  180. this.outerContainer.classList.remove("sidebarOpen");
  181. this._forceRendering();
  182. this._dispatchEvent();
  183. }
  184. toggle() {
  185. if (this.isOpen) {
  186. this.close();
  187. } else {
  188. this.open();
  189. }
  190. }
  191. _dispatchEvent() {
  192. this.eventBus.dispatch("sidebarviewchanged", {
  193. source: this,
  194. view: this.visibleView
  195. });
  196. }
  197. _forceRendering() {
  198. if (this.onToggled) {
  199. this.onToggled();
  200. } else {
  201. this.pdfViewer.forceRendering();
  202. this.pdfThumbnailViewer.forceRendering();
  203. }
  204. }
  205. _updateThumbnailViewer() {
  206. const {
  207. pdfViewer,
  208. pdfThumbnailViewer
  209. } = this;
  210. const pagesCount = pdfViewer.pagesCount;
  211. for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  212. const pageView = pdfViewer.getPageView(pageIndex);
  213. if (pageView?.renderingState === _ui_utils.RenderingStates.FINISHED) {
  214. const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
  215. thumbnailView.setImage(pageView);
  216. }
  217. }
  218. pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  219. }
  220. _showUINotification() {
  221. this.l10n.get("toggle_sidebar_notification2.title").then(msg => {
  222. this.toggleButton.title = msg;
  223. });
  224. if (!this.isOpen) {
  225. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  226. }
  227. }
  228. _hideUINotification(reset = false) {
  229. if (this.isOpen || reset) {
  230. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  231. }
  232. if (reset) {
  233. this.l10n.get("toggle_sidebar.title").then(msg => {
  234. this.toggleButton.title = msg;
  235. });
  236. }
  237. }
  238. _addEventListeners() {
  239. this.viewerContainer.addEventListener("transitionend", evt => {
  240. if (evt.target === this.viewerContainer) {
  241. this.outerContainer.classList.remove("sidebarMoving");
  242. }
  243. });
  244. this.toggleButton.addEventListener("click", () => {
  245. this.toggle();
  246. });
  247. this.thumbnailButton.addEventListener("click", () => {
  248. this.switchView(_ui_utils.SidebarView.THUMBS);
  249. });
  250. this.outlineButton.addEventListener("click", () => {
  251. this.switchView(_ui_utils.SidebarView.OUTLINE);
  252. });
  253. this.outlineButton.addEventListener("dblclick", () => {
  254. this.eventBus.dispatch("toggleoutlinetree", {
  255. source: this
  256. });
  257. });
  258. this.attachmentsButton.addEventListener("click", () => {
  259. this.switchView(_ui_utils.SidebarView.ATTACHMENTS);
  260. });
  261. this.layersButton.addEventListener("click", () => {
  262. this.switchView(_ui_utils.SidebarView.LAYERS);
  263. });
  264. this.layersButton.addEventListener("dblclick", () => {
  265. this.eventBus.dispatch("resetlayers", {
  266. source: this
  267. });
  268. });
  269. this._currentOutlineItemButton.addEventListener("click", () => {
  270. this.eventBus.dispatch("currentoutlineitem", {
  271. source: this
  272. });
  273. });
  274. const onTreeLoaded = (count, button, view) => {
  275. button.disabled = !count;
  276. if (count) {
  277. this._showUINotification();
  278. } else if (this.active === view) {
  279. this.switchView(_ui_utils.SidebarView.THUMBS);
  280. }
  281. };
  282. this.eventBus._on("outlineloaded", evt => {
  283. onTreeLoaded(evt.outlineCount, this.outlineButton, _ui_utils.SidebarView.OUTLINE);
  284. evt.currentOutlineItemPromise.then(enabled => {
  285. if (!this.isInitialViewSet) {
  286. return;
  287. }
  288. this._currentOutlineItemButton.disabled = !enabled;
  289. });
  290. });
  291. this.eventBus._on("attachmentsloaded", evt => {
  292. onTreeLoaded(evt.attachmentsCount, this.attachmentsButton, _ui_utils.SidebarView.ATTACHMENTS);
  293. });
  294. this.eventBus._on("layersloaded", evt => {
  295. onTreeLoaded(evt.layersCount, this.layersButton, _ui_utils.SidebarView.LAYERS);
  296. });
  297. this.eventBus._on("presentationmodechanged", evt => {
  298. if (evt.state === _ui_utils.PresentationModeState.NORMAL && this.isThumbnailViewVisible) {
  299. this._updateThumbnailViewer();
  300. }
  301. });
  302. }
  303. }
  304. exports.PDFSidebar = PDFSidebar;