pdf_sidebar.js 13 KB

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