ViewerObserver

The ViewerObserver simplifies event handling when working with multiple Viewer instances on one page. This is useful when creating experiences that might have a floorplan viewer and a siteplan viewer side-by-side.

You do not need to use a ViewerObserver if you are only working with a single Viewer instance.

Construction/Destruction

new locatrix.plans.ViewerObserver(viewers)

Creates a new ViewerObserver bound to the given viewers. You cannot add/remove viewers to the observer after construction, so be sure to create your viewers before creating the ViewerObserver.

viewerObserver.dispose()

Disposes of the ViewerObserver, removing its event listeners from the viewers that it is bound to.

Events

viewerObserver.on(eventType, handler)

Adds an event handler for the given eventType, which must be one of the events supported by the ViewerObserver. The handler will be called next time the given event type is emitted. You may register the same function as a handler for multiple event types, and you may register multiple handlers for the same event type.

Event handlers are called with a single event argument that contains details about the event. There will always be en event.type property that you can use to determine the event type, as well as other properties specific to each event type.

viewerObserver.off(eventType, handler)

Removes an event handler previously added by viewerObserver.on(). If the event handler has already been removed or the ViewerObserver has been disposed of, this method does nothing.

Event: load

Emitted after all plans being observed have successfully loaded. Some plans may already be visible to the user, but all will be loaded. Handling this event allows you to safely perform logic that needs multiple viewers to be loaded.

For example, this snippet ensures that the site plan viewer always has the right floor plan highlighted and is displaying evac paths associated with the floor plan.

var sitePlanViewer = new locatrix.plans.Viewer(siteDiv);
var floorPlanViewer = new locatrix.plans.Viewer(floorDiv);
var observer = new locatrix.plans.ViewerObserver([sitePlanViewer, floorPlanViewer]);

observer.on('load', e => {
  const floorPlan = floorPlanViewer.getLoadedPlan();
  const sitePlan = sitePlanViewer.getLoadedPlan();

  sitePlanViewer.setHighlightedPlanFootprints([floorPlan.getPlanCode()]);
  sitePlanViewer.setEnabledEvacPaths(sitePlan.getEvacPathsFromFloorPlan(floorPlan));
});

sitePlanViewer.loadPlan(SITE_PLAN_VIEWER_TOKEN);
floorPlanViewer.loadPlan(FLOOR_PLAN_VIEWER_TOKEN);

Event: beforeUnload

Emitted if all viewers being observed have plans loaded, and one viewer is about to unload. You may still query for plan information across all viewers in this event handler, however it is unsafe to do so after this event handler returns.

Event: unload

Emitted if all viewers being observed have plans loaded, and one viewer unloads.

Event: error

Emitted on any fatal errors within any Viewers being observed that require a page reload.