-
-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add alchemy-picture-thumbnail component
- Loading branch information
Showing
23 changed files
with
177 additions
and
137 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Alchemy | ||
module Admin | ||
class PictureThumbnail < ViewComponent::Base | ||
attr_reader :picture, :url | ||
|
||
def initialize(picture, size: :medium) | ||
@picture = picture | ||
@url = picture.thumbnail_url(size: preview_size(size)) | ||
end | ||
|
||
def call | ||
content_tag("alchemy-picture-thumbnail") do | ||
image_tag(url, alt: picture.name) | ||
end | ||
end | ||
|
||
private | ||
|
||
def preview_size(size) | ||
Alchemy::Picture::THUMBNAIL_SIZES.fetch( | ||
size, | ||
Alchemy::Picture::THUMBNAIL_SIZES[:medium] | ||
) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
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
118 changes: 118 additions & 0 deletions
118
app/javascript/alchemy_admin/components/picture_thumbnail.js
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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import Spinner from "alchemy_admin/spinner" | ||
|
||
const MAX_RETRIES = 10 | ||
const WAIT_TIME = 1000 | ||
const OFFSET_INCREMENT = 100 | ||
|
||
class PictureThumbnail extends HTMLElement { | ||
#retries = MAX_RETRIES | ||
#retryOffset = 0 | ||
#started = false | ||
|
||
constructor() { | ||
super() | ||
this.observer = new MutationObserver(this.handleMutation.bind(this)) | ||
this.classList.add("thumbnail_background") | ||
this.spinner = new Spinner(this.spinnerSize) | ||
} | ||
|
||
connectedCallback() { | ||
if (this.image && !this.image.complete) { | ||
this.start() | ||
} | ||
this.observer.observe(this, { | ||
subtree: true, | ||
childList: true, | ||
attributes: true, | ||
attributeOldValue: true, | ||
attributeFilter: ["src"] | ||
}) | ||
} | ||
|
||
disconnectedCallback() { | ||
this.observer.disconnect() | ||
this.reset() | ||
} | ||
|
||
handleMutation(mutations) { | ||
mutations.forEach((mutation) => { | ||
const hasImage = | ||
mutation.type == "childList" && | ||
Array.from(mutation.addedNodes).some((node) => node.nodeName === "IMG") | ||
const sourceChanged = | ||
mutation.type == "attributes" && | ||
mutation.oldValue !== mutation.target.src | ||
if (hasImage || sourceChanged) { | ||
this.start() | ||
} | ||
}) | ||
} | ||
|
||
handleEvent(event) { | ||
switch (event.type) { | ||
case "load": | ||
this.showImage() | ||
break | ||
case "error": | ||
this.retry() | ||
break | ||
} | ||
} | ||
|
||
start() { | ||
if (this.#started) return | ||
|
||
this.#started = true | ||
this.image.classList.add("hidden") | ||
this.spinner.spin(this) | ||
this.image.addEventListener("load", this) | ||
this.image.addEventListener("error", this) | ||
} | ||
|
||
showImage() { | ||
this.#started = false | ||
this.image.classList.remove("hidden") | ||
this.classList.add("loaded") | ||
this.spinner.stop() | ||
this.reset() | ||
} | ||
|
||
retry() { | ||
if (this.#retries > 0) { | ||
this.#retries-- | ||
setTimeout(() => { | ||
this.image.src = this.image.src | ||
}, WAIT_TIME + this.#retryOffset) | ||
this.#retryOffset += OFFSET_INCREMENT | ||
} else { | ||
this.showError() | ||
} | ||
} | ||
|
||
showError() { | ||
const message = `Could not load "${this.image.src}"` | ||
console.error(message) | ||
this.innerHTML = `<span class="icon error ri-file-damage-line" title="${message}" />` | ||
this.reset() | ||
} | ||
|
||
reset() { | ||
this.#started = false | ||
this.#retries = MAX_RETRIES | ||
|
||
if (this.image) { | ||
this.image.removeEventListener("load", this) | ||
this.image.removeEventListener("error", this) | ||
} | ||
} | ||
|
||
get spinnerSize() { | ||
return this.getAttribute("spinner-size") || "small" | ||
} | ||
|
||
get image() { | ||
return this.querySelector("img") | ||
} | ||
} | ||
|
||
customElements.define("alchemy-picture-thumbnail", PictureThumbnail) |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.