password_prompt.js 2.8 KB

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