Source: temp/jsdocinputdirs/cercalia.widget.poisuggest.js

/**
 * Constructor del widget Puntos de interés sobre el mapa
 * @class
 * @constructor
 * @param {cercaliax.widget.PoiSuggestOptions} options Opciones del PoiSuggest
 */
cercalia.widget.PoiSuggest = function (options) {
	
	/**
	 * Nombre de la classe
	 * @private
	 * @type {string}
	 */
	this.CLASS_NAME_ = "cercalia.widget.PoiSuggest";
	
	/* OPTIONS */
	
	/**
	 * DIV id donde se creara el widget.
	 * @private
	 * @type {string}
	 */
	this.divId_ = options.div ? options.div : null;
	
	/**
	 * Opciones para el widget listado de categorias
	 * @private
	 * @type {cercaliax.widget.PoiListOptions}
	 */
	this.poiListOptions_ = options.poiListOptions ? options.poiListOptions : {};
	
	/**
	 * Función que se llama al completar con éxito la llamada a cercalia.
	 * @private
	 * @type {Function}
	 */
	this.completeCallback_ = options.complete ? options.complete : null;
	
	/**
	 * Función que se llama en caso de error en la llamada a cercalia.
	 * @private
	 * @type {Function}
	 */
	this.errorCallback_ = options.error ? options.error : null;
	
	/* VARIABLES PRIVADAS */
	
	/**
	 * Cercalia map
	 * @private
	 * @type {cercalia.map}
	 */
	this.map_ = null;
	
	/**
	 * Categorias seleccionadas del listado.
	 * @private
	 * @type {Array.<string>}
	 */
	this.categoriesSelected_ = null;
	
	/**
	 * Listado de Markers pintados en el mapa
	 * @private
	 * @type {Array.<cercalia.Marker>}
	 */
	this.listMarkers_ = [];
	
	/**
	 * Input nombre a buscar para los POIs
	 * @private
	 * @type {Object}
	 */
	this.inputName_ = null;
	
	/* SERVICIOS Y WIDGETS DE CERCALIA */
	
	/**
	 * Widget listado de categorias
	 * @private
	 * @type {cercalia.widget.PoiList}
	 */
	this.poiListWidget_ = null;
	
	/**
	 * Objeto proximity para hacer las llamadas a cercalia
	 * @private
	 * @type {cercalia.service.Geocoding}
	 */
	this.geocoding_ = null;
	
	/**
	 * Servicio data para los paises.
	 * @private
	 * @type {cercalia.service.Data}
	 */
	this.data_ = null;
	
	// Inicializamos
	this.initialize_();
};

/**
 * Inicializa el elemento.
 * @private
 */
cercalia.widget.PoiSuggest.prototype.initialize_ = function () {
	
	if( ! this.divId_ ) {
		cercalia.Exception(cercalia.i18n._('WIDGET_ERR_NO_DIV "%"', this.CLASS_NAME_));
	} else {
		var element = cercalia.jQuery("#" + this.divId_).addClass("cercalia-widget cercalia-widget-poisuggest");
		var self = this;
		this.geocoding_ = new cercalia.service.Geocoding();
		
		// Input para el nombre
		this.inputName_ = cercalia.jQuery("<input />").attr("placeholder", cercalia.i18n._("POISUGGEST_PLACEHOLDER")).attr("type", "text").attr("id", "cercalia-widget-poisuggest-name").appendTo(element);
		cercalia.jQuery("<br />").appendTo(element);
		
		// Select para el pais
		var select = cercalia.jQuery("<select />")
			.attr("id", this.divId_ + "_country")
			.addClass("cercalia-widget-poisuggest-country")
			.appendTo(element);
		
		// Llenamos el select
		this.data_ = new cercalia.service.Data();
		
		this.data_.getCountries(function(listCountries){
			for(var index = 0; index < listCountries.length; index++) {
				var keyValue = listCountries[index];
				cercalia.jQuery("<option />").html(keyValue[1]).attr("value", keyValue[0]).appendTo(select);
			}
			
			select.val("ESP"); // Valor por defecto del select		
		});
		
		
		
		var divMenu = cercalia.jQuery("<div />").attr("id", this.divId_ + "_menu").appendTo(element);
		
		this.poiListOptions_.div = divMenu.attr("id");
		this.poiListOptions_.clickElementList = function () {
			var arraySelected = self.poiListWidget_.getSelectedId();
			self.categoriesSelected_ = arraySelected;
		};
		this.poiListWidget_ = new cercalia.widget.PoiList(this.poiListOptions_);
		
		cercalia.jQuery("<button />").addClass("cercalia-widget-poisuggest-find").html(cercalia.i18n._("POISUGGEST_FIND")).appendTo(element).button().click(function () {
			self.find_(self);
		});
	}
};


/**
 * Lanza la busqueda de pois en el ge seleccionado
 * @param {cercalia.widget.PoiSuggest} [context] this
 * @private
 */
