-
Notifications
You must be signed in to change notification settings - Fork 8
/
jquery.imagesloader-1.0.1.js
1150 lines (838 loc) · 34.2 KB
/
jquery.imagesloader-1.0.1.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ========================================
Images loader v1.0.1
http://www.format.it/
Copyright (c) 2021 Format s.r.l.
Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
Requires: jquery.imagesloader.css
jquery 3.3.x
bootstrap 4.3.x
======================================== */
if (typeof jQuery === 'undefined') { throw new Error('Images loader requires jQuery') }
// #region Prototype
String.prototype.replaceAll = function (search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
// #endregion
// #region Generic function
// base64ToArrayBuffer
// Convert a base64 image to byte array
function base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++)
bytes[i] = binary_string.charCodeAt(i);
return bytes.buffer;
}
// arrayBufferToBase64
// Convert a byte array image to base64 string
function arrayBufferToBase64(buffer) {
var binary = '';
var bytes = new Uint8Array(buffer);
var len = bytes.byteLength;
for (var i = 0; i < len; i++)
binary += String.fromCharCode(bytes[i]);
return window.btoa(binary);
}
// getOrientation
// Extract image orientation from JPEG EXIF image data
// return -2: not jpeg
// -1: not defined
// 1-8: orientation see: https://stackoverflow.com/questions/7584794/accessing-jpeg-exif-rotation-data-in-javascript-on-the-client-side/32490603#32490603
function getOrientation(src) {
var view = new DataView(src);
if (view.getUint16(0, false) != 0xFFD8)
return -2;
var length = view.byteLength;
var offset = 2;
while (offset < length) {
if (view.getUint16(offset + 2, false) <= 8)
-1;
var marker = view.getUint16(offset, false);
offset += 2;
if (marker == 0xFFE1) {
if (view.getUint32(offset += 2, false) != 0x45786966)
return -1;
var little = view.getUint16(offset += 6, false) == 0x4949;
offset += view.getUint32(offset + 4, little);
var tags = view.getUint16(offset, little);
offset += 2;
for (var i = 0; i < tags; i++)
if (view.getUint16(offset + (i * 12), little) == 0x0112)
return view.getUint16(offset + (i * 12) + 8, little);
}
else if ((marker & 0xFF00) != 0xFF00) {
break;
}
else
offset += view.getUint16(offset, false);
}
return -1;
}
// imageToFormat
// Convert image object to base64 string
function imageToFormat(img, maxWidth = null, maxHeight = null, imgType = 'image/jpeg', quality = .9, orientation = -1) {
var canvas = document.createElement("canvas");
// Calc witdh & heigth
var width = img.width;
var height = img.height;
if (img.width > maxWidth)
width = maxWidth;
if (img.height > maxHeight)
height = maxWidth;
// Apply width & height
canvas.width = img.width;
canvas.height = img.height;
// Set proper canvas dimensions before transform & export
if (orientation >= 5 && orientation <= 8) {
canvas.width = img.height;
canvas.height = img.width;
}
// Calc scale
var scaleX = width / img.width;
var scaleY = height / img.height;
var scale = 1;
if (scaleX < scale)
scale = scaleX;
if (scaleY < scale)
scale = scaleY;
// Apply scale
canvas.width *= scale;
canvas.height *= scale;
var ctx = canvas.getContext("2d");
ctx.scale(scale, scale);
// Transform context before drawing image
switch (orientation) {
case 1: ctx.transform(1, 0, 0, 1, 0, 0);
break;
case 2: ctx.transform(-1, 0, 0, 1, img.width, 0);
break;
case 3: ctx.transform(-1, 0, 0, -1, img.width, img.height);
break;
case 4: ctx.transform(1, 0, 0, -1, 0, img.height);
break;
case 5: ctx.transform(0, 1, 1, 0, 0, 0);
break;
case 6: ctx.transform(0, 1, -1, 0, img.height, 0);
break;
case 7: ctx.transform(0, -1, -1, 0, img.height, img.width);
break;
case 8: ctx.transform(0, -1, 1, 0, 0, img.width);
break;
default:
break;
}
ctx.drawImage(img, 0, 0);
return canvas.toDataURL(imgType, quality);
}
// imageToFormat
$.fn.imageToFormat = function () {
return this.each(function () {
this.src = imageToFormat(this);
});
}
// Rotate image
function drawRotated(img,degree) {
var canvas = document.createElement("canvas");
var cContext = canvas.getContext('2d');
var cw = img.width, ch = img.height, cx = 0, cy = 0;
// Calculate new canvas size and x/y coorditates for image
switch (degree) {
case -90:
cw = img.height;
ch = img.width;
cx = img.width * (-1);
break;
case 90:
cw = img.height;
ch = img.width;
cy = img.height * (-1);
break;
case 180:
cx = img.width * (-1);
cy = img.height * (-1);
break;
case 270:
cw = img.height;
ch = img.width;
cx = img.width * (-1);
break;
}
// Rotate image
canvas.setAttribute('width', cw);
canvas.setAttribute('height', ch);
cContext.rotate(degree * Math.PI / 180);
cContext.drawImage(img, cx, cy);
return canvas.toDataURL();
}
// #endregion
+ function ($) {
"use strict";
// Images loader PUBLIC CLASS DEFINITION
var ImagesLoader = function (name, element, options) {
this.name = name;
this.element = element;
this.$element = $(element);
this.inProgress = false;
var iconBasePath = this.path() + "img/";
this.options = $.extend({ iconBasePath: iconBasePath }, ImagesLoader.DEFAULTS, options, this.$element.data());
this.enabled = true;
this.init();
};
ImagesLoader.DEFAULTS = {
fadeTime: 'slow'
, inputID: 'files'
, maxfiles: 4
, maxSize: 5000 * 1024 // Kb
, minSelect: 1
, imagesToLoad: null
, filesType: ["image/jpeg", "image/png", "image/gif"]
, maxWidth: 1280 // pixel
, maxHeight: 1024
, imgType: "image/jpeg"
, imgQuality: .9
, errorformat: "Accepted format"
, errorsize: "Max size allowed"
, errorduplicate: "File already uploaded"
, errormaxfiles: "Max images you can upload"
, errorminfiles: "Minimum number of images to upload"
, modifyimagetext: "Modify image"
, rotation: 90
};
//#region File
// get script fullpath
ImagesLoader.prototype.fullPath = function () {
return $("script[src*='" + imagesloader_fileName + "']").attr('src');
}
// get script path
ImagesLoader.prototype.path = function () {
var fullPath = this.fullPath();
return fullPath.substr(0, fullPath.lastIndexOf("/") + 1);
}
// get script filename
ImagesLoader.prototype.fileName = function () {
var fullPath = this.fullPath();
return fullPath.substr(fullPath.lastIndexOf("/") + 1, fullPath.length);
}
//#endregion
/* Windows event */
//#region Mouse events
// Button add click
ImagesLoader.prototype.btnAddClick = function (evt, obj) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
self.$files.click();
}
ImagesLoader.prototype.btnChangeClick = function (evt, obj) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
//to make sure the user select file/files
if (!evt.target.files)
return;
//To obtaine a File reference
var files = evt.target.files;
//Obtain num of images selected
var filesLength = self.AttachmentArray.length + evt.target.files.length;
self.$progress.hide();
//To check files count according to upload conditions
if (filesLength > options.maxfiles)
self.ShowProgress(self.barStyle.warning, options.errormaxfiles + ": " + options.maxfiles, 2000);
// Find div loadind spinner
var $divLoading = self.$addImage.find('[data-type="loading"]');
$divLoading.show();
// Loop through the FileList and then to render image files as thumbnails.
(function handle(i) {
// Exit recursive function on finish
if (i >= files.length) {
self.$files.val('');
// Check if we have reach the maximum number of image
if (self.AttachmentArray.length >= options.maxfiles)
self.$addImage.hide();
$divLoading.hide();
return;
}
let file = files[i];
// Apply the validation rules for attachments upload
if (self.ApplyFileValidationRules(file) == false) {
handle(++i);
return;
}
// Fill the array of attachment
self.AttachmentArray.push({
AttachmentType: 1
, ObjectType: 1
, FileName: file.name
, FileDescription: "Attachment"
, NoteText: ''
, MimeType: file.type
, Base64: null
, Orientation: -1
, FileSizeInBytes: file.size
, File: file
});
// Instantiate a FileReader object to read its contents into memory
var fileReader = new FileReader();
// Closure to capture the file information and apply validation.
fileReader.onload = function (e) {
var main = (self.AttachmentArray.length == 1);
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(file.name);
var src = e.target.result;
var image = new Image();
image.crossOrigin = "anonymous"; // This enables CORS
image.onload = function () {
var base64 = this.src.split("base64,")[1];
var orientation = getOrientation(base64ToArrayBuffer(base64));
var src = imageToFormat(this, options.maxWidth, options.maxHeight, options.imgType, options.imgQuality, orientation);
// Render attachments thumbnails.
self.RenderThumbnail(src, file.name, main);
self.AttachmentArray[idx].Base64 = src.split("base64,")[1];
self.AttachmentArray[idx].Orientation = orientation;
handle(++i);
};
image.src = src;
};
// Read in the image file as a data URL.
// readAsDataURL: The result property will contain the file/blob's data encoded as a data URL.
// More info about Data URI scheme https://en.wikipedia.org/wiki/Data_URI_scheme
fileReader.readAsDataURL(file);
})(0);
}
// Button remove click
ImagesLoader.prototype.btnRemoveClick = function (evt, obj) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
// Find div and id image
var $div = $(obj).closest('[data-type="image"]');
// Hide Popover
var $buttonPopover = $div.find('[data-type="btn-modify"]');
$buttonPopover.popover('hide');
var id = $div.data('id');
// Update main image
if (self.AttachmentArray.length > 1 && self.AttachmentArray[0].FileName == $div.data('id')) {
var $nextDiv = $div.next('[data-type="image"]');
var $noimage = $nextDiv.find('[data-type="noimage"]');
var $preview = $nextDiv.find('[data-type="preview"]');
var $btnAdd = $nextDiv.find('[data-type="add"]');
var $btnModify = $nextDiv.find('[data-type="btn-modify"]');
var $btnRemove = $nextDiv.find('[data-type="remove"]');
var $btnMain = $nextDiv.find('[data-type="main"]');
var $ratioBox = $nextDiv.find('[data-type="image-ratio-box"]');
var $tag = $ratioBox.find('.main-tag');
$btnRemove.show();
$noimage.hide();
$btnAdd.hide();
$preview.fadeIn(options.fadeTime);
}
// Remove the image from array
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(id);
if (idx !== -1)
self.AttachmentArray.splice(idx, 1);
self.$addImage.fadeOut(options.fadeTime);
$div.fadeOut(options.fadeTime, function () {
$div.remove();
var $divImage = self.$addImage.closest('[data-type="image"]');
$divImage.attr('data-type', 'noimage');
self.$addImage.fadeIn(options.fadeTime);
});
self.$progress.fadeOut(options.fadeTime);
};
// Button main click
ImagesLoader.prototype.btnMainClick = function (evt, obj) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
// Find div and image of the image
var $div = $(obj).closest('[data-type="image"]');
var $preview = $div.find('[data-type="preview"]');
var $ratioBox = $div.find('[data-type="image-ratio-box"]');
var $tooltip = $ratioBox.attr('data-original-title');
var id = $div.attr('data-id');
var imgSource = $preview.attr('src');
// Hide Popover
var $buttonPopover = $div.find('[data-type="btn-modify"]');
$buttonPopover.popover('hide');
// Find div and image of the main image
var main = self.AttachmentArray[0];
var $divMain = $element.find('[data-id="' + main.FileName + '"]');
var $previewMain = $divMain.find('[data-type="preview"]');
var $ratioBoxMain = $divMain.find('[data-type="image-ratio-box"]');
var $tag = $ratioBoxMain.find('.main-tag');
var $tooltipMain = $ratioBoxMain.attr('data-original-title');
var imgSourceMain = $previewMain.attr('src');
// Change image preview
$tag.hide();
$preview.fadeOut(options.fadeTime, function () {
$preview.attr('src', imgSourceMain);
});
$previewMain.fadeOut(options.fadeTime, function () {
$previewMain.attr('src', imgSource);
$tag.show();
});
$preview.fadeIn(options.fadeTime);
$previewMain.fadeIn(options.fadeTime);
// Change div data id
$div.attr('data-id', main.FileName);
$divMain.attr('data-id', id);
//Change tooltip
$ratioBox.attr('data-original-title', $tooltipMain);
$ratioBoxMain.attr('data-original-title', $tooltip);
// Find array id of the image and change position of the main image
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(id);
if (idx !== -1) {
self.AttachmentArray[0] = self.AttachmentArray[idx];
self.AttachmentArray[idx] = main;
}
};
// Button left click
ImagesLoader.prototype.btnLeftClick = function (evt, obj) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
// Find div and image of the image
var $div = $(obj).closest('[data-type="image"]');
var $preview = $div.find('[data-type="preview"]');
var $ratioBox = $div.find('[data-type="image-ratio-box"]');
var $tooltip = $ratioBox.attr('data-original-title');
var id = $div.attr('data-id');
// Hide Popover
var $buttonPopover = $div.find('[data-type="btn-modify"]');
$buttonPopover.popover('hide');
// Find array id of the image and left image
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(id);
var leftImageIdx = idx - 1;
var leftImage = self.AttachmentArray[leftImageIdx];
//Find div and image of the left image
var $divLeft = $element.find('[data-id="' + leftImage.FileName + '"]');
var $previewLeft = $divLeft.find('[data-type="preview"]');
var $ratioBoxLeft = $divLeft.find('[data-type="image-ratio-box"]');
var $tooltipLeft = $ratioBoxLeft.attr('data-original-title');
var imgSource = "data:image/jpeg;base64," + self.AttachmentArray[idx].Base64;
var imgSourceLeft = "data:image/jpeg;base64," + leftImage.Base64;
var $tag = $ratioBoxLeft.find('.main-tag');
// Change image preview
$tag.hide();
$preview.fadeOut(options.fadeTime, function () {
$preview.attr('src', imgSourceLeft);
});
$previewLeft.fadeOut(options.fadeTime, function () {
$previewLeft.attr('src', imgSource);
if (leftImageIdx == 0)
$tag.show();
});
$preview.fadeIn(options.fadeTime);
$previewLeft.fadeIn(options.fadeTime);
//Change tooltip
$ratioBox.attr('data-original-title', $tooltipLeft);
$ratioBoxLeft.attr('data-original-title', $tooltip);
//Change div data
$div.attr('data-id', leftImage.FileName);
$divLeft.attr('data-id', id);
if (idx !== -1) {
self.AttachmentArray[leftImageIdx] = self.AttachmentArray[idx];
self.AttachmentArray[idx] = leftImage;
}
};
// Button right click
ImagesLoader.prototype.btnRightClick = function (evt, obj) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
// Find div and image of the image
var $div = $(obj).closest('[data-type="image"]');
var $preview = $div.find('[data-type="preview"]');
var $ratioBox = $div.find('[data-type="image-ratio-box"]');
var $tooltip = $ratioBox.attr('data-original-title');
var id = $div.attr('data-id');
// Hide Popover
var $buttonPopover = $div.find('[data-type="btn-modify"]');
$buttonPopover.popover('hide');
// Find array id of the image and right image
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(id);
var rightImageIdx = idx + 1;
var rightImage = self.AttachmentArray[rightImageIdx];
//Find div and image of the left image
var $divRight = $element.find('[data-id="' + rightImage.FileName + '"]');
var $previewRight = $divRight.find('[data-type="preview"]');
var $ratioBoxRight = $divRight.find('[data-type="image-ratio-box"]');
var $tooltipRight = $ratioBoxRight.attr('data-original-title');
var imgSource = "data:image/jpeg;base64," + self.AttachmentArray[idx].Base64;
var imgSourceRight = "data:image/jpeg;base64," + rightImage.Base64;
var $tag = $ratioBox.find('.main-tag');
// Change image preview
$tag.hide();
$preview.fadeOut(options.fadeTime, function () {
$preview.attr('src', imgSourceRight);
});
$previewRight.fadeOut(options.fadeTime, function () {
$previewRight.attr('src', imgSource);
//$tag.show();
});
$preview.fadeIn(options.fadeTime);
$previewRight.fadeIn(options.fadeTime);
//Change tooltip
$ratioBox.attr('data-original-title', $tooltipRight);
$ratioBoxRight.attr('data-original-title', $tooltip);
//Change div data
$div.attr('data-id', rightImage.FileName);
$divRight.attr('data-id', id);
if (idx !== -1) {
self.AttachmentArray[rightImageIdx] = self.AttachmentArray[idx];
self.AttachmentArray[idx] = rightImage;
}
};
// Button rotate click
ImagesLoader.prototype.btnRotateClick = function (evt, obj) {
var self = this;
var options = self.options;
var element = self.element;
var $element = $(element);
// Find div and id of the image
var $div = $(obj).closest('[data-type="image"]');
var $preview = $div.find('[data-type="preview"]');
var id = $div.attr('data-id');
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(id);
// Create new image and pass to drawRotated function to rotate image source
var image = new Image();
image.src = "data:image/jpeg;base64," + self.AttachmentArray[idx].Base64;
image.onload = function () {
var src = drawRotated(this, options.rotation);
var base64 = src.split("base64,")[1];
//Change preview with new image rotated
$preview.attr('src', src);
// Save the new image
self.AttachmentArray[idx].Base64 = base64;
}
};
//#endregion
/* FUNCTIONS */
// Show progress bar
ImagesLoader.prototype.ShowProgress = function (style, msg, delay = 0) {
var self = this;
var options = self.options;
// Already visible with the same text
if (self.$progress.is(':visible') && self.$progressbar.text() === msg)
return;
self.$progressbar.removeClass();
self.$progressbar.addClass(style.class);
self.$progressbar.attr('style', style.style);
self.$progressbar.text(msg);
self.$progress.hide().fadeIn(options.fadeTime);
if (delay > 0)
self.$progress.delay(delay).fadeOut(options.fadeTime);
}
// Execute javascript function
ImagesLoader.prototype.execJsFunction = function () {
var self = this;
var options = self.options;
if (this.jsFunction != null)
eval(this.jsFunction);
}
// #region Image upload
// Apply the validation rules for attachments upload
ImagesLoader.prototype.ApplyFileValidationRules = function (readerEvt) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
// To check if the user don't upload same images
if (self.CheckFileUnique(readerEvt.name) == false) {
self.ShowProgress(self.barStyle.danger, options.errorduplicate + " (" + readerEvt.name + ")");
return false;
}
// To check file type according to upload conditions
if (self.CheckFileType(readerEvt.type) == false) {
self.ShowProgress(self.barStyle.danger, options.errorformat + ": " + options.filesType.toString().replaceAll(',', ', ') + " (" + readerEvt.name + ")");
return false;
}
// To check file size according to upload conditions
if (self.CheckFileSize(readerEvt.size) == false) {
self.ShowProgress(self.barStyle.danger, options.errorsize + " " + options.maxSize / 1000 + " Kb (" + readerEvt.name + ")");
return false;
}
// To check files count according to upload conditions
if (self.CheckFilesCount() == false)
return false;
return true;
}
// Check file type according to upload conditions
ImagesLoader.prototype.CheckFileType = function (fileType) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
return options.filesType.indexOf(fileType) != -1
}
//To check file Size according to upload conditions
ImagesLoader.prototype.CheckFileSize = function (fileSize) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
return (fileSize < options.maxSize);
}
//To check files count according to upload conditions
ImagesLoader.prototype.CheckFilesCount = function () {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
//To check the length does not exceed files maximum
return (self.AttachmentArray.length < options.maxfiles);
}
//To check files if the image is not alredy uploaded
ImagesLoader.prototype.CheckFileUnique = function (filename) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
for (var i = 0; i < self.AttachmentArray.length; i++)
if (self.AttachmentArray[i].FileName == filename)
return false;
return true;
}
// Check validity before submit
ImagesLoader.prototype.CheckValidity = function () {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
var retVal = true;
if (self.AttachmentArray.length < options.minSelect) {
self.ShowProgress(self.barStyle.danger, options.errorminfiles +": " + options.minSelect);
retVal = false;
}
return retVal;
}
// Load images
ImagesLoader.prototype.LoadImages = function () {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
(function handle(i) {
if (i >= self.imagesToLoad.length)
return;
var url = self.imagesToLoad[i].Url;
var name = url.substring(url.lastIndexOf('/') + 1);
var main = (i == 0);
var name = escape(url.substring(url.lastIndexOf('/') + 1));
var image = new Image();
image.crossOrigin = "anonymous"; // This enables CORS
image.onload = function () {
var src = imageToFormat(this, options.maxWidth, options.maxHeight, options.imgType, options.imgQuality);
var base64 = src.split("base64,")[1];
self.AttachmentArray.push({
AttachmentType: 1
, ObjectType: 1
, FileName: name
, FileDescription: "Attachment"
, NoteText: ''
, MimeType: options.imgType
, Base64: base64
, FileSizeInBytes: base64.length
, File: null
});
self.RenderThumbnail(url, name, main, true);
handle(++i);
};
image.src = url;
})(0);
}
//Render attachments thumbnails.
ImagesLoader.prototype.RenderThumbnail = function (src, name, main = false, reload = false) {
var self = this;
var element = self.element;
var $element = $(element);
var options = self.options;
var $div = self.$model.clone(true, true);
$div.attr('data-type', 'image');
$div.attr('data-id', name);
var $noimage = $div.find('[data-type="noimage"]');
var $preview = $div.find('[data-type="preview"]');
$preview.attr('src', src);
var $btnAdd = $div.find('[data-type="add"]');
let $btnModify = $div.find('[data-type="btn-modify"]');
var $btnRemove = $div.find('[data-type="remove"]');
var $btnMain = $div.find('[data-type="main"]');
var $btnLeft = $div.find('[data-type="left"]');
var $btnRight = $div.find('[data-type="right"]');
var $ratioBox = $div.find('[data-type="image-ratio-box"]');
$ratioBox.attr('data-toggle', 'tooltip');
$ratioBox.attr('data-placement', 'top');
if(reload)
$ratioBox.attr('title', src.substring(src.indexOf('_') + 1));
else
$ratioBox.attr('title', decodeURI(name));
//if is main image
if (main) {
var $tag = $ratioBox.find('.main-tag');
$tag.show();
}
$btnModify.fadeIn(options.fadeTime);
//button modify popover
$btnModify.popover({
html: true
,trigger: "manual"
, title: function () {
return "<p class='m-0 p-0' style='text-align:center'>" + options.modifyimagetext + "</p>";
}
, content: function () {
var popoverContent = $element.find('[data-type="popover"]').first().clone(true, true);
var id = $btnModify.closest('[data-type="image"]').attr('data-id');
var idx = self.AttachmentArray.map(function (file) { return file.FileName; }).indexOf(id);
// If is the main image hide left button and main button
if (idx == 0) {
$(popoverContent).find('[data-operation="left"]').addClass('disabled');
$(popoverContent).find('[data-operation="left"]').css('cursor', 'default');
$(popoverContent).find('[data-operation="left"]').removeAttr('data-operation');
$(popoverContent).find('[data-operation="main"]').addClass('disabled');
$(popoverContent).find('[data-operation="main"]').css('cursor', 'default');
$(popoverContent).find('[data-operation="main"]').removeAttr('data-operation');
}
// Hide right button if there is no other image in the right
if (idx == self.AttachmentArray.length - 1) {
$(popoverContent).find('[data-operation="right"]').addClass('disabled');
$(popoverContent).find('[data-operation="right"]').css('cursor', 'default');
$(popoverContent).find('[data-operation="right"]').removeAttr('data-operation');
}
$(popoverContent).find('[data-operation]').click(function () {
$btnModify.parent().trigger($(this).data('operation'));
});
return popoverContent;
}
}).on("click", function () {
$element.find('[data-toggle=popover]').not(this).popover('hide');
var self = this;
var $self = $(self);
$self.popover("show");
$(".popover").on("mouseleave", function () {
$self.popover('hide');
});
});
$ratioBox.tooltip();
$noimage.hide();
$btnAdd.hide();
$div.insertBefore(self.$addImage);
$div.show();
$preview.fadeIn(options.fadeTime);
return $div;
}
//#endregion
//#region Ajax calls