secondary_toolbar.js 5.7 KB

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