comment.js
16.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* 评论及登录插件
* author:liuyue
* date:2015-01-07
*/
var $ = require("jquery"),
validate = require("./validate"),
box = require("./box"),
tools = require("yoho-tools"),
tips = require("./tips");
mustache = require("mustache");
require("./login");
(function(global, undefined) {
var Comment = function(element, options) {
this.options = options;
this.$element = $(element);
//this.$loginBox = $(this.options.loginbox).appendTo("body");
this.init();
};
Comment.DEFAULTS = {
cid: '',
commentPageUrl: '/writer/default/comment',
publishUrl: '/writer/default/publish',
deleteUrl: '/writer/default/delete',
commentNum: $(".comment-num"),
commentItemTpl: '<li class="clearfix">'+
'<div class="userimg">'+
'<img src="\{{userimg}}" alt="">'+
'<i class="\{{partnerType}}"></i>'+
'</div>'+
'<div class="userinfo">'+
'<h6>\{{username}}</h6> '+
'<p class="comment-text">\{{content}}</p>'+
'\{{# isSelf}}'+
'<a class="delete-comment" href="javascript:;" pid="{{id}}"></a>'+
'\{{/ isSelf}} '+
'</div>'+
'</li>',
thirdLogin: false,
isEdit: false,
isMobile: false,
login: ''
};
Comment.prototype = {
constructor: Comment,
init:function() {
// 组件的创建主函数。
this._creat();
},
// 主函数,主要是创建节点,绑定事件。
_creat: function() {
if ($(".comments-list").size() > 0 || $(".comment-textarea").size() > 0) return;
// 创建节点
this._creatCommentDom();
this._creatThirdLogin();
// 绑定事件
this._bindEvent();
},
_creatCommentDom:function() {
var that = this;
//var id = {id:this.options.cid};
$.ajax({
type:'GET',
url:YohoConfig.mainUrl + this.options.commentPageUrl,
/*data: id,
dataType:'json',*/
success:function(response) {
var data = response.data, //返回数据的data
viewMoreNum = data.total - 5, //评论总数-5,除显示评论剩下的评论数量
moreString = viewMoreNum + '', //剩余评论数量转为字符串
moreFirstnum = moreString.slice(0,1), //评论数量的首数字
commentTpl = $('#comment-tpl').html(), //引入评论html模板
commentPage, //分页数据
commentHtml, //用数据渲染后的html
commentData; //评论数据
if(data.length === 0) return;
commentData = {
comment: data.comment
};
//评论多于五条,渲染more节点
if (viewMoreNum > 0) {
commentData = {
comment: data.comment,
more: {
moreNum : viewMoreNum
}
}
}
//如果分页数据不为空和undefined,渲染分页
if ($.trim(data.page) !== '' || typeof(data.page) === 'undefined' || !!data.page) {
commentPage = data.page;
commentData = {
comment: data.comment,
more: {
moreNum : viewMoreNum
},
page: commentPage
}
}
commentHtml = mustache.render(commentTpl, commentData, {
html: commentPage
});
that.$element.prepend($(commentHtml));
//5条后的评论隐藏
that.$element.find("li").eq(4).nextAll().hide(); //5条后的评论隐藏
//评论总数上千显示k+,上万显示w+
if (moreString.length === 4) {
$(".view-more").find("span").text(moreFirstnum + 'k+');
} else if (moreString.length === 5) {
$(".view-more").find("span").text(moreFirstnum + 'w+');
}
}
});
},
//创建评论文本域
/*_creatTextareaDom:function(){
data = this.options.login;
var textarea = $('<div class="comment-textarea"><textarea placeholder="'+tips.getTips("textareaPlaceholder")+'" cid='+this.options.cid+'></textarea></div>'),
loginarea = $('<div class="login-status clearfix">\n\
<div class="userimg">\n\
<img src="'+YohoConfig.resUrl+'boysweb/assets/images/detail/user-img.png" alt="">\n\
<i></i>\n\
</div>\n\
<div class="userinfo">\n\
<span></span>\n\
<a class="exit-btn" href="javascript:;">退出</a>\n\
</div>\n\
<div class="publish">\n\
<a class="publish-btn" href="javascript:;">Publish</a>\n\
</div>\n\
</div>');
textarea.appendTo(this.$element);
loginarea.appendTo(textarea);
if(this.options.isFeedback){
var anonymous = $('<div class="anonymous">YohoBoy</div>');
$(".login-status .userimg").after(anonymous);
}
if(!data){
$(".login-status .userinfo").hide();
}
},*/
//创建第三方登录
_creatThirdLogin:function(){
var thirdLoginTpl = $("#third-login-tpl").html();
if (this.options.thirdLogin === true) {
$(thirdLoginTpl).appendTo(this.$element);
//this.$element.html(thirdLoginTpl);
}
},
//事件绑定
_bindEvent: function(){
var that = this;
//查看更多评论
this.$element.on("click.comment",".view-more",function() {
that.$element.find("li").show(); //显示第一页后五条
that.$element.find(".comment-pager-content").show(); //显示分页按钮
that.$element.find(".view-more").remove(); //移除view more
});
//弹出登录框
/*$(this).login();
this.$element.on("click.login",".publish-btn",function() {
if(that.options.isFeedback) return;
$(this).login("loginBoxAnimate");
});*/
//显示删除按钮
if (this.options.isMobile) {
this.$element.find(".delete-comment").show();
} else {
this.$element.on("mouseenter","li",function() {
$(this).find(".delete-comment").show();
}).on("mouseleave","li",function(){
$(this).find(".delete-comment").hide();
});
}
//发表评论
this.$element.on("click.publish",".publish-btn",function() {
var $textarea = that.$element.find("textarea"),
nameInput = that.$element.find(".name-input"),
emailInput = that.$element.find(".email-input"),
comment = $textarea.val(),
cid,
data,
commentsNum,
commentsList,
publishHtml;
//验证名字
if (!tools.IYOHO_isUserName(nameInput.val())) {
box.alert(tips.getTips("commentUncompleted"), 5, function() {
nameInput.focus();
});
return false;
};
//验证邮箱
if (emailInput.val() !== '' && !tools.IYOHO_isEmail(emailInput.val())) {
box.alert(tips.getTips("commentUncompleted"), 5, function() {
emailInput.focus();
});
return false;
};
//验证评论
if (typeof(comment) === 'undefined' || $.trim(comment) === '') {
box.alert(tips.getTips("commentUncompleted"), 5, function() {
$textarea.focus();
});
return false;
};
if (comment.length > 100) {
box.alert(tips.getTips("commentToMuch"), 5);
return false;
};
cid = that.$element.attr('cid');
if (!cid) {
return false;
};
data = {id:cid,comment:comment,sync:'222',title:'111'};
$.ajax({
type: 'GET',
url: YohoConfig.mainUrl + that.options.publishUrl,
success: function(response) {
if (response.code === '800000') {
box.alert(tips.getTips("relogin"), 5);
that.$element.login("setLogout");
return false;
};
if (response.status && response.data) {
var $commentsList = $(".comments-list");
publishHtml = mustache.render(that.options.commentItemTpl, response.data);
if ($commentsList .find("li").size() === 0){
$commentsList.html(publishHtml);
} else {
$commentsList.find("li").eq(0).before(publishHtml);
}
box.alert(tips.getTips("publishSuccess"), 5);
$(".comment-textarea textarea").val('');
commentsNum = 1;
if(!!parseInt(that.options.commentNum.eq(0).text())){
commentsNum = parseInt(that.options.commentNum.eq(0).text())+1;
};
that.options.commentNum.text(commentsNum);
} else if (900000 === response.code) {
box.alert(tips.getTips("notSameContent"), 5);
} else {
box.alert(tips.getTips("systemBus"), 5);
}
publishHtml = mustache.render(that.options.commentItemTpl, response.data);
that.$element.find(".comments-list").prepend(publishHtml);
}
})
/*$.ajax({
type:'POST',
url:YohoConfig.mainUrl + that.options.publishUrl,
data: data,
dataType:'json',
success:function(response) {
if (response.code === '800000')
{
box.alert(tips.getTips("relogin"), 5);
that.$element.login("setLogout");
return false;
}
if (response.status && response.data) {
var $commentsList = $(".comments-list");
var commentItem = '<li class="clearfix">'+
'<div class="userimg">'+
'<img src="'+response.data.headpic+'" alt="">'+
'<i class="'+response.data.partnerType+'"></i>'+
'</div>'+
'<div class="userinfo">'+
'<h6>'+response.data.nick+'</h6>'+
'<p class="comment-text">'+response.data.content+'</p>'+
'<a class="delete-comment" href="javascript:;" pid="'+response.data.id+'"></a>'+
'</div>'+
'</li>';
if(that.options.isFeedback && $(".login-status .anonymous").css("display") != "none"){
commentItem = '<li class="clearfix">'+
'<div class="userimg">'+
'<img src="'+response.data.headpic+'" alt="">'+
'<i class="'+response.data.partnerType+'"></i>'+
'</div>'+
'<div class="userinfo">'+
'<h6>'+response.data.nick+'</h6>'+
'<p class="comment-text">'+response.data.content+'</p>'+
'</div>'+
'</li>';
}
if($commentsList .find("li").size()==0){
$commentsList .html(commentItem);
}else{
$commentsList .find("li").eq(0).before(commentItem);
}
if(that.options.isFeedback){
box.alert(tips.getTips("publishFeedback"), 5);
}else if(that.options.isEdit){
box.alert(tips.getTips("editPublishSuccess"), 5);
}else{
box.alert(tips.getTips("publishSuccess"), 5);
}
$(".comment-textarea textarea").val('');
var comments = 1;
if(!!parseInt(that.options.commentNum.eq(0).text())){
comments = parseInt(that.options.commentNum.eq(0).text())+1;
};
that.options.commentNum.text(comments);
}
else if(900000 == response.code){
box.alert(tips.getTips("notSameContent"), 5);
}
else
{
box.alert(tips.getTips("systemBus"), 5);
}
}
}); */
});
//评论翻页
this.$element.on("click.commentPage",".pager-item",function(){
var url = $(this).attr("href"),
pageHtml,
pageIcon,
commentListTop;
$.ajax({
type: 'GET',
url: url,
success: function(response) {
$(".comments-list").empty();
pageHtml = mustache.render(that.options.commentItemTpl, response.data.comment);
$(".comments-list").html(pageHtml);
pageIcon = $('<div class="channel-index-pager">'+response.data.page+'</div>');
$(".comment-pager-content").html(pageIcon);
commentListTop = $(".comments-list").offset().top - $("#minEnterprise").outerHeight();
$(window).scrollTop(commentListTop);
}
});
return false;
});
//删除评论
this.$element.on("click.deleteComment",".delete-comment",function(){
var _this = $(this);
var id = _this.attr('pid');
var deleteTip = tips.getTips("deleteComment");
if(that.options.isFeedback){
deleteTip = tips.getTips("deleteFeedback");
}else if(that.options.isEdit){
deleteTip = tips.getTips("deleteEdit");
}
box.confirm(deleteTip, function()
{
$.ajax
({
type:'POST',
url:YohoConfig.mainUrl+that.options.deleteUrl,
data:'id='+id,
dataType:'json',
success:function(response)
{
if (response.status)
{
_this.parents("li").remove();
var comments = parseInt($(".comment-num").eq(0).text())-1;
if(comments > 0){
that.options.commentNum.text(comments);
}else{
that.options.commentNum.html('<img src="'+YohoConfig.resUrl+'boysweb/assets/images/mobile/comment-mobile.png">');
$(".comment-num.zero").text(0);
$(".comment-num.edit-comment").text('留言');
}
}
}
});
});
});
$(".third-login-box").on("click","a",function(){
var _this = $(this);
if (!_this.attr('ref')) return;
window.open(_this.attr('ref'), "","width=760,height=650,top=300,left=300");
return false;
})
$(".third-login-box").on("click","a.yoho",function(){
$(this).login("loginBoxAnimate");
})
},
publishComment:function(){
var textareaVal = $(".comment-textarea").find("textarea").val();
if (!!!textareaVal || $.trim(textareaVal) === '') return;
if(this.options.isFeedback) return;
$(".publish-btn").trigger("click.publish");
}
};
function Plugin(option, _relatedTarget) {
return this.each(function() {
var $this = $(this);
var data = $this.data('yoho.Comment');
var options = $.extend({}, Comment.DEFAULTS, $this.data(), typeof option == 'object' && option);
// 如果没有初始化,则初始化该对象,并且把该对象绑定到此元素的yoho.PicSilder属性下。
if (!data) $this.data('yoho.Comment', (data = new Comment(this, options)));
// 如果传递的options是一个字符串,则表示调用改对象的原型方法。
if (typeof option == 'string') data[option](_relatedTarget);
});
};
$.fn.comment = Plugin;
$.fn.comment.Constructor = Comment;
})(this);