feat: finish comment list json output

0.2.5(deprecated)
SukkaW 7 years ago
parent 1e58163e10
commit d73e9f9962

@ -17,6 +17,7 @@
* disqusjs.config.url - The url of the page * disqusjs.config.url - The url of the page
* disqusjs.config,api - Where to get data * disqusjs.config,api - Where to get data
* disqusjs.config.apikey - The apikey used to request Disqus API * disqusjs.config.apikey - The apikey used to request Disqus API
* disqusjs.config.admin - The disqus forum admin username
* *
* DisqusJS Info * DisqusJS Info
* disqusjs.page.id = The thread id, used at next API call * disqusjs.page.id = The thread id, used at next API call
@ -25,7 +26,7 @@
* disqusjs.page.lenfth - How many comment in this thread * disqusjs.page.lenfth - How many comment in this thread
*/ */
disqusjs.page = {}; disqusjs.page = [];
disqusjs.mode = 'proxy'; disqusjs.mode = 'proxy';
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
@ -80,10 +81,10 @@ function checkDisqus() {
var setmode = function () { var setmode = function () {
if (success = test) { if (success = test) {
disqusjs.mode = 'direct', disqusjs.mode = 'direct',
setLS('disqusjs_mode', 'direct'); setLS('disqusjs_mode', 'direct');
} else { } else {
disqusjs.mode = 'proxy', disqusjs.mode = 'proxy',
setLS('disqusjs_mode', 'proxy'); setLS('disqusjs_mode', 'proxy');
} }
}; };
var check = function (domain) { var check = function (domain) {
@ -119,6 +120,37 @@ function checkDisqus() {
*/ */
function getThreadInfo() { function getThreadInfo() {
/*
* Name: getComment()
* Description: get the comment content
* API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
*/
getComment = function () {
var url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey;
xhr.open('GET', url, true);
xhr.timeout = 4000;
xhr.send();
xhr.onload = function () {
if (this.status == 200 || this.status == 304) {
var res = JSON.parse(this.responseText);
if (res.code === 0) {
renderCommentList(res.response);
} else {
// Have error when get comments.
}
}
};
xhr.ontimeout = function (e) {
console.log(e)
};
xhr.onerror = function (e) {
console.log(e)
};
}
var url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey; var url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey;
xhr.open('GET', url, true); xhr.open('GET', url, true);
xhr.timeout = 4000; xhr.timeout = 4000;
@ -132,7 +164,6 @@ function getThreadInfo() {
isClosed: response.isClosed, isClosed: response.isClosed,
length: response.posts length: response.posts
}; };
console.log(disqusjs);
getComment(); getComment();
} }
}; };
@ -145,28 +176,53 @@ function getThreadInfo() {
} }
/* /*
* Name: getComment() * Name: renderCommentList(data)
* Description: get the comment content * Description: Render JSON to comment list components
* API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
*/ */
function getComment() { function renderCommentList(data) {
var url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&api_key=' + disqusjs.config.apikey; var topLevelComments = [];
xhr.open('GET', url, true); var childComments = [];
xhr.timeout = 4000;
xhr.send(); data.forEach(function (comment) {
xhr.onload = function () { (comment.parent ? childComments : topLevelComments)['push'](comment)
if (this.status == 200 || this.status == 304) { })
var response = JSON.parse(this.responseText);
console.log(response); var commentLists = topLevelComments.map(function (comment) {
return {
comment: comment,
author: comment.author.name,
isPrimary: comment.author.username === disqusjs.config.admin,
children: getChildren(Number(comment.id))
};
});
function getChildren(id) {
if (childComments.length === 0) {
return null
} }
};
xhr.ontimeout = function (e) { var list = []
console.log(e) for (var i = 0; i < childComments.length; i++) {
}; var comment = childComments[i];
xhr.onerror = function (e) { if (comment.parent === id) {
console.log(e) list.unshift({
}; comment,
author: comment.author.name,
isPrimary: comment.author.username === disqusjs.config.admin,
children: getChildren(+comment.id)
})
}
}
if (list.length) {
return list
} else {
return null
}
}
console.log(commentLists)
} }
getMode();

Loading…
Cancel
Save