-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzoomable-image-carousel.js
680 lines (470 loc) · 18.5 KB
/
zoomable-image-carousel.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
class ZoomableImageCarousel {
// This package is in need of some love, refactoring or at least documentation.
// Specifically looking over the code now it seems overly complicated and repetative
// but I am confident there were reasons. I just don't remember what those reasons
// were anymore...
visible = 3
_images = []
_pictures = []
_links = []
_attrs = []
document_root
constructor(root) {
this.document_root = (root) ? root : document;
}
make_carousel_wrapper(ctx) {
if (ctx.hasAttribute("visible")){
var n = parseInt(ctx.getAttribute("visible"));
if (n){
this.visible = n;
}
}
var _self = this;
// Start by creating a new <ul> element that will eventually replace 'this'.
var carousel = document.createElement("ul");
carousel.setAttribute("class", "zoomable-carousel");
var pictures = ctx.querySelectorAll("picture");
var count_pictures = pictures.length;
if (count_pictures < this.visible) {
ctx.setAttribute("class", "zoomable-carousel-" + count_pictures);
}
for (var i=0; i < count_pictures; i++){
var pic_el = pictures[i];
this._pictures.push(pic_el);
var img_el = pic_el.querySelector("img");
var img_src = img_el.getAttribute("src");
this._images.push(img_src);
var data_attributes = {
image_src: img_src,
"image-id": pic_el.getAttribute("zoomable-image-id"),
"tiles-url": pic_el.getAttribute("zoomable-tiles-url"),
"exif-description": pic_el.getAttribute("zoomable-exif-description"),
"exif-copyright": pic_el.getAttribute("zoomable-exif-copyright"),
"image-control": pic_el.hasAttribute("zoomable-image-control"),
"picture": pic_el.cloneNode(true),
// This is necessary because 'pic_el' get silently zero-ed out
// for reasons I don't understand so we track the raw HTML and
// if necessary parse it back in to a DOM element in make_picture_element.
// Good times....
"picture_html": pic_el.outerHTML,
};
this._attrs[img_src] = data_attributes;
var parent = img_el.parentNode;
if (parent.nodeName == "A"){
var link = parent.getAttribute("href");
this._links[ img_src ] = link
parent = parent.parentNode;
}
parent.style.display = "none";
}
// Create rewind button
var rewind_el = document.createElement("li");
rewind_el.setAttribute("id", "zoomable-carousel-control-rewind");
rewind_el.setAttribute("class", "zoomable-carousel-item zoomable-carousel-control");
rewind_el.appendChild(document.createTextNode("<"));
rewind_el.onclick = function(){
_self.rewind();
};
carousel.appendChild(rewind_el);
// Draw visible image panes
var panes = this.visible;
if (count_pictures < this.visible){
panes = count_pictures;
}
for (var i=0; i < panes; i++){
var j = (i == 0) ? count_pictures - 1 : i - 1;
var img_src = this._images[j];
var img_node = document.createElement("img");
img_node.setAttribute("class", "zoomable-carousel-item zoomable-carousel-visible");
img_node.setAttribute("zoomable-carousel-index", j);
img_node.setAttribute("src", img_src);
img_node.onclick = function(e){
var el = e.target;
var src = el.getAttribute("src");
var attrs = _self._attrs[src];
var id = attrs["image-id"];
_self.assign(id);
return false;
};
var item_node = document.createElement("li");
item_node.setAttribute("class", "zoomable-carousel-item");
item_node.setAttribute("zoomable-carousel-index", i);
item_node.appendChild(img_node);
carousel.appendChild(item_node);
}
// Create advance button
var advance_el = document.createElement("li");
advance_el.setAttribute("id", "zoomable-carousel-control-advance");
advance_el.setAttribute("class", "zoomable-carousel-item zoomable-carousel-control");
advance_el.appendChild(document.createTextNode(">"));
advance_el.onclick = function(){
_self.advance();
};
carousel.appendChild(advance_el);
var first_pic = this._pictures[0];
// Create zoomable image (custom) element which is placed above the carousel
var z;
// If we are in a WebKit context we (seemingly) can't use the new ZoomableImageElementCustom()
// methods so we just use the inline code construct the element manually. All of this needs
// to be reconciled with the equivalent code in zoomable-image.js and, perhaps, there is no
// point in trying to create elements with the ZoomableImage wrapper classes. TBD...
if ('webkitRequestAnimationFrame' in window){
var z_attrs = {
'image-id': first_pic.getAttribute("zoomable-image-id"),
'image-url-ds': '',
'tiles-url': first_pic.getAttribute("zoomable-tiles-url"),
'image-control': first_pic.getAttribute("zoomable-image-control"),
'picture': first_pic,
};
if (first_pic.hasAttribute("zoomable-exif-description")){
z_attrs['exif-description'] = first_pic.getAttribute("zoomable-exif-description");
}
if (first_pic.hasAttribute("zoomable-exif-copyright")){
z_attrs['exif-copyright'] = first_pic.getAttribute("zoomable-exif-copyright");
}
z = this.make_zoomable_element(z_attrs);
} else {
/*
Note the way we are calling new ZoomableImageElement and not document.createElement("picture")
and then assigning an is="zoomable-image" property. When we do the latter the Web Component
constructor is not trigger. I don't know why.
*/
z = new ZoomableImageElement();
z.setAttribute("zoomable-image-id", first_pic.getAttribute("zoomable-image-id"));
z.setAttribute("zoomable-tiles-url", first_pic.getAttribute("zoomable-tiles-url"));
if (first_pic.hasAttribute("zoomable-exif-description")){
z.setAttribute("zoomable-exif-description", first_pic.getAttribute("zoomable-exif-description"));
}
if (first_pic.hasAttribute("zoomable-exif-copyright")){
z.setAttribute("zoomable-exif-copyright", first_pic.getAttribute("zoomable-exif-copyright"));
}
if (first_pic.hasAttribute("zoomable-image-control")){
z.setAttribute("zoomable-image-control", "true");
}
var source_els = first_pic.querySelectorAll("source")
var img_els = first_pic.querySelectorAll("img")
var count_sources = source_els.length;
var count_imgs = img_els.length;
for (var i=0; i < count_sources; i++){
z.appendChild(source_els[i]);
}
for (var i=0; i < count_imgs; i++){
z.appendChild(img_els[i]);
}
}
var wrapper = document.createElement("div");
wrapper.setAttribute("class", "zoomable-carousel-wrapper");
var tpl_id = "zoomable-image-carousel-template";
if (ctx.hasAttribute("template-id")){
tpl_id = ctx.getAttribute("template-id");
}
var tpl = document.querySelector("#" + tpl_id);
if (tpl){
let tpl_content = tpl.content;
wrapper.appendChild(tpl_content.cloneNode(true));
}
wrapper.appendChild(z);
wrapper.appendChild(carousel);
// Let's just get everything else working first...
if (ctx.hasAttribute("zoomable-keyboard-events")){
var pagination_el = document.createElement("div");
pagination_el.setAttribute("class", "zoomable-pagination-blurb");
var previous = document.createElement("span");
previous.setAttribute("class", "zoomable-pagination-blurb-arrow");
previous.appendChild(document.createTextNode("←"));
var next = document.createElement("span");
next.setAttribute("class", "zoomable-pagination-blurb-arrow");
next.appendChild(document.createTextNode("→"));
pagination_el.appendChild(document.createTextNode("you can also use the "));
pagination_el.appendChild(previous);
pagination_el.appendChild(document.createTextNode(" and "));
pagination_el.appendChild(next);
pagination_el.appendChild(document.createTextNode(" arrow keys on your keyboard to navigate between images"));
wrapper.appendChild(pagination_el);
this.init_keyboard();
}
return wrapper;
}
assign(id) {
if (! id){
id = this._pictures[0].getAttribute("zoomable-image-id");
}
var visible_images = this.document_root.querySelectorAll(".zoomable-carousel-visible");
var count_visible = visible_images.length;
var current_idx = Math.floor(count_visible / 2);
var new_idx = this.index_for_id(id);
if (new_idx == -1){
console.error("Can't determine new index");
return false;
}
var current_el = visible_images[current_idx];
if (! current_el){
console.error("Can't get element for index " + current_idx);
return false;
}
var current_src = current_el.getAttribute("src");
var current_attrs = this._attrs[current_src];
var new_src = this._images[new_idx];
var new_attrs = this._attrs[new_src];
new_attrs.picture = this._pictures[new_src];
this.update(current_attrs, new_attrs);
// now update the bookends
var count_pictures = this._images.length;
var center_el = visible_images[current_idx];
center_el.setAttribute("src", new_src);
center_el.setAttribute("zoomable-carousel-index", new_idx);
for (var i=0; i < current_idx; i++){
var j = i + 1;
var prev_idx = (new_idx == 0) ? count_pictures - 1 : new_idx - j;
var prev_src = this._images[prev_idx];
var prev_el = visible_images[i];
prev_el.setAttribute("src", prev_src);
prev_el.setAttribute("zoomable-carousel-index", prev_idx);
}
for (var i= (current_idx + 1); i < count_visible; i++){
var next_idx = (new_idx == (count_pictures - 1)) ? 0 : new_idx + 1;
var next_src = this._images[next_idx];
var next_el = visible_images[i];
next_el.setAttribute("src", next_src);
next_el.setAttribute("zoomable-carousel-index", next_idx);
}
}
index_for_id(id){
var idx = -1;
var count = this._images.length;
for (var i=0; i < count; i++){
var src = this._images[i];
var attrs = this._attrs[src];
if (attrs["image-id"] == id){
idx = i;
break;
}
}
return idx;
}
update(current_attrs, updated_attrs){
location.hash = updated_attrs["image-id"];
var updated_zoomable = this.make_zoomable_element(updated_attrs);
var current_zoomable = this.document_root.querySelector("#zoomable-image-" + current_attrs["image-id"]);
current_zoomable.parentNode.replaceChild(updated_zoomable, current_zoomable);
zoomable.images.init(updated_zoomable, this.document_root);
var updated_id = updated_zoomable.getAttribute("zoomable-image-id");
var img_id = "#zoomable-picture-default-" + updated_id;
var im = this.document_root.querySelector(img_id);
im.onload = function() {
var w = this.width;
var h = this.height;
if (h = w){
zoomable.images.resize_visible();
}
};
}
advance(){
var count_images = this._images.length;
var visible = this.document_root.querySelectorAll(".zoomable-carousel-visible");
var count = visible.length;
var center = Math.floor(count / 2); // AGAIN, account for image count of 2
for (var i=0; i < count; i++){
var el = visible[i];
var src = el.getAttribute("src");
var idx = el.getAttribute("zoomable-carousel-index");
idx = parseInt(idx);
var next_idx = (idx == 0) ? count_images - 1 : idx - 1;
var next_src = this._images[next_idx];
el.setAttribute("src", next_src);
el.setAttribute("zoomable-carousel-index", next_idx);
if (i == center){
var current_attrs = this._attrs[src];
var next_attrs = this._attrs[next_src];
this.update(current_attrs, next_attrs);
}
}
}
rewind(){
var count_images = this._images.length;
var visible = this.document_root.querySelectorAll(".zoomable-carousel-visible");
var count = visible.length;
var center = Math.floor(count / 2);
for (var i=0; i < count; i++){
var el = visible[i];
var src = el.getAttribute("src");
var idx = el.getAttribute("zoomable-carousel-index");
idx = parseInt(idx);
var prev_idx = (idx == (count_images - 1)) ? 0 : idx + 1;
var prev_src = this._images[prev_idx];
el.setAttribute("src", prev_src);
el.setAttribute("zoomable-carousel-index", prev_idx);
if (i == center){
var current_attrs = this._attrs[src];
var prev_attrs = this._attrs[prev_src];
this.update(current_attrs, prev_attrs);
}
}
}
init_keyboard(){
var _self = this;
document.addEventListener('keydown', function(e){
if (e.keyCode == 37){
_self.rewind();
}
if (e.keyCode == 39){
_self.advance();
}
});
}
// START OF reconcile this with the code in zoomable-image.js
make_zoomable_element(args){
var static_el = this.make_static_element(args);
var tiles_el = this.make_tiles_element(args);
var zoomable_el = document.createElement("div");
zoomable_el.setAttribute("class", "zoomable-image");
zoomable_el.setAttribute("id", "zoomable-image-" + args["image-id"]);
zoomable_el.setAttribute("zoomable-image-id", args["image-id"]);
zoomable_el.setAttribute("zoomable-tiles-url", args["tiles-url"]);
if (args["image-control"]){
zoomable_el.setAttribute("zoomable-image-control", args["image-control"]);
}
if (args["exif-description"]){
zoomable_el.setAttribute("zoomable-exif-description", args["exif-description"]);
}
if (args["exif-copyright"]){
zoomable_el.setAttribute("zoomable-exif-copyright", args["exif-copyright"]);
}
zoomable_el.appendChild(static_el);
zoomable_el.appendChild(tiles_el);
return zoomable_el;
}
make_static_element(args){
var picture_el = this.make_picture_element(args);
var link_el = document.createElement("a");
link_el.setAttribute("class", "zoomable-image-link");
link_el.setAttribute("src", "#");
link_el.appendChild(picture_el);
var button_el = document.createElement("button");
button_el.setAttribute("class", "btn btn-sm btn-light zoomable-button zoomable-toggle-tiles");
button_el.setAttribute("id", "zoomable-toggle-tiles-" + args["image-id"]);
button_el.setAttribute("zoomable-image-id", args["image-id"]);
button_el.setAttribute("title", "View this image in full screen mode");
var loading_el = document.createElement("p");
loading_el.setAttribute("id", "zoomable-loading-" + args["image-id"]);
loading_el.setAttribute("class", "zoomable-loading");
loading_el.setAttribute("style", "background-image:url(" + args["image-url-ds"] + ")");
var span_el = document.createElement("span");
span_el.setAttribute("class", "zoomable-loading-text");
span_el.appendChild(document.createTextNode("loading image"));
loading_el.appendChild(span_el);
var static_el = document.createElement("div");
static_el.setAttribute("class", "zoomable-static");
static_el.setAttribute("id", "zoomable-static-" + args["image-id"]);
static_el.setAttribute("zoomable-tiles-url", args["tiles-url"]);
if (args["image-control"]){
static_el.setAttribute("zoomable-image-control", args["image-control"]);
}
if (args["exif-description"]){
static_el.setAttribute("zoomable-exif-description", args["exif-description"]);
}
if (args["exif-copyright"]){
static_el.setAttribute("zoomable-exif-copyright", args["exif-copyright"]);
}
static_el.appendChild(button_el);
static_el.appendChild(loading_el);
static_el.appendChild(link_el);
return static_el;
}
make_tiles_element(args){
var map_el = document.createElement("div");
map_el.setAttribute("class", "zoomable-map");
map_el.setAttribute("id", "zoomable-map-" + args["image-id"]);
var tiles_el = document.createElement("div");
tiles_el.setAttribute("class", "zoomable-tiles");
tiles_el.setAttribute("id", "zoomable-tiles-" + args["image-id"]);
tiles_el.setAttribute("zoomable-tiles-url", args["tiles-url"]);
if (args["image-control"]){
tiles_el.setAttribute("zoomable-image-control", args["image-control"]);
}
if (args["exif-description"]){
tiles_el.setAttribute("zoomable-exif-description", args["exif-description"]);
}
if (args["exif-copyright"]){
tiles_el.setAttribute("zoomable-exif-copyright", args["exif-copyright"]);
}
tiles_el.appendChild(map_el);
return tiles_el;
}
make_picture_element(args){
var p = document.createElement("picture");
p.setAttribute("class", "zoomable-picture");
p.setAttribute("id", "zoomable-picture-" + args["image-id"]);
p.setAttribute("zoomable-tiles-url", args["tiles-url"]);
if (args["image-control"]){
p.setAttribute("zoomable-image-control", true);
}
if (args["exif-description"]){
p.setAttribute("zoomable-exif-description", args["exif-description"]);
}
if (args["exif-copyright"]){
p.setAttribute("zoomable-exif-copyright", args["exif-copyright"]);
}
var picture_el;
// See notes above in make_carousel_wrapper. So dumb...
if (args["picture"]){
picture_el = args["picture"];
} else if (args["picture_html"]){
var parser = new DOMParser();
var doc = parser.parseFromString(args["picture_html"], "text/html");
picture_el = doc.querySelector("picture");
}
if (picture_el){
var source_els = picture_el.querySelectorAll("source");
var count_source = source_els.length;
for (var i=0; i < count_source; i++){
p.appendChild(source_els[i]);
}
}
var class_names = [
"zoomable-picture-default",
];
var i = document.createElement("img");
i.setAttribute("id", "zoomable-picture-default-" + args["image-id"]);
i.setAttribute("class", class_names.join(" "));
i.setAttribute("src", args["image_src"]);
p.appendChild(i);
return p;
}
// END OF reconcile this with the code in zoomable-image.js
}
class ZoomableImageCarouselElement extends HTMLUListElement {
constructor() {
super();
}
connectedCallback(){
var zc = new ZoomableImageCarousel();
var wrapper = zc.make_carousel_wrapper(this);
this.parentNode.replaceChild(wrapper, this);
var id;
if (location.hash != ""){
var hash = location.hash.substr(1);
id = parseInt(hash);
}
zc.assign(id);
}
}
customElements.define('zoomable-image-carousel', ZoomableImageCarouselElement, { extends: "ul" });
class ZoomableImageCarouselCustom extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const shadow = this.attachShadow({ mode: "open" });
var zc = new ZoomableImageCarousel(shadow);
var wrapper = zc.make_carousel_wrapper(this);
shadow.appendChild(wrapper);
var id;
if (location.hash != ""){
var hash = location.hash.substr(1);
id = parseInt(hash);
}
zc.assign(id);
}
}
customElements.define("zoomable-image-carousel-custom", ZoomableImageCarouselCustom);