pdf_sidebar_resizer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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.isRTL = false;
  34. this.sidebarOpen = false;
  35. this.doc = document.documentElement;
  36. this._width = null;
  37. this._outerContainerWidth = null;
  38. this._boundEvents = Object.create(null);
  39. this.outerContainer = options.outerContainer;
  40. this.resizer = options.resizer;
  41. this.eventBus = eventBus;
  42. l10n.getDirection().then(dir => {
  43. this.isRTL = dir === "rtl";
  44. });
  45. this._addEventListeners();
  46. }
  47. get outerContainerWidth() {
  48. if (!this._outerContainerWidth) {
  49. this._outerContainerWidth = this.outerContainer.clientWidth;
  50. }
  51. return this._outerContainerWidth;
  52. }
  53. _updateWidth(width = 0) {
  54. const maxWidth = Math.floor(this.outerContainerWidth / 2);
  55. if (width > maxWidth) {
  56. width = maxWidth;
  57. }
  58. if (width < SIDEBAR_MIN_WIDTH) {
  59. width = SIDEBAR_MIN_WIDTH;
  60. }
  61. if (width === this._width) {
  62. return false;
  63. }
  64. this._width = width;
  65. this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
  66. return true;
  67. }
  68. _mouseMove(evt) {
  69. let width = evt.clientX;
  70. if (this.isRTL) {
  71. width = this.outerContainerWidth - width;
  72. }
  73. this._updateWidth(width);
  74. }
  75. _mouseUp(evt) {
  76. this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
  77. this.eventBus.dispatch("resize", {
  78. source: this
  79. });
  80. const _boundEvents = this._boundEvents;
  81. window.removeEventListener("mousemove", _boundEvents.mouseMove);
  82. window.removeEventListener("mouseup", _boundEvents.mouseUp);
  83. }
  84. _addEventListeners() {
  85. const _boundEvents = this._boundEvents;
  86. _boundEvents.mouseMove = this._mouseMove.bind(this);
  87. _boundEvents.mouseUp = this._mouseUp.bind(this);
  88. this.resizer.addEventListener("mousedown", evt => {
  89. if (evt.button !== 0) {
  90. return;
  91. }
  92. this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
  93. window.addEventListener("mousemove", _boundEvents.mouseMove);
  94. window.addEventListener("mouseup", _boundEvents.mouseUp);
  95. });
  96. this.eventBus._on("sidebarviewchanged", evt => {
  97. this.sidebarOpen = !!(evt && evt.view);
  98. });
  99. this.eventBus._on("resize", evt => {
  100. if (!evt || evt.source !== window) {
  101. return;
  102. }
  103. this._outerContainerWidth = null;
  104. if (!this._width) {
  105. return;
  106. }
  107. if (!this.sidebarOpen) {
  108. this._updateWidth(this._width);
  109. return;
  110. }
  111. this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
  112. const updated = this._updateWidth(this._width);
  113. Promise.resolve().then(() => {
  114. this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
  115. if (updated) {
  116. this.eventBus.dispatch("resize", {
  117. source: this
  118. });
  119. }
  120. });
  121. });
  122. }
  123. }
  124. exports.PDFSidebarResizer = PDFSidebarResizer;