/*
* 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 .
*
* Author: Aurelien Bompard
*/
/*
* Generic
*/
function form_to_json(form) {
var form_data = form.serializeArray();
var data = {};
for (input in form_data) {
data[form_data[input].name] = form_data[input].value;
}
return data;
}
/*
* Voting
*/
function vote(elem) {
if ($(elem).hasClass("disabled")) {
return;
}
var value = parseInt($(elem).attr("data-vote"));
var form = $(elem).parents("form").first();
var data = form_to_json(form);
data['vote'] = value;
$.ajax({
type: "POST",
url: form.attr("action"),
dataType: "json",
data: data,
success: function(response) {
var newcontent = $(response.html);
form.replaceWith(newcontent);
setup_vote(newcontent); // re-bind events
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}
});
}
function setup_vote(baseElem) {
if (!baseElem) {
baseElem = document;
}
$(baseElem).find("a.vote").click(function(e) {
e.preventDefault();
vote(this);
});
}
/*
* 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();
});
},
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();
});
}
/*
* 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();
});
$('