info.js
5 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
/**
* 资讯相关API
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/10/10
*/
var $ = require('jquery'),
Hammer = require('yoho.hammer'),
ellipsis = require('mlellipsis'),
lazyLoad = require('yoho.lazyload');
var tip = require('../plugin/tip');
var loading = require('../plugin/loading');
var $loadMoreInfo = $('#load-more-info');
var $loading = $(''),
$noMore = $('');
var searching = false;
ellipsis.init();
if ($loadMoreInfo.length > 0) {
$loading = $loadMoreInfo.children('.loading');
$noMore = $loadMoreInfo.children('.no-more');
}
/**
* 设置指定资讯项的Lazyload和文字截取
* @params $infos 资讯项
*/
function setLazyLoadAndMellipsis($infos) {
lazyLoad($infos.find('img.lazy'));
$infos.each(function() {
var $this = $(this),
$title = $this.find('.info-title'),
$text = $this.find('.info-text');
$title[0].mlellipsis(2);
$text[0].mlellipsis(2);
});
}
/**
* 初始化资讯列表事件绑定
* @params $container 逛资讯列表容器
*/
function initInfosEvt($container) {
var cHammer;
if (typeof $container === 'undefined') {
return;
}
cHammer = new Hammer($container[0]);
//点赞或者收藏事件
cHammer.on('tap', function(e) {
var $this = $(e.target),
opt = 'ok',
$btn,
$info;
//e.preventDefault();
//点赞
$btn = $this.closest('.like-btn');
if ($btn.length > 0) {
e.preventDefault();
if ($btn.hasClass('like')) {
opt = 'cancel';
}
$info = $this.closest('.guang-info');
$.ajax({
type: 'POST',
url: '/guang/opt/praiseArticle',
data: {
id: $info.data('id'),
opt: opt
},
success: function(data) {
var code = data.code;
if (code === 200) {
$btn.next('.like-count').text(data.data);
//切换点赞状态
$btn.toggleClass('like');
}
},
error: function() {
tip.show('网络断开连接了~');
}
});
return;
}
//APP收藏
$btn = $this.closest('.collect-btn');
if ($btn.length > 0) {
e.preventDefault();
if ($btn.hasClass('collected')) {
opt = 'cancel';
}
$info = $this.closest('.guang-info');
$.ajax({
type: 'POST',
url: '/guang/opt/collectArticle',
data: {
id: $info.data('id'),
opt: opt
},
success: function(data) {
if (data.code && data.code === 200) {
//切换收藏状态
$btn.toggleClass('collected');
}
},
error: function() {
tip.show('网络断开连接了~');
}
});
}
});
setLazyLoadAndMellipsis($container.find('.guang-info'));
}
/**
* 资讯LoadMore
* @param $container 资讯容器 jqyeryObject
* @param opt 请求参数
* @param url[可选], 扩展请求的url而不使用默认值
*/
function loadMore($container, opt, url) {
var num;
if (searching) {
return;
}
if (opt.end) {
return;
}
if (opt.page === 1) {
//显示loading
loading.showLoadingMask();
}
num = $container.find('.guang-info').length;
searching = true;
$.ajax({
type: 'GET',
url: url ? url : '/guang/index/page',//对于指定url的使用指定url(存在不同的控制器)
data: opt,
success: function(data) {
var $newItems;
if (data === ' ') {
opt.end = true;
searching = false;
//
$loading.addClass('hide');
$noMore.removeClass('hide');
return;
}
$container.append(data);
if (num > 0) {
$newItems = $container.find('.guang-info:gt(' + (num - 1) + ')');
} else {
$newItems = $container.find('.guang-info');
}
setLazyLoadAndMellipsis($newItems);
if (opt.page === 1) {
loading.hideLoadingMask();
$loading.removeClass('hide');//显示空屏加载时hide的隐藏
window.rePosFooter();//插入内容后重新计算底部位置
}
opt.page++;
searching = false;
},
error: function() {
tip.show('网络断开连接了~');
searching = false;
}
});
}
exports.initInfosEvt = initInfosEvt;
exports.setLazyLoadAndMellipsis = setLazyLoadAndMellipsis;
exports.loadMore = loadMore;