From 288a4915b41b234a47e82b3d4a717fc8af435963 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 19 Oct 2018 15:10:05 +0800 Subject: [PATCH] feat: bring up load more comment --- src/disqus.js | 58 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/src/disqus.js b/src/disqus.js index d9d74b3..7588e25 100644 --- a/src/disqus.js +++ b/src/disqus.js @@ -174,7 +174,43 @@ function DisqusJS(config) { function loadDsqjs() { (() => { + // 在 #disqus_thread 中填充 DisqusJS Container + /* +
+
+
+
+
+ +
+
+
    + 加载更多评论 +
    + +
    + */ + + let d = document; + d.getElementById('disqus_thread').innerHTML = `
      加载更多评论
      `; + // 加载中信息 + d.getElementById('dsqjs-msg').innerHTML = `评论基础模式加载中。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式。` + let url = `${disqusjs.config.api}3.0/threads/list.json?forum=${disqusjs.config.shortname}&thread=ident:${disqusjs.config.identifier}&api_key=${disqusjs.config.apikey}`; + /* * Disqus API 只支持通过 Thread ID 获取评论列表,所以必须先通过 identifier 获取当前页面 Thread ID * @@ -182,7 +218,6 @@ function DisqusJS(config) { * API URI: /3.0/threads/list.json?forum=[disqus_shortname]&thread=ident:[identifier]&api_key=[apikey] */ get(url, (res) => { - console.log(res); // 如果只返回一条则找到了对应 thread,否则是当前 identifier 不能找到唯一的 thread // 如果 thread 不唯一则需要进行初始化 if (res.code === 0 && res.response.length === 1) { @@ -193,6 +228,11 @@ function DisqusJS(config) { isClosed: resp.isClosed, // 评论是否关闭 length: resp.posts // 评论数目 }; + + // 填充站点名称和评论数目 + let d = document; + d.getElementById('dsqjs-comment-num').innerHTML = disqusjs.page.length + d.getElementById('dsqjs-site-name').innerHTML = disqusjs.config.siteName // 获取评论列表 getComment() } else if (res.code === 0 && res.response.length !== 1) { @@ -229,10 +269,15 @@ function DisqusJS(config) { if (res.code === 0 && res.response.length > 0) { // 已获得评论列表 renderComment(res.response) + + var $loadMoreBtn = document.getElementById('dsqjs-load-more'); if (res.cursor.hasNext) { // 显示 加载更多评论 按钮 + $loadMoreBtn.classList.remove('dsqjs-hide'); + $loadMoreBtn.addEventListener('click', () => { getComment(res.cursor.next); }); } else { // 没有更多评论了,注意确保按钮隐藏 + $loadMoreBtn.classList.add('dsqjs-hide'); } } else if (res.code === 0 && res.response.length === 0) { // 当前没有评论 @@ -396,15 +441,18 @@ function DisqusJS(config) { } } - let html = '' + var html = '' data = parseCommentData(data); - html += data.map((comment) => { + data.map((comment) => { comment = processData(comment); - return ``; + html += `
    • ${renderPostItem(comment.comment)}${childrenComments(comment)}
    • `; }); - console.log(html) + let d = document; + d.getElementById('dsqjs-header').classList.remove('dsqjs-hide') + d.getElementById('dsqjs-msg').innerHTML = '你可能无法访问 Disqus,已启用评论基础模式。如需完整体验请针对 disq.us | disquscdn.com | disqus.com 启用代理并 尝试完整 Disqus 模式 | 强制完整 Disqus 模式。' + d.getElementById('dsqjs-post-container').insertAdjacentHTML('beforeend', html); } }