Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_feedback_view.js @ 336ddd59

History | View | Annotate | Download (6.6 kB)

1
// Copyright 2011 GRNET S.A. All rights reserved.
2
// 
3
// Redistribution and use in source and binary forms, with or
4
// without modification, are permitted provided that the following
5
// conditions are met:
6
// 
7
//   1. Redistributions of source code must retain the above
8
//      copyright notice, this list of conditions and the following
9
//      disclaimer.
10
// 
11
//   2. Redistributions in binary form must reproduce the above
12
//      copyright notice, this list of conditions and the following
13
//      disclaimer in the documentation and/or other materials
14
//      provided with the distribution.
15
// 
16
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
// POSSIBILITY OF SUCH DAMAGE.
28
// 
29
// The views and conclusions contained in the software and
30
// documentation are those of the authors and should not be
31
// interpreted as representing official policies, either expressed
32
// or implied, of GRNET S.A.
33
// 
34

    
35
;(function(root){
36

    
37
    // root
38
    var root = root;
39
    
40
    // setup namepsaces
41
    var snf = root.synnefo = root.synnefo || {};
42
    var api = snf.api = snf.api || {};
43
    var models = snf.models = snf.models || {}
44
    var storage = snf.storage = snf.storage || {};
45
    var ui = snf.ui = snf.ui || {};
46
    var util = snf.util = snf.util || {};
47

    
48
    var views = snf.views = snf.views || {}
49

    
50
    // shortcuts
51
    var bb = root.Backbone;
52

    
53
    views.FeedbackView = views.Overlay.extend({
54
        
55
        view_id: "feedback_view",
56
        content_selector: "#feedback-overlay-content",
57
        css_class: 'overlay-feedback overlay-info',
58
        overlay_id: "feedback-overlay",
59

    
60
        subtitle: "",
61
        title: "Send feedback",
62

    
63
        initialize: function(options) {
64
            views.FeedbackView.__super__.initialize.apply(this, arguments);
65
            
66
            _.bindAll(this, 'submit_form', 'show_error', 'show_success');
67

    
68
            this.submit = this.$("span.submit");
69
            this.text = this.$("textarea.feedback-message");
70
            this.text_row = this.$("div.form-field");
71
            this.messages = this.$(".messages").hide();
72
            this.error = this.$("p.error-message");
73
            this.success = this.$("p.success-message");
74
            this.form = this.$(".form");
75
            this.sending = this.$(".sending-message");
76

    
77
            this.init_handlers();
78
        },
79
        
80
        init_handlers: function() {
81
            this.submit.click(this.submit_form);
82
        },
83

    
84
        get_message: function() {
85
            var text = _(this.text.val()).trim();
86
            return text;
87
        },
88

    
89
        validate: function() {
90
            var msg = this.get_message();
91
            if (msg == "") {
92
                this.text_row.addClass("error");
93
                return false;
94
            } else {
95
                this.text_row.removeClass("error");
96
                return true;
97
            }
98
        },
99

    
100
        submit_form: function() {
101
            if (this.validate()) {
102
                this.show_sending();
103
                this.send_message(this.get_message(), {});
104
            } else {}
105
        },
106

    
107
        send_message: function(msg, extra) {
108
            var extra = extra || {};
109

    
110
            var data = {
111
                'feedback-msg': msg,
112
                'feedback-data': this.get_feedback_data() || ""
113
            }
114
            
115
            var opts = {
116
                'url': 'feedback',
117
                'data': $.param(data),
118
                'success': this.show_success,
119
                'error': this.show_error,
120
                'no_skip': true,
121
                'display': false,
122
                'handles_error': true
123
            }
124
            api.sync('create', undefined, opts);
125
        },
126

    
127
        get_feedback_data: function() {
128
            if (this.collect_data) return JSON.stringify(_.extend({}, snf.collect_user_data(),this.extra_data));
129
        },
130
        
131
        onOpen: function() {
132
            var self = this;
133
            var point = this.text.val().length;
134
            this.text.show().focus().setCursorPosition(point);
135

    
136
            this.$(".closeme").unbind("click");
137
            this.$(".closeme").bind("click", function(){
138
                self.hide("reset")
139
            });
140
        },
141

    
142
        show_form: function() {
143
            this.form.show();
144
            this.messages.hide();
145
            this.text.focus();
146
        },
147

    
148
        show_sending: function() {
149
            this.form.hide();
150
            this.messages.show();
151
            this.error.hide();
152
            this.success.hide();
153
            this.sending.show();
154
        },
155

    
156
        show_error: function() {
157
            this.form.hide();
158
            this.messages.show();
159
            this.error.show();
160
            this.success.hide();
161
            this.sending.hide();
162
        },
163

    
164
        show_success: function() {
165
            this.form.hide();
166
            this.messages.show();
167
            this.error.hide();
168
            this.success.show();
169
            this.sending.hide();
170
        },
171
        
172
        hide: function(reset_error_state) {
173
            // trigger api reset
174
            if (reset_error_state === "reset") {
175
                // report error feedback form
176
                if (snf.api.error_state == snf.api.STATES.ERROR) {
177
                    snf.api.trigger("reset");
178
                    ui.main.error_view.displaying_error = false;
179
                    ui.main.error_view.is_visible = false;
180
                }
181
            }
182
            ui.main.error_view.is_visible = false;
183
            views.FeedbackView.__super__.hide.apply(this);
184
        },
185

    
186
        show: function(data, collect_data, extra_data, cb) {
187
            // proxy error view visibility to avoid showing
188
            // errors while user sees feedback overlay
189
            ui.main.error_view.is_visible = true;
190

    
191
            this.data = data || "";
192
            this.cb = cb || function () {};
193
            this.collect_data = collect_data || false;
194
            this.extra_data = extra_data || {};
195

    
196
            views.FeedbackView.__super__.show.apply(this, arguments);
197
            this.text.val(data);
198
            this.show_form();
199
        }
200
    });
201
})(this);