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

/**
 * GeocodingSuggest widget constructor
 * @class
 * @constructor
 * @param {cercaliax.widget.GeocodingSuggestOptions} options Opciones del GeocodingSuggest
 */
cercalia.widget.GeocodingSuggest = function(options) {

	/**
	 * Class name
	 * @private
	 * @type {string}
	 */
	this.CLASS_NAME_ = 'cercalia.widget.GeocodingSuggest';

	/**
	 * @private
	 * @type {divId}
	 */
	this.divId_ = options.div ? options.div : null;

	/**
	 * @private
	 * @type {Function}
	 */
	this.completeCallback_ = options.complete ? options.complete : null;

	/**
	 * @private
	 * @type {boolean}
	 */
	this.filterType_ = options.filterType ? options.filterType : null;

	/**
	 * @private
	 * @type {string}
	 */
	this.defaultCountry_ = options.defaultCountry ? options.defaultCountry.toUpperCase() : null;

	/**
	 * @private
	 * @type {string}
	 */
	this.searchClosestHouseNumber_ = options.searchClosestHouseNumber ? options.searchClosestHouseNumber : false;

	/**
	 * @private
	 * @type {boolean|undefined}
	 */
	this.hnrt_ = options.hnrt ? options.hnrt : undefined;

	/**
	 * Markers painted on map.
	 * @private
	 * @type {Array.<cercalia.Marker>}
	 */
	this.listMarkers_ = null;

	/**
	 * @private
	 * @type {cercalia.Map}
	 */
	this.map_ = null;

	/**
	 * Autocomplete service.
	 * @private
	 * @type {cercalia.service.Autocomplete}
	 */
	this.autocomplete_ = null;

	/**
	 * @private
	 * @type {cercalia.service.Data}
	 */
	this.data_ = null;

	/**
	 * @private
	 * @type {cercalia.service.GeometryEntity}
	 */
	this.geomEntService_ = new cercalia.service.GeometryEntity();

	/**
	 * @private
	 * @type {cercalia.Feature|null}
	 */
	this.featureGeomEnt_ = null;

	this.initialize_();
};

/**
 * Initializes element
 * @private
 */
cercalia.widget.GeocodingSuggest.prototype.initialize_ = function() {
	if (!this.divId_) {
		cercalia.Exception(cercalia.i18n._('WIDGET_ERR_NO_DIV "%"', this.CLASS_NAME_));
	} else {
		// Buscamos div
		var element = cercalia.jQuery('#' + this.divId_).addClass('cercalia-widget cercalia-widget-geocodingsuggest');
		this.listMarkers_ = [];

		var divInput = cercalia.jQuery('<div />').addClass('deleteicon').appendTo(element);

		// Creamos input, br y select de pais
		//pose el placeholder pq agafi els symbols
		var input = cercalia.jQuery('<input placeholder ="' + cercalia.i18n._('GEOCODINGSUGGEST_PLACEHOLDER') + '" />')
			.attr('type', 'text')
			//.attr('placeholder', cercalia.i18n._('GEOCODINGSUGGEST_PLACEHOLDER'))
			.attr('id', this.divId_ + '_direction')
			.addClass('cercalia-geocodingsuggest-autocomplete')
			.appendTo(divInput);

		//icona per borrar l'input
		jQuery('<span/>').addClass('icon').click(function() {
			jQuery(this).prev('input').val('').focus();
			self.autocomplete_.close();
			input.parent().find('span').hide();
		}).appendTo(divInput);

		input.keyup(function() {
			if (jQuery(this).val().length > 0) input.parent().find('span').show();
			else input.parent().find('span').hide();
		});
		input.parent().find('span').hide();

		cercalia.jQuery('<br />').appendTo(element);
		var select = cercalia.jQuery('<select />')
			.attr('id', this.divId_ + '_country')
			.addClass('cercalia-geocodingsuggest-country')
			.appendTo(element);

		// Llenamos el select
		this.data_ = new cercalia.service.Data();

		this.data_.getCountries(function(listCountries) {

			cercalia.jQuery('<option />')
				.html(cercalia.i18n._('All')).attr('value', 'all')
				.appendTo(select);

			for (var index in listCountries) {
				var keyValue = listCountries[index];
				cercalia.jQuery('<option />').html(keyValue[1]).attr('value', keyValue[0]).appendTo(select);
			}

		});

		// Aplicamos el autocomplete al input
		var self = this;
		this.autocomplete_ = new cercalia.service.Autocomplete({
			inputId: input.attr('id'),
			searchInsideCountry: this.defaultCountry_,
			searchClosestHouseNumber: this.searchClosestHouseNumber_,
			hnrt: this.hnrt_,
			callbackfunction: function(result) {
				if (self.completeCallback_ != null) self.completeCallback_(result);
				else if (self.map_ != null) self.callbackfunction_(result);
				else cercalia.Exception(cercalia.i18n._('WIDGET_ERR_NO_MAP_NO_FUNCTION ' % '', self.CLASS_NAME_));
			}
		});

		// Aplicamos evento de change al select
		select.change(function(event) {
			var value = cercalia.jQuery(this).val();
			if (value === 'all') value = null;
			self.autocomplete_.setSearchInsideCountry(value);
		});

		var self = this;

		if (this.filterType_) {
			//Si se quiere mostrar los input radio para filtrar el suggest según tipo
			this.optionsSuggest_(element);
		}

		if (this.defaultCountry_) {
			//Si se quiere especificar que seleccione por defecto
			select.val(this.defaultCountry_);
		}

	}
};


