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();
@ -119,21 +120,27 @@ function checkDisqus() {
*/ */
function getThreadInfo() { 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.open('GET', url, true);
xhr.timeout = 4000; xhr.timeout = 4000;
xhr.send(); xhr.send();
xhr.onload = function () { xhr.onload = function () {
if (this.status == 200 || this.status == 304) { if (this.status == 200 || this.status == 304) {
var response = JSON.parse(this.responseText).response[0]; var res = JSON.parse(this.responseText);
disqusjs.page = { if (res.code === 0) {
id: response.id, renderCommentList(res.response);
title: response.title, } else {
isClosed: response.isClosed, // Have error when get comments.
length: response.posts }
};
console.log(disqusjs);
getComment();
} }
}; };
xhr.ontimeout = function (e) { xhr.ontimeout = function (e) {
@ -144,21 +151,20 @@ 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]
*/
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;
xhr.open('GET', url, true); xhr.open('GET', url, true);
xhr.timeout = 4000; xhr.timeout = 4000;
xhr.send(); xhr.send();
xhr.onload = function () { xhr.onload = function () {
if (this.status == 200 || this.status == 304) { if (this.status == 200 || this.status == 304) {
var response = JSON.parse(this.responseText); var response = JSON.parse(this.responseText).response[0];
console.log(response); disqusjs.page = {
id: response.id,
title: response.title,
isClosed: response.isClosed,
length: response.posts
};
getComment();
} }
}; };
xhr.ontimeout = function (e) { 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