inmanis/templates/y.julius
2012-09-18 16:25:01 +02:00

142 lines
4.3 KiB
Text

function log(msg) {
if (console && console.log) {
console.log(msg);
}
}
function logged() {
return $('#login').length == 0;
}
function clicked() {
return function() {
if (! logged()) {
$("#loginmessage").show();
return 1;
}
var entryLine = $(this).parent();
log("entryLine = " + entryLine);
var param=$(this).attr("class");
log("param = "+param);
var oldValue=parseInt($(this).text());
var yeahdiv = entryLine.children(".yeah");
var neahdiv = entryLine.children(".neah");
var oldYeahValue = parseInt(yeahdiv.text());
var oldNeahValue = parseInt(neahdiv.text());
// put logic here, 6 possibles cases
// old state: not voted, neah, yeah
// action: yeah, neah
//
if ($(entryLine).hasClass("yeah_voted")) { // Old state yeah
// discard the yeah
entryLine.removeClass("yeah_voted");
yeahdiv.text( oldYeahValue - 1);
if (param == "neah") { // vote yeah
entryLine.addClass("neah_voted");
neahdiv.text( oldNeahValue + 1);
}
}
else if ($(entryLine).hasClass("neah_voted")) { // Old state neah
// discard the neah
entryLine.removeClass("neah_voted");
neahdiv.text( oldNeahValue - 1);
if (param == "yeah") { // vote yeah
entryLine.addClass("yeah_voted");
yeahdiv.text( oldYeahValue + 1);
}
}
else { // Old state not voted
entryLine.addClass(param+"_voted");
$(this).text(oldValue + 1);
}
var jsonparams={};
jsonparams[param]=1;
$.post( entryLine.attr('url')
, jsonparams
, function(data) {log(data);}
, "json");
};
}
function ask(msg,f,yes,no) {
if ($('#popin').length == 0) {
$('<div/>',{ id: "popin" , click: function(){$("#popin").hide(); }
}).appendTo($('body'));
$('<div/>',{"class": "insidebox"}).appendTo($("#popin"));
}
$("#popin").show()
$("#popin .insidebox").html(msg);
$('<div/>', { "class": "buttons"}).appendTo($("#popin .insidebox"));
$('<div/>', { click: function(){f();$("#popin").hide();}
, "class": "button"
, text: yes
}).appendTo("#popin .insidebox .buttons");
$('<div/>', { click: function(){ $("#popin").hide();}
, "class": "button"
, text: no
}).appendTo("#popin .insidebox .buttons");
$('<div class="flush"></div>').appendTo("#popin .insidebox .buttons");
}
function deleteEntry(url,elem) {
$.ajax({ type: "DELETE"
, url: url
, success: function(data) {elem.remove();}
, dataType: "json"});
}
function deleteComment(url,elem) {
$.ajax({ type: "DELETE"
, url: url
, success: function(data) {
elem.addClass("deleted");
elem.find(".creator").html("by Anonymous Coward");
elem.find(".content").html("deleted");
}
, dataType: "json"});
}
function deleteEntryGoHome(url,elem) {
$.ajax({ type: "DELETE"
, url: url
, success: function(data) {window.location='/';}
, dataType: "json"});
}
function flipauto(elem, f) {
var target=elem.attr("flipshow");
var closedText=elem.attr("closed");
var openedText=elem.attr("opened");
if (elem.html() == closedText) {
elem.html(openedText);
} else {
elem.html(closedText);
}
if (typeof f === 'undefined') {
$(target).toggle();
} else {
f($(target));
}
}
function getUrlParams() {
var vars = [], hash;
var substr = window.location.href.replace(/[^\?]*\?/,'').replace(/#[^#]*$/,'');
var hashes = substr.split('&');
for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
$(document).ready(function(){
$('#message').click(function(){
$('#message').slideUp('slow')});
setTimeout(function(){
$('#message').slideUp('slow')}
, 10000);
});