detail.page.js
6.16 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
/**
* 逛详情页
* @author: liuyue<yue.liu@yoho.cn>
* @date: 2015/12/16
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload'),
$commentArea = $('#comment-area'),
articleId = $('.detail-body').data('id');
var $commentList = $commentArea.find('.comments-wrap'),
$commentNum = $('#article-comment > .comment-num'),
commenting = false, // 评论请求尚未返回变量
locating = false, // 评论页面正在跳转
$commentBtn = $('#comment-btn'),
MAX_COMMENTS_WORDS = 100,
$wordCountTip = $('#word-count-tip');
require('./right-side');
require('./img-blink');
require('../common/share');
require('./detail-dynamic');
// Pjax
require('yoho-jquery-pjax');
lazyLoad({
failure_limit: 50
});
// 点击评论滑到评论区
// $('#article-comment').click(function() {
// $('html, body').animate({
// scrollTop: $commentArea.offset().top - 20
// }, 800);
// });
// 文章点赞与取消点赞
$('#prise-btn').click(function() {
var prising = false,
url,
$this = $(this);
if (prising) {
return;
}
$this.toggleClass('liked');
if ($this.hasClass('liked')) {
// 点赞
url = '/guang/info/praise';
} else {
// 取消点赞
url = '/guang/info/cancelPraise';
}
prising = true;
$.ajax({
type: 'GET',
url: url,
data: {
id: articleId
}
}).then(function(data) {
if (data.code === 200) {
$this.find('.like-num').html(data.data);
}
prising = false;
});
}).bind('mouseenter mouseleave', function() {
$(this).toggleClass('hover');
});
// 文章收藏与取消收藏
$('#collect-btn').click(function() {
var collecting = false,
url,
col,
$this = $(this);
if (collecting) {
return;
}
if ($this.hasClass('collected')) {
// 取消收藏
url = '/guang/info/cancelcollect';
col = 0;
} else {
// 收藏
url = '/guang/info/collect';
col = 1;
}
collecting = true;
$.ajax({
type: 'GET',
url: url,
data: {
id: articleId
}
}).then(function(data) {
var hrefUrl;
switch (data.code) {
case 401:
// 防止从已有col的页面再次进行跳转后出错的情况
if (/\?col=(1|0)/.test(location.href)) {
hrefUrl = location.href.replace(/\?col=(1|0)/, '?col=' + col);
} else {
hrefUrl = location.href + '?col=' + col;
}
location.href = '//www.yohobuy.com/signin.html?refer=' + encodeURI(hrefUrl);
break;
case 400:
// alert(data.message);
break;
case 200:
if (/\?col=(1|0)/.test(location.href)) {
// 如果页面url中含有col,为了防止页面刷新时收藏或者取消收藏会根据col来的问题,进行页面跳转拿掉参数
location.href = location.href.replace(/\?col=(1|0)/, '');
} else {
$this.toggleClass('collected');
}
break;
}
collecting = false;
});
}).bind('mouseenter mouseleave', function() {
$(this).toggleClass('hover');
});
function showComment() {
var commonlist = $('.comments-list').find('li').length;
if (commonlist === 0) {
$('.commnets-resultwrapper').hide();
$('.comments-empty').show();
} else {
$('.commnets-resultwrapper').show();
$('.comments-empty').hide();
}
}
showComment();
// 评论
function comment(id) {
var commentInfo = $('#comment-info').val();
if (commentInfo === '') {
alert('评论不能为空');
return false;
}
commenting = true;
$.ajax({
url: '/guang/info/comment',
data: {
id: id,
comment: commentInfo
},
type: 'post',
success: function(data) {
switch (data.code) {
case 401:
locating = true;
location.href = '//www.yohobuy.com/signin.html?refer=' +
window.escape(location.href + '#comment-info');
break;
case 400:
alert(data.message);
break;
case 200:
if (data.data) {
$commentList.html(data.data.content);
$commentNum.html(data.data.count);
showComment();
// clear comment-text
$('#comment-info').val('').keyup();
}
break;
}
commenting = false;
}
});
}
$commentBtn.click(function(e) {
// 页面正在跳转或者正在AJAX请求时评论无效
if (locating || commenting) {
return false;
}
// 字数不符合要求
if ($('#comment-info').val().length - MAX_COMMENTS_WORDS > 0) {
return;
}
e.preventDefault();
comment(articleId);
});
// comment pager pjax
$(document).pjax('.comment-pager a', '#pjax-container', {
timeout: 5000
});
// 分页后移动到评论框的位置
$(document).on('pjax:end', function() {
showComment();
$('html,body').scrollTop($('#comment-info').offset().top);
// 设置头部评论数
$('#article-comment .comment-num').text($('#comment-area .comment-num').text());
});
$('#comment-info').keyup(function() {
var len = $(this).val().length,
showTxt;
if (len === 0) {
$wordCountTip.html('');
$commentBtn.addClass('disable');
} else {
if (len - MAX_COMMENTS_WORDS <= 0) {
showTxt = '还可以输入' + (MAX_COMMENTS_WORDS - len) + '字';
$commentBtn.removeClass('disable');
} else {
showTxt = '已超过<span class="exceed-count">' + (len - MAX_COMMENTS_WORDS) + '</span>字';
$commentBtn.addClass('disable');
}
}
$wordCountTip.html(showTxt);
});
// init
$('#comment-info').trigger('keyup');