2
0

secondary_toolbar.js 6.1 KB

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