-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupload.html
382 lines (369 loc) · 20.1 KB
/
upload.html
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
{{ template "head.html" . }}
<link rel="stylesheet" href="/libs/tom-select/dist/css/tom-select.min.css">
<div class="page">
{{ template "sidebar.html" . }}
<div class="page-wrapper">
{{template "header.html" .}}
<div class="page-body">
<div class="container-xl">
<div class="row row-cards">
<div class="col-12">
<div class="card">
<div class="card-body">
<h3 class="card-title">Upload Schematic</h3>
{{ if .IsAuthenticated }}
<div class="row">
<div class="col-md-12 col-lg-6">
<div class="mb-3">
<label class="form-label required">Title</label>
<input type="text" id="schematic-title" class="form-control" name="title"
placeholder="Schematic Title">
</div>
<div class="mb-3">
<label class="form-label">Description</label>
<textarea id="tinymce-schematic"
placeholder="Describe your schematic and include any details on how to get it running."></textarea>
</div>
<div class="row">
<div class="mb-3 col-md-12 col-lg-6">
<label class="form-label required">Category</label>
<select name="category" id="schematic-category" class="form-select">
{{ range .Categories}}
<option value="{{ .ID }}">{{ .Name }}</option>
{{ end }}
</select>
</div>
<div class="mb-3 col-md-12 col-lg-6">
<label class="form-label">Tags</label>
<select type="text" class="form-select" placeholder="Select tags" id="select-tags" value="" multiple>
{{ range .Tags}}
<option value="{{ .ID }}">{{ .Name }}</option>
{{ end }}
</select>
</div>
</div>
<div class="row">
<div class="mb-3 col-md-12 col-lg-6">
<label class="form-label required">Create Version</label>
<select id="create-version" name="" class="form-select">
{{ range .CreatemodVersions}}
<option value="{{ .ID }}">{{ .Version }}</option>
{{ end }}
</select>
</div>
<div class="mb-3 col-md-12 col-lg-6">
<label class="form-label required">Minecraft Version</label>
<select id="minecraft-version" name="" class="form-select">
{{ range .MinecraftVersions}}
<option value="{{ .ID }}">{{ .Version }}</option>
{{ end }}
</select>
</div>
</div>
<div class="mb-3">
<label class="form-label">Schematic Video</label>
<input id="video" type="text" class="form-control" name="video"
placeholder="Schematic Video URL">
</div>
</div>
<div class="col-md-12 col-lg-6">
<div class="mb-3">
<form class="dropzone" id="dropzone-schematic" action="/upload"
autocomplete="off" novalidate>
<div class="fallback">
<input name="schematic-file" type="file"/>
</div>
<div class="dz-message">
<h3 class="dropzone-msg-title">Schematic File</h3>
<span class="dropzone-msg-desc">Click here or drag the .nbt schematic file</span>
</div>
</form>
</div>
<div class="mb-3">
<form class="dropzone" id="dropzone-featured" action="/upload"
autocomplete="off" novalidate>
<div class="fallback">
<input name="featured-file" type="file"/>
</div>
<div class="dz-message">
<h3 class="dropzone-msg-title">Featured Image</h3>
<span class="dropzone-msg-desc">Click here or drag a screenshot of your build in Minecraft</span>
</div>
</form>
</div>
<div class="mb-3">
<form class="dropzone" id="dropzone-gallery" action="/upload"
autocomplete="off" novalidate>
<div class="fallback">
<input name="gallery-file" type="file"/>
</div>
<div class="dz-message">
<h3 class="dropzone-msg-title">Gallery Images</h3>
<span class="dropzone-msg-desc">Click here or drag images to showcase your schematic</span>
</div>
</form>
</div>
</div>
</div>
<div class="row">
<div class="mt-5">
<button class="btn btn-primary w-100" id="upload-schematic-btn">
Upload
</button>
<div id="progress-upload" style="display: none;" class="progress">
<div class="progress-bar bg-green"></div>
</div>
<div id="upload-success" class="alert alert-important alert-success" style="display: none;" role="alert">
<div class="d-flex">
<div>
<svg xmlns="http://www.w3.org/2000/svg" class="icon alert-icon" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M5 12l5 5l10 -10" /></svg>
</div>
<div>
Schematic has been uploaded!
</div>
</div>
</div>
</div>
</div>
{{ else }}
<h4>You must be <a href="/login">logged in</a> to upload schematics.</h4>
{{ end }}
</div>
</div>
</div>
</div>
</div>
</div>
{{template "footer.html" .}}
</div>
</div>
<style>
body .ts-control {
background: #ffffff !important;
border: none !important;
}
body .ts-wrapper.multi .ts-control>div {
background: #bf9045 !important;
}
body .ts-dropdown {
background: #ffffff !important;
color: #bf9045 !important;
}
body[data-bs-theme=dark] .ts-control {
background: #1f2121 !important;
border: none !important;
}
body[data-bs-theme=dark] .ts-wrapper.multi .ts-control>div {
background: #bf9045 !important;
}
body[data-bs-theme=dark] .ts-dropdown {
background: #1f2121 !important;
color: #bf9045 !important;
}
</style>
<script type="module" src="dropzone/dist/dropzone-min.js" defer></script>
<script type="module" src="tinymce/tinymce.min.js" defer></script>
<script src="/libs/tom-select/dist/js/tom-select.base.min.js" defer></script>
<script>
// @formatter:off
document.addEventListener("DOMContentLoaded", function () {
var el;
window.TomSelect && (new TomSelect(el = document.getElementById('select-tags'), {
copyClassesToDropdown: false,
dropdownParent: 'body',
controlInput: '<input>',
render:{
item: function(data,escape) {
if( data.customProperties ){
return '<div><span class="dropdown-item-indicator">' + data.customProperties + '</span>' + escape(data.text) + '</div>';
}
return '<div>' + escape(data.text) + '</div>';
},
option: function(data,escape){
if( data.customProperties ){
return '<div><span class="dropdown-item-indicator">' + data.customProperties + '</span>' + escape(data.text) + '</div>';
}
return '<div>' + escape(data.text) + '</div>';
},
},
}));
});
// @formatter:on
</script>
<script>
// @formatter:off
document.addEventListener("DOMContentLoaded", function () {
let options = {
license_key: "gpl",
selector: '#tinymce-schematic',
height: 300,
menubar: false,
statusbar: false,
plugins: [
'advlist', 'autolink', 'lists', 'link', 'charmap', 'preview', 'anchor',
'searchreplace', 'visualblocks', 'code', 'fullscreen',
'insertdatetime', 'media', 'table', 'code', 'help', 'wordcount'
],
toolbar: 'undo redo | formatselect | ' +
'bold italic | ' +
'link | bullist numlist outdent indent | ' +
'removeformat',
content_style: 'body { font-family: -apple-system, BlinkMacSystemFont, San Francisco, Segoe UI, Roboto, Helvetica Neue, sans-serif; font-size: 14px; -webkit-font-smoothing: antialiased; }'
}
if (localStorage.getItem("createmodTheme") === 'dark') {
options.skin = 'tinymce-5-dark';
options.content_css = 'dark';
}
tinyMCE.baseURL ="/libs/tinymce";
tinyMCE.init(options);
})
// @formatter:on
</script>
<script>
// @formatter:off
document.addEventListener("DOMContentLoaded", function() {
// based on https://stackoverflow.com/a/61357880/1260548
let dropzoneSchematic = new Dropzone("#dropzone-schematic", {
url: "/files/upload",
method: "POST",
acceptedFiles: '.nbt',
thumbnailMethod: 'crop',
addRemoveLinks: true,
autoProcessQueue: false,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 1,
})
let dropzoneFeatured = new Dropzone("#dropzone-featured", {
acceptedFiles: 'image/*',
thumbnailMethod: 'crop',
addRemoveLinks: true,
autoProcessQueue: false,
uploadMultiple: false,
parallelUploads: 1,
maxFiles: 1,
})
let dropzoneGallery = new Dropzone("#dropzone-gallery", {
acceptedFiles: 'image/*',
thumbnailMethod: 'crop',
addRemoveLinks: true,
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 10,
maxFiles: 10,
})
document.getElementById('upload-schematic-btn').addEventListener("click", async function (e) {
let titleElement = document.getElementById("schematic-title")
let descriptionElement = document.getElementById("tinymce-schematic")
document.getElementById("upload-schematic-btn").disabled = true
let errors = []
const formData = new FormData();
for (let file of dropzoneSchematic.files) {
formData.append("schematic_file", file)
}
for (let file of dropzoneFeatured.files) {
formData.append("featured_image", file)
}
for (let file of dropzoneGallery.files) {
formData.append("gallery", file)
}
formData.append("title", titleElement.value)
formData.append("author", pb.authStore.model.id)
formData.append("content", tinyMCE.activeEditor.getContent())
formData.append("description", tinyMCE.activeEditor.getContent())
formData.append("categories", document.getElementById("schematic-category").value)
let tagIDs = getSelectValues(document.getElementById("select-tags"))
for (const tid of tagIDs) {
formData.append("tags", tid)
}
formData.append("createmod_version", document.getElementById("create-version").value)
formData.append("minecraft_version", document.getElementById("minecraft-version").value)
formData.append("video", document.getElementById("video").value)
if (titleElement.value === undefined || titleElement.value === "") {
titleElement.classList.add("is-invalid")
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.innerText = "A schematic must have a title"
errors.push("A schematic must have a title")
titleElement.parentNode.insertAdjacentElement("beforeend", div)
}
if (descriptionElement === undefined || tinyMCE.activeEditor === null || tinyMCE.activeEditor.getContent() === "") {
descriptionElement.classList.add("is-invalid")
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.innerText = "A schematic must have a description"
errors.push("A schematic must have a description")
descriptionElement.parentNode.insertAdjacentElement("beforeend", div)
}
if (dropzoneSchematic.files.length === 0) {
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.style.display = "block"
div.innerText = "A schematic must have a schematic file"
errors.push("A schematic must have a schematic file")
document.getElementById("dropzone-schematic").parentNode.insertAdjacentElement("beforeend", div)
}
if (dropzoneFeatured.files.length === 0) {
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.style.display = "block"
div.innerText = "A schematic must have a featured image"
errors.push("A schematic must have a featured image")
document.getElementById("dropzone-featured").parentNode.insertAdjacentElement("beforeend", div)
}
if (errors.length === 0) {
let progressBar = document.getElementById("progress-upload")
let successAlert = document.getElementById("upload-success")
try {
progressBar.style.display = "block"
progressBar.classList.add("progress-bar-indeterminate")
const createdRecord = await pb.collection('schematics').create(formData);
if (createdRecord.code !== undefined && createdRecord.code !== 200) {
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.style.display = "block"
div.innerText = "An unexpected error occurred, please try again"
document.getElementById("upload-schematic-btn").parentNode.insertAdjacentElement("beforeend", div)
document.getElementById("upload-schematic-btn").disabled = false
progressBar.classList.remove("progress-bar-indeterminate")
progressBar.display = "none"
} else {
progressBar.classList.remove("progress-bar-indeterminate")
progressBar.style.display = "none"
successAlert.style.display = "block"
}
} catch(e) {
console.log(e)
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.style.display = "block"
div.innerText = "An unexpected error occurred, please try again"
document.getElementById("upload-schematic-btn").parentNode.insertAdjacentElement("beforeend", div)
document.getElementById("upload-schematic-btn").disabled = false
progressBar.classList.remove("progress-bar-indeterminate")
progressBar.display = "none"
}
} else {
div = document.createElement('div');
div.classList.add("invalid-feedback")
div.style.display = "block"
div.innerText = "There were errors, please check above"
document.getElementById("upload-schematic-btn").parentNode.insertAdjacentElement("beforeend", div)
document.getElementById("upload-schematic-btn").disabled = false
}
});
function getSelectValues(select) {
var result = [];
var options = select && select.options;
var opt;
for (var i=0, iLen=options.length; i<iLen; i++) {
opt = options[i];
if (opt.selected) {
result.push(opt.value || opt.text);
}
}
return result;
}
})
</script>
{{template "foot.html" .}}