aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/views/details_extra.js
blob: 160ff11d5408a0e67994f5e1ad160eef6f01275d (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
105
106
107
108
(function (FMS, Backbone, _, $) {
    _.extend( FMS, {
        DetailsExtraView: FMS.FMSView.extend({
            template: 'details_extra',
            id: 'details-extra-page',
            prev: 'details',
            next: 'submit-start',

            events: {
                'pagehide': 'destroy',
                'pagebeforeshow': 'beforeDisplay',
                'pageshow': 'afterDisplay',
                'vclick .ui-btn-left': 'onClickButtonPrev',
                'vclick .ui-btn-right': 'onClickButtonNext',
                'blur textarea': 'updateCurrentReport',
                'change select': 'updateCurrentReport',
                'blur input': 'updateCurrentReport'
            },

            afterRender: function() {
                this.populateFields();
            },

            onClickButtonPrev: function() {
                this.model.set('hasExtras', 0);
                this.updateCurrentReport();
                this.navigate( this.prev, true );
            },

            onClickButtonNext: function() {
                this.clearValidationErrors();
                var valid = 1;
                var that = this;

                var isRequired = function(index) {
                    var el = $(this);
                    if ( el.attr('required') && el.val() === '' ) {
                        valid = 0;
                        that.validationError(el.attr('id'), FMS.strings.required);
                    }
                };
                // do validation
                $('input').each(isRequired);
                $('textarea').each(isRequired);
                $('select').each(isRequired);
                this.model.set('hasExtras', 1);

                if ( valid ) {
                    this.clearValidationErrors();
                    this.updateCurrentReport();
                    this.navigate( this.next );
                }
            },

            validationError: function(id, error) {
                var el_id = '#' + id;
                var el = $(el_id);

                el.addClass('error');
                if ( el.val() === '' ) {
                    el.attr('orig-placeholder', el.attr('placeholder'));
                    el.attr('placeholder', error);
                }
            },

            clearValidationErrors: function() {
                $('.error').removeClass('error');
                $('.error').each(function(el) { if ( el.attr('orig-placeholder') ) { el.attr('placeholder', el.attr('orig-placeholder') ); } } );
            },

            updateSelect: function() {
                this.updateCurrentReport();
            },

            updateCurrentReport: function() {
                var fields = [];
                var that = this;
                var update = function(index) { 
                    var el = $(this);
                    if ( el.val() !== '' ) {
                        that.model.set(el.attr('name'), el.val());
                        fields.push(el.attr('name'));
                    } else {
                        that.model.set(el.attr('name'), '');
                    }

                };

                $('input').each(update);
                $('select').each(update);
                $('textarea').each(update);

                this.model.set('extra_details', fields);
                FMS.saveCurrentDraft();
            },

            populateFields: function() {
                var that = this;
                var populate = function(index) {
                    that.$(this).val(that.model.get(that.$(this).attr('name')));
                };
                this.$('input').each(populate);
                this.$('select').each(populate);
                this.$('textarea').each(populate);
            }
        })
    });
})(FMS, Backbone, _, $);