Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

news_ajax.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. function news_ajax(event, count, offset, privileged) {
  2. /*
  3. * event - The click event, passed to hide the element when necessary.
  4. * count - Number of news items to fetch.
  5. * offset - Database offset for fetching news.
  6. * privilege - Gotta check your privilege (used to show/hide [Edit] on news).
  7. */
  8. // Unbind onclick to avoid spamclicks.
  9. $(event.target).attr('onclick', 'return false;');
  10. // Fetch news data, check for errors etc.
  11. $.get("ajax.php", {
  12. action: "news_ajax",
  13. count: count,
  14. offset: offset
  15. })
  16. .done(function(data) {
  17. var response = $.parseJSON(data.response);
  18. if (typeof data == 'undefined' || data == null || data.status != "success" || typeof response == 'undefined' || response == null) {
  19. console.log("ERR ajax_news(" + (new Error).lineNumber + "): Unknown data or failure returned.");
  20. // Return to original paremeters, no news were added.
  21. $(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + offset + ', ' + privileged + '); return false;');
  22. } else {
  23. if (response.length == 0) {
  24. $(event.target).parent().remove();
  25. } else {
  26. var targetClass = $('#more_news').prev().attr('class');
  27. $.each(response, function() {
  28. // Create a new element, insert the news.
  29. $('#more_news').before($('<div/>', {
  30. id: 'news' + this[0],
  31. Class: targetClass
  32. }));
  33. // I'm so happy with this condition statement.
  34. if (privileged) {
  35. $('#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" toggle-target="#newsbody' + this[0] + '" toggle-replace="Show">Hide</a></span></div>');
  36. } else {
  37. $('#news' + this[0]).append('<div class="head"><strong>' + this[1] + '</strong> ' + this[2] + '<span style="float: right;"><a class="brackets" toggle-target="#newsbody' + this[0] + '" toggle-replace="Show">Hide</a></span></div>');
  38. }
  39. $('#news' + this[0]).append('<div class="pad" id="newsbody'+this[0]+'">' + this[3] + '</div>');
  40. });
  41. // Update the onclick parameters to appropriate offset.
  42. $(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + (count + offset) + ', ' + privileged + '); return false;');
  43. }
  44. }
  45. })
  46. .fail(function() {
  47. console.log("WARN ajax_news(" + (new Error).lineNumber + "): AJAX get failed.");
  48. // Return to original paremeters, no news were added.
  49. $(event.target).attr('onclick', 'news_ajax(event, ' + count + ', ' + offset + ', ' + privileged + '); return false;');
  50. });
  51. }