pdf_sidebar_resizer.js 4.3 KB

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