123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- import { createPromiseCapability, PasswordResponses } from "pdfjs-lib";
- class PasswordPrompt {
- #activeCapability = null;
- #updateCallback = null;
- #reason = null;
-
- constructor(options, overlayManager, l10n, isViewerEmbedded = false) {
- this.dialog = options.dialog;
- this.label = options.label;
- this.input = options.input;
- this.submitButton = options.submitButton;
- this.cancelButton = options.cancelButton;
- this.overlayManager = overlayManager;
- this.l10n = l10n;
- this._isViewerEmbedded = isViewerEmbedded;
-
- this.submitButton.addEventListener("click", this.#verify.bind(this));
- this.cancelButton.addEventListener("click", this.close.bind(this));
- this.input.addEventListener("keydown", e => {
- if (e.keyCode === 13) {
- this.#verify();
- }
- });
- this.overlayManager.register(this.dialog, true);
- this.dialog.addEventListener("close", this.#cancel.bind(this));
- }
- async open() {
- if (this.#activeCapability) {
- await this.#activeCapability.promise;
- }
- this.#activeCapability = createPromiseCapability();
- try {
- await this.overlayManager.open(this.dialog);
- } catch (ex) {
- this.#activeCapability = null;
- throw ex;
- }
- const passwordIncorrect =
- this.#reason === PasswordResponses.INCORRECT_PASSWORD;
- if (!this._isViewerEmbedded || passwordIncorrect) {
- this.input.focus();
- }
- this.label.textContent = await this.l10n.get(
- `password_${passwordIncorrect ? "invalid" : "label"}`
- );
- }
- async close() {
- if (this.overlayManager.active === this.dialog) {
- this.overlayManager.close(this.dialog);
- }
- }
- #verify() {
- const password = this.input.value;
- if (password?.length > 0) {
- this.#invokeCallback(password);
- }
- }
- #cancel() {
- this.#invokeCallback(new Error("PasswordPrompt cancelled."));
- this.#activeCapability.resolve();
- }
- #invokeCallback(password) {
- if (!this.#updateCallback) {
- return;
- }
- this.close();
- this.input.value = "";
- this.#updateCallback(password);
- this.#updateCallback = null;
- }
- async setUpdateCallback(updateCallback, reason) {
- if (this.#activeCapability) {
- await this.#activeCapability.promise;
- }
- this.#updateCallback = updateCallback;
- this.#reason = reason;
- }
- }
- export { PasswordPrompt };
|