2012-11-11 03:44:42 +00:00
|
|
|
|
/*jslint browser: true, white: true, plusplus: true */
|
2013-11-24 03:43:40 +00:00
|
|
|
|
/*global $, countries */
|
2012-11-11 03:44:42 +00:00
|
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2013-11-24 03:43:40 +00:00
|
|
|
|
var countriesArray = $.map(countries, function (value, key) { return { value: value, data: key }; });
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
2013-11-24 03:43:40 +00:00
|
|
|
|
// Setup jQuery ajax mock:
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: '*',
|
|
|
|
|
responseTime: 2000,
|
|
|
|
|
response: function (settings) {
|
|
|
|
|
var query = settings.data.query,
|
|
|
|
|
queryLowerCase = query.toLowerCase(),
|
|
|
|
|
re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi'),
|
|
|
|
|
suggestions = $.grep(countriesArray, function (country) {
|
|
|
|
|
// return country.value.toLowerCase().indexOf(queryLowerCase) === 0;
|
|
|
|
|
return re.test(country.value);
|
|
|
|
|
}),
|
|
|
|
|
response = {
|
|
|
|
|
query: query,
|
|
|
|
|
suggestions: suggestions
|
|
|
|
|
};
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
2013-11-24 03:43:40 +00:00
|
|
|
|
this.responseText = JSON.stringify(response);
|
|
|
|
|
}
|
|
|
|
|
});
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
2013-11-24 03:43:40 +00:00
|
|
|
|
// Initialize ajax autocomplete:
|
|
|
|
|
$('#autocomplete-ajax').autocomplete({
|
|
|
|
|
// serviceUrl: '/autosuggest/service/url',
|
|
|
|
|
lookup: countriesArray,
|
|
|
|
|
lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
|
|
|
|
|
var re = new RegExp('\\b' + $.Autocomplete.utils.escapeRegExChars(queryLowerCase), 'gi');
|
|
|
|
|
return re.test(suggestion.value);
|
|
|
|
|
},
|
|
|
|
|
onSelect: function(suggestion) {
|
|
|
|
|
$('#selction-ajax').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
|
|
|
|
|
},
|
|
|
|
|
onHint: function (hint) {
|
|
|
|
|
$('#autocomplete-ajax-x').val(hint);
|
|
|
|
|
},
|
|
|
|
|
onInvalidateSelection: function() {
|
|
|
|
|
$('#selction-ajax').html('You selected: none');
|
|
|
|
|
}
|
|
|
|
|
});
|
2013-04-26 19:49:41 +00:00
|
|
|
|
|
2013-11-24 03:43:40 +00:00
|
|
|
|
// Initialize autocomplete with local lookup:
|
2014-08-25 00:12:49 +00:00
|
|
|
|
$('#autocomplete').devbridgeAutocomplete({
|
2013-11-24 03:43:40 +00:00
|
|
|
|
lookup: countriesArray,
|
|
|
|
|
minChars: 0,
|
|
|
|
|
onSelect: function (suggestion) {
|
|
|
|
|
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
|
2014-03-26 18:51:46 +00:00
|
|
|
|
},
|
|
|
|
|
showNoSuggestionNotice: true,
|
|
|
|
|
noSuggestionNotice: 'Sorry, no matching results',
|
2013-11-24 03:43:40 +00:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Initialize autocomplete with custom appendTo:
|
|
|
|
|
$('#autocomplete-custom-append').autocomplete({
|
|
|
|
|
lookup: countriesArray,
|
|
|
|
|
appendTo: '#suggestions-container'
|
2012-11-11 03:44:42 +00:00
|
|
|
|
});
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
2013-11-24 03:43:40 +00:00
|
|
|
|
// Initialize autocomplete with custom appendTo:
|
|
|
|
|
$('#autocomplete-dynamic').autocomplete({
|
|
|
|
|
lookup: countriesArray
|
|
|
|
|
});
|
2012-11-11 03:44:42 +00:00
|
|
|
|
});
|