pdf_sidebar.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PDFSidebar = exports.SidebarView = undefined;
  20. var _ui_utils = require('pdfjs-web/ui_utils');
  21. var _pdf_rendering_queue = require('pdfjs-web/pdf_rendering_queue');
  22. var UI_NOTIFICATION_CLASS = 'pdfSidebarNotification';
  23. var SidebarView = {
  24. NONE: 0,
  25. THUMBS: 1,
  26. OUTLINE: 2,
  27. ATTACHMENTS: 3
  28. };
  29. var PDFSidebar = function PDFSidebarClosure() {
  30. function PDFSidebar(options) {
  31. this.isOpen = false;
  32. this.active = SidebarView.THUMBS;
  33. this.isInitialViewSet = false;
  34. this.onToggled = null;
  35. this.pdfViewer = options.pdfViewer;
  36. this.pdfThumbnailViewer = options.pdfThumbnailViewer;
  37. this.pdfOutlineViewer = options.pdfOutlineViewer;
  38. this.mainContainer = options.mainContainer;
  39. this.outerContainer = options.outerContainer;
  40. this.eventBus = options.eventBus;
  41. this.toggleButton = options.toggleButton;
  42. this.thumbnailButton = options.thumbnailButton;
  43. this.outlineButton = options.outlineButton;
  44. this.attachmentsButton = options.attachmentsButton;
  45. this.thumbnailView = options.thumbnailView;
  46. this.outlineView = options.outlineView;
  47. this.attachmentsView = options.attachmentsView;
  48. this.disableNotification = options.disableNotification || false;
  49. this._addEventListeners();
  50. }
  51. PDFSidebar.prototype = {
  52. reset: function PDFSidebar_reset() {
  53. this.isInitialViewSet = false;
  54. this._hideUINotification(null);
  55. this.switchView(SidebarView.THUMBS);
  56. this.outlineButton.disabled = false;
  57. this.attachmentsButton.disabled = false;
  58. },
  59. get visibleView() {
  60. return this.isOpen ? this.active : SidebarView.NONE;
  61. },
  62. get isThumbnailViewVisible() {
  63. return this.isOpen && this.active === SidebarView.THUMBS;
  64. },
  65. get isOutlineViewVisible() {
  66. return this.isOpen && this.active === SidebarView.OUTLINE;
  67. },
  68. get isAttachmentsViewVisible() {
  69. return this.isOpen && this.active === SidebarView.ATTACHMENTS;
  70. },
  71. setInitialView: function PDFSidebar_setInitialView(view) {
  72. if (this.isInitialViewSet) {
  73. return;
  74. }
  75. this.isInitialViewSet = true;
  76. if (this.isOpen && view === SidebarView.NONE) {
  77. this._dispatchEvent();
  78. return;
  79. }
  80. var isViewPreserved = view === this.visibleView;
  81. this.switchView(view, true);
  82. if (isViewPreserved) {
  83. this._dispatchEvent();
  84. }
  85. },
  86. switchView: function PDFSidebar_switchView(view, forceOpen) {
  87. if (view === SidebarView.NONE) {
  88. this.close();
  89. return;
  90. }
  91. var isViewChanged = view !== this.active;
  92. var shouldForceRendering = false;
  93. switch (view) {
  94. case SidebarView.THUMBS:
  95. this.thumbnailButton.classList.add('toggled');
  96. this.outlineButton.classList.remove('toggled');
  97. this.attachmentsButton.classList.remove('toggled');
  98. this.thumbnailView.classList.remove('hidden');
  99. this.outlineView.classList.add('hidden');
  100. this.attachmentsView.classList.add('hidden');
  101. if (this.isOpen && isViewChanged) {
  102. this._updateThumbnailViewer();
  103. shouldForceRendering = true;
  104. }
  105. break;
  106. case SidebarView.OUTLINE:
  107. if (this.outlineButton.disabled) {
  108. return;
  109. }
  110. this.thumbnailButton.classList.remove('toggled');
  111. this.outlineButton.classList.add('toggled');
  112. this.attachmentsButton.classList.remove('toggled');
  113. this.thumbnailView.classList.add('hidden');
  114. this.outlineView.classList.remove('hidden');
  115. this.attachmentsView.classList.add('hidden');
  116. break;
  117. case SidebarView.ATTACHMENTS:
  118. if (this.attachmentsButton.disabled) {
  119. return;
  120. }
  121. this.thumbnailButton.classList.remove('toggled');
  122. this.outlineButton.classList.remove('toggled');
  123. this.attachmentsButton.classList.add('toggled');
  124. this.thumbnailView.classList.add('hidden');
  125. this.outlineView.classList.add('hidden');
  126. this.attachmentsView.classList.remove('hidden');
  127. break;
  128. default:
  129. console.error('PDFSidebar_switchView: "' + view + '" is an unsupported value.');
  130. return;
  131. }
  132. this.active = view | 0;
  133. if (forceOpen && !this.isOpen) {
  134. this.open();
  135. return;
  136. }
  137. if (shouldForceRendering) {
  138. this._forceRendering();
  139. }
  140. if (isViewChanged) {
  141. this._dispatchEvent();
  142. }
  143. this._hideUINotification(this.active);
  144. },
  145. open: function PDFSidebar_open() {
  146. if (this.isOpen) {
  147. return;
  148. }
  149. this.isOpen = true;
  150. this.toggleButton.classList.add('toggled');
  151. this.outerContainer.classList.add('sidebarMoving');
  152. this.outerContainer.classList.add('sidebarOpen');
  153. if (this.active === SidebarView.THUMBS) {
  154. this._updateThumbnailViewer();
  155. }
  156. this._forceRendering();
  157. this._dispatchEvent();
  158. this._hideUINotification(this.active);
  159. },
  160. close: function PDFSidebar_close() {
  161. if (!this.isOpen) {
  162. return;
  163. }
  164. this.isOpen = false;
  165. this.toggleButton.classList.remove('toggled');
  166. this.outerContainer.classList.add('sidebarMoving');
  167. this.outerContainer.classList.remove('sidebarOpen');
  168. this._forceRendering();
  169. this._dispatchEvent();
  170. },
  171. toggle: function PDFSidebar_toggle() {
  172. if (this.isOpen) {
  173. this.close();
  174. } else {
  175. this.open();
  176. }
  177. },
  178. _dispatchEvent: function PDFSidebar_dispatchEvent() {
  179. this.eventBus.dispatch('sidebarviewchanged', {
  180. source: this,
  181. view: this.visibleView
  182. });
  183. },
  184. _forceRendering: function PDFSidebar_forceRendering() {
  185. if (this.onToggled) {
  186. this.onToggled();
  187. } else {
  188. this.pdfViewer.forceRendering();
  189. this.pdfThumbnailViewer.forceRendering();
  190. }
  191. },
  192. _updateThumbnailViewer: function PDFSidebar_updateThumbnailViewer() {
  193. var pdfViewer = this.pdfViewer;
  194. var thumbnailViewer = this.pdfThumbnailViewer;
  195. var pagesCount = pdfViewer.pagesCount;
  196. for (var pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  197. var pageView = pdfViewer.getPageView(pageIndex);
  198. if (pageView && pageView.renderingState === _pdf_rendering_queue.RenderingStates.FINISHED) {
  199. var thumbnailView = thumbnailViewer.getThumbnail(pageIndex);
  200. thumbnailView.setImage(pageView);
  201. }
  202. }
  203. thumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  204. },
  205. _showUINotification: function (view) {
  206. if (this.disableNotification) {
  207. return;
  208. }
  209. this.toggleButton.title = _ui_utils.mozL10n.get('toggle_sidebar_notification.title', null, 'Toggle Sidebar (document contains outline/attachments)');
  210. if (!this.isOpen) {
  211. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  212. } else if (view === this.active) {
  213. return;
  214. }
  215. switch (view) {
  216. case SidebarView.OUTLINE:
  217. this.outlineButton.classList.add(UI_NOTIFICATION_CLASS);
  218. break;
  219. case SidebarView.ATTACHMENTS:
  220. this.attachmentsButton.classList.add(UI_NOTIFICATION_CLASS);
  221. break;
  222. }
  223. },
  224. _hideUINotification: function (view) {
  225. if (this.disableNotification) {
  226. return;
  227. }
  228. var removeNotification = function (view) {
  229. switch (view) {
  230. case SidebarView.OUTLINE:
  231. this.outlineButton.classList.remove(UI_NOTIFICATION_CLASS);
  232. break;
  233. case SidebarView.ATTACHMENTS:
  234. this.attachmentsButton.classList.remove(UI_NOTIFICATION_CLASS);
  235. break;
  236. }
  237. }.bind(this);
  238. if (!this.isOpen && view !== null) {
  239. return;
  240. }
  241. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  242. if (view !== null) {
  243. removeNotification(view);
  244. return;
  245. }
  246. for (view in SidebarView) {
  247. removeNotification(SidebarView[view]);
  248. }
  249. this.toggleButton.title = _ui_utils.mozL10n.get('toggle_sidebar.title', null, 'Toggle Sidebar');
  250. },
  251. _addEventListeners: function PDFSidebar_addEventListeners() {
  252. var self = this;
  253. self.mainContainer.addEventListener('transitionend', function (evt) {
  254. if (evt.target === this) {
  255. self.outerContainer.classList.remove('sidebarMoving');
  256. }
  257. });
  258. self.thumbnailButton.addEventListener('click', function () {
  259. self.switchView(SidebarView.THUMBS);
  260. });
  261. self.outlineButton.addEventListener('click', function () {
  262. self.switchView(SidebarView.OUTLINE);
  263. });
  264. self.outlineButton.addEventListener('dblclick', function () {
  265. self.pdfOutlineViewer.toggleOutlineTree();
  266. });
  267. self.attachmentsButton.addEventListener('click', function () {
  268. self.switchView(SidebarView.ATTACHMENTS);
  269. });
  270. self.eventBus.on('outlineloaded', function (e) {
  271. var outlineCount = e.outlineCount;
  272. self.outlineButton.disabled = !outlineCount;
  273. if (outlineCount) {
  274. self._showUINotification(SidebarView.OUTLINE);
  275. } else if (self.active === SidebarView.OUTLINE) {
  276. self.switchView(SidebarView.THUMBS);
  277. }
  278. });
  279. self.eventBus.on('attachmentsloaded', function (e) {
  280. var attachmentsCount = e.attachmentsCount;
  281. self.attachmentsButton.disabled = !attachmentsCount;
  282. if (attachmentsCount) {
  283. self._showUINotification(SidebarView.ATTACHMENTS);
  284. } else if (self.active === SidebarView.ATTACHMENTS) {
  285. self.switchView(SidebarView.THUMBS);
  286. }
  287. });
  288. self.eventBus.on('presentationmodechanged', function (e) {
  289. if (!e.active && !e.switchInProgress && self.isThumbnailViewVisible) {
  290. self._updateThumbnailViewer();
  291. }
  292. });
  293. }
  294. };
  295. return PDFSidebar;
  296. }();
  297. exports.SidebarView = SidebarView;
  298. exports.PDFSidebar = PDFSidebar;