Files
JaredTherriault-ComfyUI-JNodes/web/js/ImageDrawer/Sorting.js
T
JaredTherriault f0e9347640 Major video update, ConfigSettings improvements, js async cancellation, load optimizations
-Roll UiSettings into SettingsManager
-Account for ConfigSettings that are objects by loading default values then loading config value son top
-Set ConfigSetting values using JSON.stringify
-Separate flyout generation into generic function
-Add video settings flyout in subfolderexplorer contexts
-Add option to control slider range value label fractional digits
-Update default slider range values to reflect 1-100 values rather than 0-1
-Rename ImageDrawerContext description to tooltip
-Update 'flex' alignment to 'normal' in context toolbars
-Automatically cancel async js operations when switching contexts
-blur or unfocus toolbar buttons on click to avoid accidental clearing or refreshing
-Get setting_ModelCardAspectRatio value as number
-Implement method to avoid lazy loading of images
-Lay some groundwork for unimplemented cancellation requests for python tasks
-Separated image list and search variables and functions to a separate script, ImageListAndSearch
-Update extra networks toolbar buttons to have a bespoke class and use specific cursor styles
-Update ImageElements 'complete' to 'bComplete' to differentiate from the img property
-Add lastSeekTime property initialization to img element
-Don't run onload if image element is already bComplete
-Sort and execute search after image is loaded (if requested). This allows search and sort to work with all of the meta data in the image.
-Click videos to pause/play
-Double click videos to toggle fullscreen
-Mute videos based on video options
-Set video volume from video options flyout
-Use scrollwheel on video to seek forward or back (seek direction depends on video options)
-Update imports
2024-04-17 14:58:59 -07:00

193 lines
4.9 KiB
JavaScript

import { $el } from "/scripts/ui.js";
import { getImageListChildren, replaceImageListChildren } from "./ImageListAndSearch.js";
let SortingWidget;
let SortTypes;
export function initializeSortTypes() {
SortTypes = {
filenameAscending: new SortTypeFilename(true),
filenameDescending: new SortTypeFilename(false),
friendlyNameAscending: new SortTypeFriendlyName(true),
friendlyNameDescending: new SortTypeFriendlyName(false),
dateAscending: new SortTypeDate(true),
dateDescending: new SortTypeDate(false),
// width: "Width",
// height: "Height",
// size: "File Size",
// type: "Type",
}
}
export function getSortTypes() {
return SortTypes;
}
export function getCurrentSortTypeName() {
return SortingWidget.value;
}
export function getCurrentSortTypeObject() {
return getSortTypeObjectFromName(getCurrentSortTypeName());
}
export function sortWithCurrentType() {
getCurrentSortTypeObject()?.sortImageList();
}
export function getSortTypeObjectFromName(sortName, bIsAscending = undefined) {
const sortValues = Object.values(SortTypes);
let foundSortType;
for (const value of sortValues) {
// returns true if the name at least partially matched and bIsAscending is not specified,
// or is specified and matches the SortType instance
if (value.name.includes(sortName) && (bIsAscending == undefined || value.bIsAscending == bIsAscending)) {
foundSortType = value;
break;
}
}
if (foundSortType) {
return foundSortType;
} else {
console.error(`SortType with name '${foundSortType}' not found.`);
return null;
}
}
export function getSortTypeObjectFromClassType(classType, bIsAscending = undefined) {
const sortValues = Object.values(SortTypes);
let foundSortType;
for (const value of sortValues) {
// returns true if the name at least partially matched and bIsAscending is not specified,
// or is specified and matches the SortType instance
const valueType = value.constructor.name;
if (valueType == classType.name && (bIsAscending == undefined || value.bIsAscending == bIsAscending)) {
foundSortType = value;
break;
}
}
if (foundSortType) {
return foundSortType;
} else {
console.error(`SortType with name '${foundSortType}' not found.`);
return null;
}
}
export class SortType {
constructor(name, bIsAscending) {
this.name = `${name} ${bIsAscending ? "⬆️" : "⬇️"}`;
this.bIsAscending = bIsAscending;
}
sortImageList() {
let imageListChildren = this.getSortableChildren();
imageListChildren.sort(this.getSortingLambda());
replaceImageListChildren(imageListChildren);
}
getSortingLambda() { }
getSortableChildren() {
let imageListChildren = getImageListChildren();
return Array.from(imageListChildren);
}
}
export class SortTypeFilename extends SortType {
constructor(bIsAscending) {
super('Filename', bIsAscending)
}
getSortingLambda() {
return (a, b) => this.bIsAscending ? a.filename.localeCompare(b.filename) : b.filename.localeCompare(a.filename);
}
}
export class SortTypeFriendlyName extends SortType {
constructor(bIsAscending) {
super('Friendly Name', bIsAscending)
}
getSortingLambda() {
return (a, b) => this.bIsAscending ? a.friendlyName.localeCompare(b.friendlyName) : b.friendlyName.localeCompare(a.friendlyName);
}
}
export class SortTypeDate extends SortType {
constructor(bIsAscending) {
super('Date', bIsAscending)
}
getSortingLambda() {
return (a, b) => this.bIsAscending ? a.file_age - b.file_age : b.file_age - a.file_age;
}
}
// Selector
export function setOptionSelectedFromOptionName(option) {
SortingWidget.value = option;
onOptionSelected(option);
}
export function setOptionSelectedFromSortType(type, bIsAscending) {
const foundType = getSortTypeObjectFromClassType(type, bIsAscending);
setOptionSelectedFromOptionName(foundType.name);
}
export function onOptionSelected(option) {
const NewType = getSortTypeObjectFromName(option);
NewType.sortImageList();
}
export function setSortingOptionsFromSortTypeArray(inOptionArray) {
SortingWidget.replaceChildren();
for (const sortType of inOptionArray) {
for (let bool = 1; bool > -1; bool--) { // Ascending and descending
const sortObject = getSortTypeObjectFromClassType(sortType, bool);
if (sortObject) {
const option = document.createElement("option");
option.value = sortObject.name;
option.textContent = sortObject.name;
SortingWidget.appendChild(option);
}
}
}
}
export function setSortingOptionsFromStringArray(inOptionArray) {
SortingWidget.replaceChildren();
for (const optionKey in inOptionArray) {
const option = document.createElement("option");
option.value = optionKey;
option.textContent = optionKey;
SortingWidget.appendChild(option);
}
}
export function makeSortingWidget() {
SortingWidget = $el("select");
// Add an event listener for the "change" event
SortingWidget.addEventListener("change", async function() {
onOptionSelected(SortingWidget.value);
});
initializeSortTypes();
return SortingWidget;
}