2
0

pdf_sidebar.js 12 KB

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