2
0

password_prompt.js 4.0 KB

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