-
Notifications
You must be signed in to change notification settings - Fork 166
Add option getPlaceOnBlur to retrieve the top autocomplete result on input's blur event #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,8 +27,8 @@ | |
| * } | ||
| **/ | ||
|
|
||
| angular.module( "ngAutocomplete", []) | ||
| .directive('ngAutocomplete', function() { | ||
| angular.module("ngAutocomplete", []) | ||
| .directive('ngAutocomplete', function () { | ||
| return { | ||
| require: 'ngModel', | ||
| scope: { | ||
|
|
@@ -37,13 +37,13 @@ angular.module( "ngAutocomplete", []) | |
| details: '=?' | ||
| }, | ||
|
|
||
| link: function(scope, element, attrs, controller) { | ||
| link: function (scope, element, attrs, controller) { | ||
|
|
||
| //options for autocomplete | ||
| var opts | ||
| var watchEnter = false | ||
| //convert options provided to opts | ||
| var initOpts = function() { | ||
| var initOpts = function () { | ||
|
|
||
| opts = {} | ||
| if (scope.options) { | ||
|
|
@@ -83,12 +83,12 @@ angular.module( "ngAutocomplete", []) | |
| if (scope.gPlace == undefined) { | ||
| scope.gPlace = new google.maps.places.Autocomplete(element[0], {}); | ||
| } | ||
| google.maps.event.addListener(scope.gPlace, 'place_changed', function() { | ||
| 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.$apply(function () { | ||
|
|
||
| scope.details = result; | ||
|
|
||
|
|
@@ -104,37 +104,37 @@ angular.module( "ngAutocomplete", []) | |
| }) | ||
|
|
||
| //function to get retrieve the autocompletes first result using the AutocompleteService | ||
| var getPlace = function(result) { | ||
| var getPlace = function (result) { | ||
| var autocompleteService = new google.maps.places.AutocompleteService(); | ||
| if (result.name.length > 0){ | ||
| if (result.name.length > 0) { | ||
| autocompleteService.getPlacePredictions( | ||
| { | ||
| input: result.name, | ||
| offset: result.name.length | ||
| }, | ||
| function listentoresult(list, status) { | ||
| if(list == null || list.length == 0) { | ||
| if (list == null || list.length == 0) { | ||
|
|
||
| scope.$apply(function() { | ||
| scope.$apply(function () { | ||
| scope.details = null; | ||
| }); | ||
|
|
||
| } else { | ||
| var placesService = new google.maps.places.PlacesService(element[0]); | ||
| placesService.getDetails( | ||
| {'reference': list[0].reference}, | ||
| { 'reference': list[0].reference }, | ||
| function detailsresult(detailsResult, placesServiceStatus) { | ||
|
|
||
| if (placesServiceStatus == google.maps.GeocoderStatus.OK) { | ||
| scope.$apply(function() { | ||
| 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) { | ||
| var watchFocusOut = element.on('focusout', function (event) { | ||
| element.val(detailsResult.formatted_address); | ||
| element.unbind('focusout') | ||
| }) | ||
|
|
@@ -148,6 +148,12 @@ angular.module( "ngAutocomplete", []) | |
| } | ||
| } | ||
|
|
||
| if (scope.options.getPlaceOnBlur) { | ||
| element.bind('blur', function () { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When user click on dropdown proposals to choose a place:
I can't find a way to check if input is blured while clicking results list dropdown to prevent it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could rely on check if the autocomplete selection was not fired? otherwise bypass de blur event |
||
| getPlace({ name: controller.$viewValue }); | ||
| }); | ||
| } | ||
|
|
||
| controller.$render = function () { | ||
| var location = controller.$viewValue; | ||
| element.val(location); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should check options presence.
if ( scope.options && scope.options.getPlaceOnBlur) {