pdf_sidebar.js 12 KB

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