-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(core): better elektra avatar loading (#1509)
Co-authored-by: Esther Schmitz <[email protected]>
- Loading branch information
Showing
7 changed files
with
63 additions
and
48 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,30 @@ | ||
window.loadAvatar = function ({ avatarUrl, elementId }) { | ||
document.addEventListener("DOMContentLoaded", function () { | ||
var img = new Image() | ||
// handle the case where the image does not exist | ||
// try to load the image | ||
img.onload = function () { | ||
// if the image exists, set the src attribute of the image element | ||
// console.info(`Loading avatar image from ${avatarUrl} into element with id ${elementId}.`) | ||
document.getElementById(elementId).src = avatarUrl | ||
} | ||
img.onerror = function () { | ||
// if the image does not exist, use the default image | ||
console.warn(`Avatar image ${avatarUrl} does not exist, using default image.`) | ||
} | ||
// set the src attribute to the image url | ||
img.src = avatarUrl | ||
const loadImage = new Promise((resolve, reject) => { | ||
var img = new Image() | ||
// handle the case where the image does not exist | ||
// try to load the image | ||
img.onload = function () { | ||
// if the image exists, set the src attribute of the image element | ||
// console.info(`Loading avatar image from ${avatarUrl} into element with id ${elementId}.`) | ||
document.getElementById(elementId).insertAdjacentHTML("beforeend", `<img src="${avatarUrl}" />`) | ||
resolve() | ||
} | ||
img.onerror = function () { | ||
// if the image does not exist, use the default image | ||
console.warn(`Avatar image ${avatarUrl} does not exist, using default image.`) | ||
reject() | ||
} | ||
// set the src attribute to the image url | ||
img.src = avatarUrl | ||
}) | ||
|
||
loadImage | ||
.then(() => { | ||
console.info(`Successfully loaded avatar from ${avatarUrl}.`) | ||
}) | ||
.catch(() => { | ||
console.warn(`Failed to load avatar image.`) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.