VistaBox Class
The VistaBox class is an abstract base class for all content types in VistaView. Extend this class to create custom content types like videos, maps, or interactive elements.
Overview
Section titled “Overview”VistaBox handles:
- Image sizing and scaling
- Zoom and pan transformations
- Lifecycle management (init, load, destroy)
- State management for width, height, and transformations
Use Case: Create custom content extensions by extending VistaBox. See Extensions for examples.
Public Properties
Section titled “Public Properties”element
Section titled “element”abstract element: HTMLImageElement | HTMLDivElementThe DOM element containing the content. Must be implemented by subclasses.
state: VistaImageState;Current state of the image including dimensions and transformations. Contains internal properties prefixed with _ (e.g., _width, _height, _transform).
pos: number;Position relative to the current image (0 = center, -1 = left, 1 = right).
index: number;Zero-based index in the gallery.
config
Section titled “config”config: VistaImgConfig;Image configuration containing src, alt, and srcSet.
origin
Section titled “origin”origin: VistaImgOrigin | undefined;Information about the origin element (thumbnail) in the DOM.
isReady
Section titled “isReady”isReady: boolean;Whether the content has finished loading.
thumb: HTMLDivElement | null;Thumbnail element used during transitions.
Public Methods
Section titled “Public Methods”constructor()
Section titled “constructor()”constructor(par: VistaImageParams)Creates a new VistaBox instance. Called by VistaView during initialization.
init()
Section titled “init()”async init(): Promise<void>Initializes the content. Override this to implement custom loading logic.
setSizes()
Section titled “setSizes()”setSizes(par?: { stableSize?: boolean; initDimension?: boolean }): voidCalculates and sets dimensions based on viewport and content size.
Parameters:
par.stableSize- Use cached dimensions without recalculationpar.initDimension- Calculate initial dimensions from origin element
prepareClose()
Section titled “prepareClose()”prepareClose(): voidPrepares the element for closing transition. Called before the lightbox closes.
cancelPendingLoad()
Section titled “cancelPendingLoad()”cancelPendingLoad(): voidCancels any in-progress image loading operations.
destroy()
Section titled “destroy()”destroy(): voidCleans up resources and removes event listeners. Called when the element is no longer needed.
cloneStyleFrom()
Section titled “cloneStyleFrom()”cloneStyleFrom(img: VistaBox, state?: VistaHiresTransitionOpt): voidCopies style and state from another VistaBox instance. Used during transitions to maintain visual continuity.
toObject()
Section titled “toObject()”toObject(): VistaImageCloneReturns a serializable representation of the current state. Used for the onContentChange event.
setInitialCenter()
Section titled “setInitialCenter()”setInitialCenter(center: { x: number; y: number }): voidSets the center point for zoom and pan operations.
onImageReady()
Section titled “onImageReady()”onImageReady(): voidOverride this method to perform actions when the content is ready.
animateZoom()
Section titled “animateZoom()”animateZoom(scaleFactor: number, center?: { x: number; y: number }): voidOverride to implement custom zoom animation logic.
scaleMove()
Section titled “scaleMove()”scaleMove(scaleFactor: number, center?: { x: number; y: number }, animate?: boolean): voidOverride to implement custom scale and movement logic.
momentumThrow()
Section titled “momentumThrow()”momentumThrow(par: { x: number; y: number }): () => voidOverride to implement momentum scrolling after a swipe gesture.
Protected Properties
Section titled “Protected Properties”The following protected properties are available when extending VistaBox:
protected initH: number = 0Initial height of the content based on thumbnail dimensions.
protected initW: number = 0Initial width of the content based on thumbnail dimensions.
protected fullH: number = 0Full height of the content when displayed in the lightbox.
protected fullW: number = 0Full width of the content when displayed in the lightbox.
protected maxW: number = 0Maximum allowed width for the content.
protected minW: number = 0Minimum allowed width for the content.
defaultWH
Section titled “defaultWH”protected defaultWH: number = 200Default width/height fallback value.
isZoomedIn
Section titled “isZoomedIn”protected isZoomedIn: boolean = falseWhether the content is currently zoomed in.
isCancelled
Section titled “isCancelled”protected isCancelled: boolean = falseWhether the loading operation has been cancelled.
isLoadedResolved
Section titled “isLoadedResolved”protected isLoadedResolved: ((val: boolean | PromiseLike<boolean>) => void) | null = nullResolver function for the isLoaded promise.
isLoadedRejected
Section titled “isLoadedRejected”protected isLoadedRejected: ((reason?: any) => void) | null = nullRejection function for the isLoaded promise.
isLoaded
Section titled “isLoaded”protected isLoaded: Promise<boolean>Promise that resolves when the content has finished loading.
replacement
Section titled “replacement”protected replacement: HTMLImageElement | null = nullPlaceholder element that replaces the original thumbnail in the DOM.
originalParent
Section titled “originalParent”protected originalParent: HTMLElement | null = nullReference to the original parent element of the thumbnail.
originalNextSibling
Section titled “originalNextSibling”protected originalNextSibling: ChildNode | null = nullReference to the next sibling of the original thumbnail for proper reinsertion.
originalStyle
Section titled “originalStyle”protected originalStyle = ''Original CSS text of the thumbnail element.
thumbImage
Section titled “thumbImage”protected thumbImage: HTMLImageElement | null = nullReference to the original thumbnail image element.
originRect
Section titled “originRect”protected originRect: { width: number; height: number; top: number; left: number }Bounding rectangle of the origin element.
fittedSize
Section titled “fittedSize”protected fittedSize: { width: number; height: number } | null = nullCalculated fitted size of the thumbnail image.
maxZoomLevel
Section titled “maxZoomLevel”protected maxZoomLevel: numberMaximum zoom level allowed for this content.
vistaView
Section titled “vistaView”protected vistaView: VistaViewReference to the parent VistaView instance.
transitionState
Section titled “transitionState”protected transitionState: VistaHiresTransitionOpt | null = nullState object for high-resolution transitions.
transitionShouldWait
Section titled “transitionShouldWait”protected transitionShouldWait: () => boolean = () => falseFunction that determines if transition should wait before starting.
initPointerCenter
Section titled “initPointerCenter”protected initPointerCenter = { x: 0, y: 0 }Initial center point for pointer interactions.
onScale
Section titled “onScale”protected onScale: (par: { vistaImage: VistaBox; scale: number; isMax: boolean; isMin: boolean;}) => voidCallback function invoked when content is scaled.
Protected Methods
Section titled “Protected Methods”The following protected methods are available when extending VistaBox:
createState()
Section titled “createState()”protected createState(): VistaImageStateCreates and returns a new state object with getters/setters that trigger DOM updates. Called in the constructor.
onLessThanMinWidthChange()
Section titled “onLessThanMinWidthChange()”protected onLessThanMinWidthChange(value: boolean): voidCalled when content width falls below minimum. Sets opacity to 0.5 when true.
onTranslateChange()
Section titled “onTranslateChange()”protected onTranslateChange(value: { x: number; y: number }): voidUpdates the CSS translate property when state.translate changes.
onTransformChange()
Section titled “onTransformChange()”protected onTransformChange(value: { x: number; y: number; scale: number }): voidUpdates the CSS transform property when state.transform changes.
onWidthChange()
Section titled “onWidthChange()”protected onWidthChange(value: number): voidUpdates the CSS width property when state.width changes.
onHeightChange()
Section titled “onHeightChange()”protected onHeightChange(value: number): voidUpdates the CSS height property when state.height changes.
getFullSizeDim()
Section titled “getFullSizeDim()”protected getFullSizeDim(): { width: number; height: number }Calculates the full size dimensions based on thumbnail aspect ratio and viewport size.
Returns: Object containing calculated width and height.
normalize()
Section titled “normalize()”protected normalize(): voidResets content to its default state with no zoom or translation. Sets transform and translate to zero, and dimensions to full size.
getFromParsedSrcSet()
Section titled “getFromParsedSrcSet()”protected getFromParsedSrcSet(targetWidth: number): string | nullSelects the most appropriate source from the parsed srcSet based on target width and device pixel ratio.
Parameters:
targetWidth- The desired width in pixels
Returns: The selected source URL, or null if no srcSet is available.
setFinalTransform()
Section titled “setFinalTransform()”setFinalTransform(par?: { propagateEvent?: boolean }): { close: boolean; cancel: () => void }Finalizes the current transform state by converting temporary transforms into permanent translate and size values. Called at the end of pan/zoom operations.
Parameters:
par.propagateEvent- Whether to trigger onContentChange events (default: true)
Returns: Object with close flag and cancel function.
Related
Section titled “Related”- VistaImage - Image implementation extending VistaBox
- Extensions - Creating custom content types
- VistaImageParams - Constructor parameters