archiveTile.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* global Android LIMITS */
  2. const html = require('choo/html');
  3. const raw = require('choo/html/raw');
  4. const assets = require('../../common/assets');
  5. const {
  6. bytes,
  7. copyToClipboard,
  8. list,
  9. percent,
  10. platform,
  11. timeLeft
  12. } = require('../utils');
  13. const expiryOptions = require('./expiryOptions');
  14. function expiryInfo(translate, archive) {
  15. const l10n = timeLeft(archive.expiresAt - Date.now());
  16. return raw(
  17. translate('frontPageExpireInfo', {
  18. downloadCount: translate('downloadCount', {
  19. num: archive.dlimit - archive.dtotal
  20. }),
  21. timespan: translate(l10n.id, l10n)
  22. })
  23. );
  24. }
  25. function password(state) {
  26. const MAX_LENGTH = 32;
  27. return html`
  28. <div class="mb-2 px-1">
  29. <div class="checkbox inline-block mr-3">
  30. <input
  31. id="add-password"
  32. type="checkbox"
  33. ${state.archive.password ? 'checked' : ''}
  34. autocomplete="off"
  35. onchange="${togglePasswordInput}"
  36. />
  37. <label for="add-password">
  38. ${state.translate('addPasswordMessage')}
  39. </label>
  40. </div>
  41. <input
  42. id="password-input"
  43. class="${state.archive.password
  44. ? ''
  45. : 'invisible'} border rounded focus:border-blue-dark leading-normal my-1 py-1 px-2 h-8"
  46. autocomplete="off"
  47. maxlength="${MAX_LENGTH}"
  48. type="password"
  49. oninput="${inputChanged}"
  50. onfocus="${focused}"
  51. placeholder="${state.translate('unlockInputPlaceholder')}"
  52. value="${state.archive.password || ''}"
  53. />
  54. <label
  55. id="password-msg"
  56. for="password-input"
  57. class="block text-xs text-grey-darker"
  58. ></label>
  59. </div>
  60. `;
  61. function togglePasswordInput(event) {
  62. event.stopPropagation();
  63. const checked = event.target.checked;
  64. const input = document.getElementById('password-input');
  65. if (checked) {
  66. input.classList.remove('invisible');
  67. input.focus();
  68. } else {
  69. input.classList.add('invisible');
  70. input.value = '';
  71. document.getElementById('password-msg').textContent = '';
  72. state.archive.password = null;
  73. }
  74. }
  75. function inputChanged() {
  76. const passwordInput = document.getElementById('password-input');
  77. const pwdmsg = document.getElementById('password-msg');
  78. const password = passwordInput.value;
  79. const length = password.length;
  80. if (length === MAX_LENGTH) {
  81. pwdmsg.textContent = state.translate('maxPasswordLength', {
  82. length: MAX_LENGTH
  83. });
  84. } else {
  85. pwdmsg.textContent = '';
  86. }
  87. state.archive.password = password;
  88. }
  89. function focused(event) {
  90. event.preventDefault();
  91. const el = document.getElementById('password-input');
  92. if (el.placeholder !== state.translate('unlockInputPlaceholder')) {
  93. el.placeholder = '';
  94. }
  95. }
  96. }
  97. function fileInfo(file, action) {
  98. return html`
  99. <send-file class="flex flex-row items-center p-3 w-full">
  100. <img class="h-8" src="${assets.get('blue_file.svg')}"/>
  101. <p class="ml-4 w-full">
  102. <h1 class="text-sm font-medium word-break-all">${file.name}</h1>
  103. <div class="text-xs font-normal opacity-75 pt-1">${bytes(
  104. file.size
  105. )}</div>
  106. <div class="hidden">${file.type}</div>
  107. </p>
  108. ${action}
  109. </send-file>`;
  110. }
  111. function archiveDetails(translate, archive) {
  112. if (archive.manifest.files.length > 1) {
  113. return html`
  114. <details
  115. class="w-full pb-1"
  116. ${archive.open ? 'open' : ''}
  117. ontoggle="${toggled}"
  118. >
  119. <summary
  120. class="flex items-center text-blue-dark text-sm cursor-pointer outline-none"
  121. >
  122. <svg
  123. class="fill-current w-4 h-4 mr-1"
  124. xmlns="http://www.w3.org/2000/svg"
  125. viewBox="0 0 20 20"
  126. >
  127. <path
  128. d="M12.95 10.707l.707-.707L8 4.343 6.586 5.757 10.828 10l-4.242 4.243L8 15.657l4.95-4.95z"
  129. />
  130. </svg>
  131. ${translate('fileCount', {
  132. num: archive.manifest.files.length
  133. })}
  134. </summary>
  135. ${list(archive.manifest.files.map(f => fileInfo(f)), 'list-reset')}
  136. </details>
  137. `;
  138. }
  139. function toggled(event) {
  140. event.stopPropagation();
  141. archive.open = event.target.open;
  142. }
  143. }
  144. module.exports = function(state, emit, archive) {
  145. const copyOrShare =
  146. platform() !== 'android'
  147. ? html`
  148. <button
  149. class="text-blue-dark hover:text-blue-darker focus:text-blue-darker focus:outline self-end flex items-center"
  150. onclick=${copy}
  151. title="${state.translate('copyUrlHover')}"
  152. >
  153. <img src="${assets.get('copy-16.svg')}" class="mr-2" />
  154. ${state.translate('copyUrlHover')}
  155. </button>
  156. `
  157. : html`
  158. <button
  159. class="text-blue-dark hover:text-blue-darker focus:text-blue-darker self-end flex items-center"
  160. onclick=${share}
  161. title="Share"
  162. >
  163. <img src="${assets.get('share-24.svg')}" class="mr-2" /> Share
  164. </button>
  165. `;
  166. const dl =
  167. platform() === 'web'
  168. ? html`
  169. <a
  170. class="flex items-baseline text-blue-dark hover:text-blue-darker focus:text-blue-darker"
  171. href="${archive.url}"
  172. title="${state.translate('downloadButtonLabel')}"
  173. tabindex="0"
  174. >
  175. <img src="${assets.get('dl.svg')}" class="mr-2" />
  176. ${state.translate('downloadButtonLabel')}
  177. </a>
  178. `
  179. : html`
  180. <div></div>
  181. `;
  182. return html`
  183. <send-archive
  184. id="archive-${archive.id}"
  185. class="flex flex-col items-start rounded shadow-light bg-white p-4 w-full">
  186. <p class="w-full">
  187. <img class="float-left mr-3" src="${assets.get('blue_file.svg')}"/>
  188. <input
  189. type="image"
  190. class="float-right self-center text-white hover:opacity-75 focus:outline"
  191. alt="${state.translate('deleteButtonHover')}"
  192. title="${state.translate('deleteButtonHover')}"
  193. src="${assets.get('close-16.svg')}"
  194. onclick=${del}/>
  195. <h1 class="text-sm font-medium word-break-all">${archive.name}</h1>
  196. <div class="text-xs font-normal opacity-75 pt-1">${bytes(
  197. archive.size
  198. )}</div>
  199. </p>
  200. <div class="text-xs text-grey-dark w-full mt-2 mb-2">
  201. ${expiryInfo(state.translate, archive)}
  202. </div>
  203. ${archiveDetails(state.translate, archive)}
  204. <hr class="w-full border-t my-4">
  205. <div class="flex justify-between w-full">
  206. ${dl}
  207. ${copyOrShare}
  208. </div>
  209. </send-archive>`;
  210. function copy(event) {
  211. event.stopPropagation();
  212. copyToClipboard(archive.url);
  213. const text = event.target.lastChild;
  214. text.textContent = state.translate('copiedUrl');
  215. setTimeout(
  216. () => (text.textContent = state.translate('copyUrlHover')),
  217. 1000
  218. );
  219. }
  220. function del(event) {
  221. event.stopPropagation();
  222. emit('delete', archive);
  223. }
  224. function share(event) {
  225. event.stopPropagation();
  226. Android.shareUrl(archive.url);
  227. }
  228. };
  229. module.exports.wip = function(state, emit) {
  230. return html`
  231. <send-upload-area class="flex flex-col bg-white h-full w-full" id="wip">
  232. ${list(
  233. Array.from(state.archive.files)
  234. .reverse()
  235. .map(f =>
  236. fileInfo(f, remove(f, state.translate('deleteButtonHover')))
  237. ),
  238. 'bg-grey-lightest rounded-t list-reset overflow-y-auto px-6 py-4 md:h-full md:max-h-half-screen',
  239. 'bg-white px-2 my-2 shadow-light rounded'
  240. )}
  241. <div
  242. class="flex-grow flex items-end p-4 bg-grey-lightest rounded-b mb-1 font-medium"
  243. >
  244. <input
  245. id="file-upload"
  246. class="opacity-0 w-0 h-0 appearance-none"
  247. type="file"
  248. multiple
  249. onfocus="${focus}"
  250. onblur="${blur}"
  251. onchange="${add}"
  252. />
  253. <div
  254. for="file-upload"
  255. class="flex flex-row items-center justify-between w-full p-2"
  256. >
  257. <label
  258. for="file-upload"
  259. class="flex items-center cursor-pointer"
  260. title="${state.translate('addFilesButton')}"
  261. >
  262. <img src="${assets.get('addfiles.svg')}" class="w-6 h-6 mr-2" />
  263. ${state.translate('addFilesButton')}
  264. </label>
  265. <div class="font-normal text-sm text-grey-darker">
  266. ${state.translate('totalSize', {
  267. size: bytes(state.archive.size)
  268. })}
  269. </div>
  270. </div>
  271. </div>
  272. ${expiryOptions(state, emit)} ${password(state, emit)}
  273. <button
  274. id="upload-btn"
  275. class="btn rounded-lg flex-no-shrink focus:outline"
  276. title="${state.translate('uploadFilesButton')}"
  277. onclick="${upload}"
  278. >
  279. ${state.translate('uploadFilesButton')}
  280. </button>
  281. </send-upload-area>
  282. `;
  283. function focus(event) {
  284. event.target.nextElementSibling.firstElementChild.classList.add('outline');
  285. }
  286. function blur(event) {
  287. event.target.nextElementSibling.firstElementChild.classList.remove(
  288. 'outline'
  289. );
  290. }
  291. function upload(event) {
  292. window.scrollTo(0, 0);
  293. event.preventDefault();
  294. event.target.disabled = true;
  295. if (!state.uploading) {
  296. emit('upload');
  297. }
  298. }
  299. function add(event) {
  300. event.preventDefault();
  301. const newFiles = Array.from(event.target.files);
  302. emit('addFiles', { files: newFiles });
  303. setTimeout(() => {
  304. document
  305. .querySelector('#wip > ul > li:first-child')
  306. .scrollIntoView({ block: 'center' });
  307. });
  308. }
  309. function remove(file, desc) {
  310. return html`
  311. <input
  312. type="image"
  313. class="self-center text-white ml-4 h-4 hover:opacity-75 focus:outline"
  314. alt="${desc}"
  315. title="${desc}"
  316. src="${assets.get('close-16.svg')}"
  317. onclick="${del}"
  318. />
  319. `;
  320. function del(event) {
  321. event.stopPropagation();
  322. emit('removeUpload', file);
  323. }
  324. }
  325. };
  326. module.exports.uploading = function(state, emit) {
  327. const progress = state.transfer.progressRatio;
  328. const progressPercent = percent(progress);
  329. const archive = state.archive;
  330. return html`
  331. <send-upload-area
  332. id="${archive.id}"
  333. class="flex flex-col items-start rounded shadow-light bg-white p-4 w-full">
  334. <p class="w-full">
  335. <img class="float-left mr-3" src="${assets.get('blue_file.svg')}"/>
  336. <h1 class="text-sm font-medium word-break-all">${archive.name}</h1>
  337. <div class="text-xs font-normal opacity-75 pt-1">${bytes(
  338. archive.size
  339. )}</div>
  340. </p>
  341. <div class="text-xs text-grey-dark w-full mt-2 mb-2">
  342. ${expiryInfo(state.translate, {
  343. dlimit: state.archive.dlimit,
  344. dtotal: 0,
  345. expiresAt: Date.now() + 500 + state.archive.timeLimit * 1000
  346. })}
  347. </div>
  348. <div class="text-blue-dark text-sm font-medium mt-2">${progressPercent}</div>
  349. <progress class="my-3" value="${progress}">${progressPercent}</progress>
  350. <button
  351. class="text-blue-dark hover:text-blue-darker focus:text-blue-darker self-end font-medium"
  352. onclick=${cancel}
  353. title="${state.translate('uploadingPageCancelShort')}">
  354. ${state.translate('uploadingPageCancelShort')}
  355. </button>
  356. </send-upload-area>`;
  357. function cancel(event) {
  358. event.stopPropagation();
  359. event.target.disabled = true;
  360. emit('cancel');
  361. }
  362. };
  363. module.exports.empty = function(state, emit) {
  364. const upsell = state.user.loggedIn
  365. ? ''
  366. : html`
  367. <p class="center font-medium text-xs text-grey-dark mt-4 mb-2">
  368. ${state.translate('signInSizeBump', {
  369. size: bytes(LIMITS.MAX_FILE_SIZE, 0)
  370. })}
  371. </p>
  372. `;
  373. return html`
  374. <send-upload-area
  375. class="flex flex-col items-center justify-center border-2 border-dashed border-grey rounded px-6 py-16 h-full w-full"
  376. onclick="${e => {
  377. if (e.target.tagName !== 'LABEL') {
  378. document.getElementById('file-upload').click();
  379. }
  380. }}"
  381. >
  382. <img src="${assets.get('addfiles.svg')}" width="48" height="48" />
  383. <div
  384. class="pt-6 pb-2 text-center text-lg font-bold capitalize tracking-wide"
  385. >
  386. ${state.translate('uploadDropDragMessage')}
  387. </div>
  388. <div class="pb-6 text-center text-base">
  389. ${state.translate('uploadDropButtonMessage')}
  390. </div>
  391. <input
  392. id="file-upload"
  393. class="opacity-0"
  394. type="file"
  395. multiple
  396. onfocus="${focus}"
  397. onblur="${blur}"
  398. onchange="${add}"
  399. onclick="${e => e.stopPropagation()}"
  400. />
  401. <label
  402. for="file-upload"
  403. role="button"
  404. class="btn rounded-lg flex items-center mt-4"
  405. title="${state.translate('addFilesButtonWithSize', {
  406. size: bytes(state.user.maxSize, 0)
  407. })}"
  408. >
  409. ${state.translate('addFilesButtonWithSize', {
  410. size: bytes(state.user.maxSize, 0)
  411. })}
  412. </label>
  413. ${upsell}
  414. </send-upload-area>
  415. `;
  416. function focus(event) {
  417. event.target.nextElementSibling.classList.add('bg-blue-darker', 'outline');
  418. }
  419. function blur(event) {
  420. event.target.nextElementSibling.classList.remove(
  421. 'bg-blue-darker',
  422. 'outline'
  423. );
  424. }
  425. function add(event) {
  426. event.preventDefault();
  427. const newFiles = Array.from(event.target.files);
  428. emit('addFiles', { files: newFiles });
  429. }
  430. };
  431. module.exports.preview = function(state, emit) {
  432. const archive = state.fileInfo;
  433. if (archive.open === undefined) {
  434. archive.open = true;
  435. }
  436. return html`
  437. <send-archive class="flex flex-col max-h-full bg-white p-4 w-full md:w-128">
  438. <div class="border rounded py-3 px-6">
  439. <p class="w-full mb-4">
  440. <img class="float-left mr-3" src="${assets.get('blue_file.svg')}"/>
  441. <h1 class="text-sm font-medium word-break-all">${archive.name}</h1>
  442. <div class="text-xs font-normal opacity-75 pt-1">${bytes(
  443. archive.size
  444. )}</div>
  445. </p>
  446. <div class="h-full md:h-48 overflow-y-auto">
  447. ${archiveDetails(state.translate, archive)}
  448. </div>
  449. </div>
  450. <button
  451. id="download-btn"
  452. class="btn rounded-lg mt-4 w-full flex-no-shrink focus:outline"
  453. title="${state.translate('downloadButtonLabel')}"
  454. onclick=${download}>
  455. ${state.translate('downloadButtonLabel')}
  456. </button>
  457. </send-archive>`;
  458. function download(event) {
  459. event.preventDefault();
  460. event.target.disabled = true;
  461. emit('download', archive);
  462. }
  463. };
  464. module.exports.downloading = function(state) {
  465. const archive = state.fileInfo;
  466. const progress = state.transfer.progressRatio;
  467. const progressPercent = percent(progress);
  468. return html`
  469. <send-archive class="flex flex-col bg-white rounded shadow-light p-4 w-full md:w-4/5">
  470. <p class="w-full mb-4">
  471. <img class="float-left mr-3" src="${assets.get('blue_file.svg')}"/>
  472. <h1 class="text-sm font-medium word-break-all">${archive.name}</h1>
  473. <div class="text-xs font-normal opacity-75 pt-1">${bytes(
  474. archive.size
  475. )}</div>
  476. </p>
  477. <div class="text-blue-dark text-sm font-medium mt-2">${progressPercent}</div>
  478. <progress class="my-3" value="${progress}">${progressPercent}</progress>
  479. </send-archive>`;
  480. };