/**
 * Shows route options in panel.
 * @private
 * @param divContent {Object} : Div where show the route options.
 */
cercalia.widget.GeocodingSuggest.prototype.optionsSuggest_ = function(divContent) {

	var self = this;
	var show = true;

	var optionsContent = cercalia.jQuery('<div />')
		.addClass('cercalia-widget-geocodingsuggest-options')
		.appendTo(divContent);

	var optionsTitle = cercalia.jQuery('<a />')
		.addClass('cercalia-widget-geocodingsuggest-control-text')
		.attr('href', '#').html(cercalia.i18n._('Search options'))
		.appendTo(optionsContent)
		.click(function() {
			show = !show;
			if (show) {
				optionsType.show();
				arrow.removeClass('cercalia-icon-arrowDown').addClass('cercalia-icon-arrowUp');
			} else {
				optionsType.hide();
				arrow.removeClass('cercalia-icon-arrowUp').addClass('cercalia-icon-arrowDown');
			}
		});

	cercalia.jQuery('<span />')
		.addClass('ui-icon cercalia-icon cercalia-icon-options cercalia-widget-geocodingsuggest-control-first-icon')
		.appendTo(optionsTitle);

	var arrow = cercalia.jQuery('<span />')
		.addClass('ui-icon cercalia-icon cercalia-widget-geocodingsuggest-control-second-icon cercalia-icon-arrowUp')
		.appendTo(optionsTitle);

	var optionsType = cercalia.jQuery('<div />')
		.addClass('cercalia-widget-geocodingsuggest-options ui-widget-content')
		.appendTo(optionsContent);


	cercalia.jQuery('<div />')
		.addClass('cercalia-widget-geocodingsuggest-option cercalia-widget-geocodingsuggest-option-type')
		.append(
			cercalia.jQuery('<span />')
			.html(cercalia.i18n._('Items included in the search:'))
		)
		.append(
			cercalia.jQuery('<br />')
		)
		.append(
			cercalia.jQuery('<input />')
			.attr('type', 'checkbox')
			.attr('name', this.divId_ + '_optionSuggest_type')
			.attr('value', 'ct').attr('checked', 'checked')
		)
		.append(
			cercalia.jQuery('<span />')
			.html(cercalia.i18n._('City'))
		)
		.append(
			cercalia.jQuery('<input />')
			.attr('type', 'checkbox').attr('name', this.divId_ + '_optionSuggest_type')
			.attr('value', 'st')
			.attr('checked', 'checked')
		)
		.append(
			cercalia.jQuery('<span />')
			.html(cercalia.i18n._('Address'))
		)
		.append(
			cercalia.jQuery('<input />')
			.attr('type', 'checkbox')
			.attr('name', this.divId_ + '_optionSuggest_type')
			.attr('value', 'p')
			.attr('checked', 'checked')
		)
		.append(
			cercalia.jQuery('<span />')
			.html(cercalia.i18n._('POIs'))
		)
		.appendTo(optionsType);

	this.changeTypeSuggest_();
	cercalia.jQuery('input[name="' + this.divId_ + '_optionSuggest_type"]').change(function() {
		self.changeTypeSuggest_();
	});

};

/**
 * @private
 */
cercalia.widget.GeocodingSuggest.prototype.changeTypeSuggest_ = function() {

	var typeSearch = [];
	cercalia.jQuery('input[name="' + this.divId_ + '_optionSuggest_type"]:checked').each(function(index, elem) {
		typeSearch.push(cercalia.jQuery(elem).val());
	});

	this.autocomplete_.setSearchFilterType(typeSearch.join(','));
};


/**
 * Clean input data
 */
cercalia.widget.GeocodingSuggest.prototype.clean = function() {
	cercalia.jQuery('#' + this.divId_ + '_direction').val('');
	cercalia.jQuery('#' + this.divId_ + '_country').val('all');
	this.cleanMarkers();
};

/**
 * Clean current geocoding search.
 */
