pdf_sidebar_resizer.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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. const SIDEBAR_WIDTH_VAR = "--sidebar-width";
  28. const SIDEBAR_MIN_WIDTH = 200;
  29. const SIDEBAR_RESIZING_CLASS = "sidebarResizing";
  30. class PDFSidebarResizer {
  31. constructor(options, eventBus, l10n) {
  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. l10n.getDirection().then(dir => {
  42. this.isRTL = dir === "rtl";
  43. });
  44. this._addEventListeners();
  45. }
  46. get outerContainerWidth() {
  47. return this._outerContainerWidth || (this._outerContainerWidth = this.outerContainer.clientWidth);
  48. }
  49. _updateWidth(width = 0) {
  50. const maxWidth = Math.floor(this.outerContainerWidth / 2);
  51. if (width > maxWidth) {
  52. width = maxWidth;
  53. }
  54. if (width < SIDEBAR_MIN_WIDTH) {
  55. width = SIDEBAR_MIN_WIDTH;
  56. }
  57. if (width === this._width) {
  58. return false;
  59. }
  60. this._width = width;
  61. this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
  62. return true;
  63. }
  64. _mouseMove(evt) {
  65. let width = evt.clientX;
  66. if (this.isRTL) {
  67. width = this.outerContainerWidth - width;
  68. }
  69. this._updateWidth(width);
  70. }
  71. _mouseUp(evt) {
  72. this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
  73. this.eventBus.dispatch("resize", {
  74. source: this
  75. });
  76. const _boundEvents = this._boundEvents;
  77. window.removeEventListener("mousemove", _boundEvents.mouseMove);
  78. window.removeEventListener("mouseup", _boundEvents.mouseUp);
  79. }
  80. _addEventListeners() {
  81. const _boundEvents = this._boundEvents;
  82. _boundEvents.mouseMove = this._mouseMove.bind(this);
  83. _boundEvents.mouseUp = this._mouseUp.bind(this);
  84. this.resizer.addEventListener("mousedown", evt => {
  85. if (evt.button !== 0) {
  86. return;
  87. }
  88. this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
  89. window.addEventListener("mousemove", _boundEvents.mouseMove);
  90. window.addEventListener("mouseup", _boundEvents.mouseUp);
  91. });
  92. this.eventBus._on("sidebarviewchanged", evt => {
  93. this.sidebarOpen = !!evt?.view;
  94. });
  95. this.eventBus._on("resize", evt => {
  96. if (evt?.source !== window) {
  97. return;
  98. }
  99. this._outerContainerWidth = null;
  100. if (!this._width) {
  101. return;
  102. }
  103. if (!this.sidebarOpen) {
  104. this._updateWidth(this._width);
  105. return;
  106. }
  107. this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
  108. const updated = this._updateWidth(this._width);
  109. Promise.resolve().then(() => {
  110. this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
  111. if (updated) {
  112. this.eventBus.dispatch("resize", {
  113. source: this
  114. });
  115. }
  116. });
  117. });
  118. }
  119. }
  120. exports.PDFSidebarResizer = PDFSidebarResizer;