pdf_sidebar.js 10 KB

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