downloadPassword.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const html = require('choo/html');
  2. module.exports = function(state, emit) {
  3. const fileInfo = state.fileInfo;
  4. const invalid = fileInfo.password === null;
  5. const div = html`
  6. <div
  7. class="h-full w-full flex flex-col items-center justify-center bg-white py-8 max-w-md mx-auto dark:bg-grey-90"
  8. >
  9. <h1 class="text-3xl font-bold mb-4">
  10. ${state.translate('downloadTitle')}
  11. </h1>
  12. <p
  13. class="w-full mb-4 text-center text-grey-80 dark:text-grey-40 leading-normal"
  14. >
  15. ${state.translate('downloadDescription')}
  16. </p>
  17. <form
  18. class="flex flex-row flex-no-wrap w-full md:w-4/5"
  19. onsubmit="${checkPassword}"
  20. data-no-csrf
  21. >
  22. <input
  23. id="password-input"
  24. class="w-full border-l border-t border-b rounded-l-lg rounded-r-none ${invalid
  25. ? 'border-red dark:border-red-40'
  26. : 'border-grey'} leading-loose px-2 py-1 dark:bg-grey-80"
  27. maxlength="32"
  28. autocomplete="off"
  29. placeholder="${state.translate('unlockInputPlaceholder')}"
  30. oninput="${inputChanged}"
  31. type="password"
  32. />
  33. <input
  34. type="submit"
  35. id="password-btn"
  36. class="btn rounded-r-lg rounded-l-none ${invalid
  37. ? 'bg-red hover:bg-red focus:bg-red dark:bg-red-40'
  38. : ''}"
  39. value="${state.translate('unlockButtonLabel')}"
  40. title="${state.translate('unlockButtonLabel')}"
  41. />
  42. </form>
  43. <label
  44. id="password-error"
  45. class="${invalid ? '' : 'invisible'} text-red dark:text-red-40 my-4"
  46. for="password-input"
  47. >
  48. ${state.translate('passwordTryAgain')}
  49. </label>
  50. </div>
  51. `;
  52. if (!(div instanceof String)) {
  53. setTimeout(() => document.getElementById('password-input').focus());
  54. }
  55. function inputChanged(event) {
  56. event.stopPropagation();
  57. event.preventDefault();
  58. const label = document.getElementById('password-error');
  59. const input = document.getElementById('password-input');
  60. const btn = document.getElementById('password-btn');
  61. label.classList.add('invisible');
  62. input.classList.remove('border-red');
  63. btn.classList.remove('bg-red', 'hover:bg-red', 'focus:bg-red');
  64. }
  65. function checkPassword(event) {
  66. event.stopPropagation();
  67. event.preventDefault();
  68. const el = document.getElementById('password-input');
  69. const password = el.value;
  70. if (password.length > 0) {
  71. document.getElementById('password-btn').disabled = true;
  72. // Strip any url parameters between fileId and secretKey
  73. const fileInfoUrl = window.location.href.replace(/\?.+#/, '#');
  74. state.fileInfo.url = fileInfoUrl;
  75. state.fileInfo.password = password;
  76. emit('getMetadata');
  77. }
  78. return false;
  79. }
  80. return div;
  81. };