summaryrefslogtreecommitdiffstats
path: root/hyperkitty/static/hyperkitty/js/hyperkitty-thread.js
blob: e6c726f92b73a111fe98ab5adcbfa65d408d9c38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
 * Copyright (C) 1998-2012 by the Free Software Foundation, Inc.
 *
 * This file is part of HyperKitty.
 *
 * HyperKitty is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * HyperKitty is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * HyperKitty.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Aurelien Bompard <abompard@fedoraproject.org>
 */


/*
 * Categories
 */

function setup_category() {
    $(".thread-category form").submit(function (e) {
        e.preventDefault();
        var widget = $(this).parents(".thread-category").first();
        $.ajax({
            type: "POST",
            //dataType: "json",
            data : $(this).serialize(),
            url: $(this).attr("action"),
            success: function(data) {
                widget.html(data);
                setup_category();
            },
            error: function(jqXHR, textStatus, errorThrown) {
                // authentication and invalid data
                alert(jqXHR.responseText);
            }
        });
    });
    $(".thread-category a.label").click(function(e) {
        e.preventDefault();
        if ($(this).hasClass("disabled")) {
            return;
        }
        $(this).hide()
            .parents(".thread-category").first()
            .find("form").show();
    });
    $(".thread-category form select").change(function() {
        $(this).parents("form").first().submit();
    });
}


/*
 * Tagging
 */

function setup_tags() {
    function post_tags(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            dataType: "json",
            data : $(this).serialize(),
            url: $(this).attr("action"),
            success: function(data) {
                $("#tags").html(data.html);
                $("#tags form").submit(post_tags);
                $("#tags form a").click(function(e) {
                    e.preventDefault();
                    $(this).parents("form").first().submit();
                });
                $(this).find("#id_tag").value("");
            },
            error: function(jqXHR, textStatus, errorThrown) {
                // authentication and invalid data
                alert(jqXHR.responseText);
            }
        });
    }
    $("#add-tag-form").submit(post_tags);
    $("#tags form").submit(post_tags);
    $("#tags form a").click(function(e) {
        e.preventDefault();
        $(this).parents("form").first().submit();
    });
    // Autocomplete
    $("#id_tag").autocomplete({
        //minLength: 2,
        source: $("#add-tag-form").attr("data-autocompleteurl")
    });
}


/*
 * Favorites
 */

