-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp.js
More file actions
152 lines (140 loc) · 4.75 KB
/
help.js
File metadata and controls
152 lines (140 loc) · 4.75 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
window.addEventListener("DOMContentLoaded",() => {
const upload = new UploadModal("#upload");
});
class UploadModal {
filename = "";
isCopying = false;
isUploading = false;
progress = 0;
progressTimeout = null;
state = 0;
constructor(el) {
this.el = document.querySelector(el);
this.el?.addEventListener("click",this.action.bind(this));
this.el?.querySelector("#file")?.addEventListener("change",this.fileHandle.bind(this));
}
action(e) {
this[e.target?.getAttribute("data-action")]?.();
this.stateDisplay();
}
cancel() {
this.isUploading = false;
this.progress = 0;
this.progressTimeout = null;
this.state = 0;
this.stateDisplay();
this.progressDisplay();
this.fileReset();
}
async copy() {
const copyButton = this.el?.querySelector("[data-action='copy']");
if (!this.isCopying && copyButton) {
// disable
this.isCopying = true;
copyButton.style.width = `${copyButton.offsetWidth}px`;
copyButton.disabled = true;
copyButton.textContent = "Copied!";
navigator.clipboard.writeText(this.filename);
await new Promise(res => setTimeout(res, 1000));
// reenable
this.isCopying = false;
copyButton.removeAttribute("style");
copyButton.disabled = false;
copyButton.textContent = "Copy Link";
}
}
fail() {
this.isUploading = false;
this.progress = 0;
this.progressTimeout = null;
this.state = 2;
this.stateDisplay();
}
file() {
this.el?.querySelector("#file").click();
}
fileDisplay(name = "") {
// update the name
this.filename = name;
const fileValue = this.el?.querySelector("[data-file]");
if (fileValue) fileValue.textContent = this.filename;
// show the file
this.el?.setAttribute("data-ready", this.filename ? "true" : "false");
}
fileHandle(e) {
return new Promise(() => {
const { target } = e;
if (target?.files.length) {
let reader = new FileReader();
reader.onload = e2 => {
this.fileDisplay(target.files[0].name);
};
reader.readAsDataURL(target.files[0]);
}
});
}
fileReset() {
const fileField = this.el?.querySelector("#file");
if (fileField) fileField.value = null;
this.fileDisplay();
}
progressDisplay() {
const progressValue = this.el?.querySelector("[data-progress-value]");
const progressFill = this.el?.querySelector("[data-progress-fill]");
const progressTimes100 = Math.floor(this.progress * 100);
if (progressValue) progressValue.textContent = `${progressTimes100}%`;
if (progressFill) progressFill.style.transform = `translateX(${progressTimes100}%)`;
}
async progressLoop() {
this.progressDisplay();
if (this.isUploading) {
if (this.progress === 0) {
await new Promise(res => setTimeout(res, 1000));
// fail randomly
if (!this.isUploading) {
return;
} else if (Utils.randomInt(0,2) === 0) {
this.fail();
return;
}
}
// …or continue with progress
if (this.progress < 1) {
this.progress += 0.01;
this.progressTimeout = setTimeout(this.progressLoop.bind(this), 50);
} else if (this.progress >= 1) {
this.progressTimeout = setTimeout(() => {
if (this.isUploading) {
this.success();
this.stateDisplay();
this.progressTimeout = null;
}
}, 250);
}
}
}
stateDisplay() {
this.el?.setAttribute("data-state", `${this.state}`);
}
success() {
this.isUploading = false;
this.state = 3;
this.stateDisplay();
}
upload() {
if (!this.isUploading) {
this.isUploading = true;
this.progress = 0;
this.state = 1;
this.progressLoop();
}
}
}
class Utils {
static randomInt(min = 0,max = 2**32) {
const percent = crypto.getRandomValues(new Uint32Array(1))[0] / 2**32;
const relativeValue = (max - min) * percent;
// end
return Math.round(min + relativeValue);
}
}