/**
* BeNomad BeMap JavaScript API - AutocompleteElement
*
* One entry in the autocomplete result list. The Worker returns the matched
* place string plus a `reference` token that can be fed back into a full
* geocoding call to obtain the structured coordinate / postal address.
*
* @since 1.5.0
* @public
* @constructor
* @param {Object} [options]
* @param {String} [options.place] The display string with `|` separators stripped.
* @param {String} [options.rawPlace] The original server string (unmodified).
* @param {String} [options.reference] Opaque reference for follow-up geocoding.
* @param {String} [options.highlight] Server-rendered highlight string (HTML-style).
* @param {Array.<Array.<Number>>} [options.positions] Match positions as `[[start, end], ...]`.
* @param {bemap.PostalAddress} [options.postalAddress] Only populated when `addressDetails: true`.
* @param {bemap.Coordinate} [options.coordinate] Only populated when the Worker resolved it.
*/
bemap.AutocompleteElement = function(options) {
var opts = options || {};
this.place = opts.place || null;
this.rawPlace = opts.rawPlace || null;
this.reference = opts.reference || null;
this.highlight = opts.highlight || null;
this.positions = Array.isArray(opts.positions) ? opts.positions.slice() : [];
this.postalAddress = opts.postalAddress || null;
this.coordinate = opts.coordinate || null;
};
bemap.AutocompleteElement.prototype.getPlace = function() { return this.place; };
bemap.AutocompleteElement.prototype.getRawPlace = function() { return this.rawPlace; };
bemap.AutocompleteElement.prototype.getReference = function() { return this.reference; };
bemap.AutocompleteElement.prototype.getHighlight = function() { return this.highlight; };
bemap.AutocompleteElement.prototype.getPositions = function() { return this.positions; };
bemap.AutocompleteElement.prototype.getPostalAddress = function() { return this.postalAddress; };
bemap.AutocompleteElement.prototype.getCoordinate = function() { return this.coordinate; };
/**
* Factory that builds an `AutocompleteElement` from the raw JSON returned by
* the BeMap autocomplete endpoint. Tolerates missing fields.
*
* @param {Object} item
* @return {bemap.AutocompleteElement}
*/
bemap.AutocompleteElement.fromJson = function(item) {
if (!item) return new bemap.AutocompleteElement();
var raw = item.place || item.label || '';
var clean = raw.replace(/\|/g, '').replace(/ {2,}/g, ' ').trim();
var postal = null;
if (item.postalAddress) {
postal = new bemap.PostalAddress({
countryCode: item.postalAddress.countryCode,
country: item.postalAddress.country,
state: item.postalAddress.state,
county: item.postalAddress.county,
city: item.postalAddress.city,
district: item.postalAddress.district,
postalCode: item.postalAddress.postalCode,
street: item.postalAddress.street,
streetNumber: item.postalAddress.streetNumber
});
}
var coord = null;
if (item.coordinate) {
var lon = (item.coordinate.longitude !== undefined)
? item.coordinate.longitude : item.coordinate.lon;
var lat = (item.coordinate.latitude !== undefined)
? item.coordinate.latitude : item.coordinate.lat;
if (lon !== undefined && lat !== undefined) {
coord = new bemap.Coordinate(lon, lat);
}
}
return new bemap.AutocompleteElement({
place: clean,
rawPlace: raw,
reference: item.reference || null,
highlight: item.highlight || null,
positions: item.positions || [],
postalAddress: postal,
coordinate: coord
});
};