aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/views/search.js
blob: 8d6d2df2c37ca18a443860968434215a81da3fa1 (plain)
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
(function (FMS, Backbone, _, $) {
    _.extend( FMS, {
        SearchView: FMS.FMSView.extend({
            template: 'address_search',
            id: 'search-page',

            events: {
                'click a.address': 'goAddress',
                'click #submit': 'search',
                'click #locate': 'goLocate',
                'pagehide': 'destroy',
                'pageshow': 'afterDisplay'
            },

            search: function() {
                var pc = this.$('#pc').val();
                this.listenTo(FMS.locator, 'search_located', this.searchSuccess );
                this.listenTo(FMS.locator, 'search_failed', this.searchFail);

                $('#ajaxOverlay').show();
                FMS.locator.lookup(pc);
            },

            searchSuccess: function( info ) {
                this.stopListening(FMS.locator);
                var coords = info.coordinates;
                FMS.currentLocation = coords;
                this.navigate('around');
            },

            goAddress: function(e) {
                var t = $(e.target);
                var lat = t.attr('data-lat');
                var long = t.attr('data-long');

                FMS.currentLocation = { latitude: lat, longitude: long };
                this.navigate('around');
            },

            searchFail: function( details ) {
                this.stopListening(FMS.locator);
                $('#ajaxOverlay').hide();
                if ( details.msg ) {
                    this.displayError( details.msg );
                } else if ( details.locations ) {
                    var multiple = '';
                    for ( var i = 0; i < details.locations.length; i++ ) {
                        var loc = details.locations[i];
                        var li = '<li><a class="address" id="location_' + i + '" data-lat="' + loc.lat + '" data-long="' + loc.long + '">' + loc.address + '</a></li>';
                        multiple = multiple + li;
                    }
                    $('#front-howto').html('<ul>' + multiple + '</ul>');
                } else {
                    this.displayError( FMS.strings.location_problem );
                }
            },

            goLocate: function(e) {
                e.preventDefault();
                this.navigate( 'around' );
            },

            destroy: function() {
                delete FMS.searchMessage;
                this.stopListening(FMS.locator);
            }
        })
    });
})(FMS, Backbone, _, $);