Gazelle/static/functions/news_ajax.js

52 lines
2.7 KiB
JavaScript
Raw Normal View History

2013-06-15 08:00:45 +00:00
function news_ajax(event, count, offset, privileged) {
/*
2013-06-14 08:18:16 +00:00
* event - The click event, passed to hide the element when necessary.
* count - Number of news items to fetch.
* offset - Database offset for fetching news.
* privilege - Gotta check your privilege (used to show/hide [Edit] on news).
*/
2013-06-15 08:00:45 +00:00
// Unbind onclick to avoid spamclicks.
2013-06-14 08:18:16 +00:00
$(event.target).attr('onclick', 'return false;');
2013-06-15 08:00:45 +00:00
// Fetch news data, check for errors etc.
2013-06-14 08:18:16 +00:00
$.get("ajax.php", {
2013-06-15 08:00:45 +00:00
action: "news_ajax",
count: count,
2013-06-14 08:18:16 +00:00
offset: offset
})
.done(function(data) {
var response = $.parseJSON(data.response);
2013-06-15 08:00:45 +00:00
if (typeof data == 'undefined' || data == null || data.status != "success" || typeof response == 'undefined' || response == null) {
console.log("ERR ajax_news(" + (new Error).lineNumber + "): Unknown data or failure returned.");
// Return to original paremeters, no news were added.
$(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + offset + ', ' + privileged + '); return false;');
2013-06-14 08:18:16 +00:00
} else {
2013-06-15 08:00:45 +00:00
if (response.length == 0) {
2013-06-14 08:18:16 +00:00
$(event.target).parent().remove();
} else {
var targetClass = $('#more_news').prev().attr('class');
2013-06-15 08:00:45 +00:00
$.each(response, function() {
// Create a new element, insert the news.
2013-06-14 08:18:16 +00:00
$('#more_news').before($('<div/>', {
2013-06-15 08:00:45 +00:00
id: 'news' + this[0],
2013-06-14 08:18:16 +00:00
Class: targetClass
}));
2013-06-15 08:00:45 +00:00
// I'm so happy with this condition statement.
if (privileged) {
2013-09-01 08:00:54 +00:00
$('#news' + this[0]).append('<div class="head"><strong>' + this[1] + '</strong> ' + this[2] + ' - <a href="tools.php?action=editnews&amp;id=' + this[0] + '" class="brackets">Edit</a><span style="float: right;"><a class="brackets" onclick="$(\'#newsbody' + this[0] + '\').gtoggle(); this.innerHTML=(this.innerHTML == \'Hide\' ? \'Show\' : \'Hide\'); return false;" href="#">Hide</a></span></div>');
2013-06-14 08:18:16 +00:00
} else {
2013-09-01 08:00:54 +00:00
$('#news' + this[0]).append('<div class="head"><strong>' + this[1] + '</strong> ' + this[2] + '<span style="float: right;"><a class="brackets" onclick="$(\'#newsbody' + this[0] + '\').gtoggle(); this.innerHTML=(this.innerHTML == \'Hide\' ? \'Show\' : \'Hide\'); return false;" href="#">Hide</a></span></div>');
2013-06-14 08:18:16 +00:00
}
2013-09-01 08:00:54 +00:00
$('#news' + this[0]).append('<div class="pad" id="newsbody'+this[0]+'">' + this[3] + '</div>');
2013-06-14 08:18:16 +00:00
});
2013-06-15 08:00:45 +00:00
// Update the onclick parameters to appropriate offset.
$(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + (count + offset) + ', ' + privileged + '); return false;');
2013-06-14 08:18:16 +00:00
}
}
})
.fail(function() {
2013-06-15 08:00:45 +00:00
console.log("WARN ajax_news(" + (new Error).lineNumber + "): AJAX get failed.");
// Return to original paremeters, no news were added.
$(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + offset + ', ' + privileged + '); return false;');
2013-06-14 08:18:16 +00:00
});
2013-06-15 08:00:45 +00:00
}