-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLitebox.js
More file actions
57 lines (50 loc) · 1.68 KB
/
Litebox.js
File metadata and controls
57 lines (50 loc) · 1.68 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
import { getElementById } from './getElementById';
const
maskId = 'liteboxMask',
downloadSelector = '#litebox .download',
closeSelector = '#litebox .close',
contentSelector = '#litebox .content',
summarySelector = '#litebox .summary';
export class Litebox {
#main;
#download;
#close;
#content;
#summary;
#imageUrl;
#isBlobUrl;
#fileName;
#downloadHandler;
open(imageUrl, filename, summary, isBlobUrl) {
this.#imageUrl = imageUrl;
this.#isBlobUrl = isBlobUrl;
this.#main.classList.add('open');
this.#fileName = filename;
this.#summary.textContent = summary;
this.#content.src = this.#imageUrl;
this.#content.title = filename;
this.#downloadHandler = () => this.#onDownloadClick();
this.#download.addEventListener('click', this.#downloadHandler);
}
close() {
this.#main.classList.remove('open');
if(this.#isBlobUrl) {
URL.revokeObjectURL(this.#imageUrl);
}
this.#download.removeEventListener('click', this.#downloadHandler);
}
#onDownloadClick() {
const hyperLink = document.createElement('a');
hyperLink.href = this.#imageUrl;
hyperLink.download = this.#fileName;
hyperLink.click();
}
constructor() {
this.#main = getElementById(maskId);
this.#download = document.querySelector(downloadSelector);
this.#close = document.querySelector(closeSelector);
this.#close.addEventListener('click', () => this.close());
this.#content = document.querySelector(contentSelector);
this.#summary = document.querySelector(summarySelector);
}
}