diff options
author | Git <git@what.cd> | 2013-06-14 08:18:16 +0000 |
---|---|---|
committer | Git <git@what.cd> | 2013-06-14 08:18:16 +0000 |
commit | 94a7d238129f6e782dfb755be253bc8ea86e3b0d (patch) | |
tree | 0542bea8f4bff04cbafff97f7dc969f2e4f6c5a0 /static/functions/news_ajax.js | |
parent | a2d7c73ab285ecb77b38134ac5f4895fe7c7aad1 (diff) | |
download | Gazelle-94a7d238129f6e782dfb755be253bc8ea86e3b0d.zip Gazelle-94a7d238129f6e782dfb755be253bc8ea86e3b0d.tar.gz Gazelle-94a7d238129f6e782dfb755be253bc8ea86e3b0d.tar.bz2 |
Empty commit
Diffstat (limited to 'static/functions/news_ajax.js')
-rw-r--r-- | static/functions/news_ajax.js | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/static/functions/news_ajax.js b/static/functions/news_ajax.js new file mode 100644 index 0000000..7e512c7 --- /dev/null +++ b/static/functions/news_ajax.js @@ -0,0 +1,55 @@ +function news_ajax(event, count, offset, privileged){ + /* + * 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). + * + * This function isn't wrapped in jQuery, be sure we use it + * instead of the mix-mashed Gazelle $ function. + */ + var $ = jQuery.noConflict(); + //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+'); 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){ + $('#news'+this[0]).append('<div class="head"><strong>'+this[1]+'</strong> '+this[2]+' - <a href="tools.php?action=editnews&id='+this[0]+'" class="brackets">Edit</a></div>'); + } else { + $('#news'+this[0]).append('<div class="head"><strong>'+this[1]+'</strong> '+this[2]+'</div>'); + } + $('#news'+this[0]).append('<div class="pad">'+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+'); return false;'); + }); +}
\ No newline at end of file |