summaryrefslogtreecommitdiffstats
path: root/hyperkitty/static/js/hyperkitty.js
blob: 13d3f8b2cbb01572d0bdd60bd6b77bd7a6a1979f (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
/*
 * 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>
 */



/*
 * Voting
 */

function vote(elem, value) {
    var form_data = $(elem).parent("form").serializeArray();
    var data = {};
    for (input in form_data) {
        data[form_data[input].name] = form_data[input].value;
    }
    data['vote'] = value;
    $.ajax({
        type: "POST",
        url: $(elem).parent("form").attr("action"),
        dataType: "json",
        data: data,
        success: function(response) {
            var likestatus = $(elem).parent("form").find(".likestatus");
            likestatus.find(".likecount").html(response.like);
            likestatus.find(".dislikecount").html(response.dislike);
        },
        error: function(jqXHR, textStatus, errorThrown) {
            // authentication or double-vote
            if (jqXHR.status === 403) {
                alert(jqXHR.responseText);
            }
        }
    });
}


function setup_vote() {
    $(".voteup").click(function(e) { e.preventDefault(); vote(this, 1); });
    $(".votedown").click(function(e) { e.preventDefault(); vote(this, -1); });
}


/*
 * Tagging
 */

function setup_add_tag() {
    $("#add_tag_form").submit( function () {
        $.ajax({
            type: "POST",
            dataType: "json",
            data : $(this).serialize(),
            url: $(this).attr("action"),
            success: function(data) {
                $("#tags").html(data.html);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                // authentication and invalid data
                alert(jqXHR.responseText);
            }
        });
        return false;
    });
}


/*
 * 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();
        var form = $(this).parents("form").first();
        var action_field = form.find("input[name='action']");
        var form_data = form.serializeArray();
        var data = {};
        for (input in form_data) {
            data[form_data[input].name] = form_data[input].value;
        }
        $.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) {
                if (jqXHR.status === 403) {
                    alert(jqXHR.responseText);
                }
            }
        });
    });
}



/*
 * Recent activity graph
 */

function activity_graph(dates, data, baseurl) {
    var w = 500,
        h = 300,
        x = pv.Scale.ordinal(pv.range(32)).splitBanded(0, w, 4/5),
        y = pv.Scale.linear(0, Math.max.apply(null, data)+1).range(0, h);

    var vis = new pv.Panel()
        .width(w)
        .height(250)
        .bottom(60)
        .left(30)
        .right(5)
        .top(5);

    var bar = vis.add(pv.Bar)
        .data(data)
        .event("click", function(n) self.location = baseurl + dates[this.index] + "/")
        .left(function() x(this.index))
        .width(x.range().band)
        .bottom(0)
        .height(y);

    bar.anchor("bottom").add(pv.Label)
        .textMargin(5)
        .textAlign("right")
        .textBaseline("middle")
        .textAngle(-Math.PI / 3)
        .text(function() xlabel(this.index));

    function xlabel(ind) {
        if (!dates[ind -1]) {
            return dates[ind];
        }
        prev = dates[ind - 1];
        cur = dates[ind];
        if (prev.substring(0,7) == cur.substring(0,7)) {
            cur = cur.substring(8);
        }
        return cur;
    }

    vis.add(pv.Rule)
        .data(y.ticks())
        .bottom(function(d) Math.round(y(d)) - .5)
        .strokeStyle(function(d) d ? "rgba(255,255,255,.3)" : "#000")
        .add(pv.Rule)
        .left(0)
        .width(5)
        .strokeStyle("#000")
        .anchor("left").add(pv.Label)
        .text(function(d) d.toFixed(1));

    vis.render();
}


/*
 * Misc.
 */

function setup_attachments() {
    $("ul.email_info li.attachments ul.attachments-list").hide();
    $("ul.email_info li.attachments > a").click(function() {
        $(this).next("ul").fadeToggle('fast');
    });
}

function setup_quotes() {
    $('div.email_body .quoted-switch a')
        .add('div.first_email_body .quoted-switch a')
        .click(function() {
            $(this).parent().next(".quoted-text").slideToggle('fast');
            return false;
        });
}

function setup_months_list() {
    var accordion = $("#months-list").accordion({
            active: $("#months-list li.current").parent().prev()
    });
}


/*
 * General
 */

$(document).ready(function() {
    setup_vote();
    setup_attachments();
    setup_add_tag();
    setup_quotes();
    setup_months_list();
    setup_favorites();
});