index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const html = require('choo/html');
  2. const MAX_LENGTH = 32;
  3. module.exports = function(file, state, emit) {
  4. const loading = state.settingPassword;
  5. const pwd = file.hasPassword;
  6. const sectionClass =
  7. pwd || state.passwordSetError
  8. ? 'passwordInput'
  9. : 'passwordInput passwordInput--hidden';
  10. const inputClass = loading || pwd ? 'input' : 'input input--noBtn';
  11. let btnClass = 'inputBtn inputBtn--password inputBtn--hidden';
  12. if (loading) {
  13. btnClass = 'inputBtn inputBtn--password inputBtn--loading';
  14. } else if (pwd) {
  15. btnClass = 'inputBtn inputBtn--password';
  16. }
  17. const action = pwd
  18. ? state.translate('changePasswordButton')
  19. : state.translate('addPasswordButton');
  20. return html`
  21. <div class="${sectionClass}">
  22. <form
  23. class="passwordInput__form"
  24. onsubmit=${setPassword}
  25. data-no-csrf>
  26. <input id="password-input"
  27. ${loading ? 'disabled' : ''}
  28. class="${inputClass}"
  29. maxlength="${MAX_LENGTH}"
  30. autocomplete="off"
  31. type="password"
  32. oninput=${inputChanged}
  33. onfocus=${focused}
  34. placeholder="${
  35. pwd && !state.passwordSetError
  36. ? passwordPlaceholder(file.password)
  37. : state.translate('unlockInputPlaceholder')
  38. }">
  39. <input type="submit"
  40. id="password-btn"
  41. ${loading ? 'disabled' : ''}
  42. class="${btnClass}"
  43. value="${loading ? '' : action}">
  44. </form>
  45. <label
  46. class="passwordInput__msg ${
  47. state.passwordSetError ? 'passwordInput__msg--error' : ''
  48. }"
  49. for="password-input">${message(state, pwd)}</label>
  50. </div>`;
  51. function inputChanged() {
  52. state.passwordSetError = null;
  53. const resetInput = document.getElementById('password-input');
  54. const resetBtn = document.getElementById('password-btn');
  55. const pwdmsg = document.querySelector('.passwordInput__msg');
  56. const length = resetInput.value.length;
  57. if (length === MAX_LENGTH) {
  58. pwdmsg.textContent = state.translate('maxPasswordLength', {
  59. length: MAX_LENGTH
  60. });
  61. } else {
  62. pwdmsg.textContent = '';
  63. }
  64. if (length > 0) {
  65. resetBtn.classList.remove('inputBtn--hidden');
  66. resetInput.classList.remove('input--noBtn');
  67. } else {
  68. resetBtn.classList.add('inputBtn--hidden');
  69. resetInput.classList.add('input--noBtn');
  70. }
  71. }
  72. function focused(event) {
  73. event.preventDefault();
  74. const el = document.getElementById('password-input');
  75. if (el.placeholder !== state.translate('unlockInputPlaceholder')) {
  76. el.placeholder = '';
  77. }
  78. }
  79. function setPassword(event) {
  80. event.preventDefault();
  81. const el = document.getElementById('password-input');
  82. const password = el.value;
  83. if (password.length > 0) {
  84. emit('password', { password, file });
  85. } else {
  86. el.focus();
  87. }
  88. return false;
  89. }
  90. };
  91. function passwordPlaceholder(password) {
  92. return password ? password.replace(/./g, '●') : '●●●●●●●●●●●●';
  93. }
  94. function message(state, pwd) {
  95. if (state.passwordSetError) {
  96. return state.translate('passwordSetError');
  97. }
  98. if (state.settingPassword || !pwd) {
  99. return '';
  100. }
  101. return state.translate('passwordIsSet');
  102. }