password_prompt.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.close.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.close.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. close() {
  59. this.overlayManager.close(this.overlayName).then(() => {
  60. this.input.value = "";
  61. });
  62. }
  63. verify() {
  64. const password = this.input.value;
  65. if (password?.length > 0) {
  66. this.close();
  67. this.updateCallback(password);
  68. }
  69. }
  70. setUpdateCallback(updateCallback, reason) {
  71. this.updateCallback = updateCallback;
  72. this.reason = reason;
  73. }
  74. }
  75. exports.PasswordPrompt = PasswordPrompt;