/*
* 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, value) {
var data = form_to_json($(elem).parent("form"));
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 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) {
if (jqXHR.status === 403) {
alert(jqXHR.responseText);
}
}
});
});
}
/*
* Replies
*/
function setup_replies() {
$("a.reply").click(function(e) {
e.preventDefault();
$(this).next().slideToggle("fast", function() {
if ($(this).css("display") === "block") {
$(this).find("textarea").focus();
}
});
});
$(".reply-form button[type='submit']").click(function(e) {
e.preventDefault();
var form = $(this).parents("form").first();
var data = form_to_json(form);
$.ajax({
type: "POST",
url: form.attr("action"),
//dataType: "json",
data: data,
success: function(response) {
form.parents(".reply-form").first().slideUp(function() {
form.find("textarea").val("");
});
$('