the blog demo works now.

This commit is contained in:
agentzh (章亦春) 2010-01-25 03:03:00 +08:00
parent 56e75e09f2
commit 3079df6d8c
2 changed files with 1073 additions and 0 deletions

445
clients/js/openresty.js Normal file
View File

@ -0,0 +1,445 @@
var console, _log;
(function(){
if(typeof console == 'undefined'){
_log = function(a, b, c){/*alert('' + a + b + c)*/};
}else{
_log = function(){
console.log.apply(console, arguments)
};
}
})();
if (typeof window.OpenResty == "undefined") {
(function() {
var undefined;
var browser = (function(){
var u = navigator.userAgent.toLowerCase(), a = /(msie|firefox|camino|chrome|opera|safari|netscape|konqueror|lynx)/, b = /(msie|firefox|chrome|opera|safari|camino|netscape|netscape6|konqueror|lynx|version)(\/|\s)([a-z0-9\.\+]*?)(\;|dev|rel|\s|$)/;
var un = 'unknown', x = 'X', r = {
name: (a.exec(u) || [un, un])[1]
};
r[r.name] = true;
r.version = (b.exec(u) || [x, x, x, x])[3];
return r;
})();
var OpenResty = {
callbackMap: {},
isDone: {},
counter: 0
};
OpenResty.Client = function (params) {
if (params == undefined) params = {};
this.callback = params.callback;
var server = params.server;
if (!/^https?:\/\//.test(server)) {
server = 'http://' + server;
}
this.server = server;
this.user = params.user;
//this.password = params.password;
};
var $ClientP = OpenResty.Client.prototype;
$ClientP.isSuccess = function (res) {
// alert(JSON.stringify(res));
return !(typeof res == 'object' && res.errcode);
};
$ClientP.logout = function () {
this.get('/=/logout');
};
$ClientP.login = function (user, password) {
this.user = user;
var userCallback = this.callback;
if (typeof userCallback == 'string') {
userCallback = eval(userCallback);
}
var self = this;
this.callback = function (data) {
//alert(data.session);
self.session = data.session;
userCallback(data);
};
if (password == null)
password = '';
else
password = hex_md5(password);
//this.callback = 'save_session';
this.get('/=/login/' + user + '/' + password);
};
$ClientP.postByGet = function (url) {
var args, content;
if (arguments.length == 3) {
args = arguments[1];
content = arguments[2];
} else {
content = arguments[1];
}
if (!args) args = {};
url = url.replace(/^\/=\//, '/=/post/');
content = JSON.stringify(content);
//alert("type of content: " + typeof(content));
//alert("content: " + content);
args._data = content;
this.get(url, args);
};
$ClientP.genId = (function (n) {
//return ( Math.random() * 1000000 );
return function() { return n++ };
//return this.counter++;
})(new Date().getTime());
/*
post: for large data transform
crossdomain ~ y/n
_post_by_form({
form: form,
url: fullURL,
content: { data: content },
preventCache: true,
method: "post",
handleAs: 'html',
handle: function () {
self.get('/=/last/response/' + args._last_response);
}
});
*/
var _post_forms_div,
_post_forms = {};
OpenResty.Client._post_forms = _post_forms;
$ClientP._post_by_form = function (url, content, args) {
if (!_post_forms_div) {
_post_forms_div = document.createElement('div');
var _s = _post_forms_div.style;
_s.position = 'absolute';
_s.left = '-1000px';
_s.width = '500px';
_s.height = '1px';
_s.overflow = 'hidden';
_s = null;
document.body.appendChild(_post_forms_div);
}
var reqId = args._last_response;
if (reqId in _post_forms) {
throw "the post already exist!!";
}
var self = this;
var obj = {
reqId: reqId
};
_post_forms[reqId] = obj;
var submited = false;
// var onloadCount = 0;
obj.onload = function() {
if (!submited) return;
// alert('contentLoad ' + (++onloadCount) + ' times\n' + this);
var _iframe = obj.iframe;
// maybe we can process response under same domain
delete obj.onload;
_post_forms_div.removeChild(_iframe);
var _form = obj.form;
_post_forms_div.removeChild(obj.form);
_iframe = null;
_form = null;
delete obj.iframe;
delete obj.form;
delete _post_forms[reqId];
obj = null;
self.get('/=/last/response/' + reqId);
};
var form_id = 'OpenResty_form_' + reqId,
iframe_id = 'OpenResty_iframe_' + reqId;
var _5 = 'http://ajax.googleapis.com/ajax/libs/dojo/1.3.2/dojo/resources/blank.html';
var _iframe;
var _6 = browser.msie ? '<iframe name="' + iframe_id + '" src="' + _5 + '" onload="OpenResty.Client._post_forms[' + reqId + '].onload();">': "iframe";
_iframe = document.createElement(_6);
with(_iframe) {
name = iframe_id;
setAttribute("name", iframe_id);
id = iframe_id;
}
if (!browser.msie) {
_iframe.src = _5;
_iframe.onload = OpenResty.Client._post_forms[reqId].onload;
}
_post_forms_div.appendChild(_iframe);
obj.iframe = _iframe;
var _form = document.createElement("form");
var _22 = _form.getAttributeNode("action");
var _23 = _form.getAttributeNode("method");
var _24 = _form.getAttributeNode("target");
if (_22) {
_22.value = url;
} else {
_form.setAttribute("action", url);
}
if (_23) {
_23.value = "post";
} else {
_form.setAttribute("method", "post");
}
if (_24) {
_24.value = iframe_id;
} else {
_form.setAttribute("target", iframe_id);
}
_post_forms_div.appendChild(_form);
obj.form = _form;
var _ipt;
if (browser.msie) {
_ipt = document.createElement('<input type="hidden" name="data">');
} else {
_ipt = document.createElement("input");
_ipt.name = "data";
_ipt.type = "hidden";
}
_ipt.value = content;
_form.appendChild(_ipt);
_ipt = null;
_form.submit();
submited = true;
};
$ClientP.post = function (url) {
/*
if (browser.opera) { // work around an Opera bug
return $ClientP.postByGet.apply(this, arguments);
}*/
try{
var args, content;
if (arguments.length == 3) {
args = arguments[1];
content = arguments[2];
} else {
content = arguments[1];
}
if (!args) args = {};
//url = url.replace(/^\/=\//, '/=/post/');
//if (url.match(/\?/)) throw "URL should not contain '?'.";
if (!this.callback) throw "No callback specified for OpenResty.";
// var formId = this.formId;
// if (!formId) throw "No form specified.";
if (this.session) args._session = this.session;
if (!this.session && !args._user)
args._user = this.user;
args._last_response = this.genId();
content = JSON.stringify(content);
//alert("type of content: " + typeof(content));
//alert("content: " + content);
//args.callback = this.callback;
//args.as_html = 1;
var arg_list = new Array();
for (var key in args) {
arg_list.push(key + "=" + encodeURIComponent(args[key]));
}
//url += "?" + arg_list.join("&");
var fullURL = this.server + url;
if ( /\?$/.test(url) )
fullURL += arg_list.join("&");
else if ( /\?/.test(url) )
fullURL += '&' + arg_list.join("&");
else
fullURL += '?' + arg_list.join("&");
var self = this;
//if (browser.opera) { // work around an Opera bug
//$("#" + formId).html($("#" + formId).html());
//}
//
// var form = document.getElementById(formId);
// form.method = 'POST';
this._post_by_form(fullURL, content, args);
}catch(ex){alert('error!!!: ' + ex)}
/*
var form = document.getElementById('new_model');
form.action = this.server + url;
form.method = 'POST';
form.target = 'blah';
$("input[@name='data']").val(content);
form.submit();
*/
//this.get(url, args);
//
};
$ClientP.putByGet = function (url) {
var args, content;
if (arguments.length == 3) {
args = arguments[1];
content = arguments[2];
} else {
content = arguments[1];
}
if (!args) args = {};
url = url.replace(/^\/=\//, '/=/put/');
content = JSON.stringify(content);
//alert("type of content: " + typeof(content));
//alert("content: " + content);
args._data = content;
this.get(url, args);
};
$ClientP.put = function (url) {
//if (browser.opera) { // work around an Opera bug
//return $ClientP.putByGet.apply(this, arguments);
//}
var args, content;
if (arguments.length == 3) {
args = arguments[1];
content = arguments[2];
} else {
content = arguments[1];
}
if (!args) args = {};
url = url.replace(/^\/=\//, '/=/put/');
//alert("type of content: " + typeof(content));
//alert("content: " + content);
this.post(url, args, content);
};
$ClientP.get = function (url, args) {
if (!args) args = {};
if (!this.callback) throw "No callback specified for OpenResty.";
if (!this.server) throw "No server specified for OpenResty.";
//if (!this.user) throw "No user specified for OpenResty.";
if (this.session) args._session = this.session;
if (!this.session && !args._user)
args._user = this.user;
//args.password = this.password || '';
//if (url.match(/\?/)) throw "URL should not contain '?'.";
var reqId = this.genId();
//args._rand = reqId;
var onerror = this.onerror || function () { alert("Failed to do GET " + url) };
//alert(args._rand);
//if (!isLogin) args.user = this.user;
//args.password = this.password;
var callback = this.callback;
if (typeof callback == 'string') {
callback = eval(callback);
}
OpenResty.isDone[reqId] = false;
this.callback = function (res) {
//debug("In call back! " + reqId);
OpenResty.isDone[reqId] = true;
OpenResty.callbackMap[reqId] = null;
callback(res);
};
OpenResty.callbackMap[reqId] = this.callback;
args._callback = "OpenResty.callbackMap[" + reqId + "]";
var headTag = document.getElementsByTagName('head')[0];
var scriptTag = document.createElement("script");
scriptTag.id = "openapiScriptTag" + reqId;
scriptTag.className = '_openrestyScriptTag';
var arg_list = new Array();
for (var key in args) {
arg_list.push(key + "=" + encodeURIComponent(args[key]));
}
var fullURL = this.server + url;
if ( /\?$/.test(url) )
fullURL += arg_list.join("&");
else if ( /\?/.test(url) )
fullURL += '&' + arg_list.join("&");
else
fullURL += '?' + arg_list.join("&");
scriptTag.src = fullURL;
scriptTag.type = "text/javascript";
scriptTag.onload = scriptTag.onreadystatechange = function () {
var done = OpenResty.isDone[reqId];
if (done) {
//alert("We're done!");
setTimeout(function () {
try {
headTag.removeChild(scriptTag);
} catch (e) {}
}, 0);
return;
}
if (!this.readyState ||
this.readyState == "loaded" ||
this.readyState == "complete") {
setTimeout(function () {
if (!OpenResty.isDone[reqId]) {
//alert("reqId: " + reqId);
//debug("onerror firing... " + reqId);
onerror();
OpenResty.isDone[reqId] = true;
setTimeout(function () {
try {
headTag.removeChild(scriptTag);
} catch (e) {}
}, 0);
}
}, 50);
}
};
setTimeout( function () {
headTag.appendChild(scriptTag);
}, 0);
};
$ClientP.del = function (url, args) {
if (!args) args = {};
url = url.replace(/^\/=\//, '/=/delete/');
this.get(url, args);
};
window.OpenResty = OpenResty;
})();
}

628
demo/Blog/js/blog.js Normal file
View File

@ -0,0 +1,628 @@
var account = 'agentzh';
var host = 'http://localhost:1984';
//var host = 'http://api.eeeeworks.org';
//var host = 'http://api.openresty.org';
//var host = 'http://10.62.136.86';
var openresty = null;
var savedAnchor = null;
var itemsPerPage = 5;
var loadingCount = 0;
var waitMessage = null;
var timer = null;
var thisYear = null;
var thisMonth = null;
var thisDay = null;
var cachedPostCountRes = null;
var months = [
null,
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
];
$(window).ready(init);
function error (msg) {
alert(msg);
}
function debug (msg) {
$("#copyright").append(msg + "<br/>");
}
$.fn.postprocess = function (className, options) {
return this.find("a[@href*='#']").each( function () {
//debug("HERE!");
var href = $(this).attr('href');
// We need the following hack because IE expands href to
// absolute URL:
var anchor = href.replace(/^.*?\#/, '');
//debug("Anchor: " + anchor);
$(this).click( function () {
//debug(location.hash);
//$(".bnchor-location")[0].id = anchor;
location.hash = anchor;
//alert(location.hash);
if (savedAnchor == anchor) savedAnchor = null;
dispatchByAnchor();
} );
} );
};
//var count = 0;;
function setStatus (isLoading, category) {
if (isLoading) {
if (++loadingCount == 1) {
if (jQuery.browser.opera)
$(waitMessage).css('top', '2px');
else
$(waitMessage).show();
}
} else {
loadingCount--;
if (loadingCount < 0) loadingCount = 0;
if (loadingCount == 0) {
// the reason we use this hack is to work around
// a rendering bug in Win32 build of Opera
// (at least 9.25 and 9.26)
if (jQuery.browser.opera)
$(waitMessage).css('top', '-200px');
else
$(waitMessage).hide();
}
}
//count++;
//debug("[" + count + "] setStatus: " + category + ": " + loadingCount + "(" + isLoading + ")");
}
function init () {
loadingCount = 0;
var now = new Date();
thisYear = now.getFullYear();
thisMonth = now.getMonth();
thisDay = now.getDate();
waitMessage = document.getElementById('wait-message');
openresty = new OpenResty.Client(
{ server: host, user: account + '.Public' }
);
//openresty.formId = 'new_model';
if (timer) {
clearInterval(timer);
}
dispatchByAnchor();
timer = setInterval(dispatchByAnchor, 600);
//debug("before getSidebar...");
getSidebar();
}
function resetAnchor () {
var anchor = location.hash;
location.hash = anchor.replace(/^\#/, '');
}
function dispatchByAnchor () {
//debug(location.hash);
var anchor = location.hash;
anchor = anchor.replace(/^\#/, '');
if (savedAnchor == anchor)
return;
if (anchor == "") {
anchor = 'posts/1';
location.hash = 'posts/1';
}
savedAnchor = anchor;
// prevent memory leaks from dynamically created <script> nodes:
//if (loadingCount <= 0) openresty.purge();
loadingCount = 0;
var match = anchor.match(/^post-(\d+)(:comments|comment-(\d+))?/);
if (match) {
var postId = match[1];
//alert("Post ID: " + postId);
getPost(postId);
return;
}
match = anchor.match(/^search\/(\d+)\/(.*)/);
if (match) {
var page = parseInt(match[1]);
var query = match[2];
$('.pager').html('');
$('#searchbox').val(query);
getSearchResults(query, page);
getPagerForSearch(query, page);
return;
}
match = anchor.match(/^archive\/(\d+)\/(\d+)$/);
if (match) {
var year = match[1];
var month = match[2];
getArchive(year, month);
return;
}
match = anchor.match(/^(?:posts|posts\/(\d+))$/);
var page = 1;
//alert(anchor + " " + location.hash);
if (match)
page = parseInt(match[1]) || 1;
else if (anchor != 'posts/1')
top.location.hash = 'posts/1';
//debug("before getPostList...");
getPostList(page);
//debug("before getPager...");
getPager(page);
//debug("after getPager...");
$(".blog-top").attr('id', 'posts/' + page);
}
function getArchive (year, month) {
setStatus(true, 'renderPostList');
$(".pager").html('');
openresty.callback = function (res) {
renderPostList(res);
setStatus(true, 'renderArchiveNav');
openresty.callback = renderArchiveNav;
openresty.get(
'/=/view/PrevNextArchive/~/~',
{
now: year + '-' + month + '-01',
month: month,
months: months
}
);
};
openresty.get(
'/=/view/FullPostsByMonth/~/~',
{ count: 40, year: year, month: month }
);
$(".blog-top").attr('id', 'archive/' + year + '/' + month);
}
function renderArchiveNav (res) {
//debug("render archive nav: " + JSON.stringify(res));
setStatus(false, 'renderArchiveNav');
if (!openresty.isSuccess(res)) {
error("Failed to get archive navigator: " + res.errstr);
return;
}
var prev = null;
var next = null;
for (var i = 0; i < res.length; i++) {
var item = res[i];
item.year = parseInt(item.year);
item.month = parseInt(item.month);
//debug("item: " + JSON.stringify(item));
if (item.id == "prev") prev = item;
else if (item.id == "next") next = item;
}
//debug("next: " + JSON.stringify(next));
//debug("prev: " + JSON.stringify(prev));
$("#post-list-nav").html(
Jemplate.process(
'archive-nav.tt',
{ next: next, prev: prev, months: months }
)
).postprocess();
}
function getPostList (page) {
setStatus(true, 'renderPostList');
openresty.callback = renderPostList;
openresty.get('/=/model/Post/~/~', {
_limit: itemsPerPage,
_order_by: 'id:desc',
_offset: itemsPerPage * (page - 1)
});
}
function getSearchResults (query, page) {
setStatus(true, 'renderSearchResults');
openresty.callback = function (res) { renderSearchResults(res, query); };
var q = query2tsquery(query);
//alert("Query: " + q);
openresty.get('/=/view/PostSearch/~/~', {
offset: itemsPerPage * (page - 1),
count: itemsPerPage,
query: q,
order_dir: 'desc'
});
}
function doPostSearch () {
var query = $('#searchbox').val();
savedAnchor = null;
var anchor = '#search/1/' + query;
location.hash = anchor;
//alert(location.hash);
if (savedAnchor == anchor.replace(/^\#/, '')) savedAnchor = null;
dispatchByAnchor();
}
function query2tsquery (query) {
return query.replace(/[\s(){},.|&$]+/g, ' ')
.replace(/^\s+|\s+$/g, '')
.replace(/ +/g, '|');
}
function getPagerForSearch (query, page) {
setStatus(true, 'renderPagerForSearch');
openresty.callback = function (res) { renderPager(res, page, 'search/', '/' + query); };
var q = query2tsquery(query);
openresty.get('/=/view/RowCountForSearch/~/~', {query: q});
}
function getPager (page) {
setStatus(true, 'renderPager');
if (cachedPostCountRes) {
//alert("Hit!");
renderPager(cachedPostCountRes, page, 'posts/', '');
} else {
openresty.callback = function (res) { renderPager(res, page, 'posts/', ''); };
openresty.get('/=/view/RowCount/~/~', { model: 'Post' });
}
}
function getSidebar () {
//getRecentPosts();
//getRecentComments();
//getArchiveList();
setStatus(true, 'renderSidebar');
openresty.callback = function (res) { renderSidebar(res) };
openresty.get('/=/batch/GetSidebar/~/~', { year: thisYear, month: thisMonth + 1 });
}
function renderSidebar (res) {
setStatus(false, 'renderSidebar');
if (!openresty.isSuccess(res)) {
error("Failed to fetch posts for calendar: " +
res.errstr);
return;
}
getCalendar(thisYear, thisMonth, res[0]);
setStatus(true, 'renderRecentPosts');
renderRecentPosts(res[1], 0, 6);
setStatus(true, 'renderRecentComments');
renderRecentComments(res[2], 0, 6);
setStatus(true, 'getArchiveList');
renderArchiveList(res[3], 0, 12);
}
function getCalendar (year, month, posts) {
if (year == undefined || month == undefined) {
year = thisYear;
month = thisMonth;
}
var date = new Date(year, month, 1);
var first_day_of_week = date.getDay();
var end_of_month;
if (month == 11) {
end_of_month = 31;
} else {
var delta = new Date(year, month + 1, 1) - date;
end_of_month = Math.round(delta/1000/60/60/24);
}
//alert(year);
//alert(month);
//alert("thisday: " + thisDay);
$(".module-calendar").html(
Jemplate.process(
'calendar.tt',
{
year: year,
month: month,
first_day_of_week: first_day_of_week,
end_of_month: end_of_month,
today: (year == thisYear && month == thisMonth) ?
thisDay : null,
months: months
}
)
).postprocess();
// We need this 0 timeout hack for IE 6 :(
if (posts) {
//alert("Shortcut!");
setStatus(true, 'renderPostsInCalendar');
renderPostsInCalendar(posts, year, month);
} else {
setTimeout( function () {
setStatus(true, 'renderPostsInCalendar');
openresty.callback = function (res) {
renderPostsInCalendar(res, year, month);
};
openresty.get('/=/view/PostsByMonth/~/~', { year: year, month: month + 1 });
}, 0 );
}
}
function renderPostsInCalendar (res, year, month) {
setStatus(false, 'renderPostsInCalendar');
//alert("hey!");
if (!openresty.isSuccess(res)) {
error("Failed to fetch posts for calendar: " +
res.errstr);
} else {
//alert(res.errstr);
var prev_day = 0;
for (var i = 0; i < res.length; i++) {
var line = res[i];
if (prev_day == line.day) continue;
prev_day = line.day;
var id = 'day-' + year + '-' + month + '-' + line.day;
//alert("ID: " + id);
var cell = $("#" + id);
if (cell.length == 0) return;
//alert("cell html: " + cell.html());
cell.html('<a href="#post-' + line.id + '"><b>' +
cell.html() + '</b></a>').postprocess();
}
}
}
function getRecentComments (offset) {
if (!offset) offset = 0;
setStatus(true, 'renderRecentComments');
openresty.callback = function (res) { renderRecentComments(res, offset, 6) };
openresty.get('/=/view/RecentComments/~/~',
{ limit:6, offset: offset });
}
function renderRecentComments (res, offset, count) {
setStatus(false, 'renderRecentComments');
if (!openresty.isSuccess(res)) {
error("Failed to get the recent comments: " + res.errstr);
} else {
//alert("Get the recent comments: " + res.errstr);
var html = Jemplate.process(
'recent-comments.tt',
{ comments: res, offset: offset, count: count }
);
$("#recent-comments").html(html).postprocess();
}
}
function getRecentPosts (offset) {
if (!offset) offset = 0;
setStatus(true, 'renderRecentPosts');
openresty.callback = function (res) { renderRecentPosts(res, offset, 6) };
openresty.get('/=/view/RecentPosts/~/~',
{ limit:6, offset: offset });
}
function renderRecentPosts (res, offset, count) {
setStatus(false, 'renderRecentPosts');
if (!openresty.isSuccess(res)) {
error("Failed to get the recent posts: " + res.errstr);
} else {
//alert("Get the recent comments: " + res.errstr);
var html = Jemplate.process(
'recent-posts.tt',
{ posts: res, offset: offset, count: count }
);
$("#recent-posts").html(html).postprocess();
}
}
function getArchiveList (offset) {
if (offset == undefined) offset = 0;
setStatus(true, 'getArchiveList');
openresty.callback = function (res) {
renderArchiveList(res, offset, 12);
};
openresty.get(
'/=/view/PostCountByMonths/~/~',
{ offset: offset, count: 12 }
);
}
function renderArchiveList (res, offset, count) {
setStatus(false, 'getArchiveList');
if (!openresty.isSuccess(res)) {
error("Failed to get archive list: " + res.errstr);
return;
}
for (var i = 0; i < res.length; i++) {
var item = res[i];
var match = item.year_month.match(/^(\d\d\d\d)-0?(\d+)/);
if (match) {
item.year = parseInt(match[1]);
item.month = parseInt(match[2]);
} else {
error("Failed to match against year_month: " + item.year_month);
}
//debug(JSON.stringify(item));
}
$("#archive-list").html(
Jemplate.process(
'archive-list.tt',
{ archives: res, count: count, offset: offset, months: months }
)
).postprocess();
}
function postComment (form) {
var data = {};
data.sender = $("#comment-author").val();
data.email = $("#comment-email").val();
data.url = $("#comment-url").val();
data.body = $("#comment-text").val();
data.post_id = $("#comment-for").val();
//alert(JSON.stringify(data));
if (!data.sender) {
error("Name is required.");
return false;
}
if (!data.email) {
error("Email address is required.");
return false;
}
if (!data.body) {
error("Comment body is required.");
return false;
}
//openresty.purge();
setStatus(true, 'afterPostComment');
openresty.callback = afterPostComment;
openresty.formId = 'comment-form';
openresty.post('/=/action/NewComment/~/~', data);
return false;
}
function afterPostComment (res) {
setStatus(false, 'afterPostComment');
//alert("HERE!!!");
if (!openresty.isSuccess(res)) {
error("Failed to post the comment: " + res.errstr);
} else {
//alert(res.errstr);
$("#comment-text").val('');
var spans = $(".comment-count");
var commentCount = parseInt(spans.text());
var postId = spans.attr('post');
//debug(JSON.stringify(res));
var commentId;
var match = res[0].last_row.match(/\d+$/);
if (match.length) commentId = match[0];
location.hash = 'post-' + postId + ':' +
(commentId ? 'comment-' + commentId : 'comments');
spans.text(commentCount + 1);
getRecentComments(0);
}
}
function getPost (id) {
//alert("Go to Post " + id);
$(".blog-top").attr('id', 'post-' + id);
//alert($(".blog-top").attr('id'));
setStatus(true, 'renderPost');
openresty.callback = renderPost;
openresty.get('/=/batch/GetFullPost/~/~',
{ id: id });
}
function renderPost (res) {
setStatus(false, 'renderPost');
//alert(JSON.stringify(post));
if (!openresty.isSuccess(res)) {
error("Failed to render post: " + res.errstr);
} else {
var post = res[0][0];
//if (!post) return;
$("#beta-inner.pkg").html(
Jemplate.process('post-page.tt', { post: post })
).postprocess();
renderPrevNextPost(post.id, res[1]);
//debug(JSON.stringify(post));
setStatus(true, 'renderComments');
renderComments(res[2]);
$(".pager").html('');
resetAnchor();
}
}
function renderPrevNextPost (currentId, res) {
if (!openresty.isSuccess(res)) {
error("Failed to render prev next post navigation: " +
res.errstr);
} else {
//alert("Going to render prev next post navigation: " + res.errstr);
$(".content-nav").html(
Jemplate.process('nav.tt', { posts: res, current: currentId })
).postprocess();
//resetAnchor();
}
}
function renderComments (res) {
setStatus(false, 'renderComments');
//alert("Comments: " + res.errstr);
if (!openresty.isSuccess(res)) {
error("Failed to get post list: " + res.errstr);
} else {
$(".comments-content").html(
Jemplate.process('comments.tt', { comments: res })
);
//resetAnchor();
//setTimeout( function () { alert("Hi"); resetAnchor(); }, 1000 );
}
}
function renderPostList (res) {
setStatus(false, 'renderPostList');
if (!openresty.isSuccess(res)) {
error("Failed to get post list: " + res.errstr);
} else {
//alert(JSON.stringify(data));
$("#beta-inner.pkg").html(
Jemplate.process('post-list.tt', { post_list: res })
).postprocess();
}
//resetAnchor();
}
function renderSearchResults (res, query) {
setStatus(false, 'renderSearchResults');
if (!openresty.isSuccess(res)) {
error("Failed to get search results: " + res.errstr);
} else {
if (res.length) {
var html = Jemplate.process('post-list.tt', { post_list: res })
$("#beta-inner.pkg").html(html).postprocess();
} else {
$("#beta-inner.pkg").html("<p>Sorry, no search results found for query <b>\"" + query + "\"</b>.</p>");
}
}
}
function renderPager (res, page, prefix, suffix) {
setStatus(false, 'renderPager');
if (!openresty.isSuccess(res)) {
error("Failed to render pager: " + res.errstr);
} else {
//debug("before rendering pager...");
cachedPostCountRes = res;
var pageCount = Math.ceil(parseInt(res[0].count) / itemsPerPage);
if (pageCount < 2) return;
//debug("before redering pager (2)...");
var html = Jemplate.process(
'pager.tt',
{ page: page, page_count: pageCount, title: 'Pages', prefix: prefix, suffix: suffix }
);
//debug("after html generation...");
// we use the .each hack here to work aound a JS runtime error in IE 6:
$(".pager").each( function () {
$(this).html( html ).postprocess();
} );
//debug("after rendering pager...");
resetAnchor();
}
}