pdf_sidebar_resizer.js 5.6 KB

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