UNPKG

8.11 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.PDFFindBar = void 0;
28
29var _ui_utils = require("./ui_utils");
30
31var _pdf_find_controller = require("./pdf_find_controller");
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35function _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); } }
36
37function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
38
39var MATCHES_COUNT_LIMIT = 1000;
40
41var PDFFindBar =
42/*#__PURE__*/
43function () {
44 function PDFFindBar(options) {
45 var _this = this;
46
47 var eventBus = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : (0, _ui_utils.getGlobalEventBus)();
48 var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
49
50 _classCallCheck(this, PDFFindBar);
51
52 this.opened = false;
53 this.bar = options.bar || null;
54 this.toggleButton = options.toggleButton || null;
55 this.findField = options.findField || null;
56 this.highlightAll = options.highlightAllCheckbox || null;
57 this.caseSensitive = options.caseSensitiveCheckbox || null;
58 this.entireWord = options.entireWordCheckbox || null;
59 this.findMsg = options.findMsg || null;
60 this.findResultsCount = options.findResultsCount || null;
61 this.findPreviousButton = options.findPreviousButton || null;
62 this.findNextButton = options.findNextButton || null;
63 this.eventBus = eventBus;
64 this.l10n = l10n;
65 this.toggleButton.addEventListener('click', function () {
66 _this.toggle();
67 });
68 this.findField.addEventListener('input', function () {
69 _this.dispatchEvent('');
70 });
71 this.bar.addEventListener('keydown', function (e) {
72 switch (e.keyCode) {
73 case 13:
74 if (e.target === _this.findField) {
75 _this.dispatchEvent('again', e.shiftKey);
76 }
77
78 break;
79
80 case 27:
81 _this.close();
82
83 break;
84 }
85 });
86 this.findPreviousButton.addEventListener('click', function () {
87 _this.dispatchEvent('again', true);
88 });
89 this.findNextButton.addEventListener('click', function () {
90 _this.dispatchEvent('again', false);
91 });
92 this.highlightAll.addEventListener('click', function () {
93 _this.dispatchEvent('highlightallchange');
94 });
95 this.caseSensitive.addEventListener('click', function () {
96 _this.dispatchEvent('casesensitivitychange');
97 });
98 this.entireWord.addEventListener('click', function () {
99 _this.dispatchEvent('entirewordchange');
100 });
101 this.eventBus.on('resize', this._adjustWidth.bind(this));
102 }
103
104 _createClass(PDFFindBar, [{
105 key: "reset",
106 value: function reset() {
107 this.updateUIState();
108 }
109 }, {
110 key: "dispatchEvent",
111 value: function dispatchEvent(type, findPrev) {
112 this.eventBus.dispatch('find', {
113 source: this,
114 type: type,
115 query: this.findField.value,
116 phraseSearch: true,
117 caseSensitive: this.caseSensitive.checked,
118 entireWord: this.entireWord.checked,
119 highlightAll: this.highlightAll.checked,
120 findPrevious: findPrev
121 });
122 }
123 }, {
124 key: "updateUIState",
125 value: function updateUIState(state, previous, matchesCount) {
126 var _this2 = this;
127
128 var notFound = false;
129 var findMsg = '';
130 var status = '';
131
132 switch (state) {
133 case _pdf_find_controller.FindState.FOUND:
134 break;
135
136 case _pdf_find_controller.FindState.PENDING:
137 status = 'pending';
138 break;
139
140 case _pdf_find_controller.FindState.NOT_FOUND:
141 findMsg = this.l10n.get('find_not_found', null, 'Phrase not found');
142 notFound = true;
143 break;
144
145 case _pdf_find_controller.FindState.WRAPPED:
146 if (previous) {
147 findMsg = this.l10n.get('find_reached_top', null, 'Reached top of document, continued from bottom');
148 } else {
149 findMsg = this.l10n.get('find_reached_bottom', null, 'Reached end of document, continued from top');
150 }
151
152 break;
153 }
154
155 this.findField.classList.toggle('notFound', notFound);
156 this.findField.setAttribute('data-status', status);
157 Promise.resolve(findMsg).then(function (msg) {
158 _this2.findMsg.textContent = msg;
159
160 _this2._adjustWidth();
161 });
162 this.updateResultsCount(matchesCount);
163 }
164 }, {
165 key: "updateResultsCount",
166 value: function updateResultsCount() {
167 var _this3 = this;
168
169 var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {},
170 _ref$current = _ref.current,
171 current = _ref$current === void 0 ? 0 : _ref$current,
172 _ref$total = _ref.total,
173 total = _ref$total === void 0 ? 0 : _ref$total;
174
175 if (!this.findResultsCount) {
176 return;
177 }
178
179 var matchesCountMsg = '',
180 limit = MATCHES_COUNT_LIMIT;
181
182 if (total > 0) {
183 if (total > limit) {
184 matchesCountMsg = this.l10n.get('find_match_count_limit', {
185 limit: limit
186 }, 'More than {{limit}} match' + (limit !== 1 ? 'es' : ''));
187 } else {
188 matchesCountMsg = this.l10n.get('find_match_count', {
189 current: current,
190 total: total
191 }, '{{current}} of {{total}} match' + (total !== 1 ? 'es' : ''));
192 }
193 }
194
195 Promise.resolve(matchesCountMsg).then(function (msg) {
196 _this3.findResultsCount.textContent = msg;
197
198 _this3.findResultsCount.classList.toggle('hidden', !total);
199
200 _this3._adjustWidth();
201 });
202 }
203 }, {
204 key: "open",
205 value: function open() {
206 if (!this.opened) {
207 this.opened = true;
208 this.toggleButton.classList.add('toggled');
209 this.bar.classList.remove('hidden');
210 }
211
212 this.findField.select();
213 this.findField.focus();
214
215 this._adjustWidth();
216 }
217 }, {
218 key: "close",
219 value: function close() {
220 if (!this.opened) {
221 return;
222 }
223
224 this.opened = false;
225 this.toggleButton.classList.remove('toggled');
226 this.bar.classList.add('hidden');
227 this.eventBus.dispatch('findbarclose', {
228 source: this
229 });
230 }
231 }, {
232 key: "toggle",
233 value: function toggle() {
234 if (this.opened) {
235 this.close();
236 } else {
237 this.open();
238 }
239 }
240 }, {
241 key: "_adjustWidth",
242 value: function _adjustWidth() {
243 if (!this.opened) {
244 return;
245 }
246
247 this.bar.classList.remove('wrapContainers');
248 var findbarHeight = this.bar.clientHeight;
249 var inputContainerHeight = this.bar.firstElementChild.clientHeight;
250
251 if (findbarHeight > inputContainerHeight) {
252 this.bar.classList.add('wrapContainers');
253 }
254 }
255 }]);
256
257 return PDFFindBar;
258}();
259
260exports.PDFFindBar = PDFFindBar;
\No newline at end of file