diff --git a/src/ngAutocomplete.js b/src/ngAutocomplete.js index 3b0b33c..aabc8a7 100644 --- a/src/ngAutocomplete.js +++ b/src/ngAutocomplete.js @@ -6,7 +6,7 @@ * * Usage: * - * * * + ng-model - autocomplete textbox value * @@ -39,128 +39,66 @@ angular.module( "ngAutocomplete", []) link: function(scope, element, attrs, controller) { - //options for autocomplete - var opts - var watchEnter = false - //convert options provided to opts - var initOpts = function() { + var watchEnter = false; - opts = {} - if (scope.options) { - - if (scope.options.watchEnter !== true) { - watchEnter = false - } else { - watchEnter = true - } - - if (scope.options.types) { - opts.types = [] - opts.types.push(scope.options.types) - scope.gPlace.setTypes(opts.types) - } else { - scope.gPlace.setTypes([]) - } - - if (scope.options.bounds) { - opts.bounds = scope.options.bounds - scope.gPlace.setBounds(opts.bounds) - } else { - scope.gPlace.setBounds(null) - } - - if (scope.options.country) { - opts.componentRestrictions = { - country: scope.options.country - } - scope.gPlace.setComponentRestrictions(opts.componentRestrictions) - } else { - scope.gPlace.setComponentRestrictions(null) - } - } - } + scope.gPlace = new google.maps.places.Autocomplete(element[0], {}); - if (scope.gPlace == undefined) { - scope.gPlace = new google.maps.places.Autocomplete(element[0], {}); - } google.maps.event.addListener(scope.gPlace, 'place_changed', function() { - var result = scope.gPlace.getPlace(); - if (result !== undefined) { - if (result.address_components !== undefined) { - - scope.$apply(function() { - - scope.details = result; - - controller.$setViewValue(element.val()); - }); - } - else { - if (watchEnter) { - getPlace(result) - } - } + if ((scope.details = scope.gPlace.getPlace()) && scope.details.address_components) { + scope.$apply(function() { + controller.$setViewValue(element.val()); + }); + } + }); + + var addListener = element[0].addEventListener || element[0].attachEvent; + var wrapper = function(type, listener) { + if (type === 'keydown') { + var orig = listener; + listener = function (event) { + if (watchEnter && event.which === 13) + orig.apply(element[0], [$.Event('keydown', { keyCode : 40, which : 40 })]); + + orig.apply(element[0], [event]); + }; } - }) - - //function to get retrieve the autocompletes first result using the AutocompleteService - var getPlace = function(result) { - var autocompleteService = new google.maps.places.AutocompleteService(); - if (result.name.length > 0){ - autocompleteService.getPlacePredictions( - { - input: result.name, - offset: result.name.length - }, - function listentoresult(list, status) { - if(list == null || list.length == 0) { - - scope.$apply(function() { - scope.details = null; - }); - - } else { - var placesService = new google.maps.places.PlacesService(element[0]); - placesService.getDetails( - {'reference': list[0].reference}, - function detailsresult(detailsResult, placesServiceStatus) { - - if (placesServiceStatus == google.maps.GeocoderStatus.OK) { - scope.$apply(function() { - - controller.$setViewValue(detailsResult.formatted_address); - element.val(detailsResult.formatted_address); - - scope.details = detailsResult; - //on focusout the value reverts, need to set it again. - var watchFocusOut = element.on('focusout', function(event) { - element.val(detailsResult.formatted_address); - element.unbind('focusout') - }) + addListener.apply(element[0], [type, listener]); + }; - }); - } - } - ); - } - }); - } - } + if (element[0].addEventListener) + element[0].addEventListener = wrapper; + else if (element[0].attachEvent) + element[0].attachEvent = wrapper; controller.$render = function () { - var location = controller.$viewValue; - element.val(location); + element.val(controller.$viewValue); }; //watch options provided to directive scope.watchOptions = function () { - return scope.options + return scope.options; }; scope.$watch(scope.watchOptions, function () { - initOpts() + if (scope.options) { + watchEnter = scope.options.watchEnter; + + if (scope.options.types) + scope.gPlace.setTypes([ scope.options.types ]); + else + scope.gPlace.setTypes([]); + + if (scope.options.bounds) + scope.gPlace.setBounds(scope.options.bounds); + else + scope.gPlace.setBounds(null); + + if (scope.options.country) + scope.gPlace.setComponentRestrictions({ country: scope.options.country }); + else + scope.gPlace.setComponentRestrictions(null); + } }, true); - } }; }); \ No newline at end of file