secondary_toolbar.js 7.3 KB

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