password_prompt.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2018 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");
  28. var _pdf = require("../pdf");
  29. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  30. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  31. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  32. var PasswordPrompt =
  33. /*#__PURE__*/
  34. function () {
  35. function PasswordPrompt(options, overlayManager) {
  36. var _this = this;
  37. var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
  38. _classCallCheck(this, PasswordPrompt);
  39. this.overlayName = options.overlayName;
  40. this.container = options.container;
  41. this.label = options.label;
  42. this.input = options.input;
  43. this.submitButton = options.submitButton;
  44. this.cancelButton = options.cancelButton;
  45. this.overlayManager = overlayManager;
  46. this.l10n = l10n;
  47. this.updateCallback = null;
  48. this.reason = null;
  49. this.submitButton.addEventListener('click', this.verify.bind(this));
  50. this.cancelButton.addEventListener('click', this.close.bind(this));
  51. this.input.addEventListener('keydown', function (e) {
  52. if (e.keyCode === 13) {
  53. _this.verify();
  54. }
  55. });
  56. this.overlayManager.register(this.overlayName, this.container, this.close.bind(this), true);
  57. }
  58. _createClass(PasswordPrompt, [{
  59. key: "open",
  60. value: function open() {
  61. var _this2 = this;
  62. this.overlayManager.open(this.overlayName).then(function () {
  63. _this2.input.focus();
  64. var promptString;
  65. if (_this2.reason === _pdf.PasswordResponses.INCORRECT_PASSWORD) {
  66. promptString = _this2.l10n.get('password_invalid', null, 'Invalid password. Please try again.');
  67. } else {
  68. promptString = _this2.l10n.get('password_label', null, 'Enter the password to open this PDF file.');
  69. }
  70. promptString.then(function (msg) {
  71. _this2.label.textContent = msg;
  72. });
  73. });
  74. }
  75. }, {
  76. key: "close",
  77. value: function close() {
  78. var _this3 = this;
  79. this.overlayManager.close(this.overlayName).then(function () {
  80. _this3.input.value = '';
  81. });
  82. }
  83. }, {
  84. key: "verify",
  85. value: function verify() {
  86. var password = this.input.value;
  87. if (password && password.length > 0) {
  88. this.close();
  89. return this.updateCallback(password);
  90. }
  91. }
  92. }, {
  93. key: "setUpdateCallback",
  94. value: function setUpdateCallback(updateCallback, reason) {
  95. this.updateCallback = updateCallback;
  96. this.reason = reason;
  97. }
  98. }]);
  99. return PasswordPrompt;
  100. }();
  101. exports.PasswordPrompt = PasswordPrompt;