Improved upload scripts

This commit is contained in:
Eduard Urbach 2018-03-03 18:29:39 +01:00
parent 634fed8e87
commit 58b54fe908
5 changed files with 42 additions and 17 deletions

View File

@ -11,6 +11,12 @@ component AvatarNoLink(user *arn.User)
else else
SVGAvatar(user) SVGAvatar(user)
component ProfileImage(user *arn.User)
if user.HasAvatar()
img.profile-image.lazy(data-src=user.LargeAvatar(), data-webp="true", alt="Profile image")
else
SVGProfileImage(user)
component SVGAvatar(user *arn.User) component SVGAvatar(user *arn.User)
svg.user-image(viewBox="0 0 50 50") svg.user-image(viewBox="0 0 50 50")
circle.head(cx="25", cy="19", r="10") circle.head(cx="25", cy="19", r="10")
@ -19,4 +25,9 @@ component SVGAvatar(user *arn.User)
if len(user.Nick) <= 6 if len(user.Nick) <= 6
text.svg-nick(x="25", y="44", text-anchor="middle")= user.Nick text.svg-nick(x="25", y="44", text-anchor="middle")= user.Nick
else else
text.svg-nick(x="25", y="44", text-anchor="middle")= user.Nick[:6] text.svg-nick(x="25", y="44", text-anchor="middle")= user.Nick[:6]
component SVGProfileImage(user *arn.User)
svg.profile-image(viewBox="0 0 50 50", alt="Profile image")
circle.head(cx="25", cy="19", r="10")
circle.body(cx="25", cy="50", r="20")

View File

@ -30,7 +30,7 @@ component InputSelection(id string, value string, label string, placeholder stri
each option in options each option in options
option(value=option.Value)= option.Label option(value=option.Value)= option.Label
component InputImage(id string, label string, src string) component InputImage(id string, label string)
.widget-section .widget-section
label(for=id)= label + ":" label(for=id)= label + ":"
button.action(data-action="selectFile", data-trigger="click", data-preview-image-id=id + "-preview") button.action(data-action="selectFile", data-trigger="click", data-preview-image-id=id + "-preview")

View File

@ -1,7 +0,0 @@
component ProfileImage(user *arn.User)
if user.HasAvatar()
img.profile-image.lazy(data-src=user.LargeAvatar(), data-webp="true", alt="Profile image")
else
svg.profile-image(viewBox="0 0 50 50", alt="Profile image")
circle.head(cx="25", cy="19", r="10")
circle.body(cx="25", cy="50", r="20")

View File

@ -51,10 +51,16 @@ component SettingsPersonal(user *arn.User)
//- File upload //- File upload
if user.Settings().Avatar.Source == "FileSystem" if user.Settings().Avatar.Source == "FileSystem"
InputImage("avatar-input", "File", user.LargeAvatar()) InputImage("avatar-input", "File")
.profile-image-container.avatar-preview .profile-image-container.avatar-preview
img.profile-image.mountable(id="avatar-input-preview", src=user.LargeAvatar(), alt="Image preview") if user.HasAvatar()
img#avatar-input-preview.profile-image.mountable(src=user.LargeAvatar(), alt="Profile image")
else
img#avatar-input-preview.profile-image.hidden(src=user.LargeAvatar(), alt="Profile image")
#avatar-input-preview-svg
SVGProfileImage(user)
component SettingsNotifications(user *arn.User) component SettingsNotifications(user *arn.User)
SettingsTabs SettingsTabs

View File

@ -21,14 +21,18 @@ function previewImage(file: File, preview: HTMLImageElement) {
let reader = new FileReader() let reader = new FileReader()
reader.onloadend = () => { reader.onloadend = () => {
let svgPreview = document.getElementById("avatar-input-preview-svg") as HTMLImageElement
if(svgPreview) {
svgPreview.classList.add("hidden")
}
preview.classList.remove("hidden") preview.classList.remove("hidden")
preview.src = reader.result preview.src = reader.result
} }
if(file) { if(file) {
reader.readAsDataURL(file) reader.readAsDataURL(file)
} else {
preview.classList.add("hidden")
} }
} }
@ -49,10 +53,7 @@ function uploadFile(file: File, endpoint: string, arn: AnimeNotifier) {
}) })
let newURL = await response.text() let newURL = await response.text()
let sidebar = document.getElementById("sidebar") updateSideBarAvatar(newURL)
let userImage = sidebar.getElementsByClassName("user-image")[0] as HTMLImageElement
userImage.dataset.src = newURL
userImage["became visible"]()
if(response.ok) { if(response.ok) {
arn.statusMessage.showInfo("Successfully uploaded your new avatar.") arn.statusMessage.showInfo("Successfully uploaded your new avatar.")
@ -62,4 +63,18 @@ function uploadFile(file: File, endpoint: string, arn: AnimeNotifier) {
} }
reader.readAsArrayBuffer(file) reader.readAsArrayBuffer(file)
}
// Update sidebar avatar
function updateSideBarAvatar(url: string) {
let sidebar = document.getElementById("sidebar")
let userImage = sidebar.getElementsByClassName("user-image")[0] as HTMLImageElement
let lazyLoad = userImage["became visible"]
if(lazyLoad) {
userImage.dataset.src = url
lazyLoad()
} else {
location.reload()
}
} }