expiryOptions.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const html = require('choo/html');
  2. const raw = require('choo/html/raw');
  3. const { secondsToL10nId } = require('../utils');
  4. const selectbox = require('./selectbox');
  5. module.exports = function(state, emit) {
  6. const el = html`
  7. <div class="px-1">
  8. ${raw(
  9. state.translate('archiveExpiryInfo', {
  10. downloadCount:
  11. '<span class="lg:inline-block md:block sm:inline-block block"></span><select id="dlCount"></select>',
  12. timespan: '<select id="timespan"></select>'
  13. })
  14. )}
  15. </div>
  16. `;
  17. if (el.__encoded) {
  18. // we're rendering on the server
  19. return el;
  20. }
  21. const counts = state.DEFAULTS.DOWNLOAD_COUNTS.filter(
  22. i => state.capabilities.account || i <= state.user.maxDownloads
  23. );
  24. const dlCountSelect = el.querySelector('#dlCount');
  25. el.replaceChild(
  26. selectbox(
  27. state.archive.dlimit,
  28. counts,
  29. num => state.translate('downloadCount', { num }),
  30. value => {
  31. const selected = parseInt(value);
  32. state.archive.dlimit = selected;
  33. emit('render');
  34. if (selected > parseInt(state.user.maxDownloads || '0')) {
  35. console.log('Chosen max download count is larger than the allowed limit', selected)
  36. }
  37. },
  38. 'expire-after-dl-count-select'
  39. ),
  40. dlCountSelect
  41. );
  42. const expires = state.DEFAULTS.EXPIRE_TIMES_SECONDS.filter(
  43. i => state.capabilities.account || i <= state.user.maxExpireSeconds
  44. );
  45. const timeSelect = el.querySelector('#timespan');
  46. el.replaceChild(
  47. selectbox(
  48. state.archive.timeLimit,
  49. expires,
  50. num => {
  51. const l10n = secondsToL10nId(num);
  52. return state.translate(l10n.id, l10n);
  53. },
  54. value => {
  55. const selected = parseInt(value);
  56. state.archive.timeLimit = selected;
  57. emit('render');
  58. if (selected > parseInt(state.user.maxExpireSeconds || '0')) {
  59. console.log('Chosen download expiration is larger than the allowed limit', selected)
  60. }
  61. },
  62. 'expire-after-time-select'
  63. ),
  64. timeSelect
  65. );
  66. return el;
  67. };