forked from mozilla/send
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchiveTile.js
More file actions
590 lines (557 loc) · 16.7 KB
/
archiveTile.js
File metadata and controls
590 lines (557 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* global Android */
const html = require('choo/html');
const raw = require('choo/html/raw');
const assets = require('../../common/assets');
const {
bytes,
copyToClipboard,
list,
percent,
platform,
timeLeft
} = require('../utils');
const expiryOptions = require('./expiryOptions');
function expiryInfo(translate, archive) {
const l10n = timeLeft(archive.expiresAt - Date.now());
return raw(
translate('archiveExpiryInfo', {
downloadCount: translate('downloadCount', {
num: archive.dlimit - archive.dtotal
}),
timespan: translate(l10n.id, l10n)
})
);
}
function password(state) {
const MAX_LENGTH = 32;
return html`
<div class="mb-2 px-1">
<input
id="autocomplete-decoy"
class="hidden"
type="password"
value="lol"
/>
<div class="checkbox inline-block mr-3">
<input
id="add-password"
type="checkbox"
${state.archive.password ? 'checked' : ''}
autocomplete="off"
onchange="${togglePasswordInput}"
/>
<label for="add-password">
${state.translate('addPassword')}
</label>
</div>
<input
id="password-input"
class="${state.archive.password
? ''
: 'invisible'} border rounded focus:border-blue-60 leading-normal my-1 py-1 px-2 h-8 dark:bg-grey-80"
autocomplete="off"
maxlength="${MAX_LENGTH}"
type="password"
oninput="${inputChanged}"
onfocus="${focused}"
placeholder="${state.translate('unlockInputPlaceholder')}"
value="${state.archive.password || ''}"
/>
<label
id="password-msg"
for="password-input"
class="block text-xs text-grey-70"
></label>
</div>
`;
function togglePasswordInput(event) {
event.stopPropagation();
const checked = event.target.checked;
const input = document.getElementById('password-input');
if (checked) {
input.classList.remove('invisible');
input.focus();
} else {
input.classList.add('invisible');
input.value = '';
document.getElementById('password-msg').textContent = '';
state.archive.password = null;
}
}
function inputChanged() {
const passwordInput = document.getElementById('password-input');
const pwdmsg = document.getElementById('password-msg');
const password = passwordInput.value;
const length = password.length;
if (length === MAX_LENGTH) {
pwdmsg.textContent = state.translate('maxPasswordLength', {
length: MAX_LENGTH
});
} else {
pwdmsg.textContent = '';
}
state.archive.password = password;
}
function focused(event) {
event.preventDefault();
const el = document.getElementById('password-input');
if (el.placeholder !== state.translate('unlockInputPlaceholder')) {
el.placeholder = '';
}
}
}
function fileInfo(file, action) {
return html`
<send-file class="flex flex-row items-center p-3 w-full">
<svg class="h-8 w-8 text-white dark:text-grey-90">
<use xlink:href="${assets.get('blue_file.svg')}#icon"/>
</svg>
<p class="ml-4 w-full">
<h1 class="text-base font-medium word-break-all">${file.name}</h1>
<div class="text-sm font-normal opacity-75 pt-1">${bytes(
file.size
)}</div>
</p>
${action}
</send-file>`;
}
function archiveInfo(archive, action) {
return html`
<p class="w-full flex items-center">
<svg class="h-8 w-6 mr-3 flex-shrink-0 text-white dark:text-grey-90">
<use xlink:href="${assets.get('blue_file.svg')}#icon"/>
</svg>
<p class="flex-grow">
<h1 class="text-base font-medium word-break-all">${archive.name}</h1>
<div class="text-sm font-normal opacity-75 pt-1">${bytes(
archive.size
)}</div>
</p>
${action}
</p>`;
}
function archiveDetails(translate, archive) {
if (archive.manifest.files.length > 1) {
return html`
<details
class="w-full pb-1"
${archive.open ? 'open' : ''}
ontoggle="${toggled}"
>
<summary
class="flex items-center link-blue text-sm cursor-pointer outline-none"
>
<svg
class="fill-current w-4 h-4 mr-1"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
>
<path
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"
/>
</svg>
${translate('fileCount', {
num: archive.manifest.files.length
})}
</summary>
${list(archive.manifest.files.map(f => fileInfo(f)))}
</details>
`;
}
function toggled(event) {
event.stopPropagation();
archive.open = event.target.open;
}
}
module.exports = function(state, emit, archive) {
const copyOrShare =
state.capabilities.share || platform() === 'android'
? html`
<button
class="link-blue self-end flex items-start"
onclick=${share}
title="Share link"
>
<svg class="h-4 w-4 mr-2">
<use xlink:href="${assets.get('share-24.svg')}#icon" />
</svg>
Share link
</button>
`
: html`
<button
class="link-blue focus:outline self-end flex items-center"
onclick=${copy}
title="${state.translate('copyLinkButton')}"
>
<svg class="h-4 w-4 mr-2">
<use xlink:href="${assets.get('copy-16.svg')}#icon" />
</svg>
${state.translate('copyLinkButton')}
</button>
`;
const dl =
platform() === 'web'
? html`
<a
class="flex items-baseline link-blue"
href="${archive.url}"
title="${state.translate('downloadButtonLabel')}"
tabindex="0"
>
<svg class="h-4 w-3 mr-2">
<use xlink:href="${assets.get('dl.svg')}#icon" />
</svg>
${state.translate('downloadButtonLabel')}
</a>
`
: html`
<div></div>
`;
return html`
<send-archive
id="archive-${archive.id}"
class="flex flex-col items-start rounded shadow-light bg-white p-4 w-full dark:bg-grey-90 dark:border dark:border-grey-70"
>
${archiveInfo(
archive,
html`
<input
type="image"
class="self-start flex-shrink-0 text-white hover:opacity-75 focus:outline"
alt="${state.translate('deleteButtonHover')}"
title="${state.translate('deleteButtonHover')}"
src="${assets.get('close-16.svg')}"
onclick=${del}
/>
`
)}
<div class="text-sm opacity-75 w-full mt-2 mb-2">
${expiryInfo(state.translate, archive)}
</div>
${archiveDetails(state.translate, archive)}
<hr class="w-full border-t my-4 dark:border-grey-70" />
<div class="flex justify-between w-full">
${dl} ${copyOrShare}
</div>
</send-archive>
`;
function copy(event) {
event.stopPropagation();
copyToClipboard(archive.url);
const text = event.target.lastChild;
text.textContent = state.translate('copiedUrl');
setTimeout(
() => (text.textContent = state.translate('copyLinkButton')),
1000
);
}
function del(event) {
event.stopPropagation();
emit('delete', archive);
}
async function share(event) {
event.stopPropagation();
if (platform() === 'android') {
Android.shareUrl(archive.url);
} else {
try {
await navigator.share({
title: state.translate('-send-brand'),
text: `Download "${archive.name}" with Firefox Send: simple, safe file sharing`,
//state.translate('shareMessage', { name }),
url: archive.url
});
} catch (e) {
// ignore
}
}
}
};
module.exports.wip = function(state, emit) {
return html`
<send-upload-area
class="flex flex-col bg-white h-full w-full dark:bg-grey-90"
id="wip"
>
${list(
Array.from(state.archive.files)
.reverse()
.map(f =>
fileInfo(f, remove(f, state.translate('deleteButtonHover')))
),
'flex-shrink bg-grey-10 rounded-t overflow-y-auto px-6 py-4 md:h-full md:max-h-half-screen dark:bg-black',
'bg-white px-2 my-2 shadow-light rounded dark:bg-grey-90 dark:border dark:border-grey-80'
)}
<div
class="flex-shrink-0 flex-grow flex items-end p-4 bg-grey-10 rounded-b mb-1 font-medium dark:bg-grey-90"
>
<input
id="file-upload"
class="opacity-0 w-0 h-0 appearance-none absolute overflow-hidden"
type="file"
multiple
onfocus="${focus}"
onblur="${blur}"
onchange="${add}"
/>
<div
for="file-upload"
class="flex flex-row items-center justify-between w-full p-2"
>
<label
for="file-upload"
class="flex items-center cursor-pointer"
title="${state.translate('addFilesButton')}"
>
<svg class="w-6 h-6 mr-2 link-blue">
<use xlink:href="${assets.get('addfiles.svg')}#plus" />
</svg>
${state.translate('addFilesButton')}
</label>
<div class="font-normal text-sm text-grey-70 dark:text-grey-40">
${state.translate('totalSize', {
size: bytes(state.archive.size)
})}
</div>
</div>
</div>
${expiryOptions(state, emit)} ${password(state, emit)}
<button
id="upload-btn"
class="btn rounded-lg flex-shrink-0 focus:outline"
title="${state.translate('uploadButton')}"
onclick="${upload}"
>
${state.translate('uploadButton')}
</button>
</send-upload-area>
`;
function focus(event) {
event.target.nextElementSibling.firstElementChild.classList.add('outline');
}
function blur(event) {
event.target.nextElementSibling.firstElementChild.classList.remove(
'outline'
);
}
function upload(event) {
window.scrollTo(0, 0);
event.preventDefault();
event.target.disabled = true;
if (!state.uploading) {
emit('upload');
}
}
function add(event) {
event.preventDefault();
const newFiles = Array.from(event.target.files);
emit('addFiles', { files: newFiles });
setTimeout(() => {
document
.querySelector('#wip > ul > li:first-child')
.scrollIntoView({ block: 'center' });
});
}
function remove(file, desc) {
return html`
<input
type="image"
class="self-center text-white ml-4 h-4 hover:opacity-75 focus:outline"
alt="${desc}"
title="${desc}"
src="${assets.get('close-16.svg')}"
onclick="${del}"
/>
`;
function del(event) {
event.stopPropagation();
emit('removeUpload', file);
}
}
};
module.exports.uploading = function(state, emit) {
const progress = state.transfer.progressRatio;
const progressPercent = percent(progress);
const archive = state.archive;
return html`
<send-upload-area
id="${archive.id}"
class="flex flex-col items-start rounded shadow-light bg-white p-4 w-full dark:bg-grey-90"
>
${archiveInfo(archive)}
<div class="text-xs opacity-75 w-full mt-2 mb-2">
${expiryInfo(state.translate, {
dlimit: state.archive.dlimit,
dtotal: 0,
expiresAt: Date.now() + 500 + state.archive.timeLimit * 1000
})}
</div>
<div class="link-blue text-sm font-medium mt-2">
${progressPercent}
</div>
<progress class="my-3" value="${progress}">${progressPercent}</progress>
<button
class="link-blue self-end font-medium"
onclick=${cancel}
title="${state.translate('deletePopupCancel')}"
>
${state.translate('deletePopupCancel')}
</button>
</send-upload-area>
`;
function cancel(event) {
event.stopPropagation();
event.target.disabled = true;
emit('cancel');
}
};
module.exports.empty = function(state, emit) {
const upsell =
state.user.loggedIn || !state.capabilities.account
? ''
: html`
<button
class="center font-medium text-sm link-blue mt-4 mb-2"
onclick="${event => {
event.stopPropagation();
emit('signup-cta', 'drop');
}}"
>
${state.translate('signInSizeBump', {
size: bytes(state.LIMITS.MAX_FILE_SIZE)
})}
</button>
`;
return html`
<send-upload-area
class="flex flex-col items-center justify-center border-2 border-dashed border-grey-transparent rounded px-6 py-16 h-full w-full dark:border-grey-60"
onclick="${e => {
if (e.target.tagName !== 'LABEL') {
document.getElementById('file-upload').click();
}
}}"
>
<svg class="w-10 h-10 link-blue">
<use xlink:href="/${assets.get('addfiles.svg')}#plus" />
</svg>
<div class="pt-6 pb-2 text-center text-lg font-bold tracking-wide">
${state.translate('dragAndDropFiles')}
</div>
<div class="pb-6 text-center text-base">
${state.translate('orClickWithSize', {
size: bytes(state.user.maxSize)
})}
</div>
<input
id="file-upload"
class="opacity-0 w-0 h-0 appearance-none absolute overflow-hidden"
type="file"
multiple
onfocus="${focus}"
onblur="${blur}"
onchange="${add}"
onclick="${e => e.stopPropagation()}"
/>
<label
for="file-upload"
role="button"
class="btn rounded-lg flex items-center mt-4"
title="${state.translate('addFilesButton', {
size: bytes(state.user.maxSize)
})}"
>
${state.translate('addFilesButton')}
</label>
<p
class="font-normal text-sm text-grey-50 dark:text-grey-40 my-6 mx-12 text-center max-w-sm leading-loose"
>
${state.translate('trustWarningMessage')}
</p>
${upsell}
</send-upload-area>
`;
function focus(event) {
event.target.nextElementSibling.classList.add('bg-blue-70', 'outline');
}
function blur(event) {
event.target.nextElementSibling.classList.remove('bg-blue-70', 'outline');
}
function add(event) {
event.preventDefault();
const newFiles = Array.from(event.target.files);
emit('addFiles', { files: newFiles });
}
};
module.exports.preview = function(state, emit) {
const archive = state.fileInfo;
if (archive.open === undefined) {
archive.open = true;
}
const single = archive.manifest.files.length === 1;
const details = single
? ''
: html`
<div class="mt-4 h-full md:h-48 overflow-y-auto">
${archiveDetails(state.translate, archive)}
</div>
`;
return html`
<send-archive
class="flex flex-col max-h-full bg-white w-full dark:bg-grey-90"
>
<div class="border rounded py-3 px-4 dark:border-grey-70">
${archiveInfo(archive)} ${details}
</div>
<div class="checkbox inline-block mt-6 mx-auto">
<input
id="trust-download"
type="checkbox"
autocomplete="off"
onchange="${toggleDownloadEnabled}"
/>
<label for="trust-download">
${state.translate('downloadTrustCheckbox', {
count: archive.manifest.files.length
})}
</label>
</div>
<button
id="download-btn"
disabled
class="btn rounded-lg mt-4 w-full flex-shrink-0 focus:outline"
title="${state.translate('downloadButtonLabel')}"
onclick=${download}
>
${state.translate('downloadButtonLabel')}
</button>
</send-archive>
`;
function toggleDownloadEnabled(event) {
event.stopPropagation();
const checked = event.target.checked;
const btn = document.getElementById('download-btn');
btn.disabled = !checked;
}
function download(event) {
event.preventDefault();
event.target.disabled = true;
emit('download', archive);
}
};
module.exports.downloading = function(state) {
const archive = state.fileInfo;
const progress = state.transfer.progressRatio;
const progressPercent = percent(progress);
return html`
<send-archive
class="flex flex-col bg-white rounded shadow-light p-4 w-full max-w-sm md:w-128 dark:bg-grey-90"
>
${archiveInfo(archive)}
<div class="link-blue text-sm font-medium mt-2">
${progressPercent}
</div>
<progress class="my-3" value="${progress}">${progressPercent}</progress>
</send-archive>
`;
};