nhmc.cleanup

This package contains a variety of utility functions that remove application elements when they’re no longer needed. It also contains a few objects that store references to these elements so that the cleanup functions know what they’re looking for.

General-purpose garbage

nhmc.cleanup.futureGarbage

Array

This is a standard JavaScript array that holds jQuery objects that might need removal later. It’s meant for use with the convenience function nhmc.cleanup.clearGarbage, which just calls .remove() on all elements of this array.

nhmc.cleanup.clearGarbage

Function

nhmc.cleanup.clearGarbage()

This calls .remove() on all elements of nhmc.cleanup.futureGarbage, which should be jQuery objects referring to elements on the page.

Map graphics

nhmc.cleanup.clearPathColors

Function

nhmc.cleanup.clearPathColors()

This function simply clears the color of every state and county path drawn on the map; more specifically, it sets the fill color of each of those paths to nhmc.config.defaultAttributes.fill.

Dialog boxes

nhmc.cleanup.activeDialogs

Array

Sometimes the Map Center interface makes use of jQuery UI dialog boxes to request or present additional information; for example, the Electoral College calculator uses them to allow users to split certain states’ electoral votes where that is possible.

nhmc.cleanup.activeDialogs is a standard JavaScript array that stores jQuery objects referring to these dialog boxes for easy reference later–primarily by nhmc.cleanup.closeDialogs.

nhmc.cleanup.closeDialogs

Function

nhmc.cleanup.closeDialogs()

This function just closes any dialogs stored in nhmc.cleanup.activeDialogs by calling .dialog("close") on each object therein.

Event handlers

nhmc.cleanup.clickHandlerTokens

Array

This is a standard JavaScript array that contains zero or more handles returned by dojo.connect. Dojo’s event handling for graphics objects (and other items in general, but we only use it for graphics objects) is powerful in that it doesn’t require DOM elements to exist as in jQuery–but it introduces a bit more complexity in that binding an event handler returns a handle that must be passed later to dojo.disconnect in order to unbind that handler.

This array specifically is meant to hold handles related to event handlers for click events on map areas. An example of its use from the Electoral College calculator:

var eventToken = nhmc.geo.usGeo['Nebraska'].statePath.connect(
    'onclick', nhmc.geo.usGeo['Nebraska'].statePath, nebraskaHandler
);
nhmc.cleanup.clickHandlerTokens.push(eventToken);

This is done to make it easier to unbind all such handlers at once when needed by using nhmc.cleanup.clearClickHandlers, described below.

nhmc.cleanup.clearClickHandlers

Function

nhmc.cleanup.clearClickHandlers()

This function simply goes through all handles stored in nhmc.cleanup.clickHandlerTokens and passes them to dojo.disconnect. It’s a handy way to unbind all click handlers at the same time when needed.

Charts

nhmc.cleanup.clearCharts

Function

nhmc.cleanup.clearCharts()

This function checks for the existence of the chart objects described in nhmc.charts; if any of those objects are found, they are destroyed using the Highcharts Chart.destroy method.

Everything

nhmc.cleanup.clearMap

Function

nhmc.cleanup.clearMap()

This calls all of the above cleanup functions and removes all tooltips using nhmc.tooltips.destroy and nhmc.tooltips.unbindHover. It’s a nuclear option for getting everything you can off of the map.