password_prompt.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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 _ui_utils = require("./ui_utils.js");
  28. var _pdf = require("../pdf");
  29. class PasswordPrompt {
  30. constructor(options, overlayManager, l10n = _ui_utils.NullL10n) {
  31. this.overlayName = options.overlayName;
  32. this.container = options.container;
  33. this.label = options.label;
  34. this.input = options.input;
  35. this.submitButton = options.submitButton;
  36. this.cancelButton = options.cancelButton;
  37. this.overlayManager = overlayManager;
  38. this.l10n = l10n;
  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. open() {
  51. this.overlayManager.open(this.overlayName).then(() => {
  52. this.input.focus();
  53. let promptString;
  54. if (this.reason === _pdf.PasswordResponses.INCORRECT_PASSWORD) {
  55. promptString = this.l10n.get("password_invalid", null, "Invalid password. Please try again.");
  56. } else {
  57. promptString = this.l10n.get("password_label", null, "Enter the password to open this PDF file.");
  58. }
  59. promptString.then(msg => {
  60. this.label.textContent = msg;
  61. });
  62. });
  63. }
  64. close() {
  65. this.overlayManager.close(this.overlayName).then(() => {
  66. this.input.value = "";
  67. });
  68. }
  69. verify() {
  70. const password = this.input.value;
  71. if (password && password.length > 0) {
  72. this.close();
  73. this.updateCallback(password);
  74. }
  75. }
  76. setUpdateCallback(updateCallback, reason) {
  77. this.updateCallback = updateCallback;
  78. this.reason = reason;
  79. }
  80. }
  81. exports.PasswordPrompt = PasswordPrompt;