pdf_sidebar_resizer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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.PDFSidebarResizer = undefined;
  27. 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; }; }();
  28. var _ui_utils = require('./ui_utils');
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. var SIDEBAR_WIDTH_VAR = '--sidebar-width';
  31. var SIDEBAR_MIN_WIDTH = 200;
  32. var SIDEBAR_RESIZING_CLASS = 'sidebarResizing';
  33. var PDFSidebarResizer = function () {
  34. function PDFSidebarResizer(options, eventBus) {
  35. var _this = this;
  36. var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
  37. _classCallCheck(this, PDFSidebarResizer);
  38. this.enabled = false;
  39. this.isRTL = false;
  40. this.sidebarOpen = false;
  41. this.doc = document.documentElement;
  42. this._width = null;
  43. this._outerContainerWidth = null;
  44. this._boundEvents = Object.create(null);
  45. this.outerContainer = options.outerContainer;
  46. this.resizer = options.resizer;
  47. this.eventBus = eventBus;
  48. this.l10n = l10n;
  49. if (typeof CSS === 'undefined' || typeof CSS.supports !== 'function' || !CSS.supports(SIDEBAR_WIDTH_VAR, 'calc(-1 * ' + SIDEBAR_MIN_WIDTH + 'px)')) {
  50. console.warn('PDFSidebarResizer: ' + 'The browser does not support resizing of the sidebar.');
  51. return;
  52. }
  53. this.enabled = true;
  54. this.resizer.classList.remove('hidden');
  55. this.l10n.getDirection().then(function (dir) {
  56. _this.isRTL = dir === 'rtl';
  57. });
  58. this._addEventListeners();
  59. }
  60. _createClass(PDFSidebarResizer, [{
  61. key: '_updateWidth',
  62. value: function _updateWidth() {
  63. var width = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
  64. if (!this.enabled) {
  65. return false;
  66. }
  67. var maxWidth = Math.floor(this.outerContainerWidth / 2);
  68. if (width > maxWidth) {
  69. width = maxWidth;
  70. }
  71. if (width < SIDEBAR_MIN_WIDTH) {
  72. width = SIDEBAR_MIN_WIDTH;
  73. }
  74. if (width === this._width) {
  75. return false;
  76. }
  77. this._width = width;
  78. this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, width + 'px');
  79. return true;
  80. }
  81. }, {
  82. key: '_mouseMove',
  83. value: function _mouseMove(evt) {
  84. var width = evt.clientX;
  85. if (this.isRTL) {
  86. width = this.outerContainerWidth - width;
  87. }
  88. this._updateWidth(width);
  89. }
  90. }, {
  91. key: '_mouseUp',
  92. value: function _mouseUp(evt) {
  93. this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
  94. this.eventBus.dispatch('resize', { source: this });
  95. var _boundEvents = this._boundEvents;
  96. window.removeEventListener('mousemove', _boundEvents.mouseMove);
  97. window.removeEventListener('mouseup', _boundEvents.mouseUp);
  98. }
  99. }, {
  100. key: '_addEventListeners',
  101. value: function _addEventListeners() {
  102. var _this2 = this;
  103. if (!this.enabled) {
  104. return;
  105. }
  106. var _boundEvents = this._boundEvents;
  107. _boundEvents.mouseMove = this._mouseMove.bind(this);
  108. _boundEvents.mouseUp = this._mouseUp.bind(this);
  109. this.resizer.addEventListener('mousedown', function (evt) {
  110. if (evt.button !== 0) {
  111. return;
  112. }
  113. _this2.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
  114. window.addEventListener('mousemove', _boundEvents.mouseMove);
  115. window.addEventListener('mouseup', _boundEvents.mouseUp);
  116. });
  117. this.eventBus.on('sidebarviewchanged', function (evt) {
  118. _this2.sidebarOpen = !!(evt && evt.view);
  119. });
  120. this.eventBus.on('resize', function (evt) {
  121. if (evt && evt.source === window) {
  122. _this2._outerContainerWidth = null;
  123. if (_this2._width) {
  124. if (_this2.sidebarOpen) {
  125. _this2.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
  126. var updated = _this2._updateWidth(_this2._width);
  127. Promise.resolve().then(function () {
  128. _this2.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
  129. if (updated) {
  130. _this2.eventBus.dispatch('resize', { source: _this2 });
  131. }
  132. });
  133. } else {
  134. _this2._updateWidth(_this2._width);
  135. }
  136. }
  137. }
  138. });
  139. }
  140. }, {
  141. key: 'outerContainerWidth',
  142. get: function get() {
  143. if (!this._outerContainerWidth) {
  144. this._outerContainerWidth = this.outerContainer.clientWidth;
  145. }
  146. return this._outerContainerWidth;
  147. }
  148. }]);
  149. return PDFSidebarResizer;
  150. }();
  151. exports.PDFSidebarResizer = PDFSidebarResizer;