aboutsummaryrefslogtreecommitdiffstats
path: root/src/js/views/reports.js
blob: f76db06a81e3aeb73cb1f227a75428d06d528179 (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
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
(function (FMS, Backbone, _, $) {
    _.extend( FMS, {
        ReportsView: FMS.FMSView.extend({
            template: 'reports',
            id: 'reports',
            next: 'around',
            prev: 'around',
            contentSelector: '#drafts',

            events: {
                'pagehide': 'destroy',
                'pagebeforeshow': 'beforeDisplay',
                'pageshow': 'afterDisplay',
                'vclick .del_report': 'deleteReport',
                'vclick .use_report': 'useReport',
                'vclick .ui-btn-left': 'onClickButtonPrev',
                'vclick .ui-btn-right': 'onClickButtonNext'
            },

            onClickButtonPrev: function(e) {
                $('#drafts').hide();
                $('body')[0].scrollTop = 0;
                e.preventDefault();
                this.navigate( this.prev, true );
            },

            onClickButtonNext: function(e) {
                $('#drafts').hide();
                $('body')[0].scrollTop = 0;
                e.preventDefault();
                this.navigate( this.next );
            },

            deleteReport: function(e) {
                e.preventDefault();
                var el = $(e.target);
                var id = el.parents('li').attr('id');
                var del = FMS.removeDraft( id, true );
                var that = this;
                del.done( function() { that.onRemoveDraft(el); } );
                del.fail( function() { that.onRemoveDraft(el); } );
            },

            setHeight: function(content, height) {
                content.css( 'min-height', content + 'px');
            },

            beforeDisplay: function() {
                if ( FMS.allDrafts.length === 0 ) {
                    $('#noreports').show();
                } else {
                    $('#report-list').show();
                }
            },

            useReport: function(e) {
                e.preventDefault();
                var el = $(e.target);
                var id = el.parents('li').attr('id');
                FMS.currentDraft = FMS.allDrafts.get(id);
                $('#drafts').hide();
                if ( FMS.currentDraft && FMS.currentDraft.get('lat') ) {
                    var coords = { latitude: FMS.currentDraft.get('lat'), longitude: FMS.currentDraft.get('lon') };
                    fixmystreet.latitude = coords.latitude;
                    fixmystreet.longitude = coords.longitude;

                    if ( fixmystreet.map ) {
                        var centre = new OpenLayers.LonLat( coords.longitude, coords.latitude );
                        centre.transform(
                            new OpenLayers.Projection("EPSG:4326"),
                            fixmystreet.map.getProjectionObject()
                        );

                        fixmystreet.map.panTo(centre);
                    }
                }
                this.navigate('around');
            },

            onRemoveDraft: function(el) {
                el.parents('li').remove();
                if ( FMS.allDrafts.length === 0 ) {
                    $('#report-list').hide();
                    $('#noreports').show();
                }
            },

            render: function(){
                if ( !this.template ) {
                    console.log('no template to render');
                    return;
                }
                template = _.template( tpl.get( this.template ) );
                if ( this.model ) {
                    this.$el.html(template({ model: this.model.toJSON(), drafts: FMS.allDrafts }));
                } else {
                    this.$el.html(template());
                }
                this.afterRender();
                return this;
            }
        })
    });
})(FMS, Backbone, _, $);