summaryrefslogtreecommitdiffstats
path: root/static/functions/news_ajax.js
blob: 6e88c1812eaa9b810018c021c4c8df7d5553f088 (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
function news_ajax(event, count, offset, privileged, authkey) {
	/*
	 * 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).
	 * authkey - Either the user's authkey or false. Used for rendering the [Delete] button on the news tool.
	 */
	// Unbind onclick to avoid spamclicks.
	$(event.target).attr('onclick', 'return false;');
	// Fetch news data, check for errors etc.
	$.get("ajax.php", {
		action: "news_ajax",
		count: count,
		offset: offset
	})
	.done(function(data) {
		var response = $.parseJSON(data.response);
		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 + ', ' + authkey + '); return false;');
		} else {
			if (response.length == 0) {
				$(event.target).parent().remove();
			} else {
				var targetClass = $('#more_news').prev().attr('class');
				$.each(response, function() {
					// Create a new element, insert the news.
					$('#more_news').before($('<div/>', {
						id: 'news' + this[0],
						Class: targetClass
					}));
					// I'm so happy with this condition statement.
					if (privileged && authkey !== false) {
						// Append [Delete] button and hide [Hide] button if on the news toolbox page
						$('#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> <a class="brackets" href="tools.php?action=deletenews&amp;id=' + this[0] + '&amp;auth=' + authkey + '">Delete</a></div>');
					} else if (privileged) {
						$('#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>');
					} else {
						$('#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>');
					}
					$('#news' + this[0]).append('<div class="pad" id="newsbody'+this[0]+'">' + this[3] + '</div>');
				});
				// Update the onclick parameters to appropriate offset.
				$(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + (count + offset) + ', ' + privileged + '); return false;');
			}
		}
	})
	.fail(function() {
		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 + ', ' + authkey + '); return false;');
	});
}