Source: maps/places/LabelPlacesButton.js


/**
 * @class
 * Manages the button that controls the display of LIMBs.
 * Specifically, this class is responsible for changing the button graphic to reflect the current state.
 *
 * @constructor
 * @param {lucid.maps.places.LabelPlacesButtonOptions} options  Configuration for the button.
 */
lucid.maps.places.LabelPlacesButton = function( options )
{
	var thisButton = this;
	
	// The state controls what happens when the button is clicked
	// and is reflected by the icon image used for the button graphic.
	var state;
	
	// Some states revert to idle after a given timeout.
	var revertToIdleTimer;
	var revertToIdleTimeout = 5000;
	
	var buttonElement;
	var graphics;
	
	
	// This initialise function is called at the end of the constructor.
	function init()
	{
		buttonElement = jQuery( options.buttonDiv );
		buttonElement.click( handleClick );
		
		graphics = jQuery( '.graphics', buttonElement );
		
		thisButton.setIdle();
	}
	
	
	function handleClick()
	{
		google.maps.event.trigger( thisButton, 'click' );
	}
	
	/**
	 * Make the button visible, after a call to hide.
	 */
	this.show = function()
	{
		buttonElement.show();
	};
	
	/**
	 * Hide the button graphic.
	 */
	this.hide = function()
	{
		buttonElement.hide();
	};
	
	/**
	 * Set this button into the "idle" state.
	 * @see The lucid.maps.places.LabelPlacesState enumeration.
	 */
	this.setIdle = function()
	{
		setState( lucid.maps.places.LabelPlacesState.IDLE );
	};
	
	/**
	 * Set this button into the "pending results" state.
	 * @see The lucid.maps.places.LabelPlacesState enumeration.
	 */
	this.setPending = function()
	{
		setState( lucid.maps.places.LabelPlacesState.PENDING );
	};
	
	/**
	 * Set this button into the "active" state.
	 * @see The lucid.maps.places.LabelPlacesState enumeration.
	 */
	this.setActive = function()
	{
		setState( lucid.maps.places.LabelPlacesState.ACTIVE );
	};
	
	/**
	 * Set this button into the "error encountered" state.
	 * @see The lucid.maps.places.LabelPlacesState enumeration.
	 */
	this.setError = function()
	{
		setState( lucid.maps.places.LabelPlacesState.ERROR );
		setRevertToIdle();
	};
	
	/**
	 * Set this button into the "no results found" state.
	 * @see The lucid.maps.places.LabelPlacesState enumeration.
	 */
	this.setNoResults = function()
	{
		setState( lucid.maps.places.LabelPlacesState.NO_RESULTS );
		setRevertToIdle();
	};
	
	function setState( newState )
	{
		clearRevertToIdle();
		
		if (state)
		{
			graphics.removeClass( state );
		}
		
		state = newState;
		
		graphics.addClass( state );
	};
	
	/**
	 * @return {lucid.maps.places.LabelPlacesState}  The current state of this button.
	 */
	this.getState = function()
	{
		return state;
	};
	
	function setRevertToIdle()
	{
		clearRevertToIdle();
		revertToIdleTimer = window.setTimeout( revertToIdle, revertToIdleTimeout );
	}
	
	function clearRevertToIdle()
	{
		if (revertToIdleTimer)
		{
			window.clearTimeout( revertToIdleTimer );
			revertToIdleTimer = null;
		}
	}
	
	function revertToIdle()
	{
		thisButton.setIdle();
	}
	
	
	init();
};

/**
 * Indicates when the button is clicked.
 *
 * @event lucid.maps.places.LabelPlacesButton#click
 * @type {object}
 */

/**
 * An enumeration of states for the process-lifecycle of labelling places.
 * The {@link lucid.maps.places.LabelPlacesButton} will adjust its appearance to reflect the current state.
 * @enum
 * @default IDLE
 */
lucid.maps.places.LabelPlacesState =
{
	/** No search results are being shown on the map.
	 *  @const
	 *  @type {string} */
	IDLE: "idle",
	
	/** A search is currently being performed.
	 *  @const
	 *  @type {string} */
	PENDING: "pending",
	
	/** A search has been triggered and places are being shown on the map.
	 *  @const
	 *  @type {string} */
	ACTIVE: "active",
	
	/** A search was triggered but there was an error finding nearby places.
	 *  @const
	 *  @type {string} */
	ERROR: "error",
	
	/** A search was triggered but no results were found.
	 *  @const
	 *  @type {string} */
	NO_RESULTS: "no-results"
};

/**
 * @type {object}
 * @property {Node} buttonDiv  The DOM element used to show/hide the place labels.
 */
lucid.maps.places.LabelPlacesButtonOptions = {};