-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathimage-processing-checker.js
More file actions
111 lines (96 loc) · 3.24 KB
/
image-processing-checker.js
File metadata and controls
111 lines (96 loc) · 3.24 KB
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
'use strict'
window.GOVUK = window.GOVUK || {}
window.GOVUK.Modules = window.GOVUK.Modules || {}
;(function (Modules) {
class ImageProcessingChecker {
static TIMEOUT_DURATION = 1000
static MAX_ATTEMPTS = 5
constructor($root) {
this.$root = $root
this.imageLink = this.$root.dataset.imageLink
this.variant = this.$root.dataset.variant || 'original'
this.timeoutDuration =
parseInt(this.$root.dataset.timeoutDuration, 10) ||
ImageProcessingChecker.TIMEOUT_DURATION
this.maxAttempts =
parseInt(this.$root.dataset.maxAttempts) ||
ImageProcessingChecker.MAX_ATTEMPTS
const imagePreview = this.$root.querySelector('.js-image-preview')
const imagePreviewFailure = this.$root.querySelector(
'.js-image-preview-failure'
)
if (imagePreview) {
this.imagePreview = document.importNode(
imagePreview.content,
true
).firstElementChild
}
if (imagePreviewFailure) {
this.imagePreviewFailure = document.importNode(
imagePreviewFailure.content,
true
).firstElementChild
}
this.imageStatus = this.$root.querySelector('.js-image-processing-status')
// the image was not already loaded on page load
if (this.imageStatus && !this.imageStatus.querySelector('img')) {
this.checkImageStatus()
}
}
checkImageStatus(attempts = 0) {
if (attempts !== this.maxAttempts) {
fetch(this.imageLink)
.then((response) => {
if (response.ok) {
return response.clone().json()
} else {
throw new Error(
`Supplied URL ${this.imageLink} returned ${response.status}`
)
}
})
// eslint-disable-next-line camelcase
.then(({ image_data: { all_assets_uploaded, assets } }) =>
// eslint-disable-next-line camelcase
!all_assets_uploaded
? setTimeout(
() => this.checkImageStatus(attempts + 1),
this.timeoutDuration * Math.pow(2, attempts + 1)
)
: this.handleSuccess(assets)
)
.catch((error) => this.handleFailure(error.message))
} else {
this.handleFailure(`Image at ${this.imageLink} was not ready in time`)
}
}
updateImageStatus(replacementEl) {
if (replacementEl) {
this.$root.replaceChild(replacementEl, this.imageStatus)
} else {
this.imageStatus.remove()
}
}
handleFailure(error) {
console.error(error)
this.updateImageStatus(this.imagePreviewFailure)
}
handleSuccess(assets) {
if (!this.imagePreview) {
this.updateImageStatus()
} else {
const imgElement =
this.imagePreview.querySelector('img') ||
(this.imagePreview.tagName === 'IMG' && this.imagePreview)
if (imgElement) {
const previewAsset = assets.find(
({ variant }) => variant === this.variant
)
imgElement.src = previewAsset.url
}
this.updateImageStatus(this.imagePreview)
}
}
}
Modules.ImageProcessingChecker = ImageProcessingChecker
})(window.GOVUK.Modules)