var userInteracted = $('.location-wrapper').length === 0;
// we don't want to focus on street element on page load, only if user actually interacts with form
$('.location-wrapper input[type="text"]').on('focus',
function(e) {
if (!userInteracted)
userInteracted = true;
return true;
});
$('.location-wrapper').on('click',
function(e) {
if (!userInteracted)
userInteracted = true;
return true;
});
$.fn.ehak2 = function(options) {
options = $.extend({
// If true, we call geocode() after selection is changed
updateMapOnChange: false,
// Placeholder for settlement select2 dropdown
placeholder: 'Поиск населенного пункта',
// Optional method to call after selection changed or cleared
onChangeCallback: null,
// Show allow clear button on Select2
allowClear: false,
// To allow this component to be used in several places on a single page, pass a prefix to differentiate
// between different elements. This is applied like this: $('#{prefix}CityName')
addressViewPrefix: '',
// To allow this component to be used in several places on a single page, pass a prefix to differentiate
// between different elements. This is applied like this: $('#{prefix}Lat')
coordinatesViewPrefix: '',
// If we have a dropdown with Location names, like in order create/edit view, we want to update that
// when an existing Location is selected, but address is changed. To allow this, pass the dropdown
// as a jQuery element here: $('#LocationName')
elLocationName: null,
// When displaying Select2 inside a bootstrap modal, this is required for correct functioning.
// To use this, pass a jQuery element here: $('#modal')
dropdownParent: null,
urls: {
// URL to query EHAK data from
ehak: 'https://itb.jetoil.ee/ajax/ehak/index',
// URL to find EHAK County by name, used in reverse geocoding
countyByName: 'https://itb.jetoil.ee/ajax/locations/getcountybyname',
// URL to find EHAK City by name, used in reverse geocoding
cityByName: 'https://itb.jetoil.ee/ajax/locations/getcitybyname',
// URL to find EHAK Village by name, used in reverse geocoding
villageByName: 'https://itb.jetoil.ee/ajax/locations/getvillagebyname',
// URL to find EHAK Parish by name, used in reverse geocoding
parishByName: 'https://itb.jetoil.ee/ajax/locations/getparishbyname',
// URL to find EHAK Borough by name, used in reverse geocoding
boroughByName: 'https://itb.jetoil.ee/ajax/locations/getboroughbyname'
}
},
options);
var elCountryId = $('#{0}CountryId'.format(options.addressViewPrefix)),
elLat = $('#{0}Lat'.format(options.coordinatesViewPrefix)),
elLng = $('#{0}Lng'.format(options.coordinatesViewPrefix)),
elCountyName = $('#{0}CountyName'.format(options.addressViewPrefix)),
elCountyId = $('#{0}CountyId'.format(options.addressViewPrefix)),
elCityName = $('#{0}CityName'.format(options.addressViewPrefix)),
elCityId = $('#{0}CityId'.format(options.addressViewPrefix)),
elVillageName = $('#{0}VillageName'.format(options.addressViewPrefix)),
elVillageId = $('#{0}VillageId'.format(options.addressViewPrefix)),
elParishName = $('#{0}ParishName'.format(options.addressViewPrefix)),
elParishId = $('#{0}ParishId'.format(options.addressViewPrefix)),
elBoroughName = $('#{0}BoroughName'.format(options.addressViewPrefix)),
elBoroughId = $('#{0}BoroughId'.format(options.addressViewPrefix)),
elStreet = $('#{0}Street'.format(options.addressViewPrefix)),
elPostCode = $('#{0}PostCode'.format(options.addressViewPrefix));
return this.each(function() {
var elSelect2 = this;
var updateSelect2 = function(select2Element, value, text) {
text = text || value;
if ($(select2Element).find("option[value='" + value + "']").length) {
$(select2Element).val(value);
} else {
var newOption = new Option(text, value, true, true);
$(select2Element).append(newOption);
}
};
$(elSelect2).select2({
language: 'ru',
dropdownParent: options.dropdownParent,
placeholder: options.placeholder || 'Поиск населенного пункта',
minimumInputLength: 2, // Does the user have to enter any data before sending the ajax request
allowClear: options.allowClear,
multiple: false,
ajax: {
// How long the user has to pause their typing before sending the next request
delay: 150,
// The url of the json service
url: options.urls.ehak,
dataType: 'jsonp'
},
templateResult: function(item) {
// Only proceed if we have a custom result
if (!item.type)
return item.text;
// Use custom select2 option formatting
var $option = $('