A quick post about events in the viewer. It's pretty straightforward to listen to events, all you need is knowing the eventId you are looking for. For example the following code illustrates how to set and remove an event listener for the selection changed:
The problem is that unfortunately the documentation of the JavaScript API doesn't expose a list of all available events you can subscribe to. No worries, your humble servant is here to the rescue and he's been skimming the viewer3d.js file to grab the list of all events for you:
Autodesk.Viewing.ANIMATION_READY_EVENT
Autodesk.Viewing.CAMERA_CHANGE_EVENT
Autodesk.Viewing.CUTPLANES_CHANGE_EVENT
Autodesk.Viewing.ESCAPE_EVENT
Autodesk.Viewing.EXPLODE_CHANGE_EVENT
Autodesk.Viewing.FULLSCREEN_MODE_EVENT
Autodesk.Viewing.GEOMETRY_LOADED_EVENT
Autodesk.Viewing.HIDE_EVENT
Autodesk.Viewing.HIGHLIGHT_EVENT
Autodesk.Viewing.ISOLATE_EVENT
Autodesk.Viewing.LAYER_VISIBILITY_CHANGED_EVENT
Autodesk.Viewing.MODEL_ROOT_LOADED_EVENT
Autodesk.Viewing.NAVIGATION_MODE_CHANGED_EVENT
Autodesk.Viewing.OBJECT_TREE_CREATED_EVENT
Autodesk.Viewing.OBJECT_TREE_UNAVAILABLE_EVENT
Autodesk.Viewing.PROGRESS_UPDATE_EVENT
Autodesk.Viewing.RENDER_OPTION_CHANGED_EVENT
Autodesk.Viewing.RESET_EVENT
Autodesk.Viewing.SELECTION_CHANGED_EVENT
Autodesk.Viewing.SHOW_EVENT
Autodesk.Viewing.TOOLBAR_CREATED_EVENT
Autodesk.Viewing.TOOL_CHANGE_EVENT
Autodesk.Viewing.VIEWER_RESIZE_EVENT
Autodesk.Viewing.VIEWER_STATE_RESTORED_EVENT
Autodesk.Viewing.VIEWER_UNINITIALIZED
I also went a bit further and created an extension which allows to set and remove events dynamically while outputting arguments in a logger window.
I took the opportunity to try a library called jquery-jsonview, pretty slick to use and handy it lets you dump a json object into a div and nicely formats it.
One limitation I had when stringifying the json to display it in the jsonview is that some of the events are circular structure, hence throwing exception when passed to the built-in JSON.stringify method. No worries once again because no matter what issue you have, there has to be a lib that can help: ended up using circular-json which does as well a pretty good job at handling circular json objects.
Here is the complete code the for the extension and a live version that can be tried from here. Some of the events cannot be tested from the EventWatcher due to timing or other constraints, for example TOOLBAR_CREATED or GEOMETRY_LOADED will be fired before the event extension is even loaded.
Comments
You can follow this conversation by subscribing to the comment feed for this post.