password_prompt.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 _ui_utils = require('pdfjs-web/ui_utils');
  21. var _overlay_manager = require('pdfjs-web/overlay_manager');
  22. var _pdfjs = require('pdfjs-web/pdfjs');
  23. var PasswordPrompt = function PasswordPromptClosure() {
  24. function PasswordPrompt(options) {
  25. this.overlayName = options.overlayName;
  26. this.container = options.container;
  27. this.label = options.label;
  28. this.input = options.input;
  29. this.submitButton = options.submitButton;
  30. this.cancelButton = options.cancelButton;
  31. this.updateCallback = null;
  32. this.reason = null;
  33. this.submitButton.addEventListener('click', this.verify.bind(this));
  34. this.cancelButton.addEventListener('click', this.close.bind(this));
  35. this.input.addEventListener('keydown', function (e) {
  36. if (e.keyCode === 13) {
  37. this.verify();
  38. }
  39. }.bind(this));
  40. _overlay_manager.OverlayManager.register(this.overlayName, this.container, this.close.bind(this), true);
  41. }
  42. PasswordPrompt.prototype = {
  43. open: function PasswordPrompt_open() {
  44. _overlay_manager.OverlayManager.open(this.overlayName).then(function () {
  45. this.input.type = 'password';
  46. this.input.focus();
  47. var promptString = _ui_utils.mozL10n.get('password_label', null, 'Enter the password to open this PDF file.');
  48. if (this.reason === _pdfjs.PasswordResponses.INCORRECT_PASSWORD) {
  49. promptString = _ui_utils.mozL10n.get('password_invalid', null, 'Invalid password. Please try again.');
  50. }
  51. this.label.textContent = promptString;
  52. }.bind(this));
  53. },
  54. close: function PasswordPrompt_close() {
  55. _overlay_manager.OverlayManager.close(this.overlayName).then(function () {
  56. this.input.value = '';
  57. this.input.type = '';
  58. }.bind(this));
  59. },
  60. verify: function PasswordPrompt_verify() {
  61. var password = this.input.value;
  62. if (password && password.length > 0) {
  63. this.close();
  64. return this.updateCallback(password);
  65. }
  66. },
  67. setUpdateCallback: function PasswordPrompt_setUpdateCallback(updateCallback, reason) {
  68. this.updateCallback = updateCallback;
  69. this.reason = reason;
  70. }
  71. };
  72. return PasswordPrompt;
  73. }();
  74. exports.PasswordPrompt = PasswordPrompt;