saunter-home.js
5.76 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
var $ = require('yoho.jquery'),
lazyLoad = require('yoho.lazyload'),
Mlellipsis = require('mlellipsis'),
Mustache = require('yoho.mustache'),
Swiper = require('yoho.iswiper');
var $nav = $('#saunter-nav'),
$msgList = $('#msg-list'),
$loadMore = $('#load-more-info'),
$noMore = $loadMore.children('.no-more'),
$loading = $loadMore.children('.loading'),
loadH = $loadMore.height(),
cached = {},
reloadFirst = false, //重新加载首页数据标志
loading = false, //正在ajax标志
winH = $(window).height(),
query = $('#query').val(), //无author时
gender = $('#gender').val(),
clientType = $('#client-type').val(),
tpl,
swiper;
require('./wxshare')();
//初始化focus
$('.saunter-nav > li:first-child').addClass('focus');
//初始化cached
$('.saunter-nav li').each(function() {
var $el = $(this),
page = $el.index() === 0 ? 1 : 0;
cached[$el.data('type')] = {
page: page,
end: false,
msgs: []
};
});
swiper = new Swiper('.swiper-container', {
pagination: '.swiper-pagination',
lazyLoading: true,
lazyLoadingInPrevNext: true,
autoplayDisableOnInteraction: false
});
lazyLoad();
//相关文章截取文字
Mlellipsis.init();
$('.tag-title').each(function() {
this.mlellipsis(2);
});
$('.tag-text').each(function() {
this.mlellipsis(3);
});
//读取模板
$.get('/common/tagtpl', function(data) {
tpl = data;
Mustache.parse(tpl);
});
/**
* 生成资讯HTML
* @param data Array 渲染数据
* @return html String 返回的HTML
*/
function getMsgHtml(msgs) {
var html = '',
i;
for (i = 0; i < msgs.length; i++) {
html += Mustache.render(tpl, msgs[i]);
}
return html;
}
$nav.delegate('li', 'click', function() {
var $el = $(this),
type = $el.data('type');
if ($el.hasClass('foucs')) {
return;
}
$nav.find('li.focus').removeClass('focus');
$el.addClass('focus');
if (cached[type].msgs.length > 0 && ($el.index() !== 0 || reloadFirst)) {
$msgList.html(getMsgHtml(cached[type].msgs));
lazyLoad($msgList.find('img.lazy'));
return;
}
$.ajax({
type: 'GET',
url: '/tags/get',
data: {
max_sort_id: type,
page: 1,
query: query,
gender: gender,
client_type: clientType
}
}).then(function(data) {
var res;
if (data.code === 200) {
res = data.data.infos;
$msgList.html(getMsgHtml(res));
//
if ($el.index() === 0) {
//点击第一个项只需更新cachedMsg,不需要更新page
reloadFirst = true;
cached[type].msgs = res.concat(cached[type].msgs);
} else {
cached[type].page++;
//cache
cached[type].msgs = res;
}
//lazyload
lazyLoad($msgList.find('img.lazy'));
}
});
});
//下拉加载数据
function loadMore() {
var $focusNav = $nav.find('li.focus'),
type = $focusNav.data('type'),
num;
if (loading || cached[type].end || $(window).scrollTop() + winH < $(document).height() - loadH) {
return;
}
if ($focusNav.index() !== 0 || reloadFirst) {
num = cached[type].msgs.length;
} else {
num = $msgList.children('.tag-content').length;
}
$loading.removeClass('hide');
$noMore.addClass('hide');
$.ajax({
type: 'GET',
url: '/tags/get',
data: {
max_sort_id: type,
page: cached[type].page + 1,
query: query,
gender: gender,
client_type: clientType
}
}).then(function(data) {
var res, msgs, html,
$newContent;
if (data.code === 200) {
res = data.data;
if (res.end) {
cached[type].end = true;
$loading.addClass('hide');
$noMore.removeClass('hide');
}
msgs = res.infos;
html = getMsgHtml(msgs);
$msgList.append(html);
//update cache
cached[type].msgs = cached[type].msgs.concat(msgs);
cached[type].page++;
//lazyload & mlellipsis
if (num === 0) {
$newContent = $msgList.children('.tag-content');
} else {
$newContent = $msgList.children('.tag-content:gt(' + (num - 1) + ')');
}
lazyLoad($newContent.find('img.lazy'));
$newContent.find('.tag-title').each(function() {
this.mlellipsis(2);
}).end().find('.tag-text').each(function() {
this.mlellipsis(3);
});
}
loading = false;
});
}
$(window).scroll(loadMore);
//资讯点赞和取消点赞
$('.msg-list').delegate('.like-btn', 'touchstart', function(e) {
var $cur = $(e.currentTarget),
$info = $cur.closest('.tag-content'),
id = $info.data('id'),
url,
udid = $('.client-ip').val();
$cur.toggleClass('like');
if ($cur.hasClass('like')) {
url = '/guang/info/praise';
} else {
url = '/guang/info/cancelpraise';
}
$.ajax({
type: 'GET',
url: url,
data: {
id: id,
udid: udid
}
}).then(function(data) {
if (data.code === 200) {
//更新点赞数
$cur.next('span.like-count').text(data.data);
} else if (data.code === 400) {
//提示登录信息
$('#login-tip').fadeIn(500, function() {
setTimeout(function() {
$('#login-tip').fadeOut(500);
}, 1000);
});
}
});
});