2
0

secondary_toolbar.js 7.0 KB

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