aboutsummaryrefslogtreecommitdiffstats
path: root/www/js/views/fms.js
blob: 6ff569bd7ccd82dfb69d5419f91acfa51b9dae92 (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
109
110
111
112
113
114
115
116
(function (FMS, Backbone, _, $) {
    _.extend( FMS, {
        FMSView: Backbone.View.extend({
            tag: 'div',
            bottomMargin: 20,
            contentSelector: '[data-role="content"]',

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

            back: function(e) {
                if ( $('#help #dismiss').is(':visible') ) {
                    FMS.helpHide();
                } else if ( this._back ) {
                    this._back(e);
                } else if ( this.prev ) {
                    this.onClickButtonPrev(e);
                }
            },

            render: function(){
                if ( !this.template ) {
                    FMS.printDebug('no template to render');
                    return;
                }
                template = _.template( tpl.get( this.template ) );
                var args = null;
                if ( this.options.msg ) {
                    args = { msg: this.options.msg };
                }
                if ( this.model ) {
                    if ( args ) {
                        args.model = this.model.toJSON();
                    } else {
                        args = this.model.toJSON();
                    }
                }
                this.$el.html(template(args));
                this.afterRender();
                return this;
            },

            fixPageHeight: function() {
                var header = this.$("div[data-role='header']:visible"),
                content = this.$(this.contentSelector),
                top = content.position().top,
                viewHeight = $(window).height(),
                contentHeight = FMS.windowHeight - header.outerHeight() - this.bottomMargin;

                this.setHeight( content, contentHeight - top );
            },

            setHeight: function(content, height) {
                content.height(height);
            },

            afterRender: function() {},

            beforeDisplay: function() {
                this.fixPageHeight();
            },

            afterDisplay: function() {},

            navigate: function( route, reverse ) {
                if ( FMS.isAndroid ) {
                    cordova.plugins.Keyboard.close();
                }
                if ( reverse ) {
                    FMS.router.reverseTransition();
                }

                FMS.router.navigate( route, { trigger: true } );
            },

            onClickButtonPrev: function(e) {
                e.preventDefault();
                this.navigate( this.prev, true );
            },

            onClickButtonNext: function(e) {
                e.preventDefault();
                this.navigate( this.next );
            },

            displayAlert: function(msg) {
                navigator.notification.alert(msg, null, CONFIG.APP_NAME);
            },

            validationError: function( id, error ) {
                var el_id = '#' + id;
                var el = $(el_id);
                var err = '<div for="' + id + '" class="form-error">' + error + '</div>';
                if ( $('div[for='+id+']').length === 0 ) {
                    el.before(err);
                    el.addClass('form-error');
                }
            },

            clearValidationErrors: function() {
                $('div.form-error').remove();
                $('.form-error').removeClass('form-error');
            },

            destroy: function() { FMS.printDebug('destory for ' + this.id); this._destroy(); this.remove(); },

            _destroy: function() {}
        })
    });
    _.extend( FMS.FMSView, Backbone.Events );
})(FMS, Backbone, _, $);