Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_feedback_view.js @ edd1d565

History | View | Annotate | Download (4.3 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
                'handles_error': true
87
            }
88
            api.sync('create', undefined, opts);
89
        },
90

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

    
100
        show_form: function() {
101
            this.form.show();
102
            this.messages.hide();
103
            this.text.focus();
104
        },
105

    
106
        show_sending: function() {
107
            this.form.hide();
108
            this.messages.show();
109
            this.error.hide();
110
            this.success.hide();
111
            this.sending.show();
112
        },
113

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

    
122
        show_success: function() {
123
            this.form.hide();
124
            this.messages.show();
125
            this.error.hide();
126
            this.success.show();
127
            this.sending.hide();
128
        },
129
        
130
        hide: function() {
131
            snf.api.error_state = false;
132
            snf.api.trigger("change:error_state", snf.api.error_state);
133
            views.FeedbackView.__super__.hide.apply(this);
134
        },
135

    
136
        show: function(data, collect_data, extra_data, cb) {
137
            this.data = data || "";
138
            this.cb = cb || function () {};
139
            this.collect_data = collect_data || false;
140
            this.extra_data = extra_data || {};
141

    
142
            views.FeedbackView.__super__.show.apply(this, arguments);
143
            this.text.val(data);
144
            this.show_form();
145
        }
146
    });
147
})(this);