cercalia.widget.GeocodingSuggest.prototype.cleanMarkers = function() {
	for (var index in this.listMarkers_) {
		var marker = this.listMarkers_[index];
		marker.destroy();
	}

	this.listMarkers_ = [];
};

/**
 * Default callback function.
 * @param {Object} ge Cercalia response
 * @private
 */
cercalia.widget.GeocodingSuggest.prototype.callbackfunction_ = function(ge) {
	var popupContent = function(ge) {
		var content = cercalia.jQuery('<div />');
		var table = cercalia.jQuery('<table />').appendTo(content);
		var portalBefore = ['ARE', 'AUS', 'CAN', 'EGY', 'GBR', 'IND', 'IRL', 'ISR', 'KWT', 'NZL', 'OMN', 'QAT', 'SAU', 'THA', 'USA', 'ZAF'].indexOf(ge.country.id) != -1;

		if (ge.type === 'st' || ge.type === 'adr' || ge.type === 'p') {

			var address = portalBefore ? ((ge.housenumber ? ge.housenumber.value + ' ' : '') + ge.sname) : ge.name.value;
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Address')).appendTo(tr);
			cercalia.jQuery('<td />').html(address).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) {
			var 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) {
			var 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 (ge.coord) {
			var tr = cercalia.jQuery('<tr />').appendTo(table);
			cercalia.jQuery('<td />').css('font-weight', 'bold').html(cercalia.i18n._('Coords')).appendTo(tr);
			cercalia.jQuery('<td />').html(ge.coord.x + ', ' + ge.coord.y).appendTo(tr);
		}

		return content.html();
	};

	var popup = new cercalia.Popup({
		visible: true,
		title: cercalia.i18n._('GEOCODINGSUGGEST_POPUP_TITLE'),
		content: popupContent(ge)
	});

	var marker = new cercalia.Marker({
		position: new cercalia.LonLat(ge.coord.x, ge.coord.y),
		popup: popup
	});

	this.map_.addMarker(marker);
	this.listMarkers_.push(marker);

	this.map_.panTo(marker.getPosition(), cercalia.MapAnimation.PAN);

	//var zoom = (ge.type === 'st' || ge.type === 'adr' || ge.type === 'p') ? 17 : 12;
	var zoom;
	if (ge.type === 'ct') {
		zoom = 12;
		if ((ge.municipality && (ge.name.value === ge.municipality.value))) {
			var municipalityId = ge.municipality.id;
			this.geomEntService_.getMunicipalityGeometry(municipalityId, this.handleDrawGeometryEntity_.bind(this, false));
		}
	} else { //st | adr | p
		zoom = 17;
	}
	this.map_.setZoom(zoom, true);
};

/**
 * Modify input text
 * @param {string} text Text to show inside input
 */
cercalia.widget.GeocodingSuggest.prototype.setInputText = function(text) {
	if (typeof(text) === 'string') {
		cercalia.jQuery('#' + this.divId_ + '_direction').val(text);
	}
};

/**
 * to remove the focus
 */
cercalia.widget.GeocodingSuggest.prototype.blur = function() {
	cercalia.jQuery('#' + this.divId_ + '_direction').blur();
};

/**
 * Modify the map which the widget is interacting with.
 * @param {cercalia.Map} map Map which the widget is interacting with.
 */
cercalia.widget.GeocodingSuggest.prototype.setMap = function(map) {
	this.map_ = map;
};

/**
 * Get autocomplete service. Then you can modify its properties to do accurate searches.
 * @return {cercalia.service.Autocomplete} Returns autocomplete service.
 */
cercalia.widget.GeocodingSuggest.prototype.getService = function() {
	return this.autocomplete_;
};

/**
 * @private
 * @param {boolean} panTo Optional pan to location.
 * @param {cercaliax.GeometryEntityResult} data Geocoding result.
 */
cercalia.widget.GeocodingSuggest.prototype.handleDrawGeometryEntity_ = function(panTo, data) {

	if (!data.cercalia.error) {
		var geometry = data.cercalia.ge.geometry;
		var wkt = geometry.wkt.value;

		if (this.featureGeomEnt_) {
			this.map_.removeFeatures(this.featureGeomEnt_);
		}

		this.featureGeomEnt_ = new cercalia.Feature({
			wkt: wkt,
			fillOpacity: 0.1,
			fillColor: '#ff4d4d',
			strokeColor: '#ff4d4d',
			strokeWidth: 1.5,
			strokeOpacity: 1,
			outlineWidth: 0
		});

		this.map_.addFeature(this.featureGeomEnt_);

		if (panTo) {
			this.map_.fitBounds(this.featureGeomEnt_.getBounds());
		}
	}
};

/**
 * Returns the object type.
 * @return {string}
 */
cercalia.widget.GeocodingSuggest.prototype.getClass = function() {
	return this.CLASS_NAME_;
};