2
0

password_prompt.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.PasswordPrompt = undefined;
  20. var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  21. var _ui_utils = require('./ui_utils');
  22. var _pdf = require('../pdf');
  23. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  24. var PasswordPrompt = function () {
  25. function PasswordPrompt(options, overlayManager) {
  26. var _this = this;
  27. var l10n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _ui_utils.NullL10n;
  28. _classCallCheck(this, PasswordPrompt);
  29. this.overlayName = options.overlayName;
  30. this.container = options.container;
  31. this.label = options.label;
  32. this.input = options.input;
  33. this.submitButton = options.submitButton;
  34. this.cancelButton = options.cancelButton;
  35. this.overlayManager = overlayManager;
  36. this.l10n = l10n;
  37. this.updateCallback = null;
  38. this.reason = null;
  39. this.submitButton.addEventListener('click', this.verify.bind(this));
  40. this.cancelButton.addEventListener('click', this.close.bind(this));
  41. this.input.addEventListener('keydown', function (e) {
  42. if (e.keyCode === 13) {
  43. _this.verify();
  44. }
  45. });
  46. this.overlayManager.register(this.overlayName, this.container, this.close.bind(this), true);
  47. }
  48. _createClass(PasswordPrompt, [{
  49. key: 'open',
  50. value: function open() {
  51. var _this2 = this;
  52. this.overlayManager.open(this.overlayName).then(function () {
  53. _this2.input.focus();
  54. var promptString = void 0;
  55. if (_this2.reason === _pdf.PasswordResponses.INCORRECT_PASSWORD) {
  56. promptString = _this2.l10n.get('password_invalid', null, 'Invalid password. Please try again.');
  57. } else {
  58. promptString = _this2.l10n.get('password_label', null, 'Enter the password to open this PDF file.');
  59. }
  60. promptString.then(function (msg) {
  61. _this2.label.textContent = msg;
  62. });
  63. });
  64. }
  65. }, {
  66. key: 'close',
  67. value: function close() {
  68. var _this3 = this;
  69. this.overlayManager.close(this.overlayName).then(function () {
  70. _this3.input.value = '';
  71. });
  72. }
  73. }, {
  74. key: 'verify',
  75. value: function verify() {
  76. var password = this.input.value;
  77. if (password && password.length > 0) {
  78. this.close();
  79. return this.updateCallback(password);
  80. }
  81. }
  82. }, {
  83. key: 'setUpdateCallback',
  84. value: function setUpdateCallback(updateCallback, reason) {
  85. this.updateCallback = updateCallback;
  86. this.reason = reason;
  87. }
  88. }]);
  89. return PasswordPrompt;
  90. }();
  91. exports.PasswordPrompt = PasswordPrompt;