-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
308 lines (239 loc) · 11.2 KB
/
test.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
$("#post-create-images").change(function () {
if($(this)[0].files.length <= 4){
if (typeof (FileReader) != "undefined") {
var dvPreview = $("#create-post-preview-image");
dvPreview.html("");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
//For single image only
$($(this)[0].files).each(function () {
var file = $(this);
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
var singleImg = $(`<div class="each-img-container single-pr-image"></div>`)
var img = $("<img />");
var label = $(`<button type="button" class="btn btn-cancel-img-pr"><i class="fas fa-2x fa-times-circle fa-cancel-pr"></i></button>`);
img.attr("class", "cr-post-pr-images p-0 pr-img1");
img.attr("src", e.target.result);
singleImg.append(img);
singleImg.append(label);
dvPreview.append(singleImg);
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
dvPreview.html("");
return false;
}
});
}
}
});
// --------------------------------------------------------------
$('.new-post-creation-form').on('submit', function(e){
e.preventDefault();
$form = $(this);//used for clearing the preview images
var actionUrl = $(this).attr('action');
var parent = $(this).closest('.new-post-creation-container');
$postsList = parent.siblings('.topic-own-posts-list');
var fd = new FormData();
// COPYING the 'filelist' array bcz it is immutable can't be modified
var filesArr = Array.from($("#post-create-images")[0].files);
var videoFile = Array.from($('#post-create-video')[0].files);
for(var i = 0; i<filesArr.length; i++){
var fileName = filesArr[i].name;
$('.pr-image-wrapper').each(function(){
//supportive element for deletion
var imgName = $(this).find('p').text();
if((fileName === imgName) && ($(this).hasClass('marked-for-delete'))){
filesArr.splice(i, 1);
}
});
}//don't forget upload.array('postImages', 4) helper in the route
debugger;
for(var i=0; i < videoFile.length; i++){
fd.append('postVideo', videoFile[i]);
}
for(var i = 0; i < filesArr.length; i++){
fd.append("postImages", filesArr[i]);
}
var other_data = $(this).serializeArray();
$.each(other_data,function(key,input){
fd.append(input.name,input.value);
});
debugger;
// ajax
$.ajax({
url:actionUrl,
data:fd,
contentType: false,
processData: false,
form:$form,
videoDiv:$videoDiv,
type: 'POST',
success:function(data){
},
error:function(err){
console.log('Error has occured', err);
}
});
});
// ----------------------------------------------
function prPostImages(e) {
e.preventDefault();
if($(this)[0].files.length <= 4){
//All the things needed to tbe done
}
}
$("#post-create-images").change(function (e) {
prPostImages(e);
}
$("#post-create-images").change(function (e) {
prPostImages(e);
e.preventDefault();
if($(this)[0].files.length <= 4){
if (typeof (FileReader) != "undefined") {
var dvPreview = $("#create-post-preview-image");
dvPreview.html("");
var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/;
if($(this)[0].files.length === 1){
//For single image only
$($(this)[0].files).each(function () {
var file = $(this);
//Need the original filename without lowercase for checking
var orgFileName = file[0].name;
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
var imgWrapper = $(`<div class="pr-image-wrapper d-inline p-0 m-0"></div>`);//
var singleImg = $(`<div class="each-img-container single-pr-image"></div>`)
var img = $("<img />");
var pName = $("<p class='d-none'></p>");
pName.text(orgFileName);
var label = $(`<button type="button" class="btn btn-cancel-img-pr"><i class="fas fa-2x fa-times-circle fa-cancel-pr"></i></button>`);
img.attr("class", "cr-post-pr-images p-0 pr-img1");
img.attr("src", e.target.result);
singleImg.append(img);
singleImg.append(label);
imgWrapper.append(singleImg);
imgWrapper.append(pName);
dvPreview.append(imgWrapper);
debugger;
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
dvPreview.html("");
return false;
}
});
}else if($(this)[0].files.length === 2 || $(this)[0].files.length === 4){
var imgCounter=0;
//For two images
$($(this)[0].files).each(function () {
var file = $(this);
//Need the original filename without lowercase for checking
var orgFileName = file[0].name;
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
imgCounter++;
var imgWrapper = $(`<div class="pr-image-wrapper d-inline p-0 m-0"></div>`);//
var singleImg = $(`<div class="each-img-container double-pr-image d-inline-flex px-auto"></div>`);
var img = $("<img />");
var pName = $("<p class='d-none'></p>");
pName.text(orgFileName);
var label = $(`<button type="button" class="btn btn-cancel-img-pr"><i class="fas fa-2x fa-times-circle fa-cancel-pr"></i></button>`);
img.attr(`class`, `cr-post-pr-images p-0 pr-img${imgCounter}`);
img.attr("src", e.target.result);
singleImg.append(img);
singleImg.append(label);
imgWrapper.append(singleImg);
imgWrapper.append(pName);
dvPreview.append(imgWrapper);
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
dvPreview.html("");
return false;
}
});
}else if($(this)[0].files.length === 3){
//For three images
var counter = 0, imgCounter=0;
$($(this)[0].files).each(function () {
counter++;
if( counter >= 1 && counter <= 2){
var file = $(this);
//Need the original filename without lowercase for checking
var orgFileName = file[0].name;
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
imgCounter++;//1 , 2
var imgWrapper = $(`<div class="pr-image-wrapper d-inline p-0 m-0"></div>`);//
var singleImg = $(`<div class="each-img-container triple-pr-image d-inline-flex mt-0"></div>`)
var img = $("<img />");
var pName = $("<p class='d-none'></p>");
pName.text(orgFileName);
var label = $(`<button type="button" class="btn btn-cancel-img-pr"><i class="fas fa-2x fa-times-circle fa-cancel-pr"></i></button>`);
img.attr(`class`, `cr-post-pr-images p-0 m-0 pr-img${imgCounter}`);
img.attr("src", e.target.result);
singleImg.append(img);
singleImg.append(label);
imgWrapper.append(singleImg);
imgWrapper.append(pName);
dvPreview.append(imgWrapper);
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
dvPreview.html("");
return false;
}
}else{// for the third counter adding extra class(triple-last-img)
var file = $(this);
//Need the original filename without lowercase for checking
var orgFileName = file[0].name;
if (regex.test(file[0].name.toLowerCase())) {
var reader = new FileReader();
reader.onload = function (e) {
imgCounter++;//3
var imgWrapper = $(`<div class="pr-image-wrapper d-inline p-0 m-0"></div>`);//
var singleImg = $(`<div class="each-img-container triple-pr-image d-inline-flex mt-0 triple-last-img"></div>`)
var img = $("<img />");
var pName = $("<p class='d-none'></p>");
pName.text(orgFileName);
var label = $(`<button type="button" class="btn btn-cancel-img-pr"><i class="fas fa-2x fa-times-circle fa-cancel-pr"></i></button>`);
img.attr(`class`, `cr-post-pr-images p-0 m-0 pr-img${imgCounter}`);
img.attr("src", e.target.result);
singleImg.append(img);
singleImg.append(label);
imgWrapper.append(singleImg);
imgWrapper.append(pName);
dvPreview.append(imgWrapper);
}
reader.readAsDataURL(file[0]);
} else {
alert(file[0].name + " is not a valid image file.");
dvPreview.html("");
return false;
}
}
});//each loop
}
} else {
alert("This browser does not support HTML5 FileReader.");
}
}else{
//Properly render the error in ajax
alert("Please enter less than 4 images");
}
});
// before updating
ISODate("2020-07-17T06:03:08.770Z")
// after updating
ISODate("2020-07-17T06:03:08.770Z")
// just after some time
ISODate("2020-07-17T06:08:42.761Z")