UrlList_wev8.js
11.1 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
define(['mUtil', "Component", "wev-loading"], function (mUtil, Component, WevLoading) {
var UrlList = function (options) {
var _list = {}, $comp, $list;
Component.super(this, options);
this.type = "UrlList";
this.tpl = this.type + "_html";
this.keysOfSkipedVarParse = ['dataurl', 'imgfield', 'titlefield', 'otherfields'];
this.dataload = true;
this.components = {
loading: new WevLoading({
delay: 300,
animation: 1
}),
moreLoading: new WevLoading({
btn: 1,
onclick: function (hideLoading) {
_list.loadData(hideLoading);
}
})
};
var vm = this.viewModel = {
pageSize: 10,
normalview: {
imgfield: "",
titlefield: "",
otherfields: []
},
normalSearch: {
hide: false,
tip: ""
},
btns: [],
swipe: {},
options: {
readonly: false,
selectable: false, //数据可选
showOnePage: false, //显示一页
inParams: [{
paramName: "pageNo",
paramValue: "{PAGE_NO}",
isSystem: "1",
desc: "" //当前页
}, {
paramName: "pageSize",
paramValue: "{PAGE_SIZE}",
isSystem: "1",
desc: "" //每页显示数量
}, {
paramName: "searchKey",
paramValue: "{SEARCH_KEY}",
isSystem: "1",
desc: "" //查询参数
}],
outputFormat: {
DATAS: "datas",
TOTAL_SIZE: "totalSize"
}
},
onload: function () { }
};
this.mounted = function () {
var that = this;
$comp = this.$comp;
$list = $comp.find(".wev-table-view");
// 初始化列表数据
_list.refreshList(function () {
mUtil.eval(vm.onload, that.pageid);
mUtil.trigger('dataload', that.pageid, that.id);
});
// 初始化搜索功能
_list.initSearch();
//初始化自定义按钮,切换显示按钮
_list.initBtns(this.pageid);
//绑定单条数据,左滑按钮点击事件
$comp.on("click.nav", "a.wev-navigate-right, .wev-table-checkbox, .wev-file", function (e) {
var $this = $(this);
var item = getDataItem($this);
var isImg = e.target.tagName.toLowerCase() === "img";
var isFile = $this.hasClass("wev-file");
//选择数据checkbox
if($this.hasClass("wev-table-checkbox") || (vm.options.readonly && !isImg && !isFile)){
var $li = $this.closest(".wev-table-view-cell");
$li.toggleClass("checked");
var checkList = _list.state.checkList;
if($li.hasClass("checked")){
checkList.indexOf(item) < 0 && checkList.push(item);
}else{
var index = checkList.indexOf(item);
if (index > -1) {
checkList.splice(index, 1);
}
}
//选择数据功能 阻止冒泡和默认行为
e.stopPropagation();
e.preventDefault();
return;
}
if (vm.options.readonly) return;
if (vm.callback && mUtil.isFunction(vm.callback.click)) {
vm.callback.click.call(this, getDataItem($(this)));
}
if(item.dataurl){
var dataurl = item.dataurl;
if(item.dataurl.indexOf("javascript:") == -1){
$u(dataurl);
}else{
mUtil.eval(dataurl, that.pageid);
}
}
}).on("click.swipeAction", ".btnBox", function () {
if (vm.swipe && mUtil.isFunction(vm.swipe.click)) {
vm.swipe.click.call(this, getDataItem($(this)));
}
});
};
function getDataItem($el) {
var index = $el.closest(".wev-table-view-cell").index();
return _list.state.list[index];
}
this.reload = function (args, callback) {
_list.state.dynamicParam = {};
if (mUtil.isObject(args)) {
$.extend(_list.state.dynamicParam, args);
}
_list.refreshList(callback);
};
this.getCheckedData = function(callback){
mUtil.isFunction(callback) && callback(_list.state.checkList);
};
this.toggleData = function(flag){
if(!vm.options.selectable) return;
var checkList = [];
$list.find(".wev-table-view-cell").toggleClass("checked", flag);
if(flag){
_list.state.list.forEach(function(item){
checkList.indexOf(item) < 0 && checkList.push(item);
});
}
_list.state.checkList = checkList;
};
_list.state = {
timestamp: 0,
pageNo: 0,
list: [],
checkList: [],
searchKey: "",
dynamicParam: {}
};
_list.initBtns = function (pageid) {
if (!vm.btns.length) return;
$(".wev-list-btn-container", $comp).on("click", ".btn", function () {
var $btn = $(this);
var index = $(this).data("index");
mUtil.eval(vm.btns[index].click, pageid);
});
};
_list.initSearch = function () {
var $search = $(".wev-search", $comp),
$searchInput = $("input", $search);
$search.on("click.active", ".wev-placeholder", function (e) {
$search.addClass("wev-active").removeClass("wev-inactive");
$searchInput.focus();
}).on("click.clear", ".wev-clear-btn", function () {
$searchInput.val("").focus().triggerHandler("input");
_list.state.searchKey = "";
});
// 搜索框事件
$searchInput.on("blur", function () {
if (this.value) return;
$search.removeClass("wev-active").addClass("wev-inactive");
}).on("input", function () {
$search.toggleClass("wev-has-value", !!this.value);
}).on("keyup", function (e) {
if (e.keyCode !== 13) return;
_list.state.searchKey = this.value;
_list.refreshList();
this.blur();
});
};
_list.refreshList = function (cb) {
var that = this;
var coms = this.components;
var loading = coms.loading,
moreLoading = coms.moreLoading;
var state = _list.state;
state.pageNo = 0;
state.list = [];
loading.setRefs(this.$comp, "wev-refreshing");
loading.show();
_list.loadData(function (hasMore, noData) {
loading.hide();
moreLoading.hide(hasMore, noData);
cb && cb();
// 支持上拉刷新功能
if(mUtil.canPullToRefresh(that.id) && hasMore) {
require(['pullToRefreshHelper'], function (PullToRefresh) {
var $container = $comp.closest('.page-content');
// 日历+列表的情况
if('auto' === $comp.css('overflow-y')) {
$container = $comp;
}
new PullToRefresh({
el: $list,
container: $container.get(0),
loadData: _list.loadData,
loading: that.components.moreLoading
});
});
}
});
};
_list.loadData = function (callback) {
var that = this;
var state = _list.state;
var timestamp = new Date().valueOf();
state.timestamp = timestamp;
state.pageNo++;
var pageNo = state.pageNo;
var pageSize = vm.pageSize;
var requestParam = {};
vm.options.inParams.forEach(function (p) {
requestParam[p.paramName] = mUtil.fmtParamValue(p.paramValue, {
pageNo: pageNo,
pageSize: pageSize,
searchKey: _list.state.searchKey
});
});
//拓展requestParam
$.extend(true, requestParam, mUtil.getPageParam(this.pageid), state.dynamicParam);
mUtil.action(vm.url, requestParam, function (responseText) {
if (timestamp != state.timestamp) return;
var result = responseText;
try {
if (typeof responseText == 'string'){
result = JSON.parse(responseText);
}
} catch (e) {
mUtil.getLabel(5294, "列表数据转换成JSON时出现异常,数据如下:",function(msg){
console.error(msg + "\n" + responseText);
});
if (typeof (callback) == "function") { callback.call(this, false); }
return;
}
var fmt = vm.options.outputFormat;
var jsonDatas = result[fmt.DATAS];
var totalSize = parseInt(result[fmt.TOTAL_SIZE]);
var totalPageCount = totalSize % pageSize === 0 ? totalSize / pageSize : parseInt(totalSize / pageSize) + 1;
var rd = {};
mUtil.concat(state.list, jsonDatas);
rd.datas = _list.getShowDatas(jsonDatas, vm.normalview);
var templateHtml = [
'{@each datas as d}',
'<li class="wev-table-view-cell wev-media">',
'<a class="wev-navigate-right {@if d.readonly}wev-data-readonly{@/if}" href="javascript:void(0);">',
'<div class="wev-table-checkbox">'+(vm.options.selectable ? '<i class="wev-css-icon wev-multi-check"></i>' : "")+'</div>',
'<div class="wev-media-object wev-pull-left">',
'$${d.imgFieldValue}',
'</div>',
'<div class="wev-media-body">',
'$${d.titleFieldValue}',
'{@each d.otherFieldValue as row}',
'<div class="wev-ellipsis">',
'$${row}',
'</div>',
'{@/each}',
'</div>',
'</a>',
'{@if d.swipeContent}',
'<div class="wev-slider-handle">$${d.swipeContent}</div>',
'{@/if}',
'</li>',
'{@/each}'
].join('');
var html = mUtil.parseTemplate(templateHtml, { datas: rd.datas});
var $newList = $(html);
if (pageNo == 1) {
$list.html("");
}
$list.append($newList);
initLazyImg();
var hasMore = (pageNo < totalPageCount) && !vm.options.showOnePage;
var noData = totalSize <= 0;
callback(hasMore, noData);
mUtil.renderVarParser(vm.needParseVar);
initSwipe($newList, that.pageid);
});
};
_list.getShowDatas = function (jsonDatas, viewset) {
return jsonDatas.map(function (d) {
var dataurl = "javascript:void(0);";
if (!vm.options.readonly && vm.options.dataurl) {
dataurl = mUtil.replaceValAndVarParser(vm.options.dataurl, d);
}
d.dataurl = dataurl;
return {
imgFieldValue: mUtil.replaceValAndVarParser(viewset.imgfield, d), // 图片
titleFieldValue: mUtil.replaceValAndVarParser(viewset.titlefield, d), // 标题
swipeContent: getSwipeContent(vm.swipe.content || "", d, vm.swipe.params),
readonly: vm.options.readonly,
otherFieldValue: viewset.otherfields.map(function (field) {
return mUtil.replaceValAndVarParser(field, d);
}).filter(function (v) { return v; })
};
});
};
function initLazyImg() {
require(["lazyImgHelper"]);
}
function initSwipe($ele, pageid) {
if (!vm.swipe.content) return;
require(["swipeHelper", "css!listSwipe_css"], function (h) {
$ele.each(function () {
h.swipe($(this).children("a"), pageid);
});
});
}
function getSwipeContent(swipeContentTmp, jsonData, swipeParams) {
if (!swipeContentTmp) return "";
var swipeContent = mUtil.replaceValAndVarParser(swipeContentTmp, jsonData);
swipeParams = swipeParams || [];
swipeParams.forEach(function (p) {
if (p.paramFunction !== "普通按钮") return;
var field = p.paramField;
var decode = mUtil.replaceValAndVarParser(field, jsonData); // 加密前的
var encode = encodeURIComponent(decode); // 加密后的
var start = swipeContent.indexOf(decode); // 起始位置
if (!~start) return;
var end = swipeContent.indexOf(decode) + decode.length;
var prefix = swipeContent.substring(0, start); // 前面部分
var suffix = swipeContent.substring(end); // 后面部分
swipeContent = prefix + encode + suffix;
});
return swipeContent;
}
_list.refreshList = _list.refreshList.bind(this);
_list.loadData = _list.loadData.bind(this);
};
return Component.init(UrlList);
});