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,api - Where to get data
* disqusjs.config.apikey - The apikey used to request Disqus API
* disqusjs.config.admin - The disqus forum admin username
*
* DisqusJS Info
* 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 = {};
disqusjs.page = [];
disqusjs.mode = 'proxy';
var xhr = new XMLHttpRequest();
@ -119,21 +120,27 @@ function checkDisqus() {
*/
function getThreadInfo() {
var url = disqusjs.config.api + '3.0/threads/list.json?forum=' + disqusjs.config.shortname + '&thread=ident:' + disqusjs.config.identifier + '&api_key=' + disqusjs.config.apikey;
/*
* 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 response = JSON.parse(this.responseText).response[0];
disqusjs.page = {
id: response.id,
title: response.title,
isClosed: response.isClosed,
length: response.posts
};
console.log(disqusjs);
getComment();
var res = JSON.parse(this.responseText);
if (res.code === 0) {
renderCommentList(res.response);
} else {
// Have error when get comments.
}
}
};
xhr.ontimeout = function (e) {
@ -144,21 +151,20 @@ function getThreadInfo() {
};
}
/*
* Name: getComment()
* Description: get the comment content
* API URI: /3.0/posts/list.json?forum=[shortname]&thread=[thread id]&api_key=[apikey]
*/
function getComment() {
var url = disqusjs.config.api + '3.0/posts/list.json?forum=' + disqusjs.config.shortname + '&thread=' + disqusjs.page.id + '&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.timeout = 4000;
xhr.send();
xhr.onload = function () {
if (this.status == 200 || this.status == 304) {
var response = JSON.parse(this.responseText);
console.log(response);
var response = JSON.parse(this.responseText).response[0];
disqusjs.page = {
id: response.id,
title: response.title,
isClosed: response.isClosed,
length: response.posts
};
getComment();
}
};
xhr.ontimeout = function (e) {
@ -169,4 +175,54 @@ function getComment() {
};
}
getMode();
/*
* Name: renderCommentList(data)
* Description: Render JSON to comment list components
*/
function renderCommentList(data) {
var topLevelComments = [];
var childComments = [];
data.forEach(function (comment) {
(comment.parent ? childComments : topLevelComments)['push'](comment)
})
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
}
var list = []
for (var i = 0; i < childComments.length; i++) {
var comment = childComments[i];
if (comment.parent === id) {
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)
}

Loading…
Cancel
Save