pdf_sidebar.js 12 KB

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