forked from elninotech/uppload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
309 lines (281 loc) Β· 9.88 KB
/
index.js
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
import { addGlobalEvent } from "./modules/dispatch";
import en from "./modules/i18n/en";
import dispatch from "./modules/dispatch";
import servicesFunction from "./modules/services";
import css from "./uppload.scss";
import loadFile from "./modules/loadFile";
import arrayIncludes from "./modules/arrayIncludes";
/*
* Converts number of bytes to readable string
* 10000 => "10 KB"
* Source: https://stackoverflow.com/a/18650828
*/
const bytesToSize = (bytes, decimals) => {
if (bytes == 0) return "0 bytes";
let k = 1000,
dm = decimals || 2,
sizes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"],
i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
};
/*
* Checks whether browsers supports video
* without actually asking permission for it
*/
let webcamAvailable = false;
navigator.getMedia =
navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (typeof navigator.getMedia === "function") {
webcamAvailable = true;
}
class Uppload {
/**
* Initialize the plugin
* @constructor
* @param {object} settings - Object with user's config
*/
constructor(settings) {
/*
* Metadata containing unique ID (multiple instances)
* Also includes globals like files
*/
this.meta = {
uniqueId: Math.random()
.toString(36)
.slice(2),
file: null
};
// Set user preferences as `settings`
this.settings = settings || {};
// Internationalization of text
this.i18n = this.settings.i18n || en;
// Stores boolean values if modal is open/uploading
this.isOpen = false;
this.isUploading = false;
// Stores current image URL value
this.value = null;
// Current page you're on, fallback to default service or "upload"
this.currentPage = this.settings.defaultService || "upload";
// Array of services plugin should have, fallback default
this.settings.services = this.settings.services || ["upload", "camera", "link", "instagram", "facebook"];
// Remove `camera` from the array if browser doesn't support it
if (!webcamAvailable) {
const index = this.settings.services.indexOf("camera");
if (index > -1) {
this.settings.services.splice(index, 1);
}
}
// Array or string contains allowed file types, default "*" => all
this.settings.allowedTypes = this.settings.allowedTypes || "*";
// Object contains configuration for image cropping
this.settings.crop = this.settings.crop || {};
// Object contains configuration for image format
this.settings.uploadFormat = this.settings.uploadFormat || {};
// Integer containing maximum file size, fallback to 100 MB
this.settings.maxFileSize = parseInt(this.settings.maxFileSize) || "infinite";
/**
* Returns whether current file type is allowed or not
* @param {File} file - File object containing selected file
* @returns {boolean}
*/
this.isFileTypeAllowed =
this.settings.isFileTypeAllowed ||
((file = this.meta.file) => {
if (typeof this.settings.allowedTypes === "object" && this.settings.allowedTypes.length > 0) {
if (arrayIncludes(this.settings.allowedTypes, file.type)) {
return true;
}
} else if (this.settings.allowedTypes === file.type) {
return true;
} else if (this.settings.allowedTypes === "*") {
return true;
} else {
if (arrayIncludes(file.type, `${this.settings.allowedTypes}/`)) {
return true;
}
}
return false;
});
/**
* Returns whether file size is in allowed range
* @param {File} file - File object containing selected file
* @returns {boolean}
*/
this.isFileSizeAllowed =
this.settings.isFileSizeAllowed ||
((file = this.meta.file) => {
if (this.settings.maxFileSize === "infinite") {
return true;
} else if (this.settings.maxFileSize > file.size) {
return true;
}
return false;
});
// Get all services
this.services = servicesFunction(this);
// Append modal to body
this.backgroundElement = document.createElement("div");
this.backgroundElement.classList.add("uppload-bg");
document.body.appendChild(this.backgroundElement);
this.modalElement = document.createElement("div");
this.modalElement.classList.add("uppload-modal");
this.modalElement.setAttribute("id", `uppload_${this.meta.uniqueId}`);
this.modalElement.innerHTML = `
<div>
${this.services.navbar.html}
<section>
<div class="errorMessage"></div>
<div class="currentPage"></div>
</section>
</div>
`;
document.body.appendChild(this.modalElement);
const navbarChildren = document.querySelectorAll(`#uppload_${this.meta.uniqueId} .button_service`);
for (let i = 0; i < navbarChildren.length; i++) {
let currentChild = navbarChildren[i];
currentChild.addEventListener("click", () => {
this.changePage(currentChild.className.replace("button_service button_service_", ""));
});
}
// Add keyboard and click events to close modal
this.backgroundElement.addEventListener("click", this.closeModal.bind(this));
window.addEventListener("keyup", event => {
if (event.keyCode === 27 || event.which === 27 || event.key === "Escape" || event.code === "Escape") {
this.backgroundElement.click();
}
});
// Update default value of image
if (this.settings.value) {
this.updateValue(this.settings.value, 1);
}
// Add click event to button
this.settings.call = this.settings.call || ["[data-uppload-button]"];
for (let i = 0; i < this.settings.call.length; i++) {
let $button = document.querySelectorAll(this.settings.call[i]);
for (let j = 0; j < $button.length; j++) {
$button[j].addEventListener("click", this.openModal.bind(this));
}
}
}
/**
* Shows error after selecting file
* @param {string} error - String for error text
*/
showError(error) {
dispatch("fileError", error);
const errorDiv = document.querySelector(`#uppload_${this.meta.uniqueId} .errorMessage`);
errorDiv.style.display = "block";
errorDiv.innerHTML = `<strong>${this.i18n.error}: </strong>${error}.`;
setTimeout(() => {
errorDiv.classList.add("visible");
}, 1);
setTimeout(() => {
errorDiv.classList.remove("visible");
const hideMe = () => {
errorDiv.style.display = "none";
errorDiv.removeEventListener("transitionend", hideMe);
};
errorDiv.addEventListener("transitionend", hideMe);
}, this.settings.errorDelay || 3000);
}
/**
* Binds dispatch to user's callbacks
* @param {String} upploadEvent - Name of event listener
* @param {Function} upploadFunction - Function that receives callback
*/
on(upploadEvent, upploadFunction) {
addGlobalEvent(upploadEvent, upploadFunction, this);
}
/**
* Binds dispatch to user's callbacks
* @param {String} newValue - New URL value to update to
* @param {Number} initial - 1 or 0 to say whether it's the initial URL update
*/
updateValue(newValue, initial = 0) {
const elements = this.settings.bind || ["[data-uppload-value]"];
for (let i = 0; i < elements.length; i++) {
let $element = document.querySelector(elements[i]);
if ($element) {
if ($element.tagName === "IMG") {
$element.setAttribute("src", newValue);
} else {
$element.setAttribute("value", newValue);
}
$element.classList.add(`uppload-${initial === 0 ? "updated" : "initialized"}`);
}
}
this.value = newValue;
if (initial === 0) {
setTimeout(() => {
this.closeModal();
}, this.settings.successDelay || 1000);
}
}
/**
* Opens the modal
*/
openModal() {
loadFile("https://use.fontawesome.com/releases/v5.0.12/js/all.js");
if (this.isOpen === true) return;
this.changePage(this.currentPage);
this.isOpen = true;
dispatch("modalOpened");
this.modalElement.classList.add("visible");
this.backgroundElement.classList.add("visible");
this.modalElement.classList.add("fadeIn");
this.backgroundElement.classList.add("fadeIn");
setTimeout(() => {
this.modalElement.classList.remove("fadeIn");
this.backgroundElement.classList.remove("fadeIn");
}, 399);
}
/**
* Closes the modal
*/
closeModal() {
if (this.isOpen === false) return;
this.isOpen = false;
dispatch("modalClosed");
this.modalElement.classList.add("fadeOut");
this.backgroundElement.classList.add("fadeOut");
setTimeout(() => {
this.modalElement.classList.remove("fadeOut");
this.modalElement.classList.remove("visible");
this.backgroundElement.classList.remove("fadeOut");
this.backgroundElement.classList.remove("visible");
}, 399);
}
/**
* Navigates to a new service/page
* @param {String} newPage - Name of the service to go to
*/
changePage(newPage) {
if (window.globalStream && typeof window.globalStream.getTracks === "function") window.globalStream.getTracks()[0].stop();
if (!this.services[newPage]) return;
document.querySelector(`#uppload_${this.meta.uniqueId} .currentPage`).innerHTML = this.services[newPage].html;
if (typeof this.services[newPage].init === "function") this.services[newPage].init();
dispatch("pageChanged", newPage);
const navbarChildren = document.querySelectorAll(`#uppload_${this.meta.uniqueId} .button_service`);
for (let i = 0; i < navbarChildren.length; i++) {
navbarChildren[i].classList.remove("active");
}
const currentChild = document.querySelector(`#uppload_${this.meta.uniqueId} .button_service_${newPage}`);
const navbar = document.querySelector(`#uppload_${this.meta.uniqueId} aside`);
if (currentChild) {
currentChild.classList.add("active");
}
navbar.querySelector(".button_service_preview").classList.add("active");
if (newPage === "uploading" || newPage === "uploaded") {
navbar.classList.add("hidden");
} else {
navbar.classList.remove("hidden");
}
if (arrayIncludes(["preview", "crop", "uploading", "uploaded"], newPage)) {
navbar.querySelector(".button_service_preview").style.display = "";
} else {
navbar.querySelector(".button_service_preview").style.display = "none";
}
}
}
window.Uppload = Uppload; // for CDN
export default Uppload; // for ES6/CJS