VistaView Class
The VistaView class is the core controller that manages the entire lifecycle of the lightbox viewer. Each instance represents a lightbox that can be opened, navigated, and controlled programmatically.
Overview
Section titled “Overview”VistaView orchestrates all aspects of the lightbox experience:
- Instance Management: Ensures only one lightbox is active at a time via
GlobalVistaState - Image Navigation: Handles next/prev navigation with smooth transitions and rapid-swap optimization
- UI Controls: Manages zoom, close, and navigation buttons
- Event Coordination: Integrates pointer events, keyboard shortcuts, and extension hooks
- State Management: Tracks current index, zoom level, and UI activation state
- Extension Integration: Calls extension lifecycle hooks at key moments
Constructor
Section titled “Constructor”new VistaView(elements: string | VistaImgConfig[], options?: VistaOpt)Creates a new VistaView instance.
Parameters:
elements: Either a CSS selector string or an array of image configurationsoptions: Optional configuration object (see VistaOpt)
Example:
// Using CSS selectorconst viewer = new VistaView('.gallery-item', { maxZoomLevel: 3, preloads: 2,});
// Using configuration arrayconst viewer = new VistaView([ { src: 'image1.jpg', alt: 'First image' }, { src: 'image2.jpg', alt: 'Second image' },]);Public Properties
Section titled “Public Properties”options
Section titled “options”options: VistaOpt;The merged configuration options for this instance. Combines user-provided options with DefaultOptions.
See VistaOpt for complete type definition.
state: VistaState;The current state manager containing:
currentIndex: Current image indexelmLength: Total number of imageszoomedIn: Whether the current image is zoomedextensions: Set of registered extensionschildren: Current DOM elements and VistaBox instancesabortController: For canceling transitions
See VistaState for complete documentation.
imageContainer
Section titled “imageContainer”imageContainer: HTMLElement | null;Reference to the DOM element containing the image elements. null when the lightbox is closed.
externalPointerListener
Section titled “externalPointerListener”externalPointerListener: ((e: VistaExternalPointerListenerArgs) => void)[]Array of external pointer event listeners registered via registerPointerListener(). Used by extensions to hook into pointer events.
See VistaExternalPointerListenerArgs for the event argument type.
Public Methods
Section titled “Public Methods”open()
Section titled “open()”open(startIndex?: number): voidOpens the lightbox at the specified image index.
Parameters:
startIndex: The index of the image to display first (default:0)
Behavior:
- Prevents opening if another VistaView instance is already open
- Prevents body scrolling
- Creates and injects the lightbox DOM structure
- Sets up event handlers and keyboard listeners
- Calls
onOpenevent callback and extension hooks - Supports negative indices and values beyond the array length (wraps around)
Example:
const viewer = new VistaView('.gallery-item');viewer.open(2); // Opens at the third imageclose()
Section titled “close()”close(animate?: boolean): Promise<void>Closes the lightbox and cleans up resources.
Parameters:
animate: Whether to animate the close transition (default:true)
Behavior:
- Waits for close animation to complete (if
animateistrue) - Removes DOM elements and event handlers
- Destroys all VistaBox instances
- Restores body scrolling
- Calls
onCloseevent callback and extension hooks - Resets
GlobalVistaState
Example:
// Close with animationawait viewer.close();
// Close immediately without animationawait viewer.close(false);destroy()
Section titled “destroy()”destroy(): voidCompletely destroys the VistaView instance and removes all event listeners.
Behavior:
- Closes the lightbox without animation
- Removes click listeners from trigger elements
- Clears external pointer listeners
- Cannot be reopened after destruction
Example:
viewer.destroy();// viewer is now unusablenext()
Section titled “next()”next(): voidNavigates to the next image in the sequence.
Behavior:
- Wraps around to the first image after the last one
- Triggers transition animation
- Calls
onImageViewevent callback
Example:
viewer.next();prev()
Section titled “prev()”prev(): voidNavigates to the previous image in the sequence.
Behavior:
- Wraps around to the last image when at the first one
- Triggers transition animation
- Calls
onImageViewevent callback
Example:
viewer.prev();view()
Section titled “view()”view(index: number, via?: { next: boolean; prev: boolean }): voidJumps to a specific image by index.
Parameters:
index: The target image indexvia: Optional object indicating navigation direction (used internally)
Behavior:
- Supports negative indices and values beyond array length (wraps around)
- Aborts any in-progress transition
- Calls
onImageViewevent callback - Handles rapid navigation with optimized transitions
Example:
viewer.view(5); // Jump to 6th imageviewer.view(-1); // Jump to last imagezoomIn()
Section titled “zoomIn()”zoomIn(): voidZooms in on the current image by a factor of 1.68x.
Behavior:
- Respects
maxZoomLeveloption - Throttled to prevent excessive calls (222ms)
- Disables zoom-in button when at maximum zoom
- Can be disabled via
deactivateUi(['zoomIn'])
Example:
viewer.zoomIn();zoomOut()
Section titled “zoomOut()”zoomOut(): voidZooms out of the current image by a factor of 0.68x.
Behavior:
- Returns to minimum zoom (fit to viewport)
- Throttled to prevent excessive calls (222ms)
- Disables zoom-out button when at minimum zoom
- Can be disabled via
deactivateUi(['zoomOut'])
Example:
viewer.zoomOut();getCurrentIndex()
Section titled “getCurrentIndex()”getCurrentIndex(): numberReturns the current image index.
Returns: The zero-based index of the currently displayed image, or -1 if closed.
Example:
const index = viewer.getCurrentIndex();console.log(`Viewing image ${index + 1}`);reset()
Section titled “reset()”reset(): voidRecalculates the image list and reattaches click listeners.
Behavior:
- Queries the DOM for elements matching the selector (if using string selector)
- Updates
state.elmLength - Removes and re-adds click event listeners
Use Case: Call this if the DOM has changed (elements added/removed) and you need to update the gallery.
Example:
// After dynamically adding more images to the pagedocument.querySelector('.gallery').innerHTML += '<a href="new.jpg" class="gallery-item">New</a>';viewer.reset();qs<T extends HTMLElement>(selector: string): T | nullQueries for an element within the lightbox root element.
Parameters:
selector: CSS selector string
Returns: The first matching element or null
Example:
const closeButton = viewer.qs<HTMLButtonElement>('.vvw-close');qsOrigin()
Section titled “qsOrigin()”qsOrigin<T extends NodeListOf<HTMLElement>>(selector: string): TQueries for elements in the document (not limited to lightbox root).
Parameters:
selector: CSS selector string
Returns: NodeList of matching elements
Example:
const galleryItems = viewer.qsOrigin<NodeListOf<HTMLAnchorElement>>('.gallery-item');registerPointerListener()
Section titled “registerPointerListener()”registerPointerListener(listener: (e: VistaExternalPointerListenerArgs) => void): voidRegisters an external pointer event listener.
Parameters:
listener: Function that receives VistaExternalPointerListenerArgs pointer event data
Use Case: Extensions can use this to respond to pointer events without directly managing event handlers.
Example:
viewer.registerPointerListener((e) => { console.log('Pointer event:', e.event, e.pointer); console.log('Active pointers:', e.pointers.length);});deactivateUi()
Section titled “deactivateUi()”deactivateUi(names: string[], requestBy?: VistaBox): voidTemporarily disables UI controls.
Parameters:
names: Array of control names to disable (e.g.,['zoomIn', 'zoomOut'])requestBy: Optional VistaBox instance making the request
Behavior:
- Sets
disabledattribute on specified buttons - Notifies extensions via
onDeactivateUihook - Automatically reactivated before navigation or transitions
Example:
// Disable zoom during a custom animationviewer.deactivateUi(['zoomIn', 'zoomOut']);Related
Section titled “Related”- Main Function - The
vistaView()function that creates VistaView instances - VistaOpt - Configuration options type
- Event Callbacks - Events fired during lifecycle
- Lifecycle Functions - Customizable lifecycle hooks