Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_feedback_view.js @ 9ffd10ce

History | View | Annotate | Download (5 kB)

1
;(function(root){
2

    
3
    // root
4
    var root = root;
5
    
6
    // setup namepsaces
7
    var snf = root.synnefo = root.synnefo || {};
8
    var api = snf.api = snf.api || {};
9
    var models = snf.models = snf.models || {}
10
    var storage = snf.storage = snf.storage || {};
11
    var ui = snf.ui = snf.ui || {};
12
    var util = snf.util = snf.util || {};
13

    
14
    var views = snf.views = snf.views || {}
15

    
16
    // shortcuts
17
    var bb = root.Backbone;
18

    
19
    views.FeedbackView = views.Overlay.extend({
20
        
21
        view_id: "feedback_view",
22
        content_selector: "#feedback-overlay-content",
23
        css_class: 'overlay-feedback overlay-info',
24
        overlay_id: "feedback-overlay",
25

    
26
        subtitle: "",
27
        title: "Send feedback",
28

    
29
        initialize: function(options) {
30
            views.FeedbackView.__super__.initialize.apply(this, arguments);
31
            
32
            _.bindAll(this, 'submit_form', 'show_error', 'show_success');
33

    
34
            this.submit = this.$("span.submit");
35
            this.text = this.$("textarea.feedback-message");
36
            this.text_row = this.$("div.form-field");
37
            this.messages = this.$(".messages").hide();
38
            this.error = this.$("p.error-message");
39
            this.success = this.$("p.success-message");
40
            this.form = this.$(".form");
41
            this.sending = this.$(".sending-message");
42

    
43
            this.init_handlers();
44
        },
45
        
46
        init_handlers: function() {
47
            this.submit.click(this.submit_form);
48
        },
49

    
50
        get_message: function() {
51
            var text = _(this.text.val()).trim();
52
            return text;
53
        },
54

    
55
        validate: function() {
56
            var msg = this.get_message();
57
            if (msg == "") {
58
                this.text_row.addClass("error");
59
                return false;
60
            } else {
61
                this.text_row.removeClass("error");
62
                return true;
63
            }
64
        },
65

    
66
        submit_form: function() {
67
            if (this.validate()) {
68
                this.show_sending();
69
                this.send_message(this.get_message(), {});
70
            } else {}
71
        },
72

    
73
        send_message: function(msg, extra) {
74
            var extra = extra || {};
75

    
76
            var data = {
77
                'feedback-msg': msg,
78
                'feedback-data': this.get_feedback_data() || ""
79
            }
80
            
81
            var opts = {
82
                'url': 'feedback',
83
                'data': $.param(data),
84
                'success': this.show_success,
85
                'error': this.show_error,
86
                'no_skip': true,
87
                'display': false,
88
                'handles_error': true
89
            }
90
            api.sync('create', undefined, opts);
91
        },
92

    
93
        get_feedback_data: function() {
94
            if (this.collect_data) return JSON.stringify(_.extend({}, snf.collect_user_data(),this.extra_data));
95
        },
96
        
97
        onOpen: function() {
98
            var self = this;
99
            var point = this.text.val().length;
100
            this.text.show().focus().setCursorPosition(point);
101

    
102
            this.$(".closeme").unbind("click");
103
            this.$(".closeme").bind("click", function(){
104
                self.hide("reset")
105
            });
106
        },
107

    
108
        show_form: function() {
109
            this.form.show();
110
            this.messages.hide();
111
            this.text.focus();
112
        },
113

    
114
        show_sending: function() {
115
            this.form.hide();
116
            this.messages.show();
117
            this.error.hide();
118
            this.success.hide();
119
            this.sending.show();
120
        },
121

    
122
        show_error: function() {
123
            this.form.hide();
124
            this.messages.show();
125
            this.error.show();
126
            this.success.hide();
127
            this.sending.hide();
128
        },
129

    
130
        show_success: function() {
131
            this.form.hide();
132
            this.messages.show();
133
            this.error.hide();
134
            this.success.show();
135
            this.sending.hide();
136
        },
137
        
138
        hide: function(reset_error_state) {
139
            // trigger api reset
140
            if (reset_error_state === "reset") {
141
                // report error feedback form
142
                if (snf.api.error_state == snf.api.STATES.ERROR) {
143
                    snf.api.trigger("reset");
144
                    ui.main.error_view.displaying_error = false;
145
                    ui.main.error_view.is_visible = false;
146
                }
147
            }
148
            ui.main.error_view.is_visible = false;
149
            views.FeedbackView.__super__.hide.apply(this);
150
        },
151

    
152
        show: function(data, collect_data, extra_data, cb) {
153
            // proxy error view visibility to avoid showing
154
            // errors while user sees feedback overlay
155
            ui.main.error_view.is_visible = true;
156

    
157
            this.data = data || "";
158
            this.cb = cb || function () {};
159
            this.collect_data = collect_data || false;
160
            this.extra_data = extra_data || {};
161

    
162
            views.FeedbackView.__super__.show.apply(this, arguments);
163
            this.text.val(data);
164
            this.show_form();
165
        }
166
    });
167
})(this);