secondary_toolbar.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.SecondaryToolbar = 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 _pdf_cursor_tools = require('./pdf_cursor_tools');
  29. var _ui_utils = require('./ui_utils');
  30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  31. var SecondaryToolbar = function () {
  32. function SecondaryToolbar(options, mainContainer, eventBus) {
  33. _classCallCheck(this, SecondaryToolbar);
  34. this.toolbar = options.toolbar;
  35. this.toggleButton = options.toggleButton;
  36. this.toolbarButtonContainer = options.toolbarButtonContainer;
  37. this.buttons = [{
  38. element: options.presentationModeButton,
  39. eventName: 'presentationmode',
  40. close: true
  41. }, {
  42. element: options.openFileButton,
  43. eventName: 'openfile',
  44. close: true
  45. }, {
  46. element: options.printButton,
  47. eventName: 'print',
  48. close: true
  49. }, {
  50. element: options.downloadButton,
  51. eventName: 'download',
  52. close: true
  53. }, {
  54. element: options.viewBookmarkButton,
  55. eventName: null,
  56. close: true
  57. }, {
  58. element: options.firstPageButton,
  59. eventName: 'firstpage',
  60. close: true
  61. }, {
  62. element: options.lastPageButton,
  63. eventName: 'lastpage',
  64. close: true
  65. }, {
  66. element: options.pageRotateCwButton,
  67. eventName: 'rotatecw',
  68. close: false
  69. }, {
  70. element: options.pageRotateCcwButton,
  71. eventName: 'rotateccw',
  72. close: false
  73. }, {
  74. element: options.cursorSelectToolButton,
  75. eventName: 'switchcursortool',
  76. eventDetails: { tool: _pdf_cursor_tools.CursorTool.SELECT },
  77. close: true
  78. }, {
  79. element: options.cursorHandToolButton,
  80. eventName: 'switchcursortool',
  81. eventDetails: { tool: _pdf_cursor_tools.CursorTool.HAND },
  82. close: true
  83. }, {
  84. element: options.documentPropertiesButton,
  85. eventName: 'documentproperties',
  86. close: true
  87. }];
  88. this.items = {
  89. firstPage: options.firstPageButton,
  90. lastPage: options.lastPageButton,
  91. pageRotateCw: options.pageRotateCwButton,
  92. pageRotateCcw: options.pageRotateCcwButton
  93. };
  94. this.mainContainer = mainContainer;
  95. this.eventBus = eventBus;
  96. this.opened = false;
  97. this.containerHeight = null;
  98. this.previousContainerHeight = null;
  99. this.reset();
  100. this._bindClickListeners();
  101. this._bindCursorToolsListener(options);
  102. this.eventBus.on('resize', this._setMaxHeight.bind(this));
  103. }
  104. _createClass(SecondaryToolbar, [{
  105. key: 'setPageNumber',
  106. value: function setPageNumber(pageNumber) {
  107. this.pageNumber = pageNumber;
  108. this._updateUIState();
  109. }
  110. }, {
  111. key: 'setPagesCount',
  112. value: function setPagesCount(pagesCount) {
  113. this.pagesCount = pagesCount;
  114. this._updateUIState();
  115. }
  116. }, {
  117. key: 'reset',
  118. value: function reset() {
  119. this.pageNumber = 0;
  120. this.pagesCount = 0;
  121. this._updateUIState();
  122. }
  123. }, {
  124. key: '_updateUIState',
  125. value: function _updateUIState() {
  126. this.items.firstPage.disabled = this.pageNumber <= 1;
  127. this.items.lastPage.disabled = this.pageNumber >= this.pagesCount;
  128. this.items.pageRotateCw.disabled = this.pagesCount === 0;
  129. this.items.pageRotateCcw.disabled = this.pagesCount === 0;
  130. }
  131. }, {
  132. key: '_bindClickListeners',
  133. value: function _bindClickListeners() {
  134. var _this = this;
  135. this.toggleButton.addEventListener('click', this.toggle.bind(this));
  136. var _loop = function _loop(button) {
  137. var _buttons$button = _this.buttons[button],
  138. element = _buttons$button.element,
  139. eventName = _buttons$button.eventName,
  140. close = _buttons$button.close,
  141. eventDetails = _buttons$button.eventDetails;
  142. element.addEventListener('click', function (evt) {
  143. if (eventName !== null) {
  144. var details = { source: _this };
  145. for (var property in eventDetails) {
  146. details[property] = eventDetails[property];
  147. }
  148. _this.eventBus.dispatch(eventName, details);
  149. }
  150. if (close) {
  151. _this.close();
  152. }
  153. });
  154. };
  155. for (var button in this.buttons) {
  156. _loop(button);
  157. }
  158. }
  159. }, {
  160. key: '_bindCursorToolsListener',
  161. value: function _bindCursorToolsListener(buttons) {
  162. this.eventBus.on('cursortoolchanged', function (evt) {
  163. buttons.cursorSelectToolButton.classList.remove('toggled');
  164. buttons.cursorHandToolButton.classList.remove('toggled');
  165. switch (evt.tool) {
  166. case _pdf_cursor_tools.CursorTool.SELECT:
  167. buttons.cursorSelectToolButton.classList.add('toggled');
  168. break;
  169. case _pdf_cursor_tools.CursorTool.HAND:
  170. buttons.cursorHandToolButton.classList.add('toggled');
  171. break;
  172. }
  173. });
  174. }
  175. }, {
  176. key: 'open',
  177. value: function open() {
  178. if (this.opened) {
  179. return;
  180. }
  181. this.opened = true;
  182. this._setMaxHeight();
  183. this.toggleButton.classList.add('toggled');
  184. this.toolbar.classList.remove('hidden');
  185. }
  186. }, {
  187. key: 'close',
  188. value: function close() {
  189. if (!this.opened) {
  190. return;
  191. }
  192. this.opened = false;
  193. this.toolbar.classList.add('hidden');
  194. this.toggleButton.classList.remove('toggled');
  195. }
  196. }, {
  197. key: 'toggle',
  198. value: function toggle() {
  199. if (this.opened) {
  200. this.close();
  201. } else {
  202. this.open();
  203. }
  204. }
  205. }, {
  206. key: '_setMaxHeight',
  207. value: function _setMaxHeight() {
  208. if (!this.opened) {
  209. return;
  210. }
  211. this.containerHeight = this.mainContainer.clientHeight;
  212. if (this.containerHeight === this.previousContainerHeight) {
  213. return;
  214. }
  215. this.toolbarButtonContainer.setAttribute('style', 'max-height: ' + (this.containerHeight - _ui_utils.SCROLLBAR_PADDING) + 'px;');
  216. this.previousContainerHeight = this.containerHeight;
  217. }
  218. }, {
  219. key: 'isOpen',
  220. get: function get() {
  221. return this.opened;
  222. }
  223. }]);
  224. return SecondaryToolbar;
  225. }();
  226. exports.SecondaryToolbar = SecondaryToolbar;