pdf_sidebar.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.outerContainer = options.outerContainer;
  43. this.viewerContainer = options.viewerContainer;
  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() {
  68. var view = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : SidebarView.NONE;
  69. if (this.isInitialViewSet) {
  70. return;
  71. }
  72. this.isInitialViewSet = true;
  73. if (this.isOpen && view === SidebarView.NONE) {
  74. this._dispatchEvent();
  75. return;
  76. }
  77. var isViewPreserved = view === this.visibleView;
  78. this.switchView(view, true);
  79. if (isViewPreserved) {
  80. this._dispatchEvent();
  81. }
  82. }
  83. }, {
  84. key: 'switchView',
  85. value: function switchView(view) {
  86. var forceOpen = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  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. }, {
  146. key: 'open',
  147. value: function open() {
  148. if (this.isOpen) {
  149. return;
  150. }
  151. this.isOpen = true;
  152. this.toggleButton.classList.add('toggled');
  153. this.outerContainer.classList.add('sidebarMoving');
  154. this.outerContainer.classList.add('sidebarOpen');
  155. if (this.active === SidebarView.THUMBS) {
  156. this._updateThumbnailViewer();
  157. }
  158. this._forceRendering();
  159. this._dispatchEvent();
  160. this._hideUINotification(this.active);
  161. }
  162. }, {
  163. key: 'close',
  164. value: function close() {
  165. if (!this.isOpen) {
  166. return;
  167. }
  168. this.isOpen = false;
  169. this.toggleButton.classList.remove('toggled');
  170. this.outerContainer.classList.add('sidebarMoving');
  171. this.outerContainer.classList.remove('sidebarOpen');
  172. this._forceRendering();
  173. this._dispatchEvent();
  174. }
  175. }, {
  176. key: 'toggle',
  177. value: function toggle() {
  178. if (this.isOpen) {
  179. this.close();
  180. } else {
  181. this.open();
  182. }
  183. }
  184. }, {
  185. key: '_dispatchEvent',
  186. value: function _dispatchEvent() {
  187. this.eventBus.dispatch('sidebarviewchanged', {
  188. source: this,
  189. view: this.visibleView
  190. });
  191. }
  192. }, {
  193. key: '_forceRendering',
  194. value: function _forceRendering() {
  195. if (this.onToggled) {
  196. this.onToggled();
  197. } else {
  198. this.pdfViewer.forceRendering();
  199. this.pdfThumbnailViewer.forceRendering();
  200. }
  201. }
  202. }, {
  203. key: '_updateThumbnailViewer',
  204. value: function _updateThumbnailViewer() {
  205. var pdfViewer = this.pdfViewer,
  206. pdfThumbnailViewer = this.pdfThumbnailViewer;
  207. var pagesCount = pdfViewer.pagesCount;
  208. for (var pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
  209. var pageView = pdfViewer.getPageView(pageIndex);
  210. if (pageView && pageView.renderingState === _pdf_rendering_queue.RenderingStates.FINISHED) {
  211. var thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
  212. thumbnailView.setImage(pageView);
  213. }
  214. }
  215. pdfThumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
  216. }
  217. }, {
  218. key: '_showUINotification',
  219. value: function _showUINotification(view) {
  220. var _this = this;
  221. if (this.disableNotification) {
  222. return;
  223. }
  224. this.l10n.get('toggle_sidebar_notification.title', null, 'Toggle Sidebar (document contains outline/attachments)').then(function (msg) {
  225. _this.toggleButton.title = msg;
  226. });
  227. if (!this.isOpen) {
  228. this.toggleButton.classList.add(UI_NOTIFICATION_CLASS);
  229. } else if (view === this.active) {
  230. return;
  231. }
  232. switch (view) {
  233. case SidebarView.OUTLINE:
  234. this.outlineButton.classList.add(UI_NOTIFICATION_CLASS);
  235. break;
  236. case SidebarView.ATTACHMENTS:
  237. this.attachmentsButton.classList.add(UI_NOTIFICATION_CLASS);
  238. break;
  239. }
  240. }
  241. }, {
  242. key: '_hideUINotification',
  243. value: function _hideUINotification(view) {
  244. var _this2 = this;
  245. if (this.disableNotification) {
  246. return;
  247. }
  248. var removeNotification = function removeNotification(view) {
  249. switch (view) {
  250. case SidebarView.OUTLINE:
  251. _this2.outlineButton.classList.remove(UI_NOTIFICATION_CLASS);
  252. break;
  253. case SidebarView.ATTACHMENTS:
  254. _this2.attachmentsButton.classList.remove(UI_NOTIFICATION_CLASS);
  255. break;
  256. }
  257. };
  258. if (!this.isOpen && view !== null) {
  259. return;
  260. }
  261. this.toggleButton.classList.remove(UI_NOTIFICATION_CLASS);
  262. if (view !== null) {
  263. removeNotification(view);
  264. return;
  265. }
  266. for (view in SidebarView) {
  267. removeNotification(SidebarView[view]);
  268. }
  269. this.l10n.get('toggle_sidebar.title', null, 'Toggle Sidebar').then(function (msg) {
  270. _this2.toggleButton.title = msg;
  271. });
  272. }
  273. }, {
  274. key: '_addEventListeners',
  275. value: function _addEventListeners() {
  276. var _this3 = this;
  277. this.viewerContainer.addEventListener('transitionend', function (evt) {
  278. if (evt.target === _this3.viewerContainer) {
  279. _this3.outerContainer.classList.remove('sidebarMoving');
  280. }
  281. });
  282. this.thumbnailButton.addEventListener('click', function () {
  283. _this3.switchView(SidebarView.THUMBS);
  284. });
  285. this.outlineButton.addEventListener('click', function () {
  286. _this3.switchView(SidebarView.OUTLINE);
  287. });
  288. this.outlineButton.addEventListener('dblclick', function () {
  289. _this3.pdfOutlineViewer.toggleOutlineTree();
  290. });
  291. this.attachmentsButton.addEventListener('click', function () {
  292. _this3.switchView(SidebarView.ATTACHMENTS);
  293. });
  294. this.eventBus.on('outlineloaded', function (evt) {
  295. var outlineCount = evt.outlineCount;
  296. _this3.outlineButton.disabled = !outlineCount;
  297. if (outlineCount) {
  298. _this3._showUINotification(SidebarView.OUTLINE);
  299. } else if (_this3.active === SidebarView.OUTLINE) {
  300. _this3.switchView(SidebarView.THUMBS);
  301. }
  302. });
  303. this.eventBus.on('attachmentsloaded', function (evt) {
  304. if (evt.attachmentsCount) {
  305. _this3.attachmentsButton.disabled = false;
  306. _this3._showUINotification(SidebarView.ATTACHMENTS);
  307. return;
  308. }
  309. Promise.resolve().then(function () {
  310. if (_this3.attachmentsView.hasChildNodes()) {
  311. return;
  312. }
  313. _this3.attachmentsButton.disabled = true;
  314. if (_this3.active === SidebarView.ATTACHMENTS) {
  315. _this3.switchView(SidebarView.THUMBS);
  316. }
  317. });
  318. });
  319. this.eventBus.on('presentationmodechanged', function (evt) {
  320. if (!evt.active && !evt.switchInProgress && _this3.isThumbnailViewVisible) {
  321. _this3._updateThumbnailViewer();
  322. }
  323. });
  324. }
  325. }, {
  326. key: 'visibleView',
  327. get: function get() {
  328. return this.isOpen ? this.active : SidebarView.NONE;
  329. }
  330. }, {
  331. key: 'isThumbnailViewVisible',
  332. get: function get() {
  333. return this.isOpen && this.active === SidebarView.THUMBS;
  334. }
  335. }, {
  336. key: 'isOutlineViewVisible',
  337. get: function get() {
  338. return this.isOpen && this.active === SidebarView.OUTLINE;
  339. }
  340. }, {
  341. key: 'isAttachmentsViewVisible',
  342. get: function get() {
  343. return this.isOpen && this.active === SidebarView.ATTACHMENTS;
  344. }
  345. }]);
  346. return PDFSidebar;
  347. }();
  348. exports.SidebarView = SidebarView;
  349. exports.PDFSidebar = PDFSidebar;