Revision 123fd2f5 ui/static/snf/js/ui/web/ui_invitations_view.js

b/ui/static/snf/js/ui/web/ui_invitations_view.js
24 24
        overlay_id: "invitations-overlay",
25 25

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

  
29 29
        initialize: function(options) {
30 30
            views.InvitationsView.__super__.initialize.apply(this, arguments);
......
45 45
            this.inv_sent_per_page = 9;
46 46

  
47 47
            this.init_handlers();
48

  
49
            this.invitations_retrieved = false;
50
            this.loading_invitations = this.$(".loading-invitations");
48 51
        },
49 52

  
50 53
        init_handlers: function() {
51 54
            var self = this;
52
            this.add.click(this.add_new_entry);
55
            this.add.click(function(){
56
                self.add_new_entry().find("input.name").focus();
57
            });
53 58
            this.send.click(this.send_entries);
54 59
            this.remove.live('click', function() {
55 60
                return self.remove_entry($(this).parent().parent());
56 61
            });
62

  
57 63
        },
58 64
        
59 65
        remove_entry: function(entry) {
......
64 70

  
65 71
        add_new_entry: function() {
66 72
            var new_entry = this.create_form_entry().show()
73

  
74
            var name = "inv-entry-" + this.get_entries().length;
75
            new_entry.find("input.name").attr("name", "name-" + name);
76
            new_entry.find("input.email").attr("name", "email-" + name);
77
            
78
            var self = this;
79
            new_entry.find("input").bind("keydown", function(e){
80
                e.keyCode = e.keyCode || e.which;
81
                if (e.keyCode == 13) { self.send_entries() };
82
            })
83

  
67 84
            this.form_entries.append(new_entry).show();
68
            $(new_entry.find("input").get(0)).focus();
69 85
            this.fix_entries();
86
            return new_entry;
70 87
        },
71 88
        
72 89
        show_entry_error: function(entry, error) {
......
84 101
        entry_is_valid: function(entry) {
85 102
            var data = this.get_entry_data(entry);
86 103

  
104
            if (data.name == "" && data.email == "") {
105
                return false;
106
            }
107

  
87 108
            entry.find(".send-error").hide();
88 109
            entry.removeClass("error");
89 110
            entry.find("input").removeClass("has-errors");
......
92 113
            if (!data.name || data.name.split(" ").length == 1) {
93 114
                error = "Invalid name";
94 115
                entry.find("input.name").addClass("has-errors");
116
                entry.find("input.name").focus();
95 117
            }
96 118

  
97 119
            var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
98 120
            if (!data.email || reg.test(data.email) == false) {
99 121
                error = "Invalid email";
100 122
                entry.find("input.email").addClass("has-errors");
123
                entry.find("input.email").focus();
101 124
            }
102 125
            
103 126
            if (error) { this.show_entry_error(entry, error) };
......
108 131
            var self = this;
109 132
            this.form_entries.find(".form-entry").each(function(index, el) { self.entry_is_valid($(el)) });
110 133
            var entries_to_send = this.form_entries.find(".form-entry:not(.error):not(.sending)");
111
            this._send_entries(entries_to_send);
134

  
135
            entries = _.filter(entries_to_send, function(e){
136
                var data = self.get_entry_data($(e));
137
                if (data.name == "" && data.email == "") {
138
                    return false;
139
                }
140
                return true;
141
            })
142
            this._send_entries(entries);
112 143
        },
113 144

  
114 145
        _send_entries: function(entries) {
......
126 157

  
127 158
        invitation_send: function(entry, data) {
128 159
            entry.removeClass("sending");
129
            if (data.errors && data.errors.length) {
130
                this.show_entry_error($(entry), data.errors[0]);
160
            if (data.errors && data.errors.length > 0) {
161
                this.show_entry_error($(entry), data.errors[0].msg);
131 162
                return;
132 163
            } else {
133 164
                entry.remove();
......
136 167
        },
137 168

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

  
142 173
            this.top_info.append(msg_el);
143 174

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

  
148 179
            this.fix_entries();
149 180
            this.reset_invitations_sent();
......
175 206
            this.$(".remove-invitation").hide();
176 207
            if (this.get_entries().length == 0) {
177 208
                this.add_new_entry();
209
                this.add_new_entry();
178 210
            }
179 211

  
180 212
            if (this.get_entries().length > 1) {
......
183 215
            this.$(".form-entry:first-child label").show();
184 216
            this.$(".form-entry:not(:first-child) label").hide();
185 217
        },
186

  
218
        
187 219
        show: function() {
188 220
            views.InvitationsView.__super__.show.apply(this, arguments);
189 221
            this.current_page = 0;
190
            this.reset_invitations_sent();
222
            this.reset_sent();
191 223
            this.reset();
192 224

  
193 225
            this.add_new_entry();
194 226
            this.add_new_entry();
195
            this.add_new_entry();
227

  
228
            if (this.invitations_retrieved) {
229
                this.loading_invitations.hide();
230
            }
231
        },
232

  
233
        reset_sent: function() {
234
            this.reset_invitations_sent();
235
            this.add_invitations_sent([]);
196 236
        },
197 237

  
198 238
        create_form_entry: function() {
......
218 258
            var url = snf.config.invitations_url;
219 259
            params = {
220 260
                success: function(data) {
261
                    self.invitations_retrieved = true;
262
                    self.loading_invitations.hide();
263

  
221 264
                    if (!data || !data.invitations) {
222 265
                        self.show_invitations_sent_error();
223 266
                    } else {
......
244 287

  
245 288
            snf.api.sync("read", undefined, params);
246 289
        },
290
        
291
        resend_succeed: function(inv, el) {
292
            el.find(".status.sent").removeClass("hidden").hide();
293
            el.find(".status.resend").removeClass("hidden").show();
294
            el.find(".status.sending").removeClass("hidden").hide();
295

  
296
            var msg = $('<div class="msg success">Invitation has been resent to <span class="email">{0}</span>.</div>'.format(inv.target));
297
            this.$(".resent-info").append(msg);
298
            setTimeout(function(){ $(msg).fadeOut(600)}, 5000);
299
        },
300

  
301
        resend_failed: function(inv, el) {
302
            el.find(".status.sent").removeClass("hidden").hide();
303
            el.find(".status.resend").removeClass("hidden").show();
304
            el.find(".status.sending").removeClass("hidden").hide();
305
            
306
            var msg = $('<div class="msg err-msg">Resend to <span class="email">{0}</span> failed.</div>'.format(inv.target));
307
            this.$(".resent-info").append(msg);
308
            setTimeout(function(){ $(msg).fadeOut(600)}, 5000);
309
        },
310

  
311
        resend_invitation: function(id, el, inv) {
312
            var self = this;
313
            var inv = inv;
314
            var id = id;
315
            var el = el;
316

  
317
            el.find(".status.sent").removeClass("hidden").hide();
318
            el.find(".status.resend").removeClass("hidden").hide();
319
            el.find(".status.sending").removeClass("hidden").show();
320

  
321
            var url = snf.config.invitations_url + "/resend/";
322
            var payload = "invid=" + id;
323
            params = {
324
                success: function(data) {
325
                    self.resend_succeed(inv, el);
326
                },
327
                error: function() {
328
                    self.resend_failed(inv, el);
329
                },
330
                data: payload,
331
                url: url,
332
                skip_api_error: true
333
            }
334

  
335
            snf.api.sync("create", undefined, params);
336
        },
247 337

  
248 338
        add_invitations_sent: function(invs) {
339
            var self = this;
249 340
            _.each(invs, _.bind(function(inv) {
250 341
                var el = this.new_invitation_sent_el();
342
                var invitation = inv;
343

  
251 344
                el.find(".name").text(inv.targetname);
252 345
                el.find(".email").text(inv.target);
253
                el.find(".action").addClass("sent");
346
                
347
                el.find(".status.sent").removeClass("hidden").show();
348
                el.find(".status.resend").removeClass("hidden").hide();
349
                el.find(".status.sending").removeClass("hidden").hide();
350

  
254 351
                if (!inv.accepted) {
255
                    el.find(".action").removeClass("resend").addClass("resend")
352
                    el.find(".status.resend").show();
353
                    el.find(".status.sent").hide();
354
                    el.find(".status.sending").hide();
256 355
                }
356

  
357
                el.find(".status.resend").click(function(){
358
                    self.resend_invitation(invitation.id, el, invitation);
359
                })
360

  
257 361
                el.removeClass("hidden");
258 362
                this.sent.append(el);
259 363
            }, this));
260 364
            this.update_pagination();
261 365
            this.sent_pages.trigger("setPage", this.current_page || 0);
262 366
        },
367

  
368
        onOpen: function() {
369
            views.InvitationsView.__super__.onOpen.apply(this, arguments);
370
            setTimeout(function(){$(this.$("input.name:visible").get(0)).focus()}, 100);
371
        },
263 372
        
264 373
        inv_sent_per_page: 5,
265 374
        update_pagination: function() {
266 375
            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});
376
            this.sent_pages.pagination(this.sent.children().length, 
377
                                       {items_per_page:this.inv_sent_per_page, callback: this.page_cb});
268 378
        },
269 379

  
270 380
        page_cb: function(index, pager) {

Also available in: Unified diff