2012-11-11 03:44:42 +00:00
|
|
|
|
/*jslint browser: true, white: true, plusplus: true */
|
|
|
|
|
/*global $: true */
|
|
|
|
|
|
|
|
|
|
$(function () {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2012-12-18 22:07:02 +00:00
|
|
|
|
// Load countries then initialize plugin:
|
2012-11-11 03:44:42 +00:00
|
|
|
|
$.ajax({
|
|
|
|
|
url: 'content/countries.txt',
|
|
|
|
|
dataType: 'json'
|
2012-12-18 22:07:02 +00:00
|
|
|
|
}).done(function (source) {
|
|
|
|
|
|
|
|
|
|
var countriesArray = $.map(source, function (value, key) { return { value: value, data: key }; }),
|
|
|
|
|
countries = $.map(source, function (value) { return value; });
|
|
|
|
|
|
|
|
|
|
// Setup jQuery ajax mock:
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: '*',
|
2013-06-04 23:12:01 +00:00
|
|
|
|
responseTime: 2000,
|
2012-12-18 22:07:02 +00:00
|
|
|
|
response: function (settings) {
|
|
|
|
|
var query = settings.data.query,
|
|
|
|
|
queryLowerCase = query.toLowerCase(),
|
2013-06-04 23:12:01 +00:00
|
|
|
|
re = new RegExp('\\b' + queryLowerCase, 'gi'),
|
|
|
|
|
suggestions = $.grep(countriesArray, function (country) {
|
|
|
|
|
// return country.value.toLowerCase().indexOf(queryLowerCase) === 0;
|
|
|
|
|
return re.test(country.value);
|
2012-12-18 22:07:02 +00:00
|
|
|
|
}),
|
|
|
|
|
response = {
|
|
|
|
|
query: query,
|
|
|
|
|
suggestions: suggestions
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.responseText = JSON.stringify(response);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Initialize ajax autocomplete:
|
|
|
|
|
$('#autocomplete-ajax').autocomplete({
|
2013-06-04 23:12:01 +00:00
|
|
|
|
// serviceUrl: '/autosuggest/service/url',
|
|
|
|
|
lookup: countriesArray,
|
|
|
|
|
lookupFilter: function(suggestion, originalQuery, queryLowerCase) {
|
|
|
|
|
var re = new RegExp('\\b' + queryLowerCase, 'gi');
|
|
|
|
|
return re.test(suggestion.value);
|
|
|
|
|
},
|
2012-12-18 22:07:02 +00:00
|
|
|
|
onSelect: function(suggestion) {
|
|
|
|
|
$('#selction-ajax').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
|
2013-06-04 23:12:01 +00:00
|
|
|
|
},
|
|
|
|
|
onHint: function (hint) {
|
|
|
|
|
$('#autocomplete-ajax-x').val(hint);
|
2012-12-18 22:07:02 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Initialize autocomplete with local lookup:
|
|
|
|
|
$('#autocomplete').autocomplete({
|
|
|
|
|
lookup: countriesArray,
|
2012-11-11 03:44:42 +00:00
|
|
|
|
onSelect: function (suggestion) {
|
2012-12-18 22:07:02 +00:00
|
|
|
|
$('#selection').html('You selected: ' + suggestion.value + ', ' + suggestion.data);
|
2012-11-11 03:44:42 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
2013-01-20 21:17:04 +00:00
|
|
|
|
// Initialize autocomplete with custom appendTo:
|
|
|
|
|
$('#autocomplete-custom-append').autocomplete({
|
|
|
|
|
lookup: countriesArray,
|
|
|
|
|
appendTo: '#suggestions-container'
|
|
|
|
|
});
|
2013-04-26 19:49:41 +00:00
|
|
|
|
|
|
|
|
|
// Initialize autocomplete with custom appendTo:
|
|
|
|
|
$('#autocomplete-dynamic').autocomplete({
|
|
|
|
|
lookup: countriesArray
|
|
|
|
|
});
|
2013-01-20 21:17:04 +00:00
|
|
|
|
|
2012-11-11 03:44:42 +00:00
|
|
|
|
});
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
2012-11-11 03:44:42 +00:00
|
|
|
|
});
|