Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bay-components",
"version": "0.3.3",
"version": "0.3.4",
"description": "common functions for shanbay frontend projects",
"main": "lib/index.js",
"scripts": {
Expand Down
15 changes: 12 additions & 3 deletions src/js/PullToLoadList.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global bayUtils */
const { ajax } = bayUtils;

export class PullToLoadList {
Expand Down Expand Up @@ -59,13 +60,21 @@ export class PullToLoadList {
if (container === window) {
const docRoot = document.documentElement;

if (docRoot.scrollHeight - Math.ceil(window.scrollY || window.pageYOffset) - this.threshold <= docRoot.clientHeight) {
if (
docRoot.scrollHeight -
Math.ceil(window.scrollY || window.pageYOffset) -
this.threshold <=
docRoot.clientHeight
) {
this.loadMoreData();
}
} else {
// reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight
// Note: container 固定高 且 scroll
if (container.scrollHeight - Math.ceil(container.scrollTop) - this.threshold <= container.clientHeight) {
if (
container.scrollHeight - Math.ceil(container.scrollTop) - this.threshold <=
container.clientHeight
) {
this.loadMoreData();
}
}
Expand Down Expand Up @@ -118,7 +127,7 @@ export class PullToLoadList {
ajax({
url,
type: 'GET',
success: (data) => {
success: data => {
const parsedData = this.parseData(data);
this.$loadHint.hide();
this.items = parsedData.items;
Expand Down
94 changes: 58 additions & 36 deletions src/js/comment.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global bayUtils */
const { merge, ajax, formatDate } = bayUtils;

class Comment {
Expand Down Expand Up @@ -55,13 +56,15 @@ class Comment {
updateLikeStatus(status) {
const { comment, buildLikeApiData } = this.options;

ajax(merge(buildLikeApiData({ vote: status, commentId: comment.id }), {
type: 'PUT',
data: { vote: status },
success: () => {
this.changeLikeStatus(status);
},
}));
ajax(
merge(buildLikeApiData({ vote: status, commentId: comment.id }), {
type: 'PUT',
data: { vote: status },
success: () => {
this.changeLikeStatus(status);
},
}),
);
}

// 改变喜欢/不喜欢状态
Expand Down Expand Up @@ -89,7 +92,7 @@ class Comment {
this.$toggleDislikeBtn.addClass('disable');
this.$toggleDislikeBtn.html('已不喜欢');
if (isVotedUp) {
$voteNum.html((voteNum - 1) || '');
$voteNum.html(voteNum - 1 || '');
}
}

Expand All @@ -98,7 +101,7 @@ class Comment {
this.$toggleDislikeBtn.removeClass('disable');
this.$toggleDislikeBtn.html('不喜欢');
if (isVotedUp) {
$voteNum.html((voteNum - 1) || '');
$voteNum.html(voteNum - 1 || '');
}
}
}
Expand Down Expand Up @@ -126,8 +129,14 @@ class Comment {
// 渲染
render() {
const {
$el, comment, currentUser, isInteractiveDisabled,
hasLikeBtn, hasDislikeBtn, hasNestReplyBtn, hasReportBtn,
$el,
comment,
currentUser,
isInteractiveDisabled,
hasLikeBtn,
hasDislikeBtn,
hasNestReplyBtn,
hasReportBtn,
} = this.options;
const data = comment;
const user = data.user || {};
Expand All @@ -143,47 +152,60 @@ class Comment {
</div>
</div>
<div class="operation-partial">
${currentUser.user_id_str === user.id && !isInteractiveDisabled ?
`<i
${
currentUser.user_id_str === user.id && !isInteractiveDisabled
? `<i
class="ib ib-pencil operation-item show-input-btn"
data-comment='${JSON.stringify(data)}'
/>` :
''
/>`
: ''
}
${hasLikeBtn ?
`<div class="toggle-like-btn operation-item ${data.is_voted_up ? 'active' : ''}">
${
hasLikeBtn
? `<div class="toggle-like-btn operation-item ${
data.is_voted_up ? 'active' : ''
}">
<i class="ib ib-thumbs-up-o" />
<span class="vote-num">${data.vote_score || ''}</span>
</div> ` :
''
</div> `
: ''
}
${(hasDislikeBtn || hasNestReplyBtn || hasReportBtn) && !isInteractiveDisabled ?
`<div class="operation-list">
${
(hasDislikeBtn || hasNestReplyBtn || hasReportBtn) &&
!isInteractiveDisabled
? `<div class="operation-list">
<div class="dots show-operations-btn">…</div>
<div class="list">
${hasDislikeBtn ?
`<span class="toggle-dislike-btn ${data.is_voted_down ? 'disable' : ''}">
${
hasDislikeBtn
? `<span class="toggle-dislike-btn ${
data.is_voted_down ? 'disable' : ''
}">
${data.is_voted_down ? '已不喜欢' : '不喜欢'}
</span> ` :
''
</span> `
: ''
}
${hasNestReplyBtn ?
`<span
${
hasNestReplyBtn
? `<span
class="show-input-btn"
data-to-user-id=${user.id}
>回复</span>` :
''
>回复</span>`
: ''
}
${hasReportBtn ?
`<a
${
hasReportBtn
? `<a
class="report-btn"
href="/web/report?report_url=${encodeURIComponent(data.report_url)}&next=${encodeURIComponent(window.location.href)}"
> 举报 </a>` :
''
href="/web/report?report_url=${encodeURIComponent(
data.report_url,
)}&next=${encodeURIComponent(window.location.href)}"
> 举报 </a>`
: ''
}
</div>
</div>` :
''
</div>`
: ''
}
</div>
</div>
Expand Down
75 changes: 49 additions & 26 deletions src/js/comments.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { merge, ajax } = bayUtils;
/* global bayUtils bayComponents */
import comment from './comment';

const { merge, ajax } = bayUtils;

class Comments {
constructor(options) {
this.options = merge(options, {
Expand Down Expand Up @@ -104,37 +106,49 @@ class Comments {

// 更新评论
if (this.editingComment.id) {
ajax(merge(buildCommentApiData({
commentId: this.editingComment.id,
content,
}), {
type: 'PUT',
success: this.render,
}));
ajax(
merge(
buildCommentApiData({
commentId: this.editingComment.id,
content,
}),
{
type: 'PUT',
success: this.render,
},
),
);

return;
}

// 回复评论
if (this.toUserId) {
ajax(merge(buildCommentApiData({
to_user_id: this.toUserId,
content,
}), {
url: apiUrl,
type: 'POST',
success: this.render,
}));
ajax(
merge(
buildCommentApiData({
to_user_id: this.toUserId,
content,
}),
{
url: apiUrl,
type: 'POST',
success: this.render,
},
),
);

return;
}

// 添加评论
ajax(merge(buildCommentApiData({ content }), {
url: apiUrl,
type: 'POST',
success: this.render,
}));
ajax(
merge(buildCommentApiData({ content }), {
url: apiUrl,
type: 'POST',
success: this.render,
}),
);
}

// 显示输入页面
Expand All @@ -149,7 +163,10 @@ class Comments {
}

this.$commentInputBox.show();
this.$commentInputBox.find('input').val(this.editingComment.content || '').focus();
this.$commentInputBox
.find('input')
.val(this.editingComment.content || '')
.focus();

return false;
}
Expand All @@ -164,9 +181,14 @@ class Comments {

// 渲染单个评论
renderComment(data) {
comment(merge({
comment: this.options.parseItemData(data),
}, this.options));
comment(
merge(
{
comment: this.options.parseItemData(data),
},
this.options,
),
);
}

renderEmptyBlock() {
Expand Down Expand Up @@ -205,6 +227,7 @@ class Comments {
$el.html('');
this.isCreating = false;

// eslint-disable-next-line no-new
new bayComponents.PullToLoadList({
ipp: this.options.ipp,
pageNum: this.options.pageNum,
Expand All @@ -214,7 +237,7 @@ class Comments {

renderItem: this.renderComment,
parseData: this.options.parseData,
onLoadedFirstPage: (data) => {
onLoadedFirstPage: data => {
const commentsData = parseData(data);
let commentData = {
user: {},
Expand Down
1 change: 1 addition & 0 deletions src/js/tabs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* global bayUtils */
const { merge } = bayUtils;

/*
Expand Down