UNPKG

3.86 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 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
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.PDFSidebarResizer = void 0;
28
29var _ui_utils = require("./ui_utils.js");
30
31const SIDEBAR_WIDTH_VAR = "--sidebar-width";
32const SIDEBAR_MIN_WIDTH = 200;
33const SIDEBAR_RESIZING_CLASS = "sidebarResizing";
34
35class PDFSidebarResizer {
36 constructor(options, eventBus, l10n) {
37 this.isRTL = false;
38 this.sidebarOpen = false;
39 this._width = null;
40 this._outerContainerWidth = null;
41 this._boundEvents = Object.create(null);
42 this.outerContainer = options.outerContainer;
43 this.resizer = options.resizer;
44 this.eventBus = eventBus;
45 l10n.getDirection().then(dir => {
46 this.isRTL = dir === "rtl";
47 });
48
49 this._addEventListeners();
50 }
51
52 get outerContainerWidth() {
53 return this._outerContainerWidth ||= this.outerContainer.clientWidth;
54 }
55
56 _updateWidth(width = 0) {
57 const maxWidth = Math.floor(this.outerContainerWidth / 2);
58
59 if (width > maxWidth) {
60 width = maxWidth;
61 }
62
63 if (width < SIDEBAR_MIN_WIDTH) {
64 width = SIDEBAR_MIN_WIDTH;
65 }
66
67 if (width === this._width) {
68 return false;
69 }
70
71 this._width = width;
72
73 _ui_utils.docStyle.setProperty(SIDEBAR_WIDTH_VAR, `${width}px`);
74
75 return true;
76 }
77
78 _mouseMove(evt) {
79 let width = evt.clientX;
80
81 if (this.isRTL) {
82 width = this.outerContainerWidth - width;
83 }
84
85 this._updateWidth(width);
86 }
87
88 _mouseUp(evt) {
89 this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
90 this.eventBus.dispatch("resize", {
91 source: this
92 });
93 const _boundEvents = this._boundEvents;
94 window.removeEventListener("mousemove", _boundEvents.mouseMove);
95 window.removeEventListener("mouseup", _boundEvents.mouseUp);
96 }
97
98 _addEventListeners() {
99 const _boundEvents = this._boundEvents;
100 _boundEvents.mouseMove = this._mouseMove.bind(this);
101 _boundEvents.mouseUp = this._mouseUp.bind(this);
102 this.resizer.addEventListener("mousedown", evt => {
103 if (evt.button !== 0) {
104 return;
105 }
106
107 this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
108 window.addEventListener("mousemove", _boundEvents.mouseMove);
109 window.addEventListener("mouseup", _boundEvents.mouseUp);
110 });
111
112 this.eventBus._on("sidebarviewchanged", evt => {
113 this.sidebarOpen = !!evt?.view;
114 });
115
116 this.eventBus._on("resize", evt => {
117 if (evt?.source !== window) {
118 return;
119 }
120
121 this._outerContainerWidth = null;
122
123 if (!this._width) {
124 return;
125 }
126
127 if (!this.sidebarOpen) {
128 this._updateWidth(this._width);
129
130 return;
131 }
132
133 this.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
134
135 const updated = this._updateWidth(this._width);
136
137 Promise.resolve().then(() => {
138 this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
139
140 if (updated) {
141 this.eventBus.dispatch("resize", {
142 source: this
143 });
144 }
145 });
146 });
147 }
148
149}
150
151exports.PDFSidebarResizer = PDFSidebarResizer;
\No newline at end of file