-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
945 lines (798 loc) · 27.9 KB
/
index.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
/* global
HTMLElement,
*/
(/** @param {Document} document */function (document) {
class Validators {
/**
* @public
* @param {*} something
* @returns {boolean}
*/
static nonEmptyString (something) {
return typeof something === 'string' && something.length > 0
}
}
class DomHelper {
/**
* @public
* @param {Document} document
*/
constructor (document) {
this.document = document
}
/**
* @public
* @param {string} css
*/
addStylesheet (css) {
const styleElem = this.document.head.appendChild(this.document.createElement('style'))
styleElem.innerHTML = css
}
/**
* @public
* @param {string} html
* @param {Element} destElem
* @param {string} method
*/
attachHtmlTo (html, destElem, method) {
const templateElem = destElem.appendChild(destElem.ownerDocument.createElement('template'))
templateElem.innerHTML = html
destElem[method](templateElem.content)
templateElem.remove()
}
}
class HtmlUtils {
/**
* @public
* @param {string} block
* @param {string} [element]
* @param {string} [modifier]
* @returns {string}
*/
static bem (block, element, modifier) {
let name = `${block}`
if (Validators.nonEmptyString(element)) {
name += `__${element}`
}
if (Validators.nonEmptyString(modifier)) {
name += `--${modifier}`
}
return name
}
/**
* See https://stackoverflow.com/a/19842865/1063649
*
* @public
* @param {string} [prefix]
* @returns {string}
*/
static uniqid (prefix = '') {
return prefix + Date.now().toString(36) + Math.random().toString(36).substring(2)
}
}
class EventDecorator {
/**
* @public
* @param {Event} e
*/
constructor (e) {
this.event = e
}
/**
* @public
* @param {HTMLElement|string} selector
* @param {number} [depth = 1]
* @returns {HTMLElement|null}
*/
getActionTarget (selector, depth = 1) {
let targetElem = this.getTarget()
do {
if (
(selector instanceof HTMLElement && targetElem === selector) ||
(typeof selector === 'string' && targetElem.matches(selector))
) {
return targetElem
}
targetElem = targetElem.parentElement
depth -= 1
} while (depth >= 0 && targetElem !== null)
return null
}
/**
* @public
* @param {number} [depth = 1]
* @returns {HTMLAnchorElement|null}
*/
getAnchorTarget (depth = 1) {
return this.getActionTarget('a', depth)
}
/**
* @private
* @returns {EventTarget|HTMLElement}
*/
getTarget () {
return this.event.target
}
}
/**
* @abstract
*/
class AbstractComponent {
/**
* @public
* @param {HTMLElement} rootElem
* @param {string} blockName
*/
constructor (rootElem, blockName) {
this.setComponents()
this.setRootElem(rootElem)
this.setBlockName(blockName)
this.setDomHelper(new DomHelper(this.getDocument()))
this.addClassBem()
}
/**
* Call when you're ready to render the GUI
*
* @public
*/
setUp () {
}
/**
* @private
* @param {object} [components]
*/
setComponents (components = {}) {
this.components = components
}
/**
* @protected
* @param {string} id
* @param {AbstractComponent} component
*/
setComponent (id, component) {
this.components[id] = component
}
/**
* @protected
* @param {string} id
* @returns {AbstractComponent}
* @throws {Error} If the specified component doesn't exist
*/
getComponent (id) {
if (!(id in this.components)) {
throw new Error(`The component, \`${id}\`, doesn't exist`)
}
return this.components[id]
}
/**
* @private
* @param {HTMLElement} elem
*/
setRootElem (elem) {
this.rootElem = elem
}
/**
* @public
* @returns {HTMLElement}
*/
getRootElem () {
return this.rootElem
}
/**
* @private
* @param {DomHelper} domHelper
*/
setDomHelper (domHelper) {
this.domHelper = domHelper
}
/**
* @protected
* @returns {DomHelper}
*/
getDomHelper () {
return this.domHelper
}
/**
* @protected
* @param {HTMLElement|string} elemOrTagName
* @returns {HTMLElement}
*/
appendChild (elemOrTagName) {
const childElem = typeof elemOrTagName === 'string'
? this.getDocument().createElement(elemOrTagName)
: elemOrTagName
return this.getRootElem().appendChild(childElem)
}
/**
* @private
* @param {string} name
*/
setBlockName (name) {
this.blockName = name
}
/**
* @private
* @returns {string}
*/
getBlockName () {
return this.blockName
}
/**
* @protected
* @param {string} [element]
* @param {string} [modifier]
* @returns {string}
*/
bem (
element = '',
modifier = ''
) {
return HtmlUtils.bem(this.getBlockName(), element, modifier)
}
/**
* @protected
* @param {string} [element]
* @param {string} [modifier]
* @returns {HTMLElement|null}
*/
queryBem (
element = '',
modifier = ''
) {
const rootElem = this.getRootElem()
return element === '' && modifier === ''
? rootElem
: rootElem.querySelector('.' + this.bem(element, modifier))
}
/**
* @protected
* @param {string} [element]
* @param {string} [modifier]
*/
addClassBem (
element = '',
modifier = ''
) {
this.getRootElem().classList.add(this.bem(element, modifier))
}
/**
* @protected
* @returns {Document}
*/
getDocument () {
return this.getRootElem().ownerDocument
}
/**
* @protected
* @returns {Window}
*/
getWindow () {
return this.getDocument().defaultView
}
}
class Images {
/**
* @public
* @constant
* @type {string}
*/
static ICON_PAUSE = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><title>Pause</title><!--!Font Awesome Pro 6.6.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2024 Fonticons, Inc.--><path d="M48 96c-8.8 0-16 7.2-16 16l0 288c0 8.8 7.2 16 16 16l48 0c8.8 0 16-7.2 16-16l0-288c0-8.8-7.2-16-16-16L48 96zM0 112C0 85.5 21.5 64 48 64l48 0c26.5 0 48 21.5 48 48l0 288c0 26.5-21.5 48-48 48l-48 0c-26.5 0-48-21.5-48-48L0 112zM224 96c-8.8 0-16 7.2-16 16l0 288c0 8.8 7.2 16 16 16l48 0c8.8 0 16-7.2 16-16l0-288c0-8.8-7.2-16-16-16l-48 0zm-48 16c0-26.5 21.5-48 48-48l48 0c26.5 0 48 21.5 48 48l0 288c0 26.5-21.5 48-48 48l-48 0c-26.5 0-48-21.5-48-48l0-288z"/></svg>'
/**
* @public
* @constant
* @type {string}
*/
static ICON_PLAY = '<svg class="stw-icon-play" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><title>Play</title><!--!Font Awesome Pro 6.6.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2024 Fonticons, Inc.--><path d="M56.3 66.3c-4.9-3-11.1-3.1-16.2-.3s-8.2 8.2-8.2 14l0 352c0 5.8 3.1 11.1 8.2 14s11.2 2.7 16.2-.3l288-176c4.8-2.9 7.7-8.1 7.7-13.7s-2.9-10.7-7.7-13.7l-288-176zM24.5 38.1C39.7 29.6 58.2 30 73 39L361 215c14.3 8.7 23 24.2 23 41s-8.7 32.2-23 41L73 473c-14.8 9.1-33.4 9.4-48.5 .9S0 449.4 0 432L0 80C0 62.6 9.4 46.6 24.5 38.1z"/></svg>'
/**
* @public
* @constant
* @type {string}
*/
static SRC_STW_LOGO = 'data:image/svg+xml;base64,PHN2ZyBpZD0iR3JleSIgeG1s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uMTMsNy43Mi03LjkzLDcuNzJaTTE3Ny43Myw4YzAtMy4zNC0yLjExLTYtNS45NC02aC0zLjM2VjE0aDMuMzZBNS42OCw1LjY4LDAsMCwwLDE3Ny43Myw4WiIgdHJhbnNmb3JtPSJ0cmFuc2xhdGUoMCAtMC4wMSkiLz48L3N2Zz4='
}
/**
* @fires #seetheworld:click-control
*/
class ViewerOverlay extends AbstractComponent {
/**
* @private
* @constant
* @type {string}
*/
static ACTION_PAUSE = 'pause'
/**
* @private
* @constant
* @type {string}
*/
static ACTION_PLAY = 'play'
/**
* @private
* @constant
* @type {object}
*/
static ACTION_ICONS = {
[this.ACTION_PAUSE]: Images.ICON_PAUSE,
[this.ACTION_PLAY]: Images.ICON_PLAY
}
/**
* @private
* @param {string} actionName
* @returns {string}
* @throws {Error} If the action-name is invalid
*/
createControlHtml (actionName) {
if (!(actionName in ViewerOverlay.ACTION_ICONS)) {
throw new Error('The action-name is invalid')
}
return `<a href="#" data-stw-action="${actionName}">${ViewerOverlay.ACTION_ICONS[actionName]}</a>`
}
/**
* @private
* @param {PointerEvent} e
*/
handleControlClicked (e) {
const evt = new EventDecorator(e)
const targetElem = evt.getAnchorTarget(2)
if (!targetElem) {
// (No anchor target)
return
}
e.preventDefault()
// #########> Toggle #########
const currActionName = targetElem.dataset.stwAction
this.getRootElem().dispatchEvent(new CustomEvent('seetheworld:click-control', {
detail: { action: currActionName }
}))
const nextActionName = currActionName === ViewerOverlay.ACTION_PAUSE
? ViewerOverlay.ACTION_PLAY
: ViewerOverlay.ACTION_PAUSE
this.getDomHelper().attachHtmlTo(this.createControlHtml(nextActionName), targetElem, 'after')
targetElem.remove()
// #########< Toggle #########
}
/**
* @override
*/
setUp () {
const names = {
root: this.bem(),
title: this.bem('title'),
byline: this.bem('byline'),
dateCreated: this.bem('date-created'),
controls: this.bem('controls')
}
this.getDomHelper().addStylesheet(`
.${names.root} {
--overlay-padding: var(--spacer-3);
--overlay-color-body: var(--color-white);
--overlay-color-body-bg: var(--color-black-semi);
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: +1;
color: var(--overlay-color-body);
background: transparent;
padding: var(--overlay-padding);
}
/* ######> Main Header ###### */
.${names.root} > header {
display: none;
justify-content: space-between;
align-items: start;
}
.${this.bem('', 'has-meta')} > header {
display: flex;
animation: fadeIn ease 2s;
animation-fill-mode: forwards;
}
/* With a background */
.${names.title},
.${names.byline},
.${names.dateCreated} > time,
.${names.controls} a {
background-color: var(--overlay-color-body-bg);
backdrop-filter: var(--filter-bg-obscure);
box-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}
/* Text content, inline */
.${names.title},
.${names.byline},
.${names.dateCreated} > time {
padding: 0 var(--spacer-1);
}
/* Text content, inline */
.${names.title},
.${names.byline} {
display: inline-block; /* Tightens-up vertical whitespace */
}
.${names.byline},
.${names.dateCreated} > time {
font-size: var(--fs-sm);
font-weight: var(--fw-lighter);
}
.${names.title} {
font-size: var(--fs-5);
font-weight: var(--fw-semibold);
}
/* Interactive elements */
a.${names.byline},
.${names.controls} a {
background-color: var(--color-accent-semi);
}
a.${names.byline}:has(.stw-strapline) {
font-weight: var(--fw-normal);
}
a.${names.byline} .stw-strapline {
font-weight: var(--fw-lighter);
font-style: italic;
}
/* ######< Main Header ###### */
/* ######> Controls ###### */
.${names.controls} {
position: absolute;
bottom: var(--overlay-padding);
width: 100%;
display: none;
justify-content: center;
align-items: center;
}
.${this.bem('', 'has-controls')} .${names.controls} {
display: flex;
animation: fadeIn ease 2s;
animation-fill-mode: forwards;
}
.${names.controls} a {
display: block;
line-height: 1;
width: var(--fs-1);
height: var(--fs-1);
text-align: center;
padding: var(--spacer-2);
border: 0;
border-radius: 50%;
}
.${names.controls} a svg {
height: 100%;
fill: var(--color-white);
}
/* ######< Controls ###### */
`)
// Just the framework for now
this.getRootElem().innerHTML = `
<header></header>
<div class="${names.controls}"></div>
`
}
/**
* @public
* @param {object|null} meta
*/
addMetadata (meta) {
if (!meta) {
// (For now, do nothing)
return
}
const tplContext = { meta }
const bylineHtml = typeof tplContext.meta.url === 'string'
? `<a href="${tplContext.meta.url}" target="_blank" class="${this.bem('byline')}">${tplContext.meta.byline}</a>`
: `<span>${tplContext.meta.byline}</span>`
this.getRootElem().querySelector('header').innerHTML = `
<div class="${this.bem('heading')}">
<span class="${this.bem('title')}">${tplContext.meta.title}</span>
<br>
${bylineHtml}
</div>
<div class="${this.bem('date-created')}"><time>${tplContext.meta.dateCreated}</time></div>
`
// "Loaded", effectively
this.addClassBem('', 'has-meta')
}
/**
* @public
*/
useControls () {
const controlsElem = this.queryBem('controls')
controlsElem.innerHTML = this.createControlHtml(ViewerOverlay.ACTION_PAUSE)
controlsElem.addEventListener('click', this.handleControlClicked.bind(this))
// "Loaded", effectively
this.addClassBem('', 'has-controls')
}
}
class Viewer extends AbstractComponent {
/**
* The ID of the image animation, the 'main' animation
*
* @private
* @type {string}
*/
static IMAGE_ANIMATION_ID = 'stw-wiv-image'
/**
* Determined through testing: (1146px * 2) / 30s
*
* @private
* @type {number}
*/
static PIXELS_PER_SEC = 76
/**
* @override
* @param {string} imageViewerId
* @param {string} imagesBaseUrl
*/
constructor (
rootElem,
blockName,
imageViewerId,
imagesBaseUrl
) {
super(rootElem, blockName)
this.setImageViewerId(imageViewerId)
this.setImagesBaseUrl(imagesBaseUrl)
}
/**
* @private
* @returns {Animation}
* @throws {Error} If the image animation is missing
*/
getImageAnimation () {
const animations = this.getImageWrapperElem().getAnimations()
const numAnimations = animations.length
let currAnimation
for (let i = 0; i < numAnimations; i++) {
currAnimation = animations[i]
if (currAnimation.id === Viewer.IMAGE_ANIMATION_ID) {
return currAnimation
}
}
throw new Error('The image animation is missing')
}
/**
* @private
* @returns {number}
*/
calculateOneWayDelta () {
const viewerViewportWidth = this.getRootElem().clientWidth
return this.getImageElem().clientWidth - viewerViewportWidth
}
/**
* @private
* @param {CustomEvent} e
*/
handleOverlayClicked (e) {
const animation = this.getImageAnimation()
const animationIsActive = animation && ['running', 'paused'].includes(animation.playState)
if (!animationIsActive) {
return
}
switch (e.detail.action) {
case ViewerOverlay.ACTION_PAUSE:
this.getImageAnimation().pause()
break
case ViewerOverlay.ACTION_PLAY:
this.getImageAnimation().play()
break
}
}
/**
* @private
*/
setUpAnimation () {
// Lock it down
// See https://stackoverflow.com/a/4770179/1063649
const wheelEventName = 'onwheel' in this.getImageWrapperElem() ? 'wheel' : 'mousewheel'
this.getRootElem().addEventListener(wheelEventName, e => e.preventDefault())
this.getDomHelper().addStylesheet(`
.${this.bem()} {
overflow-x: auto;
}
`)
const oneWayDeltaPx = this.calculateOneWayDelta()
// (There and back. See animation spec, below.)
const durationMs = ((oneWayDeltaPx * 2) / Viewer.PIXELS_PER_SEC) * 1000
this
.getImageWrapperElem()
.animate(
// (Left to right and back again)
{ transform: ['translateX(0)', `translateX(-${oneWayDeltaPx}px)`, 'translateX(0)'] },
{ id: Viewer.IMAGE_ANIMATION_ID, duration: durationMs, iterations: Infinity, easing: 'linear' }
)
.ready
.then(() => {
// *Now* we're ready to use the controls
/** @type {ViewerOverlay} */
const overlay = this.getComponent('overlay')
overlay.useControls()
overlay.getRootElem().addEventListener('seetheworld:click-control', this.handleOverlayClicked.bind(this))
})
}
/**
* The image has loaded. Now we can get on and finish setting-up the viewer.
*
* @private
*/
continueSetUp () {
// #########> Fetch and process metadata #########
/** @type {ViewerOverlay} */
const overlay = this.getComponent('overlay')
const nameOfCallbackForProcessingMetadata = HtmlUtils.uniqid('stwWivAddMetaToOverlay')
this.getWindow()[nameOfCallbackForProcessingMetadata] = overlay.addMetadata.bind(overlay)
this.appendChild('script').setAttribute('src', this.createImageMetadataUrl(nameOfCallbackForProcessingMetadata))
// #########< Fetch and process metadata #########
// Put another way: *don't* bother animating if the delta is equivalent to less than 5% of the width of the image
const imageNeedsToBeAnimated = (this.calculateOneWayDelta() / this.getImageElem().clientWidth) >= 0.05
if (imageNeedsToBeAnimated) {
this.setUpAnimation()
} else {
// "Loaded", effectively
this.addClassBem('', 'static')
}
}
/**
* @override
*/
setUp () {
const imageUrl = this.createImageUrl()
const names = {
root: this.bem(),
imageWrapper: this.bem('image-wrapper')
}
// Includes base styles
this.getDomHelper().addStylesheet(`
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@200;400;600&display=swap');
@keyframes fadeIn {
from { opacity: 0; }
to { opacity: 1; }
}
/* Annoyingly, this particular icon is off-set slightly. The following rule corrects that. */
.stw-icon-play {
position: relative;
left: 2px;
}
.${names.root},
.${names.root} * {
box-sizing: border-box;
}
.${names.root} {
--line-height-base: 1.5;
--small-coef: 0.875;
--fs-base: calc(1rem * var(--small-coef));
--fs-5: calc(var(--fs-base) * 1.25);
--fs-1: calc(var(--fs-base) * 2.5);
--fs-sm: calc(var(--fs-base) * var(--small-coef));
--fw-lighter: 200;
--fw-normal: 400;
--fw-semibold: 600;
--spacer-base: 1rem;
--spacer-1: calc(var(--spacer-base) * 0.25);
--spacer-2: calc(var(--spacer-base) * 0.5);
--spacer-3: var(--spacer-base);
--color-white: #fff;
--color-black-semi: rgba(0,0,0,0.5);
--color-gray-light: #e2e2e5;
--color-accent-semi: rgba(225,35,84,0.9); /* #e12354 */
--filter-bg-obscure: blur(7px) opacity(0.85);
position: relative;
overflow: hidden;
/* Defaults: */
font-family: "Inter", sans-serif;
font-size: var(--fs-base);
font-weight: var(--fw-normal);
font-style: normal;
line-height: var(--line-height-base);
}
.${names.root} a {
font-weight: bold;
text-decoration: none;
color: inherit;
}
.${names.root} a:hover {
text-decoration: none;
}
.${this.bem('', 'static')} .${names.imageWrapper}::before {
content: "";
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
z-index: -1;
background-image: url("${imageUrl}");
background-size: cover;
background-repeat: no-repeat;
filter: var(--filter-bg-obscure);
}
.${names.imageWrapper},
.${names.imageWrapper} img {
height: 100%;
}
.${names.imageWrapper} {
position: relative;
z-index: +1;
text-align: center;
background-color: var(--color-gray-light);
background-image: url("${Images.SRC_STW_LOGO}");
background-position: center center;
background-repeat: no-repeat;
background-size: 60%;
}
`)
this.getRootElem().innerHTML = `
<div class="${names.imageWrapper}">
<img src="${imageUrl}" loading="lazy">
</div>
`
this.setComponent('overlay', new ViewerOverlay(this.appendChild('div'), `${names.root}-overlay`))
this.getComponent('overlay').setUp()
// Continue setting-up when we definitely have an image and can see what we're dealing with
this.getImageElem().addEventListener('load', this.continueSetUp.bind(this))
}
/**
* N.B. We no longer care about what the ID looks like: at the moment it's better to leave it up to the server to
* decide what to do
*
* @private
* @param {string} id
*/
setImageViewerId (id) {
this.imageViewerId = id
}
/**
* @private
* @param {string} url
* @throws {Error} If the image base-URL is invalid
*/
setImagesBaseUrl (url) {
if (!Validators.nonEmptyString(url)) {
throw new Error('The image base-URL is invalid')
}
this.imagesBaseUrl = url
}
/**
* @private
* @returns {string}
*/
createImageUrl () {
const imageViewerId = this.imageViewerId
const imagePath = imageViewerId.indexOf('://') === -1
? ('/' + imageViewerId)
: '/show-offsite?url=' + encodeURIComponent(imageViewerId)
return this.imagesBaseUrl + imagePath
}
/**
* @private
* @param {string} nameOfCallbackToProcessData
* @returns {string}
*/
createImageMetadataUrl (nameOfCallbackToProcessData) {
return `${this.imagesBaseUrl}/${this.imageViewerId}/meta?callback=` + encodeURIComponent(nameOfCallbackToProcessData)
}
/**
* @private
* @returns {HTMLElement}
*/
getImageWrapperElem () {
return this.queryBem('image-wrapper')
}
/**
* @private
* @returns {HTMLImageElement}
*/
getImageElem () {
return this.getImageWrapperElem().querySelector('img')
}
}
const viewerBlockName = 'stw-wiv'
// const webcamImagesBaseUrl = 'http://images-service.dan.spongebob/images/webcams'
const webcamImagesBaseUrl = 'https://plum.powderblue.co.uk/images/webcams'
document.querySelectorAll(`.${viewerBlockName}`).forEach((/** @type {HTMLElement} */viewerRootElem) => {
const viewer = new Viewer(
viewerRootElem,
viewerBlockName,
viewerRootElem.dataset.stwImageId,
webcamImagesBaseUrl
)
viewer.setUp()
})
})(document)