UNPKG

6.04 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2019 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");
30
31function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
32
33function _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); } }
34
35function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
36
37var SIDEBAR_WIDTH_VAR = '--sidebar-width';
38var SIDEBAR_MIN_WIDTH = 200;
39var SIDEBAR_RESIZING_CLASS = 'sidebarResizing';
40
41var PDFSidebarResizer =
42/*#__PURE__*/
43function () {
44 function PDFSidebarResizer(options, eventBus) {
45 var _this = this;
46
47 var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
48
49 _classCallCheck(this, PDFSidebarResizer);
50
51 this.enabled = false;
52 this.isRTL = false;
53 this.sidebarOpen = false;
54 this.doc = document.documentElement;
55 this._width = null;
56 this._outerContainerWidth = null;
57 this._boundEvents = Object.create(null);
58 this.outerContainer = options.outerContainer;
59 this.resizer = options.resizer;
60 this.eventBus = eventBus;
61 this.l10n = l10n;
62
63 if (typeof CSS === 'undefined' || typeof CSS.supports !== 'function' || !CSS.supports(SIDEBAR_WIDTH_VAR, "calc(-1 * ".concat(SIDEBAR_MIN_WIDTH, "px)"))) {
64 console.warn('PDFSidebarResizer: ' + 'The browser does not support resizing of the sidebar.');
65 return;
66 }
67
68 this.enabled = true;
69 this.resizer.classList.remove('hidden');
70 this.l10n.getDirection().then(function (dir) {
71 _this.isRTL = dir === 'rtl';
72 });
73
74 this._addEventListeners();
75 }
76
77 _createClass(PDFSidebarResizer, [{
78 key: "_updateWidth",
79 value: function _updateWidth() {
80 var width = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
81
82 if (!this.enabled) {
83 return false;
84 }
85
86 var maxWidth = Math.floor(this.outerContainerWidth / 2);
87
88 if (width > maxWidth) {
89 width = maxWidth;
90 }
91
92 if (width < SIDEBAR_MIN_WIDTH) {
93 width = SIDEBAR_MIN_WIDTH;
94 }
95
96 if (width === this._width) {
97 return false;
98 }
99
100 this._width = width;
101 this.doc.style.setProperty(SIDEBAR_WIDTH_VAR, "".concat(width, "px"));
102 return true;
103 }
104 }, {
105 key: "_mouseMove",
106 value: function _mouseMove(evt) {
107 var width = evt.clientX;
108
109 if (this.isRTL) {
110 width = this.outerContainerWidth - width;
111 }
112
113 this._updateWidth(width);
114 }
115 }, {
116 key: "_mouseUp",
117 value: function _mouseUp(evt) {
118 this.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
119 this.eventBus.dispatch('resize', {
120 source: this
121 });
122 var _boundEvents = this._boundEvents;
123 window.removeEventListener('mousemove', _boundEvents.mouseMove);
124 window.removeEventListener('mouseup', _boundEvents.mouseUp);
125 }
126 }, {
127 key: "_addEventListeners",
128 value: function _addEventListeners() {
129 var _this2 = this;
130
131 if (!this.enabled) {
132 return;
133 }
134
135 var _boundEvents = this._boundEvents;
136 _boundEvents.mouseMove = this._mouseMove.bind(this);
137 _boundEvents.mouseUp = this._mouseUp.bind(this);
138 this.resizer.addEventListener('mousedown', function (evt) {
139 if (evt.button !== 0) {
140 return;
141 }
142
143 _this2.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
144
145 window.addEventListener('mousemove', _boundEvents.mouseMove);
146 window.addEventListener('mouseup', _boundEvents.mouseUp);
147 });
148 this.eventBus.on('sidebarviewchanged', function (evt) {
149 _this2.sidebarOpen = !!(evt && evt.view);
150 });
151 this.eventBus.on('resize', function (evt) {
152 if (evt && evt.source === window) {
153 _this2._outerContainerWidth = null;
154
155 if (_this2._width) {
156 if (_this2.sidebarOpen) {
157 _this2.outerContainer.classList.add(SIDEBAR_RESIZING_CLASS);
158
159 var updated = _this2._updateWidth(_this2._width);
160
161 Promise.resolve().then(function () {
162 _this2.outerContainer.classList.remove(SIDEBAR_RESIZING_CLASS);
163
164 if (updated) {
165 _this2.eventBus.dispatch('resize', {
166 source: _this2
167 });
168 }
169 });
170 } else {
171 _this2._updateWidth(_this2._width);
172 }
173 }
174 }
175 });
176 }
177 }, {
178 key: "outerContainerWidth",
179 get: function get() {
180 if (!this._outerContainerWidth) {
181 this._outerContainerWidth = this.outerContainer.clientWidth;
182 }
183
184 return this._outerContainerWidth;
185 }
186 }]);
187
188 return PDFSidebarResizer;
189}();
190
191exports.PDFSidebarResizer = PDFSidebarResizer;
\No newline at end of file