cercalia.widget.PoiSuggest.prototype.find_ = function (context) {
	if (typeof(context) === "undefined" || context === null) context = this;
	
	// Limpiamos proximity y resultados anteriores
	context.geocoding_.clear();
	context.cleanMarkers();
	
	if(this.inputName_.val().length >= 4) {
		
		context.geocoding_.poicat = context.categoriesSelected_.join(",");
		context.geocoding_.poiName = this.inputName_.val();
		context.geocoding_.countryId = cercalia.jQuery("#" + context.divId_).find(".cercalia-widget-poisuggest-country").val();
		
		context.geocoding_.geocode(function (result) {
			if(typeof(result.cercalia.error) !== "undefined" && result.cercalia.error !== null) {
				
				if(context.errorCallback_ != null) context.errorCallback_(result);
				else context.callbackErrorfunction_(result); 
				
			} else {
				
				if(context.completeCallback_ != null) context.completeCallback_(result);
				else if(context.map_ != null) context.geocodeResponse_(result);
				else cercalia.Exception(cercalia.i18n._('WIDGET_ERR_NO_MAP_NO_FUNCTION "%"', this.CLASS_NAME_));
			}
		});
	}
};

/**
 * Funcion de callback de error llamada por defecto.
 * @private
 * @param {Object} result
 */
cercalia.widget.PoiSuggest.prototype.callbackErrorfunction_ = function (result) {
	cercalia.Exception(cercalia.i18n._('WIDGET_ERR_CERCALIA "%" "%" "%"', this.CLASS_NAME_, result.cercalia.error.id, result.cercalia.error.value));
};

/**
 * Funciona que muestra el resultado del geocode
 * @param {Object} response Respuesta de cercalia 
 * @private
 */
cercalia.widget.PoiSuggest.prototype.geocodeResponse_ = function (response) {
	if(response.cercalia.candidates.total <= 0) {
		cercalia.Exception(cercalia.i18n._('WIDGET_WARN_NO_CANDIDATES "%"', this.CLASS_NAME_));
	} else {
		
		var poisList = response.cercalia.candidates.candidate;
		for(var index = 0 ; index < poisList.length; index++) {
			var poi = poisList[index];
			
			var popup = new cercalia.Popup({
				visible: false,
				title: poi.name,
				content: this.popupContent_(poi)
			});
			
			var marker = new cercalia.Marker({
				position: new cercalia.LonLat(poi.ge.coord.x, poi.ge.coord.y),
				icon : new cercalia.Icon({ src: cercaliaGlobals.img + '/pois/' +  poi.ge.category_id + '.png', size: [28, 38], anchor: [14, 38] }),
				popup: popup
			});
			
			this.map_.addMarker(marker);
			this.listMarkers_.push(marker);
		}
		
		if(this.listMarkers_.length > 1)
			this.map_.centerToMarkers(this.listMarkers_);
		else
			this.map_.setCenter(this.listMarkers_[0].getPosition(), 10);
	}
};

/**
 * Genera el contenido HTML del Popup.
 * @private
 * @param {Object} poi Objeto POI de Cercalia.
 * @return {string}
 */
cercalia.widget.PoiSuggest.prototype.popupContent_ = function (poi) {
	var content = cercalia.jQuery("<div />");
	var table = cercalia.jQuery("<table />").css("width", "300px").appendTo(content);
	var ge = poi.ge;
	
	if(poi.info) {
		var tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Information")).appendTo(tr);
		cercalia.jQuery("<td />").css("width", "225px").html(poi.info.value).appendTo(tr);
	}
	
	if(ge.city) {
		var tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("City")).appendTo(tr);
		cercalia.jQuery("<td />").html(ge.city.value).appendTo(tr);
	}
	
	if(ge.postalcode) {
		var tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Postal code")).appendTo(tr);
		cercalia.jQuery("<td />").html(ge.postalcode.id).appendTo(tr);
	}
	
	if(ge.municipality) {
		var tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Municipality")).appendTo(tr);
		cercalia.jQuery("<td />").html(ge.municipality.value).appendTo(tr);
	}
	
	if(ge.subregion) {
		var tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Subregion")).appendTo(tr);
		cercalia.jQuery("<td />").html(ge.subregion.value).appendTo(tr);
	}
	
	if(ge.region) {
		tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Region")).appendTo(tr);
		cercalia.jQuery("<td />").html(ge.region.value).appendTo(tr);
	}
	
	if(ge.country) {
		tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Country")).appendTo(tr);
		cercalia.jQuery("<td />").html(ge.country.value).appendTo(tr);
	}
	
	if(poi.coord) {
		tr = cercalia.jQuery("<tr />").appendTo(table);
		cercalia.jQuery("<td />").css("font-weight", "bold").html(cercalia.i18n._("Coords")).appendTo(tr);
		cercalia.jQuery("<td />").html(poi.coord.x +", " + poi.coord.y).appendTo(tr);
	}
	
	return content.html();
};

/**
 * Limpia las busquedas activas de geocoding. 
 */
cercalia.widget.PoiSuggest.prototype.cleanMarkers = function () {
	for(var index = 0 ; index < this.listMarkers_.length; index++) {
		var marker = this.listMarkers_[index];
		marker.destroy();
	}
	
	this.listMarkers_ = [];
};

/**
 * Modifica el mapa con el que interactua el widget.
 * @param {cercalia.Map} map Mapa con el que interactuará el Widget. 
 */
cercalia.widget.PoiSuggest.prototype.setMap = function (map) {
	this.map_ = map;
};

/**
* Devuelve el tipo del objeto.
* @return {string}
*/
cercalia.widget.PoiSuggest.prototype.getClass = function(){
	return this.CLASS_NAME_;
};