-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
360 lines (327 loc) · 11.7 KB
/
script.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
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
var videoImg = 'https://www.shareicon.net/data/128x128/2015/08/20/87931_video_512x512.png',
filelist = [],
steptsMedia = [
{
'title' : 'Paso 1: Seleccionar y Ordenar Elementos',
'visible' : ['previewGallery','btnNextStep'],
'hidden' : ['dataFile','selAutor','btnSaveMedia'],
},
{
'title' : 'Paso 2: Cargar Fecha y Descripcion',
'visible' : ['dataFile','btnNextStep'],
'hidden' : ['previewGallery','selAutor','btnSaveMedia']
},
{
'title' : 'Paso 3: Seleccionar Autor',
'visible' : ['selAutor','btnSaveMedia'],
'hidden' : ['previewGallery','dataFile','btnNextStep']
}
];
function ShowHideElements(step) {
$.each(steptsMedia[step].visible,function(i,elem) {
$(`#${this}`).removeClass('d-none');
});
$.each(steptsMedia[step].hidden,function() {
$(`#${this}:not(.d-none)`).addClass('d-none');
});
$('#currentStep').val(step);
$('#step h6').text(steptsMedia[step].title);
}
$(document).ready(function() {
$('.autor').on('click', function(){
$('.autor-invalid').removeClass('autor-invalid');
$('.autor-sel').removeClass('autor-sel').addClass('autor');
$(this).removeClass('autor').addClass('autor-sel');
$('input[name="autor"]').val($(this).attr('id'));
})
$('#btnSaveMedia').on('click', function() {
AddMedia();
})
$('#btnNextStep').on('click', function() {
StepsMedia('right');
});
$('#formMedia').on('click', '.expandItem', function() {
PreviewFile($(this).parent().parent()[0].id.split('-')[1]);
});
$('#formMedia').on('click', '.removeItem', function() {
let id = $(this).parent().parent()[0].id.split('-')[1];
filelist[id] = (function () { return; }) ();
filelist = $.grep(filelist, function(e){return e});
LoadPreviewGallery($.grep(filelist,function(e){ return e.file}).map(function(value,index){ return value['file'] }),1);
});
$('#formMedia').on('click', '#btnPrevFile', function() {
ButtonDirectionGallery('left');
});
$('#formMedia').on('click', '#btnNextFile', function() {
ButtonDirectionGallery('right');
});
$('#formMedia').on('focus','.datepicker', function() {
$(this).datepicker({
format: "dd/mm/yyyy",
language: "es",
autoclose: true,
todayHighlight: true,
clearBtn: true
});
});
$('#btnPrevStep').on('click', function() {
StepsMedia('left');
});
$("#previewGallery").on("drop", function(e) {
e.preventDefault();
e.stopPropagation();
$('#custom-fileLabel').removeClass('drop');
LoadPreviewGallery(e.originalEvent.dataTransfer.files);
});
});
function AddMedia() {
if (!ValidateStep($('#currentStep').val())) {
$(".autor").addClass("autor-invalid");
return false;
}
$('.cover').toggleClass('d-none');
setTimeout(function() {
$.when.apply(null,GetPromises()).then(function(){
$('.cover').toggleClass('d-none');
ResetForm();
})
}, 5000);
}
function allowDrop(e, itemGallery = 0) {
e.preventDefault();
e.stopPropagation();
if (!itemGallery)
$('#custom-fileLabel').addClass('drop');
else
$(itemGallery).children('img').addClass('drop');
}
function ButtonDirectionGallery(direction) {
$(".invalid").removeClass("invalid");
if (direction == 'left')
GetDataFile(PrevFile());
else if (ValidateDataFile())
GetDataFile(NextFile());
}
function ClosePreviewFile() {
$('#previewFile').toggleClass('d-none');
$('#videopreview')[0].pause();
$('#videopreview').removeAttr('src');
$('#videopreview')[0].load();
$('#previewGallery').toggleClass('d-none');
}
function drag(e,div) {
e.dataTransfer.setData("text", div.id);
}
function dragLeave(e, itemGallery = 0) {
e.preventDefault();
e.stopPropagation();
if (!itemGallery)
$('#custom-fileLabel').removeClass('drop');
else
$(itemGallery).children('img').removeClass('drop');
}
function drop(e,div) {
e.preventDefault();
e.stopPropagation();
var data = e.dataTransfer.getData("text");
$(div).children('img').removeClass('drop');
MoveItemGallery(data,div.id);
}
function GetDataFile(index) {
if (filelist[index].type == 'image') {
$('.card img').attr('src', filelist[index].tmpfile);
$('.card img').show();
$('.card video').hide();
} else {
$('.card video').attr('src', filelist[index].tmpfile);
$('.card video').show();
$('.card img').hide();
}
$('.card input[name="fecha"]').val(filelist[index].fecha);
$('.card input[name="titulo"]').val(filelist[index].titulo);
$('#currentFile').val(index);
if (index == 0)
$('#btnPrevFile').attr('disabled','true');
else
$('#btnPrevFile').removeAttr('disabled');
if (index == filelist.length - 1)
$('#btnNextFile').attr('disabled','true');
else
$('#btnNextFile').removeAttr('disabled');
$('#current_file').text(`${(index + 1)} / ${filelist.length}`);
}
function GetIndexIncompleteFile() {
let indexEmpty = -1;
$.each(filelist, function(i, elem) {
if (elem.fecha == '' || elem.titulo == '') {
indexEmpty = i;
return false;
}
});
return indexEmpty;
}
function GetPromises() {
var promises = [];
$.each(filelist, function(i, elem) {
//Fill with ajax call to post data to server
promises.push(
console.log(`Post image ${(i + 1)}`)
);
});
return promises;
}
function LoadFiles(input) {
LoadPreviewGallery(input.files);
}
function LoadPreviewGallery(files, refresh = 0) {
$('.cover').toggleClass('d-none');
let gallery = '';
let iStart = !refresh ? filelist.length : 0;
$('#custom-fileLabel').removeClass('error');
if (refresh) $('.itemGallery').remove();
let current_elements = !refresh ? $.grep(filelist,function(e){ return e.file}).map(function(value,index){ return value['file']['name'] }) : '';
$.each(files, function(i, elem) {
if (!refresh) if (jQuery.inArray(elem.name, current_elements) !== -1) return;
let type = elem.type.split("/")[0];
let tmpfile = window.webkitURL.createObjectURL(elem);
gallery += `<div class='itemGallery' id='itemGallery-${iStart + i}'
ondrop='drop(event,this)' ondragover='allowDrop(event,this)' ondragleave='dragLeave(event,this)' draggable='true' ondragstart='drag(event,this)'>
<img src='${(type != 'image' ? videoImg : tmpfile)}' draggable='true'>
<input type='hidden' value='${(type != 'image' ? tmpfile : '')}'>
<div><span class='fas fa-search-plus expandItem'></span><span class='fas fa-times removeItem'></span></div></div>`;
if (!refresh) filelist[iStart + i] = {'file': elem, 'tmpfile': tmpfile, 'type': type, 'fecha': '', 'titulo': ''};
});
$('#wrapperGallery').append(gallery);
$('.cover').toggleClass('d-none');
}
function MoveItemGallery(from,to) {
let iFrom = from.split('-')[1],
iTo = to.split('-')[1],
itemAux = filelist[iFrom];
filelist[iFrom] = filelist[iTo];
filelist[iTo] = itemAux;
if (filelist[iFrom].type == 'image') {
$(`#${from}`).children('img').attr('src', filelist[iFrom].tmpfile);
$(`#${from}`).children('input').val('');
} else {
$(`#${from}`).children('img').attr('src', videoImg);
$(`#${from}`).children('input').val(filelist[iFrom].tmpfile);
}
if (filelist[iTo].type == 'image') {
$(`#${to}`).children('img').attr('src', filelist[iTo].tmpfile);
$(`#${to}`).children('input').val('');
} else {
$(`#${to}`).children('img').attr('src', videoImg);
$(`#${to}`).children('input').val(filelist[iTo].tmpfile);
}
}
function NextFile() {
let curIndex = $('#currentFile').val();
SetDataFile(curIndex);
return parseInt(curIndex) + 1;
}
function PrevFile() {
return parseInt($('#currentFile').val()) - 1;
}
function PreviewFile(index) {
if (filelist[index].type == 'image') {
$('#videopreview').hide();
$('#imagepreview').attr('src', filelist[index].tmpfile).show();
} else {
$('#imagepreview').hide();
$('#videopreview').attr('src', filelist[index].tmpfile).show();
}
$('#previewGallery').toggleClass('d-none');
$('#previewFile').toggleClass('d-none');
}
function RemoveFile(input) {
$(`#${input}`).val("");
$(`#${input}`).next('label').removeClass("file-ok").css("background-image","none");
$(`#expand${input}`).removeAttr("href");
filelist[input] = (function () { return; })();
let iid = input.split("-")[1];
if ($(`#fecha-${iid}`).val() == "")
$(`#fecha-${iid}`).removeClass("invalid");
if ($(`#titulo-${iid}`).val() == "")
$(`#titulo-${iid}`).removeClass("invalid");
}
function ResetForm() {
filelist = [];
$('.autor-sel').removeClass('autor-sel').addClass('autor');
$('input[name="autor"]').val('');
LoadPreviewGallery(filelist, 1);
ShowHideElements(0);
}
function SetDataFile(index) {
filelist[index].fecha = $('.card input[name="fecha"]').val();
filelist[index].titulo = $('.card input[name="titulo"]').val();
}
function SetDataMedia(index = -1) {
if (index == -1)
index = 0;
GetDataFile(index);
}
function StepsMedia(direction) {
let curStep = $('#currentStep').val();
let newStep = direction == 'left' ? parseInt(curStep) - 1 : parseInt(curStep) + 1;
switch (curStep) {
case '0':
if (!ValidateStep(curStep)) {
$('#custom-fileLabel').addClass('error');
return false;
}
ShowHideElements(newStep);
SetDataMedia(GetIndexIncompleteFile());
break;
case '1':
if (direction == 'left')
ShowHideElements(newStep);
else {
$(".invalid").removeClass("invalid");
if (!ValidateDataFile()) return false;
let indexIncompleteFile = GetIndexIncompleteFile();
if (indexIncompleteFile != -1)
if (indexIncompleteFile == filelist.length - 1 && indexIncompleteFile == $('#currentFile').val()) {
SetDataFile(indexIncompleteFile);
ShowHideElements(newStep);
} else {
SetDataMedia(indexIncompleteFile);
return false;
}
ShowHideElements(newStep);
}
break;
case '2':
if (direction == 'left') {
ShowHideElements(newStep);
SetDataMedia();
}
break;
}
if (newStep == 0)
$('#btnPrevStep').attr('disabled','true');
else {
$('#btnPrevStep').removeAttr('disabled');
}
}
function ValidateDataFile() {
if ($('.card input[name="fecha"]').val() == '') $('.card input[name="fecha"]').addClass('invalid');
if ($('.card input[name="titulo"]').val() == '') $('.card input[name="titulo"]').addClass('invalid');
if ($(".invalid").length > 0) return false;
return true;
}
function ValidateStep(step) {
switch (step) {
case '0':
if ($('.itemGallery').length == 0) return false;
break;
case '1':
let indexIncompleteFile = GetIndexIncompleteFile();
if (indexIncompleteFile != -1) return indexIncompleteFile;
break;
case '2':
if ($(".autor-sel").length == 0) return false;
break;
}
return true;
}