-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
983 lines (897 loc) · 38.3 KB
/
index.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
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
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.5.1/knockout-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/blueimp-file-upload/9.28.0/js/jquery.fileupload.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery-flexImages/1.0.4/jquery.flex-images.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.imagesloaded/4.1.4/imagesloaded.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>
<sciprt src="https://cdnjs.cloudflare.com/ajax/libs/loading-attribute-polyfill/1.5.4/loading-attribute-polyfill.min.js"></sciprt>
<script>
function getBlobFromUrl(url, name, callback) {
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function() {
if (xhr.status === 200) {
var headers = xhr.getAllResponseHeaders();
var result = xhr.response;
if (!result.name) result.name = name;
// console.log("response", xhr, headers, result);
callback(result);
} else {
console.log("error", xhr);
}
};
xhr.send();
}
var uploadUrl = 'upload';
var sampleKeyword = "tree";
$(function() {
var vm = {
search: ko.observable(sampleKeyword),
backend: ko.observable(),
searchError: ko.observable(false),
searchPages: ko.observable(false),
searchCount: ko.observable(false),
thumbHeight: ko.observable(150),
};
vm.searchResult = ko.observableArray([]);
vm.downloadAndUploadUrl = function(url, name) {
console.log("downloadAndUploadUrl", url, name);
getBlobFromUrl(url, name, function(blob) {
console.log("Retrieved", blob);
$('#fileupload').fileupload('add', {files: [blob]} );
});
};
vm.downloadAndUploadMe = function(imgData, event) {
vm.downloadAndUploadUrl(imgData.largeSrc(), imgData.name());
};
vm.downloadAndUploadIndex = function(index) {
var imgData = vm.searchResult()[index];
vm.downloadAndUploadMe(imgData);
};
vm.photoSwipeInstance = false;
function pushImages(items, allImages, from, to) {
for (var i = from; i < to; i++) {
items.push({
src: allImages[i].largeSrc(),
w: allImages[i].width(),
h: allImages[i].height(),
msrc: allImages[i].thumbSrc(),
title: 'Image '+allImages[i].name(),
_page: allImages[i].largeSrc(),
_index: i,
});
}
}
function openPhotoSwipe(index) {
if (vm.photoSwipeInstance) vm.photoSwipeInstance.close();
var allImages = vm.searchResult();
var count = allImages ? allImages.length : 0;
if (count) {
var items = [];
pushImages(items, allImages, 0, count);
var options = {
index: index,
getThumbBoundsFn: function(index) {
var thumbnail = document.querySelectorAll('.results .item')[index];
var pageYScroll = window.pageYOffset || document.documentElement.scrollTop;
var rect = thumbnail.getBoundingClientRect();
return {x:rect.left, y:rect.top + pageYScroll, w:rect.width};
},
loop: false,
// history: false, if we remove history support we can even remove history.js from the sources
shareButtons: [
{id:'imagepage', label:'Source page', url:'https://www.google.com'},
{id:'download', label:'Download image', url:'{{raw_image_url}}', download:true},
],
addCaptionHTMLFn: function(item, captionEl, isFake) {
// console.log("caption", item, captionEl, isFake);
captionEl.children[0].innerHTML = 'Prova <a target="_new" href="https://www.google.com">original source</a> - <a href="javascript:vm.downloadAndUploadIndex('+item._index+');vm.photoSwipeInstance.close();" style="background-color: white; padding: 5px; border-radius: 3px; text-color: black;">DOWNLOAD</a>';
return true;
},
};
vm.photoSwipeInstance = new PhotoSwipe( document.querySelectorAll('.pswp')[0] , PhotoSwipeUI_Default, items, options);
// prevent loop moves
var origNext = vm.photoSwipeInstance.next;
vm.photoSwipeInstance.next = function() {
if (this.getCurrentIndex() < count - 1) origNext();
};
var origPrev = vm.photoSwipeInstance.prev;
vm.photoSwipeInstance.prev = function() {
if (this.getCurrentIndex() > 0) origPrev();
};
// deal with automatic pagination
var subscription = vm.searchResult.subscribe(function() {
if (vm.photoSwipeInstance) {
pushImages(items, allImages, count, vm.searchResult().length);
count = vm.searchResult().length;
vm.photoSwipeInstance.invalidateCurrItems();
vm.photoSwipeInstance.updateSize(true);
}
});
vm.photoSwipeInstance.listen('afterChange', function() {
// if we are on the last or previous element then we try to preload more items.
if (this.getCurrentIndex() >= count - 2 && count < vm.searchCount()) vm.loadMore();
});
vm.photoSwipeInstance.listen('close', function() {
// scroll into view: this method have full knowledge of the html&CSS for the gallery
var thumbnail = document.querySelectorAll('.results .item')[this.getCurrentIndex()];
var scrollable = document.querySelectorAll('.resultsContainer')[0];
var rect = thumbnail.getBoundingClientRect();
var scrollableRect = scrollable.getBoundingClientRect();
if (rect.top - scrollableRect.top < 0) scrollable.scrollTop = scrollable.scrollTop + (rect.top - scrollableRect.top) - 20;
else if (scrollableRect.bottom - rect.bottom < 0) scrollable.scrollTop = scrollable.scrollTop - (scrollableRect.bottom - rect.bottom) + 20;
vm.photoSwipeInstance = false;
subscription.dispose();
// TODO scroll into view of the selected item?
});
vm.photoSwipeInstance.listen('imageLoadComplete', function(index, item) {
var img = item.container.children[0];
console.log("imgeLoadComplete", this._imagesToAppendPool, index, item.container.outerHTML, item.w, item.h, img.naturalWidth, img.naturalHeight);
/*
if (img.naturalWidth) {
item.w = img.naturalWidth;
item.h = img.naturalHeight;
vm.photoSwipeInstance.invalidateCurrItems();
vm.photoSwipeInstance.updateSize(true);
}
*/
});
vm.photoSwipeInstance.init();
}
}
var errorSvg = function(w, h, text) {
var svgCode = ('<svg xmlns="http://www.w3.org/2000/svg" width="__WIDTH__" height="__HEIGHT__">'+
'<defs><pattern id="pinstripe" patternUnits="userSpaceOnUse" width="56.568" height="56.568" patternTransform="rotate(135)">'+
'<line x1="14.142" y1="0" x2="14.142" y2="56.568" stroke="#808080" stroke-width="28.284" /></pattern></defs>'+
'<rect width="100%" height="100%" fill="#707070"/><rect width="100%" height="100%" fill="url(#pinstripe)" />'+
'<text x="50%" y="50%" font-size="20" text-anchor="middle" alignment-baseline="middle" font-family="monospace, sans-serif" fill="#B0B0B0">__TEXT__</text>'+
'</svg>').replace('__WIDTH__', w).replace('__HEIGHT__', h).replace('__TEXT__', text);
return 'data:image/svg+xml;base64,'+window.btoa(svgCode);
};
var scheduledLayout = false;
vm.imageLoadHandler = function(imgData, event) {
if (event.type == 'error' || !(event.target.naturalWidth > 0)) {
console.log("TODO", imgData, event);
var text = "";
if (imgData.width() == 0) {
// We couldn't find the thumbnail and we didn't know the original size, let's pretend it's a square
imgData.width(100);
imgData.height(100);
} else {
text = imgData.width()+"x"+imgData.height();
}
/*
var src = errorSvg(Math.round(imgData.width()/imgData.height()*vm.thumbHeight()), vm.thumbHeight(), text);
imgData.thumbSrc(src);
imgData.largeSrc(src);
event.target.src = src;
*/
imgData.thumbSrc('');
}
if (imgData.width() > 0 && imgData.height() > 0) {
var armismatch = Math.abs((imgData.width() / imgData.height()) / (event.target.naturalWidth / event.target.naturalHeight));
if (armismatch > 1.01) console.log("A/R diff", armismatch);
// we could check if aspect ratio matches the thumbnail A/R.
} else {
// console.log(imgData.width, imgData.height, event.target.naturalWidth, event.target.naturalHeight);
imgData.width(event.target.naturalWidth);
imgData.height(event.target.naturalHeight);
if (!scheduledLayout) scheduledLayout = setTimeout(doLayout, 100);
}
};
vm.zoomMe = function(imgData, event) {
openPhotoSwipe(vm.searchResult.indexOf(imgData));
};
vm.layout = ko.observable('flex');
var doLayout = function() {
scheduledLayout = false;
$('.flex-images').flexImages({rowHeight: vm.thumbHeight() });
}
function updateResult(res, append /* images, append, pages, count, error */) {
vm.searchError(res.error !== undefined ? res.error : false);
vm.searchPages(res.pages !== undefined ? res.pages : false);
vm.searchCount(res.count !== undefined ? res.count : false);
var resImages = [];
// Make every img property an observable property (we probably better create an observable size object)
// Or maybe we should start a logical sorted preload here and add items only when we have their sizes
if (res.images !== undefined && res.images.length) {
for (var i = 0; i < res.images.length; i++) {
var newImg = {};
var img = res.images[i];
for (var prop in img) {
newImg[prop] = ko.observable(img[prop]);
}
// protect from images with no thumbnail source and no sizes
if (newImg.thumbSrc() == undefined) newImg.thumbSrc('');
if (newImg.thumbSrc() == '' && newImg.width() == 0) newImg.width(100);
if (newImg.thumbSrc() == '' && newImg.height() == 0) newImg.height(100);
resImages.push(newImg);
}
}
if (append) {
if (resImages.length) {
var underlyingArray = vm.searchResult();
vm.searchResult.valueWillMutate();
ko.utils.arrayPushAll(underlyingArray, resImages);
vm.searchResult.valueHasMutated();
vm.pagesLoaded(vm.pagesLoaded()+1);
}
} else {
vm.searchResult(resImages);
vm.pagesLoaded(1);
}
doLayout();
vm.loading(false);
}
ko.computed(doLayout);
var backends = {
unsplash: {
name: "Unsplash",
_quota: "50 requests / hour development, 5000 requests / hour production",
_maxPageSize: 30,
apikey: "SERVEROVERRIDDEN",
baseHost: "/apibridge/unsplash/", // "https://api.unsplash.com/photos/search/",
requestData: function(obj) {
return {
path: backends.unsplash.baseHost,
params: { query: obj.keyword, page: obj.page, per_page: obj.per_page, client_id: backends.unsplash.apikey }
};
},
resultConversion: function(obj, req) {
var images = [];
var results = obj.body.results || obj.body;
for (var i = 0; i < results.length; i++) {
images.push({
thumbSrc: results[i].urls.thumb.replace(/&w=200&/,'&h=140&'),
originalSrc: results[i].urls.raw,
largeSrc: results[i].urls.regular,
width: results[i].width,
height: results[i].height,
// I want a name for uploading.
name: 'unsplash-'+results[i].id+'.jpg',
})
}
return {
images: images,
pages: Math.ceil(obj.headers['x-total'] / obj.headers['x-per-page']),
count: obj.headers['x-total']
};
}
},
pixabay: {
name: "Pixabay",
_quota: "5000 requests / hour",
_maxPageSize: 200,
apikey: "SERVEROVERRIDDEN",
baseHost: "/apibridge/pixabay/", // "https://pixabay.com/api/",
requestData: function(req) {
return {
path: backends.pixabay.baseHost,
params: { search_term: req.keyword, page: req.page, per_page: req.per_page, key: backends.pixabay.apikey },
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.hits.length; i++) {
// w150: obj.body.hits[i].previewURL,
// h180: obj.body.hits[i].webformatURL.replace(/_640\./,'_180.')
// h340: obj.body.hits[i].webformatURL.replace(/_640\./,'_340.'),
// w960: obj.body.hits[i].webformatURL.replace(/_640\./,'_960.'),
// w1280: obj.body.hits[i].largeImageURL
// w1920: obj.body.hits[i].fullHDURL (only appoved API)
// original: obj.body.hits[i].imageURL (only approved API)
images.push({
thumbSrc: obj.body.hits[i].webformatURL.replace(/_640\./,'_180.'),
originalSrc: obj.body.hits[i].imageURL,
largeSrc: obj.body.hits[i].largeImageURL,
width: obj.body.hits[i].imageWidth,
height: obj.body.hits[i].imageHeight,
// I want a name for uploading.
name: 'pixabay-'+obj.body.hits[i].id+'.jpg',
})
}
// TODO impleemnt "per_page"
return {
images: images,
pages: Math.ceil(obj.body.totalHits / req.per_page),
count: obj.body.totalHits
};
}
},
pexels: {
name: "Pexels",
_quota: "200 requests / hour , 20.000 requests / month",
_maxPageSize: 80,
apikey: "SERVEROVERRIDDEN",
baseHost: "/apibridge/pexels/", // "http://api.pexels.com/v1/search",
// TODO implement Authorization headers
requestData: function(req) {
return {
path: backends.pexels.baseHost,
params: { query: req.keyword, page: req.page, per_page: req.per_page },
headers: backends.pexels.apikey ? [ { name: 'Authorization', value: backends.pexels.apiKey }] : undefined
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.photos.length; i++) {
// large max 940 x max 650
// large2x max 1880 x max 1300
// medium x 350
// small x 130
// portrait crop 800 x 1200
// landscape crop 1200 x 627
// tiny crop 280 x 200
images.push({
thumbSrc: obj.body.photos[i].src.small, // h130
originalSrc: obj.body.photos[i].src.original,
largeSrc: obj.body.photos[i].src.large,
width: obj.body.photos[i].width,
height: obj.body.photos[i].height,
// I want a name for uploading.
name: 'pexels-'+obj.body.photos[i].id+'.jpg',
})
}
return {
images: images,
pages: Math.ceil(obj.body.total_results / obj.body.per_page),
count: obj.body.total_results
};
}
},
flickr: {
name: "Flickr",
_quota: "unknown",
_defaultPageSize: 100,
_minPageSize: 100,
_maxPageSize: 500,
apikey: "SERVEROVERRIDDEN",
baseHost: "/apibridge/flickr/", // "https://api.flickr.com/services/rest/",
// TODO implement Authorization headers
requestData: function(req) {
return {
path: backends.flickr.baseHost,
params: {
method: "flickr.photos.search",
api_key: backends.flickr.apikey,
text: req.keyword,
page: req.page,
perpage: req.per_page,
license: 9,
format: "json",
nojsoncallback: 1,
extras: "owner_name,o_dims,url_l", // TODO remove unused urls (o_dims,url_sq,url_t,url_q,url_m,url_n,url_z,url_c,url_o,url_s,url_l,)
},
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.photos.photo.length; i++) {
// s crop 75x75
// [q crop 150x150
// t max 100
// m max 240
// [n max 320
// [- max 500
// z max 640
// [c max 800
// b max 1024
// [h max 1600
// [k max 2048
// [o original
if (obj.body.photos.photo[i].width_l == undefined && obj.body.photos.photo[i].o_width == undefined) {
console.log(obj.body.photos.photo[i]);
}
images.push({
thumbSrc: "https://farm"+obj.body.photos.photo[i].farm+".staticflickr.com/"+obj.body.photos.photo[i].server+"/"+obj.body.photos.photo[i].id+"_"+obj.body.photos.photo[i].secret+"_m.jpg", // obj.body.photos.photo[i].url_s, // max 240
originalSrc: "https://farm"+obj.body.photos.photo[i].farm+".staticflickr.com/"+obj.body.photos.photo[i].server+"/"+obj.body.photos.photo[i].id+"_"+obj.body.photos.photo[i].secret+"_o.jpg", // obj.body.photos.photo[i].url_o, // this one is only available when using the extras=url_o (but can be generated antway)
largeSrc: "https://farm"+obj.body.photos.photo[i].farm+".staticflickr.com/"+obj.body.photos.photo[i].server+"/"+obj.body.photos.photo[i].id+"_"+obj.body.photos.photo[i].secret+"_b.jpg", // obj.body.photos.photo[i].url_b,
width: obj.body.photos.photo[i].width_l || obj.body.photos.photo[i].o_width,
height: obj.body.photos.photo[i].height_l || obj.body.photos.photo[i].o_height,
// I want a name for uploading.
name: 'flickr-'+obj.body.photos.photo[i].id+'.jpg',
})
}
return {
images: images,
pages: Math.ceil(obj.body.photos.total / obj.body.photos.perpage),
count: obj.body.photos.total
};
}
},
// doesn't work because of CORS, so must be proxied
stocksnap: {
name: "StockSnap",
_quota: "TODO",
// _maxPageSize: 80,
baseHost: "/apibridge/stocksnap/", // "https://stocksnap.io/api/search-photos/",
// TODO implement Authorization headers
requestData: function(req) {
return {
path: backends.stocksnap.baseHost+encodeURIComponent(req.keyword)+"/downloads/desc/1",
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.results.length; i++) {
images.push({
thumbSrc: "https://cdn.stocksnap.io/img-thumbs/280h/"+obj.body.results[i].img_id+".jpg",
// originalSrc: obj.body.results[i].original,
largeSrc: "https://cdn.stocksnap.io/img-thumbs/960w/"+obj.body.results[i].img_id+".jpg",
width: obj.body.results[i].img_width,
height: obj.body.results[i].img_height,
// I want a name for uploading.
name: 'stocksnap-'+obj.body.results[i].img_id+'.jpg',
})
}
return {
images: images,
pages: Math.ceil(obj.body.count / req.per_page),
count: obj.body.count
};
}
},
// url is HTTP, so it would be better to have it proxied.
// "nature" doesn't give results at all.
// NOTE: we only cat get a single page
splashbase: {
name: "SplashBase",
baseHost: "/apibridge/splashbase/", // "http://www.splashbase.co/api/v1/images/search",
// TODO implement Authorization headers
requestData: function(req) {
return {
path: backends.splashbase.baseHost,
params: {
query: req.keyword
}
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.images.length; i++) {
images.push({
thumbSrc: obj.body.images[i].url,
originalSrc: obj.body.images[i].large_url,
largeSrc: obj.body.images[i].url,
// No width/height ?!? we need them or javascript will break.
width: 0,
height: 0,
// I want a name for uploading.
name: 'splashbase-'+obj.body.images[i].id+'.jpg',
})
}
return {
images: images,
pages: 1,
count: obj.body.images.length
};
}
},
localjqfu: {
name: "Local",
baseHost: "/upload/", // "http://www.splashbase.co/api/v1/images/search",
// TODO implement Authorization headers
requestData: function(req) {
return {
path: backends.localjqfu.baseHost,
params: {
query: req.keyword
}
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.files.length; i++) {
images.push({
thumbSrc: obj.body.files[i].thumbnailUrl,
largeSrc: obj.body.files[i].url,
width: obj.body.files[i].width,
height: obj.body.files[i].height,
name: obj.body.files[i].name,
})
}
return {
images: images,
pages: 1,
count: obj.body.files.length
};
}
},
localjqfunosizesandbroken: {
name: "Local Broken",
baseHost: "/upload/", // "http://www.splashbase.co/api/v1/images/search",
// TODO implement Authorization headers
requestData: function(req) {
return {
path: backends.localjqfunosizesandbroken.baseHost,
params: {
query: req.keyword
}
};
},
resultConversion: function(obj, req) {
var images = [];
for (var i = 0; i < obj.body.files.length; i++) {
// 1 on 2 with no size and 1 on 3 is with no thumbnail
images.push({
thumbSrc: i % 3 ? obj.body.files[i].thumbnailUrl : '',
largeSrc: obj.body.files[i].url,
width: i % 2 ? obj.body.files[i].width : 0,
height: i % 2 ? obj.body.files[i].height : 0,
name: obj.body.files[i].name,
})
}
console.log("images", images);
return {
images: images,
pages: 1,
count: obj.body.files.length
};
}
},
error: {
name: "Bad Backend",
}
};
vm.backends = [];
for (var k in backends) if (backends.hasOwnProperty(k)) {
backends[k].key = k;
vm.backends.push(backends[k]);
}
vm.backend(backends.pexels); // default
vm.pagesLoaded = ko.observable(0);
vm.loading = ko.observable(false);
vm.autoSearch = ko.observable(true);
var doRemoteSearch = function(append) {
vm.loading(true);
try {
var searchData = {
keyword: vm.search(),
per_page: 30, // TODO maybe we should leave the
page: append ? vm.pagesLoaded()+1 : 1,
append: append
};
var backendObj = vm.backend();
if (backendObj) {
if (searchData.keyword !== "") {
var requestData = backendObj.requestData(searchData);
var xhr = new XMLHttpRequest();
var url = requestData.path+(requestData.params ? '?'+$.param(requestData.params) : '');
xhr.open('GET', url);
if (requestData.headers) for(var i = 0; i < requestData.headers.length; i++) {
xhr.setRequestHeader(requestData.headers[i].name, requestData.headers[i].value);
}
xhr.onreadystatechange = function(param) {
if (xhr.status === 0) {
console.log("API call failed probably due to CORS restrictions", xhr.status, xhr.readystate, param);
}
};
xhr.onload = function() {
// console.log("response", xhr.response);
if (xhr.status === 200) {
const headers = {};
xhr.getAllResponseHeaders().trim()
.split(/[\r\n]+/)
.map(function(value) { return value.split(/: /) })
.forEach(function (keyValue) {
headers[keyValue[0].trim()] = keyValue[1].trim();
});
if (xhr.response) {
console.log("XHR response type", typeof xhr.response);
var response = {
headers: headers,
body: JSON.parse(xhr.response),
};
updateResult(backendObj.resultConversion(response, searchData), append);
} else {
updateResult({ error: "unexpected xhr result with status 200 and no response" }, append);
}
} else {
// TODO error reporting
console.log("request failed", xhr);
updateResult({ error: xhr.status }, append);
}
};
xhr.send();
} else updateResult({ error: "no keyword supplied" }, append);
} else updateResult({ error: "no backend supplied" }, append);
} catch (e) {
console.log("Unexpected error", e);
updateResult({ error: "unexpected exception" }, append);
}
};
vm.doSearch = function() {
doRemoteSearch();
};
vm.loadMore = function() {
doRemoteSearch(true);
};
ko.computed(function() {
if (vm.autoSearch()) doRemoteSearch();
});
// "{"files":[{"name":"YpLN4HacUS4.jpg","size":60813,"type":"image\/jpeg","url":"https:\/\/mosaico.io\/files\/099jj47\/YpLN4HacUS4.jpg","thumbnailUrl":"https:\/\/mosaico.io\/files\/099jj47\/mcith\/mcith_YpLN4HacUS4.jpg","deleteUrl":"\/srv\/f-099jj47\/upload?file=YpLN4HacUS4.jpg","deleteType":"DELETE"}]}"
$('#fileupload').fileupload({
url: uploadUrl,
dataType: 'json',
})
.bind('fileuploaddone', function (e, data) {
console.log('fileuploaddone', e, data, data.result.files[0].url);
$('#uploadedimg').attr('src', data.result.files[0].url);
})
.bind('fileuploadfail', function (e, data) { console.log('fileuploadfail', e, data) })
.bind('fileuploadprogressall', function (e, data) { console.log('fileuploadprogressall', e, data) })
ko.applyBindings(vm);
$('.resizableContainer').resizable();
window.vm = vm;
// Polyfill: IE11 doesn't trigger the knockout value binding. Edge and modern browsers do. This is a fix.
var isIE = !!document.documentMode;
if (isIE) {
$("body").on("keydown", "input[type='text']", function (e) {
// Ensure 'value' binding is fired on enter in IE
if ((e.keyCode || e.which) === 13) $(this).change();
});
}
var pasteTest = function(me, event) {
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
var blob = null;
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") === 0) {
blob = items[i].getAsFile();
console.log("Found blob", i, blob);
}
}
if (blob !== null) {
var reader = new FileReader();
reader.onload = function (event) {
var image = new Image();
image.src = event.target.result;
image.onload = function () {
var range = typeof me.getSelection == 'function' && me.getSelection() && me.getSelection().getRangeAt(0);
console.log("Fake image load", range, image);
if (range) {
range.deleteContents();
range.insertNode(image);
me.getSelection().removeAllRanges();
} else {
me.appendChild(image)
}
}
}
reader.readAsDataURL(blob);
}
}
var urlRegex = new RegExp(/^https?:\/\/[^ ]*\/([^\/]*)$/i);
var pasteHnd = function(name, e) {
console.log('window paste action initiated', e);
var clipboardData, pastedData;
// Stop data actually being pasted into div
e.stopPropagation();
e.preventDefault();
// Get pasted data via clipboard API
clipboardData = e.clipboardData || window.clipboardData;
pastedData = clipboardData.getData('Text');
console.log(clipboardData, clipboardData.types);
if (pastedData !== null) {
let matches = pastedData.match(urlRegex);
// Do whatever with pasteddata
console.log("####################", pastedData, matches);
// se è CORS non funziona (quindi praticamente mai, ma con un proxy lato server dovrebbe andare
if (matches) {
vm.downloadAndUploadUrl(pastedData, "clipboard-"+matches[1]);
}
for (let i = 0; i < clipboardData.types.length; i++) {
console.log("DT ", i, clipboardData.types[i], clipboardData.getData(clipboardData.types[i]));
}
}
pasteTest(document.getElementById('testcontenteditable'), e);
}
window.addEventListener('paste', function (e) {
pasteHnd('window paste', e);
});
var fixPageEvents = function(global) {
// This is global code to prevent dragging/dropping in the page where we don't deal with it.
// IE8 doesn't have window.addEventListener, but doesn't support drag&drop too.
if (global.addEventListener) {
// prevent generic file droppping in the page
global.addEventListener("drag", function(e) {
console.log("browser is using drag listener on window", e);
e = e || global.event;
e.preventDefault();
}, false);
global.addEventListener("dragstart", function(e) {
console.log("browser is using dragstart listener on window", e);
e = e || global.event;
e.preventDefault();
}, false);
global.addEventListener("dragover", function(e) {
// this is called on mouse move on every supported browser.
console.log("browser is using dragover listener on window", e);
e = e || global.event;
e.preventDefault();
}, false);
global.addEventListener("drop", function(e) {
console.log("browser is using drop listener on window", e);
e = e || global.event;
e.preventDefault();
}, false);
global.document.body.addEventListener('drop', function(e) {
// I browser supportati entrato tutti qui quando si droppa qualcosa sul body
console.log("browser is using drop listener on body tag", e);
e.preventDefault();
}, false);
}
if (global.document.ondragstart) {
global.document.ondragstart = function() {
// console.log("browser called ondragstart. return false!");
return false;
};
}
};
fixPageEvents(window);
document.getElementById('testcontenteditable').addEventListener('paste', function (e) {
pasteHnd('testcontenteditable paste', e);
});
document.addEventListener('paste', function(event) {
console.log('document paste action initiated', event);
});
window.addEventListener('dragover', function(event) {
// console.log("windows dragover", event);
}, false);
document.addEventListener('dragover', function(event) {
// console.log("document dragover", event);
}, false);
window.addEventListener('drop', function(event) {
evtHand('window drop', event);
}, false);
function evtHand(eventname, evt) {
console.log(eventname, evt);
evt.stopPropagation();
evt.preventDefault();
var imageUrl = evt.dataTransfer.getData('text/html');
// var rex = /src="?([^"\s]+)"?\s*/;
// var url, res;
// url = rex.exec(imageUrl);
console.log("text/html: ", imageUrl);
for (let i = 0; i < evt.dataTransfer.types.length; i++) {
console.log("DT ", i, evt.dataTransfer.types[i], evt.dataTransfer.getData(evt.dataTransfer.types[i]));
}
var dataurl = evt.dataTransfer.getData('URL');
console.log("URL: ", dataurl);
vm.downloadAndUploadUrl(dataurl, "dragdrop");
}
document.addEventListener('drop', function(event) {
evtHand('document drop', event);
}, false);
document.getElementById('fileupload').addEventListener('dragover', function(event) {
// console.log("fileupload dragover", event);
}, false);
document.getElementById('fileupload').addEventListener('drop', function(event) {
evtHand('fileupload drop', event);
}, false);
});
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jQuery-flexImages/1.0.4/jquery.flex-images.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/photoswipe.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.3/default-skin/default-skin.min.css" />
<style>
.results img { }
.flex-images .item.more {
display: flex !important;
flex-direction: column;
justify-content: center;
align-content: center;
text-align: center;
font-color: #CCC;
font-family: sans-serif;
}
.flex-images .item.more .big {
text-decoration: none;
font-size: 1.5em;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 1; }
}
.flex-images .item.more.loading .big {
animation: pulse 1s infinite;
}
.resizableContainer {
width: 354px;
height: calc(100vh - 120px);
}
.resultsContainer {
overflow-y: scroll;
overflow-x: hidden;
height: 100%;
}
</style>
</head>
<body>
<select data-bind="options: backends, optionsValues: 'key', optionsText: 'name', value: backend"></select>
<input type="text" data-bind="value: search" />
<a href="#" data-bind="click: search.bind(null, 'tree')">tree</a>,<a href="#" data-bind="click: search.bind(null, 'bottle')">bottle</a>,<a href="#" data-bind="click: search.bind(null, 'car')">car</a>,<a href="#" data-bind="click: search.bind(null, 'nature')">nature</a>,<a href="#" data-bind="click: search.bind(null, 'clock')">clock</a>,<a href="#" data-bind="click: search.bind(null, 'lake')">lake</a>
<input type="text" data-bind="value: thumbHeight" />
<input type="checkbox" data-bind="checked: autoSearch" />
<input type="submit" data-bind="click: doSearch, disable: autoSearch" />
<hr />
<input type="file" id="fileupload" data-bind="droppable: $root.downloadAndUploadMe" />
<img id="uploadedimg" width="500" style="max-width: 200px; max-height: 200px;" />
<div id="testcontenteditable" contenteditable="true">PROVA</div>
<hr />
Count: <span data-bind="text: searchCount">#count#</span>
Pages: <span data-bind="text: searchPages">#pages#</span>
Error: <span data-bind="text: searchError">#error#</span>
<!-- make sure the flexImage item doesn't have scrollbars otherwise Firefox incorrectly compute widths -->
<div class="resizableContainer">
<div class="resultsContainer">
<div class="flex-images results">
<!-- data-size="1600x1600" data-med="https://farm4.staticflickr.com/3894/15008518202_b016d7d289_b.jpg" data-med-size="1024x1024" data-author="Folkert Gorter" class="demo-gallery__img- -main"> -->
<!-- ko foreach: searchResult -->
<div class="item" data-bind="attr: { 'data-w': width, 'data-h': height }, draggable: $data">
<img loading="lazy" data-bind="event: { load: $root.imageLoadHandler, error: $root.imageLoadHandler }, attr: { 'data-src': thumbSrc, 'title': width()+'x'+height() }, click: $root.zoomMe" asrc="blank.gif" /> <!-- click: $root.downloadAndUploadMe -->
<!-- <span data-bind="text: width()+'x'+height()">widthxheight</span> -->
</div>
<!-- /ko -->
<!-- ko if: searchResult().length < searchCount() -->
<div class="item more" data-bind="css: { loading: loading }" data-w="500" data-h="500">
<span class="progress"><!-- ko text: searchResult().length -->x<!-- /ko -->/<!-- ko text: searchCount -->y<!-- /ko --></span>
<!-- ko if: !$root.loading() --><a class="big" href="#" data-bind="click: $root.loadMore">more</a><!-- /ko -->
<!-- ko if: $root.loading() --><span class="big">..loading..</span><!-- /ko -->
</div>
<!-- /ko -->
</div>
</div>
</div>
<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
<!-- Background of PhotoSwipe.
It's a separate element as animating opacity is faster than rgba(). -->
<div class="pswp__bg"></div>
<!-- Slides wrapper with overflow:hidden. -->
<div class="pswp__scroll-wrap">
<!-- Container that holds slides.
PhotoSwipe keeps only 3 of them in the DOM to save memory.
Don't modify these 3 pswp__item elements, data is added later on. -->
<div class="pswp__container">
<div class="pswp__item"></div>
<div class="pswp__item"></div>
<div class="pswp__item"></div>
</div>
<!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
<div class="pswp__ui pswp__ui--hidden">
<div class="pswp__top-bar">
<!-- Controls are self-explanatory. Order can be changed. -->
<div class="pswp__counter"></div>
<button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
<button class="pswp__button pswp__button--share" title="Share"></button>
<button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
<button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
<!-- Preloader demo https://codepen.io/dimsemenov/pen/yyBWoR -->
<!-- element will get class pswp__preloader- -active when preloader is running -->
<div class="pswp__preloader">
<div class="pswp__preloader__icn">
<div class="pswp__preloader__cut">
<div class="pswp__preloader__donut"></div>
</div>
</div>
</div>
</div>
<div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
<div class="pswp__share-tooltip"></div>
</div>
<button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)"></button>
<button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)"></button>
<div class="pswp__caption">
<div class="pswp__caption__center"></div>
</div>
</div>
</div>
</div>
</body>