password_prompt.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 _overlay_manager = require('./overlay_manager');
  23. var _pdfjs = require('./pdfjs');
  24. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  25. var PasswordPrompt = function () {
  26. function PasswordPrompt(options) {
  27. var _this = this;
  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.updateCallback = null;
  36. this.reason = null;
  37. this.submitButton.addEventListener('click', this.verify.bind(this));
  38. this.cancelButton.addEventListener('click', this.close.bind(this));
  39. this.input.addEventListener('keydown', function (e) {
  40. if (e.keyCode === 13) {
  41. _this.verify();
  42. }
  43. });
  44. _overlay_manager.OverlayManager.register(this.overlayName, this.container, this.close.bind(this), true);
  45. }
  46. _createClass(PasswordPrompt, [{
  47. key: 'open',
  48. value: function open() {
  49. var _this2 = this;
  50. _overlay_manager.OverlayManager.open(this.overlayName).then(function () {
  51. _this2.input.focus();
  52. var promptString = _ui_utils.mozL10n.get('password_label', null, 'Enter the password to open this PDF file.');
  53. if (_this2.reason === _pdfjs.PasswordResponses.INCORRECT_PASSWORD) {
  54. promptString = _ui_utils.mozL10n.get('password_invalid', null, 'Invalid password. Please try again.');
  55. }
  56. _this2.label.textContent = promptString;
  57. });
  58. }
  59. }, {
  60. key: 'close',
  61. value: function close() {
  62. var _this3 = this;
  63. _overlay_manager.OverlayManager.close(this.overlayName).then(function () {
  64. _this3.input.value = '';
  65. });
  66. }
  67. }, {
  68. key: 'verify',
  69. value: function verify() {
  70. var password = this.input.value;
  71. if (password && password.length > 0) {
  72. this.close();
  73. return this.updateCallback(password);
  74. }
  75. }
  76. }, {
  77. key: 'setUpdateCallback',
  78. value: function setUpdateCallback(updateCallback, reason) {
  79. this.updateCallback = updateCallback;
  80. this.reason = reason;
  81. }
  82. }]);
  83. return PasswordPrompt;
  84. }();
  85. exports.PasswordPrompt = PasswordPrompt;