This repository has been archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathion-google-place.js
162 lines (138 loc) · 7.03 KB
/
ion-google-place.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
angular.module('ion-google-place', [])
.directive('ionGooglePlace', [
'$ionicTemplateLoader',
'$ionicBackdrop',
'$ionicPlatform',
'$q',
'$timeout',
'$rootScope',
'$document',
function($ionicTemplateLoader, $ionicBackdrop, $ionicPlatform, $q, $timeout, $rootScope, $document) {
return {
require: '?ngModel',
restrict: 'E',
template: '<input type="text" readonly="readonly" class="ion-google-place" autocomplete="off">',
replace: true,
scope: {
ngModel: '=?'
},
link: function(scope, element, attrs, ngModel) {
var unbindBackButtonAction;
scope.locations = [];
var geocoder = new google.maps.Geocoder();
var searchEventTimeout = undefined;
var POPUP_TPL = [
'<div class="ion-google-place-container">',
'<div class="bar bar-header item-input-inset">',
'<label class="item-input-wrapper">',
'<i class="icon ion-ios7-search placeholder-icon"></i>',
'<input class="google-place-search" type="search" ng-model="searchQuery" placeholder="' + (attrs.searchPlaceholder || 'Enter an address, place or ZIP code') + '">',
'</label>',
'<button class="button button-clear">',
attrs.labelCancel || 'Cancel',
'</button>',
'</div>',
'<ion-content class="has-header has-header">',
'<ion-list>',
'<ion-item ng-repeat="location in locations" type="item-text-wrap" ng-click="selectLocation(location)">',
'{{location.formatted_address}}',
'</ion-item>',
'</ion-list>',
'</ion-content>',
'</div>'
].join('');
var popupPromise = $ionicTemplateLoader.compile({
template: POPUP_TPL,
scope: scope,
appendTo: $document[0].body
});
popupPromise.then(function(el){
var searchInputElement = angular.element(el.element.find('input'));
scope.selectLocation = function(location){
ngModel.$setViewValue(location);
ngModel.$render();
el.element.css('display', 'none');
$ionicBackdrop.release();
if (unbindBackButtonAction) {
unbindBackButtonAction();
unbindBackButtonAction = null;
}
};
scope.$watch('searchQuery', function(query){
if (searchEventTimeout) $timeout.cancel(searchEventTimeout);
searchEventTimeout = $timeout(function() {
if(!query) return;
if(query.length < 3);
var req = scope.geocodeOptions || {};
req.address = query;
geocoder.geocode(req, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
scope.$apply(function(){
scope.locations = results;
});
} else {
// @TODO: Figure out what to do when the geocoding fails
}
});
}, 350); // we're throttling the input by 350ms to be nice to google's API
});
var onClick = function(e){
e.preventDefault();
e.stopPropagation();
$ionicBackdrop.retain();
unbindBackButtonAction = $ionicPlatform.registerBackButtonAction(closeOnBackButton, 250);
el.element.css('display', 'block');
searchInputElement[0].focus();
setTimeout(function(){
searchInputElement[0].focus();
},0);
};
var onCancel = function(e){
scope.searchQuery = '';
$ionicBackdrop.release();
el.element.css('display', 'none');
if (unbindBackButtonAction){
unbindBackButtonAction();
unbindBackButtonAction = null;
}
};
closeOnBackButton = function(e){
e.preventDefault();
el.element.css('display', 'none');
$ionicBackdrop.release();
if (unbindBackButtonAction){
unbindBackButtonAction();
unbindBackButtonAction = null;
}
}
element.bind('click', onClick);
element.bind('touchend', onClick);
el.element.find('button').bind('click', onCancel);
});
if(attrs.placeholder){
element.attr('placeholder', attrs.placeholder);
}
ngModel.$formatters.unshift(function (modelValue) {
if (!modelValue) return '';
return modelValue;
});
ngModel.$parsers.unshift(function (viewValue) {
return viewValue;
});
ngModel.$render = function(){
if(!ngModel.$viewValue){
element.val('');
} else {
element.val(ngModel.$viewValue.formatted_address || '');
}
};
scope.$on("$destroy", function(){
if (unbindBackButtonAction){
unbindBackButtonAction();
unbindBackButtonAction = null;
}
});
}
};
}
]);