toolbar.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.Toolbar = void 0;
  27. var _ui_utils = require("./ui_utils");
  28. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  29. 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); } }
  30. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  31. var PAGE_NUMBER_LOADING_INDICATOR = 'visiblePageIsLoading';
  32. var SCALE_SELECT_CONTAINER_PADDING = 8;
  33. var SCALE_SELECT_PADDING = 22;
  34. var Toolbar =
  35. /*#__PURE__*/
  36. function () {
  37. function Toolbar(options, eventBus) {
  38. var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
  39. _classCallCheck(this, Toolbar);
  40. this.toolbar = options.container;
  41. this.eventBus = eventBus;
  42. this.l10n = l10n;
  43. this.items = options;
  44. this._wasLocalized = false;
  45. this.reset();
  46. this._bindListeners();
  47. }
  48. _createClass(Toolbar, [{
  49. key: "setPageNumber",
  50. value: function setPageNumber(pageNumber, pageLabel) {
  51. this.pageNumber = pageNumber;
  52. this.pageLabel = pageLabel;
  53. this._updateUIState(false);
  54. }
  55. }, {
  56. key: "setPagesCount",
  57. value: function setPagesCount(pagesCount, hasPageLabels) {
  58. this.pagesCount = pagesCount;
  59. this.hasPageLabels = hasPageLabels;
  60. this._updateUIState(true);
  61. }
  62. }, {
  63. key: "setPageScale",
  64. value: function setPageScale(pageScaleValue, pageScale) {
  65. this.pageScaleValue = (pageScaleValue || pageScale).toString();
  66. this.pageScale = pageScale;
  67. this._updateUIState(false);
  68. }
  69. }, {
  70. key: "reset",
  71. value: function reset() {
  72. this.pageNumber = 0;
  73. this.pageLabel = null;
  74. this.hasPageLabels = false;
  75. this.pagesCount = 0;
  76. this.pageScaleValue = _ui_utils.DEFAULT_SCALE_VALUE;
  77. this.pageScale = _ui_utils.DEFAULT_SCALE;
  78. this._updateUIState(true);
  79. this.updateLoadingIndicatorState();
  80. }
  81. }, {
  82. key: "_bindListeners",
  83. value: function _bindListeners() {
  84. var _this = this;
  85. var eventBus = this.eventBus,
  86. items = this.items;
  87. var self = this;
  88. items.previous.addEventListener('click', function () {
  89. eventBus.dispatch('previouspage', {
  90. source: self
  91. });
  92. });
  93. items.next.addEventListener('click', function () {
  94. eventBus.dispatch('nextpage', {
  95. source: self
  96. });
  97. });
  98. items.zoomIn.addEventListener('click', function () {
  99. eventBus.dispatch('zoomin', {
  100. source: self
  101. });
  102. });
  103. items.zoomOut.addEventListener('click', function () {
  104. eventBus.dispatch('zoomout', {
  105. source: self
  106. });
  107. });
  108. items.pageNumber.addEventListener('click', function () {
  109. this.select();
  110. });
  111. items.pageNumber.addEventListener('change', function () {
  112. eventBus.dispatch('pagenumberchanged', {
  113. source: self,
  114. value: this.value
  115. });
  116. });
  117. items.scaleSelect.addEventListener('change', function () {
  118. if (this.value === 'custom') {
  119. return;
  120. }
  121. eventBus.dispatch('scalechanged', {
  122. source: self,
  123. value: this.value
  124. });
  125. });
  126. items.presentationModeButton.addEventListener('click', function () {
  127. eventBus.dispatch('presentationmode', {
  128. source: self
  129. });
  130. });
  131. items.openFile.addEventListener('click', function () {
  132. eventBus.dispatch('openfile', {
  133. source: self
  134. });
  135. });
  136. items.print.addEventListener('click', function () {
  137. eventBus.dispatch('print', {
  138. source: self
  139. });
  140. });
  141. items.download.addEventListener('click', function () {
  142. eventBus.dispatch('download', {
  143. source: self
  144. });
  145. });
  146. items.scaleSelect.oncontextmenu = _ui_utils.noContextMenuHandler;
  147. eventBus.on('localized', function () {
  148. _this._localized();
  149. });
  150. }
  151. }, {
  152. key: "_localized",
  153. value: function _localized() {
  154. this._wasLocalized = true;
  155. this._adjustScaleWidth();
  156. this._updateUIState(true);
  157. }
  158. }, {
  159. key: "_updateUIState",
  160. value: function _updateUIState() {
  161. var resetNumPages = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  162. if (!this._wasLocalized) {
  163. return;
  164. }
  165. var pageNumber = this.pageNumber,
  166. pagesCount = this.pagesCount,
  167. pageScaleValue = this.pageScaleValue,
  168. pageScale = this.pageScale,
  169. items = this.items;
  170. if (resetNumPages) {
  171. if (this.hasPageLabels) {
  172. items.pageNumber.type = 'text';
  173. } else {
  174. items.pageNumber.type = 'number';
  175. this.l10n.get('of_pages', {
  176. pagesCount: pagesCount
  177. }, 'of {{pagesCount}}').then(function (msg) {
  178. items.numPages.textContent = msg;
  179. });
  180. }
  181. items.pageNumber.max = pagesCount;
  182. }
  183. if (this.hasPageLabels) {
  184. items.pageNumber.value = this.pageLabel;
  185. this.l10n.get('page_of_pages', {
  186. pageNumber: pageNumber,
  187. pagesCount: pagesCount
  188. }, '({{pageNumber}} of {{pagesCount}})').then(function (msg) {
  189. items.numPages.textContent = msg;
  190. });
  191. } else {
  192. items.pageNumber.value = pageNumber;
  193. }
  194. items.previous.disabled = pageNumber <= 1;
  195. items.next.disabled = pageNumber >= pagesCount;
  196. items.zoomOut.disabled = pageScale <= _ui_utils.MIN_SCALE;
  197. items.zoomIn.disabled = pageScale >= _ui_utils.MAX_SCALE;
  198. var customScale = Math.round(pageScale * 10000) / 100;
  199. this.l10n.get('page_scale_percent', {
  200. scale: customScale
  201. }, '{{scale}}%').then(function (msg) {
  202. var options = items.scaleSelect.options;
  203. var predefinedValueFound = false;
  204. for (var i = 0, ii = options.length; i < ii; i++) {
  205. var option = options[i];
  206. if (option.value !== pageScaleValue) {
  207. option.selected = false;
  208. continue;
  209. }
  210. option.selected = true;
  211. predefinedValueFound = true;
  212. }
  213. if (!predefinedValueFound) {
  214. items.customScaleOption.textContent = msg;
  215. items.customScaleOption.selected = true;
  216. }
  217. });
  218. }
  219. }, {
  220. key: "updateLoadingIndicatorState",
  221. value: function updateLoadingIndicatorState() {
  222. var loading = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  223. var pageNumberInput = this.items.pageNumber;
  224. pageNumberInput.classList.toggle(PAGE_NUMBER_LOADING_INDICATOR, loading);
  225. }
  226. }, {
  227. key: "_adjustScaleWidth",
  228. value: function _adjustScaleWidth() {
  229. var container = this.items.scaleSelectContainer;
  230. var select = this.items.scaleSelect;
  231. _ui_utils.animationStarted.then(function () {
  232. if (container.clientWidth === 0) {
  233. container.setAttribute('style', 'display: inherit;');
  234. }
  235. if (container.clientWidth > 0) {
  236. select.setAttribute('style', 'min-width: inherit;');
  237. var width = select.clientWidth + SCALE_SELECT_CONTAINER_PADDING;
  238. select.setAttribute('style', 'min-width: ' + (width + SCALE_SELECT_PADDING) + 'px;');
  239. container.setAttribute('style', 'min-width: ' + width + 'px; ' + 'max-width: ' + width + 'px;');
  240. }
  241. });
  242. }
  243. }]);
  244. return Toolbar;
  245. }();
  246. exports.Toolbar = Toolbar;