downloadPassword.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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-nowrap w-full md:w-4/5"
  19. onsubmit="${checkPassword}"
  20. data-no-csrf
  21. >
  22. <input
  23. id="autocomplete-decoy"
  24. class="hidden"
  25. type="password"
  26. value="lol"
  27. />
  28. <input
  29. id="password-input"
  30. class="w-full border-l border-t border-b rounded-l-lg rounded-r-none ${invalid
  31. ? 'border-red dark:border-red-40'
  32. : 'border-grey'} leading-loose px-2 py-1 dark:bg-grey-80"
  33. maxlength="4096"
  34. autocomplete="off"
  35. placeholder="${state.translate('unlockInputPlaceholder')}"
  36. oninput="${inputChanged}"
  37. type="password"
  38. />
  39. <input
  40. type="submit"
  41. id="password-btn"
  42. class="btn rounded-r-lg rounded-l-none ${invalid
  43. ? 'bg-red hover:bg-red focus:bg-red dark:bg-red-40'
  44. : ''}"
  45. value="${state.translate('unlockButtonLabel')}"
  46. title="${state.translate('unlockButtonLabel')}"
  47. />
  48. </form>
  49. <label
  50. id="password-error"
  51. class="${invalid ? '' : 'invisible'} text-red dark:text-red-40 my-4"
  52. for="password-input"
  53. >
  54. ${state.translate('passwordTryAgain')}
  55. </label>
  56. </div>
  57. `;
  58. if (!(div instanceof String)) {
  59. setTimeout(() => document.getElementById('password-input').focus());
  60. }
  61. function inputChanged(event) {
  62. event.stopPropagation();
  63. event.preventDefault();
  64. const label = document.getElementById('password-error');
  65. const input = document.getElementById('password-input');
  66. const btn = document.getElementById('password-btn');
  67. label.classList.add('invisible');
  68. input.classList.remove('border-red', 'dark:border-red-40');
  69. btn.classList.remove(
  70. 'bg-red',
  71. 'hover:bg-red',
  72. 'focus:bg-red',
  73. 'dark:bg-red-40'
  74. );
  75. }
  76. function checkPassword(event) {
  77. event.stopPropagation();
  78. event.preventDefault();
  79. const el = document.getElementById('password-input');
  80. const password = el.value;
  81. if (password.length > 0) {
  82. document.getElementById('password-btn').disabled = true;
  83. // Strip any url parameters between fileId and secretKey
  84. const fileInfoUrl = window.location.href.replace(/\?.+#/, '#');
  85. state.fileInfo.url = fileInfoUrl;
  86. state.fileInfo.password = password;
  87. emit('getMetadata');
  88. }
  89. return false;
  90. }
  91. return div;
  92. };