Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_invitations_view.js @ 1e827d67

History | View | Annotate | Download (10 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.InvitationsView = views.Overlay.extend({
20
        
21
        view_id: "invitations_view",
22
        content_selector: "#invitations-overlay-content",
23
        css_class: 'overlay-invitations overlay-info',
24
        overlay_id: "invitations-overlay",
25

    
26
        subtitle: "",
27
        title: "Invitations",
28

    
29
        initialize: function(options) {
30
            views.InvitationsView.__super__.initialize.apply(this, arguments);
31

    
32
            _.bindAll(this);
33

    
34
            this.entry_tpl = this.$(".form-entry-tpl");
35
            this.form_entries = this.$(".form-entries");
36
            this.add = this.$(".add-new-invitation");
37
            this.remove = this.$(".remove-invitation");
38
            this.send = this.$(".send-invitations");
39
            this.top_info = this.$(".top-info");
40
            this.sent = this.$(".invitations-sent-cont");
41
            this.sent_pages = this.$(".invitations-sent-pages");
42
            this.sent_tpl = this.$(".invitation-sent-tpl");
43
            this.entry_tpl.hide();
44

    
45
            this.inv_sent_per_page = 9;
46

    
47
            this.init_handlers();
48
        },
49

    
50
        init_handlers: function() {
51
            var self = this;
52
            this.add.click(this.add_new_entry);
53
            this.send.click(this.send_entries);
54
            this.remove.live('click', function() {
55
                return self.remove_entry($(this).parent().parent());
56
            });
57
        },
58
        
59
        remove_entry: function(entry) {
60
            if (entry.hasClass("sending")) { return };
61
            entry.remove();
62
            this.fix_entries();
63
        },
64

    
65
        add_new_entry: function() {
66
            var new_entry = this.create_form_entry().show()
67
            this.form_entries.append(new_entry).show();
68
            $(new_entry.find("input").get(0)).focus();
69
            this.fix_entries();
70
        },
71
        
72
        show_entry_error: function(entry, error) {
73
            entry.find(".send-error").text(error)
74
            entry.find(".send-error").show();
75
            entry.addClass("error");
76
            entry.find("input").attr("disabled", false);
77
        },
78
        
79
        get_entry_data: function(entry) {
80
            var data = {name: entry.find("input.name").val(), email:entry.find("input.email").val()};
81
            return data;
82
        },
83

    
84
        entry_is_valid: function(entry) {
85
            var data = this.get_entry_data(entry);
86

    
87
            entry.find(".send-error").hide();
88
            entry.removeClass("error");
89
            entry.find("input").removeClass("has-errors");
90

    
91
            error = false;
92
            if (!data.name || data.name.split(" ").length == 1) {
93
                error = "Invalid name";
94
                entry.find("input.name").addClass("has-errors");
95
            }
96

    
97
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
98
            if (!data.email || reg.test(data.email) == false) {
99
                error = "Invalid email";
100
                entry.find("input.email").addClass("has-errors");
101
            }
102
            
103
            if (error) { this.show_entry_error(entry, error) };
104
            return error
105
        },
106
        
107
        send_entries: function() {
108
            var self = this;
109
            this.form_entries.find(".form-entry").each(function(index, el) { self.entry_is_valid($(el)) });
110
            var entries_to_send = this.form_entries.find(".form-entry:not(.error):not(.sending)");
111
            this._send_entries(entries_to_send);
112
        },
113

    
114
        _send_entries: function(entries) {
115
            $(entries).addClass("sending").find("input").attr("disabled", true);
116
            var self = this;
117
            _.each(entries, function(e) {
118
                var e = $(e);
119
                var data = self.get_entry_data(e);
120
                self.send_invitation(data.name, 
121
                                     data.email,
122
                                     _.bind(self.invitation_send, this, e), 
123
                                     _.bind(self.invitation_failed, this, e));
124
            });
125
        },
126

    
127
        invitation_send: function(entry, data) {
128
            entry.removeClass("sending");
129
            if (data.errors && data.errors.length) {
130
                this.show_entry_error($(entry), data.errors[0]);
131
                return;
132
            } else {
133
                entry.remove();
134
                this.show_send_success(entry.find("input.name").val(), data);
135
            }
136
        },
137

    
138
        show_send_success: function(to, data) {
139
            var msg = "Invitation to " + to + " was sent.";
140
            var msg_el = $('<div class="msg">{0}</div>'.format(msg));
141

    
142
            this.top_info.append(msg_el);
143

    
144
            window.setTimeout(function(){
145
                msg_el.fadeOut(600, function(){$(this).remove()});
146
            }, 2000);
147

    
148
            this.fix_entries();
149
            this.reset_invitations_sent();
150
        },
151

    
152
        invitation_failed: function(entry) {
153
            entry.removeClass("sending");
154
            this.show_entry_error(entry, "Cannot send email, please try again later");
155
        },
156

    
157
        send_invitation: function(name, email, success, fail) {
158
            var url = snf.config.invitations_url;
159
            var payload = {name_1: name, email_1: email, csrftoken: $.cookie('csrftoken')};
160
            params = {
161
                success: success,
162
                error: fail,
163
                url: url,
164
                data: $.param(payload),
165
                skip_api_error: true
166
            }
167
            snf.api.sync("create", undefined, params);
168
        },
169

    
170
        get_entries: function() {
171
            return this.form_entries.find(".form-entry");
172
        },
173

    
174
        fix_entries: function() {
175
            this.$(".remove-invitation").hide();
176
            if (this.get_entries().length == 0) {
177
                this.add_new_entry();
178
            }
179

    
180
            if (this.get_entries().length > 1) {
181
                this.$(".remove-invitation").show();
182
            }
183
            this.$(".form-entry:first-child label").show();
184
            this.$(".form-entry:not(:first-child) label").hide();
185
        },
186

    
187
        show: function() {
188
            views.InvitationsView.__super__.show.apply(this, arguments);
189
            this.current_page = 0;
190
            this.reset_invitations_sent();
191
            this.reset();
192

    
193
            this.add_new_entry();
194
            this.add_new_entry();
195
            this.add_new_entry();
196
        },
197

    
198
        create_form_entry: function() {
199
            return this.entry_tpl.clone().removeClass("form-entry-tpl").addClass("form-entry").removeClass("hidden");
200
        },
201
            
202
        reset: function() {
203
            this.get_entries().remove();
204
            this.add_new_entry();
205
        },
206
        
207
        new_invitation_sent_el: function() {
208
            return this.sent_tpl.clone().removeClass("invitation-sent-tpl").addClass("invitation-sent");
209
        },
210
        
211
        show_invitations_sent_error: function() {
212
            this.sent.hide();
213
            this.$(".invitations-sent-error").show();
214
        },
215

    
216
        reset_invitations_sent: function() {
217
            var self = this;
218
            var url = snf.config.invitations_url;
219
            params = {
220
                success: function(data) {
221
                    if (!data || !data.invitations) {
222
                        self.show_invitations_sent_error();
223
                    } else {
224
                        self.sent.empty();
225
                        self.add_invitations_sent(data.invitations);
226
                    }
227
                    
228
                    //data.invitations_left = 0;
229
                    self.$(".description .left").text(data.invitations_left);
230
                    if(data.invitations_left > 0) {
231
                        self.$(".invitations-form").show();
232
                        self.$(".description .left").removeClass("none");
233
                        self.el.removeClass("none-left");
234
                    } else {
235
                        self.$(".invitations-form").hide();
236
                        self.$(".description .left").addClass("none");
237
                        self.el.addClass("none-left");
238
                    }
239
                },
240
                error: _.bind(this.show_invitations_sent_error, this),
241
                url: url,
242
                skip_api_error: true
243
            }
244

    
245
            snf.api.sync("read", undefined, params);
246
        },
247

    
248
        add_invitations_sent: function(invs) {
249
            _.each(invs, _.bind(function(inv) {
250
                var el = this.new_invitation_sent_el();
251
                el.find(".name").text(inv.targetname);
252
                el.find(".email").text(inv.target);
253
                el.find(".action").addClass("sent");
254
                if (!inv.accepted) {
255
                    el.find(".action").removeClass("resend").addClass("resend")
256
                }
257
                el.removeClass("hidden");
258
                this.sent.append(el);
259
            }, this));
260
            this.update_pagination();
261
            this.sent_pages.trigger("setPage", this.current_page || 0);
262
        },
263
        
264
        inv_sent_per_page: 5,
265
        update_pagination: function() {
266
            this.sent.css({minHeight:this.inv_sent_per_page * 35 + "px"})
267
            this.sent_pages.pagination(this.sent.children().length, {items_per_page:this.inv_sent_per_page, callback: this.page_cb});
268
        },
269

    
270
        page_cb: function(index, pager) {
271
            this.current_page = index;
272
            var start = index * this.inv_sent_per_page;
273
            var end = start + this.inv_sent_per_page -1;
274
            var items = this.sent.children();
275
            items.hide().removeClass("last");
276
            for (var i = start; i<=end; i++) {
277
                $(items.get(i)).show();
278
            }
279
            $(items.get(end)).addClass("last");
280
            return false;
281
        }
282
        
283
    });
284
})(this);
285