Blame view

src/js/directives/address-search/directive.js 3.22 KB
a0fba7c8   David Beech   david
1
(function(angular) {
ec938bf0   Tarpit Grover   first commit
2
3
    var app = angular.module('batmanDelivers');
    app.directive("addressSearch", addressSearch);
a0fba7c8   David Beech   david
4
    addressSearch.$inject = ['$parse', '$rootScope', '$http', '$timeout', '$sce']
ec938bf0   Tarpit Grover   first commit
5

a0fba7c8   David Beech   david
6
    function addressSearch($parse, $rootScope, $http, $timeout, $sce) {
ec938bf0   Tarpit Grover   first commit
7
8
        return {
            templateUrl: '/src/js/directives/address-search/partial.html',
a0fba7c8   David Beech   david
9
10
11
12
            replace: true, //study
            transclude: true, //study
            link: function(scope, elm, attrs) {

ec938bf0   Tarpit Grover   first commit
13
                scope.placeholder = attrs["placeholder"];
a0fba7c8   David Beech   david
14
15
16
17

                scope.address = {
                    AddressLine1: null,
                    AddressLine2: null,
484036e0   Tarpit Grover   WIP
18
19
20
                    City: null,
                    PostCode: null,
                    Latitude: null,
a0fba7c8   David Beech   david
21
22
                    Longitude: null,
                    Description: null
484036e0   Tarpit Grover   WIP
23
                }
a0fba7c8   David Beech   david
24
25
26
27
28
29
30
31
32

                scope.$watch('address.Description', function(value) {
                    scope.show = true;
                    //learn debounce
                    if (value && value.length > 3) {
                        scope.searching = true;
                        $http({
                            method: "GET",
                            url: "https://maps.googleapis.com/maps/api/place/autocomplete/json?input=" + value + "&key=AIzaSyAPYatPrCcmNcVdAO_MF0YhO3c1mgdLvuQ"
a668a573   Sean Wills   new
33
                        }).then(function mySuccess(response) {
ec938bf0   Tarpit Grover   first commit
34
                            scope.searching = false;
a0fba7c8   David Beech   david
35
36
37
38
39
40
41
42
43
44
45
46
47
                            scope.addresses = response.data.predictions;
                        }, function myError(response) {
                            scope.searching = false;
                            scope.error = response.statusText;
                        });
                    }
                });

                scope.selectAddress = function(place) {
                    $http({
                        method: "GET",
                        url: "https://maps.googleapis.com/maps/api/place/details/json?placeid=" + place.place_id + "&fields=geometry,adr_address,address_component,formatted_address&key=AIzaSyAPYatPrCcmNcVdAO_MF0YhO3c1mgdLvuQ"
                    }).then(function mySuccess(response) {
a0fba7c8   David Beech   david
48
                        scope.show = false;
ec938bf0   Tarpit Grover   first commit
49
                        scope.res = response.data.result;
ec938bf0   Tarpit Grover   first commit
50

a0fba7c8   David Beech   david
51
52
53
54
55
56
57
58
59
60
61
62
63
64
                        scope.address = {
                            AddressLine1: result.address_components[1].long_name,
                            AddressLine2: result.address_components[2].long_name,
                            City: result.address_components[3].long_name,
                            PostCode: result.address_components[0].long_name,
                            Latitude: result.geometry.location.lat,
                            Longitude: result.geometry.location.lng,
                            Description: place.description
                        }

                        scope.googleSearch = place.description;
                    }, function myError(response) {
                        scope.error = response.statusText;
                    });
ec938bf0   Tarpit Grover   first commit
65
66
67
                }

                scope.blur = function() {
a668a573   Sean Wills   new
68
                    setTimeout(function() {