75 lines
1.8 KiB
JavaScript
75 lines
1.8 KiB
JavaScript
import { api } from "../../scripts/api.js";
|
|
|
|
/**
|
|
* A list of images to display and their selected state.
|
|
*/
|
|
export class ImageList extends EventTarget {
|
|
|
|
constructor(comfyUrls) {
|
|
super();
|
|
|
|
this.items = [];
|
|
for (const url of comfyUrls) {
|
|
this.items.push({
|
|
url,
|
|
selected: false
|
|
})
|
|
}
|
|
}
|
|
|
|
get length() {
|
|
return this.items.length;
|
|
}
|
|
|
|
// Count selected items in the list.
|
|
get selectedCount() {
|
|
let count = 0;
|
|
for (const image of this.items) {
|
|
if (image.selected) {
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
// Get an array of the selected indexes.
|
|
get selectedIndexes() {
|
|
let result = []
|
|
for (let i = 0; i < this.items.length; i++) {
|
|
if (this.items[i].selected) {
|
|
result.push(i);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// True if the image at index is selected, otherwise false.
|
|
isSelected(index) {
|
|
return this.items[index].selected;
|
|
}
|
|
|
|
// Select or de-select the image at index.
|
|
select(index, value) {
|
|
this.items[index].selected = value;
|
|
this.dispatchEvent(new CustomEvent('image-select', {
|
|
detail: {
|
|
imageList: this,
|
|
index,
|
|
selected: value
|
|
}
|
|
}));
|
|
}
|
|
|
|
// Toggle selection of the image at index.
|
|
toggleSelect(index) {
|
|
this.select(index, !this.items[index].selected);
|
|
}
|
|
|
|
// Get the URL for the image at index.
|
|
getImageUrl (index) {
|
|
const url = this.items[index].url;
|
|
return api.apiURL(`/view?filename=${encodeURIComponent(url.filename)}&type=${url.type ?? "input"}&subfolder=${url.subfolder ?? ""}&r=${Math.random()}`);
|
|
}
|
|
|
|
}
|