pdf_sidebar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _ui_utils = require('./ui_utils');
  22. var _pdf_rendering_queue = require('./pdf_rendering_queue');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var UI_NOTIFICATION_CLASS = 'pdfSidebarNotification';
  25. var SidebarView = {
  26. NONE: 0,
  27. THUMBS: 1,
  28. OUTLINE: 2,
  29. ATTACHMENTS: 3
  30. };
  31. var PDFSidebar = function () {
  32. function PDFSidebar(options) {
  33. _classCallCheck(this, PDFSidebar);
  34. this.isOpen = false;
  35. this.active = SidebarView.THUMBS;
  36. this.isInitialViewSet = false;
  37. this.onToggled = null;
  38. this.pdfViewer = options.pdfViewer;
  39. this.pdfThumbnailViewer = options.pdfThumbnailViewer;
  40. this.pdfOutlineViewer = options.pdfOutlineViewer;
  41. this.mainContainer = options.mainContainer;
  42. this.outerContainer = options.outerContainer;
  43. this.eventBus = options.eventBus;
  44. this.toggleButton = options.toggleButton;
  45. this.thumbnailButton = options.thumbnailButton;
  46. this.outlineButton = options.outlineButton;
  47. this.attachmentsButton = options.attachmentsButton;
  48. this.thumbnailView = options.thumbnailView;
  49. this.outlineView = options.outlineView;
  50. this.attachmentsView = options.attachmentsView;
  51. this.disableNotification = options.disableNotification || false;
  52. this._addEventListeners();
  53. }
  54. _createClass(PDFSidebar, [{
  55. key: 'reset',
  56. value: function reset() {
  57. this.isInitialViewSet = false;
  58. this._hideUINotification(null);
  59. this.switchView(SidebarView.THUMBS);
  60. this.outlineButton.disabled = false;
  61. this.attachmentsButton.disabled = false;
  62. }
  63. }, {
  64. key: 'setInitialView',
  65. value: function setInitialView(view) {
  66. if (this.isInitialViewSet) {
  67. return;
  68. }
  69. this.isInitialViewSet = true;
  70. if (this.isOpen && view === SidebarView.NONE) {
  71. this._dispatchEvent();
  72. return;
  73. }
  74. var isViewPreserved = view === this.visibleView;
  75. this.switchView(view, true);
  76. if (isViewPreserved) {
  77. this._dispatchEvent();
  78. }
  79. }
  80. }, {
  81. key: 'switchView',
  82. value: function switchView(view) {
  83. var forceOpen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  84. if (view === SidebarView.NONE) {
  85. this.close();
  86. return;
  87. }
  88. var isViewChanged = view !== this.active;
  89. var shouldForceRendering = false;
  90. switch (view) {
  91. case SidebarView.THUMBS:
  92. this.thumbnailButton.classList.add('toggled');
  93. this.outlineButton.classList.remove('toggled');
  94. this.attachmentsButton.classList.remove('toggled');
  95. this.thumbnailView.classList.remove('hidden');
  96. this.outlineView.classList.add('hidden');
  97. this.attachmentsView.classList.add('hidden');
  98. if (this.isOpen && isViewChanged) {
  99. this._updateThumbnailViewer();
  100. shouldForceRendering = true;
  101. }
  102. break;
  103. case SidebarView.OUTLINE:
  104. if (this.outlineButton.disabled) {
  105. return;
  106. }
  107. this.thumbnailButton.classList.remove('toggled');
  108. this.outlineButton.classList.add('toggled');
  109. this.attachmentsButton.classList.remove('toggled');
  110. this.thumbnailView.classList.add('hidden');
  111. this.outlineView.classList.remove('hidden');
  112. this.attachmentsView.classList.add('hidden');
  113. break;
  114. case SidebarView.ATTACHMENTS:
  115. if (this.attachmentsButton.disabled) {
  116. return;
  117. }
  118. this.thumbnailButton.classList.remove('toggled');
  119. this.outlineButton.classList.remove('toggled');
  120. this.attachmentsButton.classList.add('toggled');
  121. this.thumbnailView.classList.add('hidden');
  122. this.outlineView.classList.add('hidden');
  123. this.attachmentsView.classList.remove('hidden');
  124. break;
  125. default:
  126. console.error('PDFSidebar_switchView: "' + view + '" is an unsupported value.');
  127. return;
  128. }
  129. this.active = view | 0;
  130. if (forceOpen && !this.isOpen) {
  131. this.open();
  132. return;
  133. }
  134. if (shouldForceRendering) {
  135. this._forceRendering();
  136. }
  137. if (isViewChanged) {
  138. this._dispatchEvent();
  139. }
  140. this._hideUINotification(this.active);
  141. }
  142. }, {
  143. key: 'open',
  144. value: function open() {
  145. if (this.isOpen) {
  146. return;
  147. }
  148. this.isOpen = true;
  149. this.toggleButton.classList.add('toggled');
  150. this.outerContainer.classList.add('sidebarMoving');
  151. this.outerContainer.classList.add('sidebarOpen');
  152. if (this.active === SidebarView.THUMBS) {
  153. this._updateThumbnailViewer();
  154. }
  155. this._forceRendering();
  156. this._dispatchEvent();
  157. this._hideUINotification(this.active);
  158. }
  159. }, {
  160. key: 'close',
  161. value: function close() {
  162. if (!this.isOpen) {
  163. return;
  164. }
  165. this.isOpen = false;
  166. this.toggleButton.classList.remove('toggled');
  167. this.outerContainer.classList.add('sidebarMoving');
  168. this.outerContainer.classList.remove('sidebarOpen');
  169. this._forceRendering();
  170. this._dispatchEvent();
  171. }
  172. }, {
  173. key: 'toggle',
  174. value: function toggle() {
  175. if (this.isOpen) {
  176. this.close();
  177. } else {
  178. this.open();
  179. }
  180. }
  181. }, {
  182. key: '_dispatchEvent',
  183. value: function _dispatchEvent() {
  184. this.eventBus.dispatch('sidebarviewchanged', {
  185. source: this,
  186. view: this.visibleView
  187. });
  188. }
  189. }, {
  190. key: '_forceRendering',
  191. value: function _forceRendering() {
  192. if (this.onToggled) {
  193. this.onToggled();
  194. } else {
  195. this.pdfViewer.forceRendering();
  196. this.pdfThumbnailViewer.forceRendering();
  197. }
  198. }
  199. }, {
  200. key: '_updateThumbnailViewer',
  201. value: function _updateThumbnailViewer() {
  202. var pdfViewer = this.pdfViewer;
  203. var thumbnailViewer = this.pdfThumbnailViewer;
  204. var pagesCount = pdfViewer.pagesCount;
  205. for (var pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  206. var pageView = pdfViewer.getPageView(pageIndex);
  207. if (pageView && pageView.renderingState === _pdf_rendering_queue.RenderingStates.FINISHED) {
  208. var thumbnailView = thumbnailViewer.getThumbnail(pageIndex);
  209. thumbnailView.setImage(pageView);
  210. }
  211. }
  212. thumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  213. }
  214. }, {
  215. key: '_showUINotification',
  216. value: function _showUINotification(view) {
  217. if (this.disableNotification) {
  218. return;
  219. }
  220. this.toggleButton.title = _ui_utils.mozL10n.get('toggle_sidebar_notification.title', null, 'Toggle Sidebar (document contains outline/attachments)');
  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. }, {
  236. key: '_hideUINotification',
  237. value: function _hideUINotification(view) {
  238. var _this = this;
  239. if (this.disableNotification) {
  240. return;
  241. }
  242. var removeNotification = function removeNotification(view) {
  243. switch (view) {
  244. case SidebarView.OUTLINE:
  245. _this.outlineButton.classList.remove(UI_NOTIFICATION_CLASS);
  246. break;
  247. case SidebarView.ATTACHMENTS:
  248. _this.attachmentsButton.classList.remove(UI_NOTIFICATION_CLASS);
  249. break;
  250. }
  251. };
  252. if (!this.isOpen && view !== null) {
  253. return;
  254. }
  255. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  256. if (view !== null) {
  257. removeNotification(view);
  258. return;
  259. }
  260. for (view in SidebarView) {
  261. removeNotification(SidebarView[view]);
  262. }
  263. this.toggleButton.title = _ui_utils.mozL10n.get('toggle_sidebar.title', null, 'Toggle Sidebar');
  264. }
  265. }, {
  266. key: '_addEventListeners',
  267. value: function _addEventListeners() {
  268. var _this2 = this;
  269. this.mainContainer.addEventListener('transitionend', function (evt) {
  270. if (evt.target === _this2.mainContainer) {
  271. _this2.outerContainer.classList.remove('sidebarMoving');
  272. }
  273. });
  274. this.thumbnailButton.addEventListener('click', function () {
  275. _this2.switchView(SidebarView.THUMBS);
  276. });
  277. this.outlineButton.addEventListener('click', function () {
  278. _this2.switchView(SidebarView.OUTLINE);
  279. });
  280. this.outlineButton.addEventListener('dblclick', function () {
  281. _this2.pdfOutlineViewer.toggleOutlineTree();
  282. });
  283. this.attachmentsButton.addEventListener('click', function () {
  284. _this2.switchView(SidebarView.ATTACHMENTS);
  285. });
  286. this.eventBus.on('outlineloaded', function (evt) {
  287. var outlineCount = evt.outlineCount;
  288. _this2.outlineButton.disabled = !outlineCount;
  289. if (outlineCount) {
  290. _this2._showUINotification(SidebarView.OUTLINE);
  291. } else if (_this2.active === SidebarView.OUTLINE) {
  292. _this2.switchView(SidebarView.THUMBS);
  293. }
  294. });
  295. this.eventBus.on('attachmentsloaded', function (evt) {
  296. var attachmentsCount = evt.attachmentsCount;
  297. _this2.attachmentsButton.disabled = !attachmentsCount;
  298. if (attachmentsCount) {
  299. _this2._showUINotification(SidebarView.ATTACHMENTS);
  300. } else if (_this2.active === SidebarView.ATTACHMENTS) {
  301. _this2.switchView(SidebarView.THUMBS);
  302. }
  303. });
  304. this.eventBus.on('presentationmodechanged', function (evt) {
  305. if (!evt.active && !evt.switchInProgress && _this2.isThumbnailViewVisible) {
  306. _this2._updateThumbnailViewer();
  307. }
  308. });
  309. }
  310. }, {
  311. key: 'visibleView',
  312. get: function get() {
  313. return this.isOpen ? this.active : SidebarView.NONE;
  314. }
  315. }, {
  316. key: 'isThumbnailViewVisible',
  317. get: function get() {
  318. return this.isOpen && this.active === SidebarView.THUMBS;
  319. }
  320. }, {
  321. key: 'isOutlineViewVisible',
  322. get: function get() {
  323. return this.isOpen && this.active === SidebarView.OUTLINE;
  324. }
  325. }, {
  326. key: 'isAttachmentsViewVisible',
  327. get: function get() {
  328. return this.isOpen && this.active === SidebarView.ATTACHMENTS;
  329. }
  330. }]);
  331. return PDFSidebar;
  332. }();
  333. exports.SidebarView = SidebarView;
  334. exports.PDFSidebar = PDFSidebar;