-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploadJS.js
More file actions
144 lines (126 loc) · 5.03 KB
/
uploadJS.js
File metadata and controls
144 lines (126 loc) · 5.03 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
export default class UploadJS {
/**
*
* @param {string} bindId
* @param {object} settings
* @param {object} data
*/
constructor ({data, settings}, {onUploadClick, onFileChange} = {}) {
this.settings = UploadJS.checkSettings(settings);
this.data = this.checkData(data);
this.uploadBtn = this.setUpButton();
this.uploadModal = this.setUpModal();
this.onUploadClick = onUploadClick;
this.onFileChange = onFileChange;
if(!this.uploadBtn) return;
document.body.appendChild(this.uploadModal);
this.uploadModalBtn = this.uploadModal.querySelector(".uploadJS-uploadBtn");
this.uploadModalInput = this.uploadModal.querySelector(".uploadJS-input")
this.uploadModalBtn.addEventListener("click", () => {
if(typeof onUploadClick === "function") {
this.onUploadClick(this.uploadModalInput);
bootstrap.Modal.getInstance(this.uploadModal).hide();
}
else {
console.warn(this.data.lang.errors.onUploadClick)
}
})
this.uploadModalInput.addEventListener("change", () => {
if(typeof onFileChange === "function") {
this.onFileChange(this.uploadModalInput);
}
})
}
setUpButton () {
let tempBtn = document.querySelector("#" + this.data.bindId);
if(!tempBtn) {
console.warn(this.data.lang.errors.noMatchingId + this.data.bindId);
alert(this.data.lang.errors.noMatchingId + this.data.bindId);
return;
}else {
tempBtn.innerHTML = this.data.text;
}
tempBtn.type = "button"
tempBtn.setAttribute("data-bs-toggle", "modal")
tempBtn.setAttribute("data-bs-target", "#" + this.data.modalId)
return tempBtn;
}
setUpModal () {
let tempModal = document.createElement("div");
tempModal.classList.add("modal", "fade")
tempModal.setAttribute("tabindex", "-1");
tempModal.setAttribute("id", this.data.modalId);
tempModal.setAttribute("aria-labelledby", this.data.modalId);
tempModal.setAttribute("aria-hidden", "true");
tempModal.setAttribute("role", "dialog");
tempModal.innerHTML = `
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">${this.data.lang.modalTitle}</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<input class="uploadJS-input form-control" type="file"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">${this.data.lang.close}</button>
<button type="button" class="btn btn-primary uploadJS-uploadBtn">${this.data.lang.upload}</button>
</div>
</div>
</div>
`
return tempModal;
}
static checkSettings (settings) {
let newSettings = {};
if(typeof settings !== "object") {
settings = {};
}
newSettings.defaultLang = UploadJS.getLanguage(settings.defaultLang) || UploadJS.getLanguage("en");
newSettings.defaultBindId = settings.defaultBindId || "uploadJS";
newSettings.defaultBtnText = settings.defaultBtnText || newSettings.defaultLang.upload;
newSettings.defaultModalId = settings.defaultModalId || "uploadModal";
return newSettings;
}
checkData (data) {
let newData = {}
if(typeof data !== "object") {
data = {};
}
newData.lang = UploadJS.getLanguage(data.lang) || this.settings.defaultLang;
newData.bindId = data.bindId || this.settings.defaultBindId;
newData.text = data.text || newData.lang.upload;
newData.modalId = data.modalId || this.settings.defaultModalId;
return newData;
}
static getLanguage (lang) {
let ger = {
upload: "Hochladen",
close: "Schließen",
modalTitle: "Wählen Sie eine Datei aus",
errors: {
noMatchingId: "Diese Id konnte nicht gefunden werden | id: ",
onUploadClick: "onUploadClick ist nicht definiert oder ist keine Funktion"
}
}
let en = {
upload: "upload",
close: "close",
modalTitle: "Choose a file",
errors: {
noMatchingId: "Can't find id | id: ",
onUploadClick: "onUploadClick is not defined or not a function"
}
}
switch (lang) {
case "ger":
return ger;
case "en":
return en;
}
return undefined;
}
}