function setup_favorites() {
    $(".favorite input[name='action']").bind("change", function() {
        // bind the links' visibilities to the hidden input status
        var form = $(this).parents("form").first();
        if ($(this).val() === "add") {
            form.find("a.saved").hide();
            form.find("a.notsaved").show();
        } else {
            form.find("a.notsaved").hide();
            form.find("a.saved").show();
        }
    }).trigger("change");
    $(".favorite a").bind("click", function(e) {
        e.preventDefault();
        if ($(this).hasClass("disabled")) {
            return;
        }
        var form = $(this).parents("form").first();
        var action_field = form.find("input[name='action']");
        var data = form_to_json(form);
        $.ajax({
            type: "POST",
            url: form.attr("action"),
            dataType: "text",
            data: data,
            success: function(response) {
                // Update the UI
                if (action_field.val() === "add") {
                    action_field.val("rm");
                } else {
                    action_field.val("add");
                }
                action_field.trigger("change");
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
    });
}


/*
 * Replies
 */

function setup_emails_list(baseElem) {
    if (!baseElem) {
        baseElem = document;
    }
    // Attachements
    $(baseElem).find(".email-info .attachments a.attachments").each(function() {
        var att_list = $(this).next("ul.attachments-list");
        var pos = $(this).position();
        att_list.css("left", pos.left);
        $(this).click(function() {
            att_list.slideToggle('fast');
        });
    });
    // Quotes
    $(baseElem).find('div.email-body .quoted-switch a')
        .click(function(e) {
            e.preventDefault();
            $(this).parent().next(".quoted-text").slideToggle('fast');
        });
    setup_replies(baseElem);
}

function fold_quotes(baseElem) {
    $(baseElem).find('div.email-body .quoted-text').each(function() {
        var linescount = $(this).text().split("\n").length;
        if (linescount > 3) {
            // hide if the quote is more than 3 lines long
            $(this).hide();
        }
    });
}

function setup_replies(baseElem) {
    if (!baseElem) {
        baseElem = document;
    }
    $(baseElem).find("a.reply").click(function(e) {
        e.preventDefault();
        if (!$(this).hasClass("disabled")) {
            $(this).next().slideToggle("fast", function() {
                if ($(this).css("display") === "block") {
                    $(this).find("textarea").focus();
                }
            });
        }
    });
    $(baseElem).find(".reply-form button[type='submit']").click(function(e) {
        e.preventDefault();
        var form = $(this).parents("form").first();
        // remove previous error messages
        form.find("div.reply-result").remove();
        var data = form_to_json(form);
        $.ajax({
            type: "POST",
            url: form.attr("action"),
            dataType: "json",
            data: data,
            success: function(response) {
                var reply = $(response.message_html);
                reply.insertAfter(form.parents(".email").first().parent());
                form.parents(".reply-form").first().slideUp(function() {
                    form.find("textarea").val("");
                    reply.slideDown();
                });
                $('<div class="reply-result"><div class="alert alert-success">'
                  + response.result + '</div></div>')
                    .appendTo(form.parents('.email-info').first())
                    .delay(2000).fadeOut('slow', function() { $(this).remove(); });
            },
            error: function(jqXHR, textStatus, errorThrown) {
                $('<div class="reply-result"><div class="alert alert-error">'
                  + '<button type="button" class="close" data-dismiss="alert">&times;</button> '
                  + jqXHR.responseText + '</div></div>')
                    .css("display", "none").prependTo(form).slideDown();
            }
        });
    });
    $(baseElem).find(".reply-form a.cancel").click(function(e) {
        e.preventDefault();
        $(this).parents(".reply-form").first().slideUp();
    });
    $(baseElem).find(".reply-form a.quote").click(function(e) {
        e.preventDefault();
        var quoted = $(this).parents(".email").first()
                        .find(".email-body").clone()
                        .find(".quoted-switch").remove().end()
                        .find(".quoted-text").remove().end()
                        .text();
        var textarea = $(this).parents(".reply-form").find("textarea");
        // remove signature
        var sig_index = quoted.search(/^-- $/m);
        if (sig_index != -1) {
            quoted = quoted.substr(0, sig_index);
        }
        // add quotation marks
        quoted = $.trim(quoted).replace(/^/mg, "> ");
        // insert before any previous text
        textarea.val(quoted + "\n" + textarea.val());
        textarea.focus();
    });
    function set_new_thread(checkbox) {
        var this_form = checkbox.parents("form").first();
        var subj = this_form.find("input[name='subject']").parents("p").first();
        if (checkbox.is(":checked")) {
            subj.slideDown("fast");
            subj.find("input").focus();
        } else {
            subj.slideUp("fast");
            this_form.find("textarea").focus();
        }
    }
    $(baseElem).find(".reply-form input[name='newthread']").change(function() {
        set_new_thread($(this));
    }).change();
}

function setup_unreadnavbar(element) {
    element = $(element);
    if (element.length === 0) {
        return;
    }
    var current_index;
    function scroll(inc) {
        var unreads = $(".email.unread");
        if (unreads.length == 0) { return; }
        if (typeof current_index == "undefined") {
            if (inc == 1) { current_index = -1; }
            if (inc == -1) { current_index = unreads.length; }
        }
        current_index = current_index + inc;
        if (current_index < 0) { current_index = unreads.length - 1; }
        if (current_index >= unreads.length) { current_index = 0; }
        element.find(".unreadindex").text(current_index + 1);
        // compensate for the fixed banner at the top
        var target = unreads.eq(current_index).offset().top - 70;
        $("html,body").stop(true, false).animate({
            scrollTop: target
        }, 500);
    }
    element.find(".nextunread").click(function(e) { e.preventDefault(); scroll(1); });
    element.find(".prevunread").click(function(e) { e.preventDefault(); scroll(-1); });
    $(document).bind("keydown", "j", function(e) { scroll(1); });
    $(document).bind("keydown", "k", function(e) { scroll(-1); });
    element.find("a").tooltip();
    element.animate({height: "show"}, 700);
}


/*
 * Thread replies list
 */
function update_thread_replies(url) {
    function load_more(current_url) {
        $.ajax({
            dataType: "json",
            url: current_url,
            success: function(data) {
                // replies
                var newcontent = $(data.replies_html);
                $(".replies").append(newcontent)
                             .append($(".replies .ajaxloader"));
                // re-bind events
                setup_emails_list(newcontent);
                fold_quotes(newcontent);
                setup_disabled_tooltips(newcontent);
                setup_vote(newcontent);
                // load the rest if applicable
                if (data.more_pending) {
                    load_more(url+"&offset="+data.next_offset);
                } else {
                    $(".replies .ajaxloader").remove();
                }
            },
            error: function(jqXHR, textStatus, errorThrown) {
                if (jqXHR.responseText !== "") {
                    alert(jqXHR.responseText);
                }
            }
        });
    }
    load_more(url);
}


/*
 * Re-attach threads
 */
function setup_reattach() {
    $(".reattach-thread li.manual input[type='text']").focus( function() {
        $(this).parents("li").first()
            .find("input[type='radio']")
            .prop("checked", true);
    });
    $(".reattach-thread form.search").submit(function (e) {
        e.preventDefault();
        var results_elem = $(this).parent().find("ul.suggestions");
        var url = $(this).attr("action") + "?" + $(this).serialize();
        results_elem.find("img.ajaxloader").show();
        $.ajax({
            url: url,
            success: function(data) {
                results_elem.html(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            },
            complete: function(jqXHR, textStatus) {
                results_elem.find("img.ajaxloader").hide();
            }
        });
    }).submit();
}