password_prompt.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.PasswordPrompt = void 0;
  27. var _pdf = require("../pdf");
  28. class PasswordPrompt {
  29. constructor(options, overlayManager, l10n, isViewerEmbedded = false) {
  30. this.overlayName = options.overlayName;
  31. this.container = options.container;
  32. this.label = options.label;
  33. this.input = options.input;
  34. this.submitButton = options.submitButton;
  35. this.cancelButton = options.cancelButton;
  36. this.overlayManager = overlayManager;
  37. this.l10n = l10n;
  38. this._isViewerEmbedded = isViewerEmbedded;
  39. this.updateCallback = null;
  40. this.reason = null;
  41. this.submitButton.addEventListener("click", this.#verify.bind(this));
  42. this.cancelButton.addEventListener("click", this.#cancel.bind(this));
  43. this.input.addEventListener("keydown", e => {
  44. if (e.keyCode === 13) {
  45. this.#verify();
  46. }
  47. });
  48. this.overlayManager.register(this.overlayName, this.container, this.#cancel.bind(this), true);
  49. }
  50. async open() {
  51. await this.overlayManager.open(this.overlayName);
  52. const passwordIncorrect = this.reason === _pdf.PasswordResponses.INCORRECT_PASSWORD;
  53. if (!this._isViewerEmbedded || passwordIncorrect) {
  54. this.input.focus();
  55. }
  56. this.label.textContent = await this.l10n.get(`password_${passwordIncorrect ? "invalid" : "label"}`);
  57. }
  58. async close() {
  59. await this.overlayManager.close(this.overlayName);
  60. this.input.value = "";
  61. }
  62. #verify() {
  63. const password = this.input.value;
  64. if (password?.length > 0) {
  65. this.close();
  66. this.updateCallback(password);
  67. }
  68. }
  69. #cancel() {
  70. this.close();
  71. this.updateCallback(new Error("PasswordPrompt cancelled."));
  72. }
  73. setUpdateCallback(updateCallback, reason) {
  74. this.updateCallback = updateCallback;
  75. this.reason = reason;
  76. }
  77. }
  78. exports.PasswordPrompt = PasswordPrompt;