UNPKG

2.85 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.PasswordPrompt = void 0;
28
29var _pdf = require("../pdf");
30
31class PasswordPrompt {
32 #updateCallback = null;
33 #reason = null;
34
35 constructor(options, overlayManager, l10n, isViewerEmbedded = false) {
36 this.dialog = options.dialog;
37 this.label = options.label;
38 this.input = options.input;
39 this.submitButton = options.submitButton;
40 this.cancelButton = options.cancelButton;
41 this.overlayManager = overlayManager;
42 this.l10n = l10n;
43 this._isViewerEmbedded = isViewerEmbedded;
44 this.submitButton.addEventListener("click", this.#verify.bind(this));
45 this.cancelButton.addEventListener("click", this.#cancel.bind(this));
46 this.input.addEventListener("keydown", e => {
47 if (e.keyCode === 13) {
48 this.#verify();
49 }
50 });
51 this.overlayManager.register(this.dialog, true);
52 this.dialog.addEventListener("close", this.#cancel.bind(this));
53 }
54
55 async open() {
56 await this.overlayManager.open(this.dialog);
57 const passwordIncorrect = this.#reason === _pdf.PasswordResponses.INCORRECT_PASSWORD;
58
59 if (!this._isViewerEmbedded || passwordIncorrect) {
60 this.input.focus();
61 }
62
63 this.label.textContent = await this.l10n.get(`password_${passwordIncorrect ? "invalid" : "label"}`);
64 }
65
66 async close() {
67 if (this.overlayManager.active === this.dialog) {
68 this.overlayManager.close(this.dialog);
69 }
70 }
71
72 #verify() {
73 const password = this.input.value;
74
75 if (password?.length > 0) {
76 this.#invokeCallback(password);
77 }
78 }
79
80 #cancel() {
81 this.#invokeCallback(new Error("PasswordPrompt cancelled."));
82 }
83
84 #invokeCallback(password) {
85 if (!this.#updateCallback) {
86 return;
87 }
88
89 this.close();
90 this.input.value = "";
91 this.#updateCallback(password);
92 this.#updateCallback = null;
93 }
94
95 setUpdateCallback(updateCallback, reason) {
96 this.#updateCallback = updateCallback;
97 this.#reason = reason;
98 }
99
100}
101
102exports.PasswordPrompt = PasswordPrompt;
\No newline at end of file