LargeList_wev8.js
17.2 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
define(['mUtil', "Component", "fieldParseHelper", "pageExpandHelper", "mApi/popup", "wev-loading"], function (mUtil, Component, fieldHelper, pageExpandHelper) {
var WevLoading = require("wev-loading");
var LargeList = function (options) {
var _list = {}, $comp, $list;
Component.super(this, options);
this.type = "LargeList";
this.tpl = this.type + "_html";
this.css = this.type + "_css";
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: "请输入", //搜索提示语
fields: [] //指定查询列
},
advancedSearch: {},
btns: [],
options: {
dataurl: "",
readonly: 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: "" //查询参数
}],
outFormat: {
DATAS: "datas",
TOTAL_SIZE: "totalSize"
}
},
onload: function () { },
standalone: true
};
this.beforeMount = function(){
if(vm.sourceid > 0){
vm.contentSource = 2;
}
if(vm.mockData){
vm.contentSource = 3;
}
//过滤历史数据中静态页中的扩展按钮,改为动态获取
if (vm.btns) {
vm.btns = vm.btns.filter(function (ele) {
//历史数据无vm.pageExtend参数
if (ele.id.startsWith("pageexpandBtn")) vm.pageExtend = true;
return !ele.id.startsWith("pageexpandBtn");
});
}
};
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();
//绑定单条数据,左滑按钮点击事件
$comp.on("click.nav", ".wev-navigate-right, .wev-table-checkbox, .wev-file", function (e) {
if($(e.target).closest('.wev-file[data-docid]').length) return;
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.id) < 0 && checkList.push(item.id);
}else{
var index = checkList.indexOf(item.id);
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, item);
}
if (vm.contentSource == 2 && vm.options.urltype == 0){
if (vm.layoutUrl) return $u(vm.layoutUrl + "&billid=" + item.id);
Mobile_NS.getLayoutUrl(vm.relate.modeid, 1, item.id);
}else if(item.dataurl){
var dataurl = item.dataurl;
if(item.dataurl.indexOf("javascript:") == -1){
$u(dataurl);
}else{
mUtil.eval(dataurl, that.pageid);
}
}
});
// 解析布局url,当表单建模只存在默认布局时生效
!vm.options.readonly && vm.contentSource == 2 && vm.options.urltype === 0 && parseLayoutUrl();
};
function parseLayoutUrl() {
var url = mUtil.getActionUrl("service.FormComponent", { action: "resolveDefaultLayout" });
mUtil.getJSON(url, { modelid: vm.relate.modeid, appid: vm.relate.appid, uitype: 1 }, function (result) {
vm.layoutUrl = result.data || "";
require(["mApi/other"]);//TODO 预加载
}, function (errMsg) { });
}
this.reload = function (args, callback) {
_list.state.dynamicParam = {};
if (mUtil.isObject(args)) {
$.extend(_list.state.dynamicParam, args);
}
_list.refreshList(callback);
};
_list.state = {
timestamp: 0,
pageNo: 0,
list: [],
conditions: {},
dynamicParam: {},
checkList: []
};
_list.initBtns = function () {
var that = this,
pageid = this.pageid;
if (vm.pageExtend) {
var cfg = {
pageid: pageid,
searchid: vm.sourceid
};
pageExpandHelper.parsePageExpand($comp.find(".wev-search"), that.id, cfg);
}
if (!vm.btns.length) return;
vm.btns.forEach(function (item) {
if(item.script && !item.click){
item.click = mUtil.strToFunc(item.script);
}
});
$(".wev-list-btn-container", $comp).on("click", ".btn.wev-btn-custom", function () {
var $btn = $(this);
var index = $(this).data("index");
mUtil.eval(vm.btns[index].click, pageid);
});
};
_list.initSearch = function () {
var $search = $(".wev-search", this.$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.conditions = {};
});
// 搜索框事件
$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.conditions.searchKey = encodeURIComponent(this.value);
_list.state.conditions.searchFields = JSON.stringify(vm.normalSearch.fields || []);
_list.refreshList();
this.blur();
});
if (!vm.advancedSearch.enable) return;
require(["css!listAdvancedSearch_css"]);
// 高级搜索
var timestamp = new Date().valueOf(), pageid = this.pageid.split("_").slice(1).join("."), id = this.id;
$search.on("click.advancedSearch", ".wev-search-advanced", function () {
require(["mService"], function (mService) {
mService.show("search", {
_identity: id + "_" + timestamp,
pageid: pageid,
id: id,
searchid: vm.sourceid,
conditions: JSON.stringify(vm.advancedSearch.fields),
title: vm.advancedSearch.title
});
});
});
};
_list.refreshList = function (cb) {
var that = this;
var coms = this.components;
var loading = coms.loading;
var state = _list.state;
loading.setRefs(this.$comp, "wev-refreshing");
loading.show();
state.pageNo = 0;
state.list = [];
_list.loadData(function (hasMore, noData) {
loading.hide();
coms.moreLoading.hide(hasMore, noData);
cb && cb();
// 支持上拉刷新功能
if(mUtil.canPullToRefresh(that.id) && hasMore) {
require(['pullToRefreshHelper'], function (PullToRefresh) {
var $pageContent = $comp.closest('.page-content');
new PullToRefresh({
el: $list,
container: $pageContent.get(0),
loadData: _list.loadData,
loading: that.components.moreLoading
});
});
}
});
};
_list.loadData = function(callback){
if (vm.contentSource == 2) {
_list.loadDataFromFormmode(callback);
}else if(vm.contentSource == 3){
_list.loadDataFromMock(callback);
}else{
_list.loadDataFromURL(callback);
}
};
_list.loadDataFromMock = function(callback){
var mockData = vm.mockData;
if(mUtil.isArray(mockData)){
var that = this;
var state = _list.state, searchKey = decodeURIComponent(_list.state.conditions.searchKey||"");
var titlefield = vm.normalview.titlefield || "";
if(titlefield && searchKey){
var newMockData = [];
var regex = /\{(\w+)\}/g;
var fields = titlefield.match(regex);
if(fields){
fields = fields.map(function(field) {
return field.replace(/\{|\}/g, "");
});
mockData.forEach(function(mockItem){
fields.forEach(function(field) {
if(mockItem[field] && new String(mockItem[field]).toLowerCase().indexOf(searchKey.toLowerCase()) != -1){
newMockData.push(mockItem);
}
});
});
}
mockData = newMockData;
}
state.pageNo++;
var pageNo = state.pageNo, pageSize = vm.pageSize, totalSize = mockData.length
var totalPageCount = totalSize%pageSize > 0 ? parseInt(totalSize/pageSize) + 1 : parseInt(totalSize/pageSize);
if(pageNo < totalPageCount){
mockData = mockData.slice((pageNo-1)*pageSize, pageNo*pageSize);
}else if(pageNo == totalPageCount){
mockData = mockData.slice((pageNo-1)*pageSize);
}
mUtil.concat(state.list, mockData);
var showDatas = convertURLShowValue(mockData, vm.normalview);
_list.renderView(showDatas, totalSize, totalPageCount, callback);
}else{
callback(false, true);
}
};
_list.loadDataFromURL = 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.conditions.searchKey
});
});
//拓展requestParam
$.extend(true, requestParam, mUtil.getPageParam(this.pageid), state.dynamicParam);
mUtil.action(vm.sourceurl, 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.outFormat;
var jsonDatas = result[fmt.DATAS];
var totalSize = parseInt(result[fmt.TOTAL_SIZE]);
var totalPageCount = totalSize % pageSize === 0 ? totalSize / pageSize : parseInt(totalSize / pageSize) + 1;
mUtil.concat(state.list, jsonDatas);
var showDatas = convertURLShowValue(jsonDatas, vm.normalview);
_list.renderView(showDatas, totalSize, totalPageCount, callback);
});
};
function convertURLShowValue(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), // 标题
readonly : vm.options.readonly,
otherFieldValue : viewset.otherfields.map(function (field) {
return mUtil.replaceValAndVarParser(field, d);
}).filter(function (v) { return v; })
};
});
}
_list.loadDataFromFormmode = function (callback) {
var state = _list.state,
timestamp = new Date().valueOf(); //时间戳
state.timestamp = timestamp;
state.pageNo++;
var parseFields = fieldHelper.parseFieldIds(JSON.stringify(vm));
var reqparam = {action: "getDatas", searchid: vm.sourceid, pageNo: state.pageNo, pageSize: vm.pageSize, fieldparse: parseFields};
//拓展请求参数
$.extend(true, reqparam, state.dynamicParam);
var actionUrl = mUtil.getActionUrl(this.type, reqparam, this.pageid);
mUtil.getJSON(actionUrl, state.conditions, function (result) {
result = result.data;
if (timestamp != state.timestamp) return;
var fieldmap = result.fieldMap, listDatas = [], normalDatas = [];
var needReplaceUrl = mUtil.containsVariables(vm.options.dataurl);
result.datas.forEach(function(data){
var dataItem = {};
var dataId = data.keyvalue;
var dataurl = "";
if(vm.options.urltype == 1 && vm.options.dataurl){
dataurl = needReplaceUrl ? fieldHelper.replaceVal(vm.options.dataurl, data.dataMap) : vm.options.dataurl;
if(dataurl.indexOf("javascript:") == -1){
if(!vm.options.isNewLayout && dataurl.indexOf("&billid=") == -1 && dataurl.indexOf("?billid=") == -1){
dataurl += (dataurl.indexOf("?") == -1 ? "?" : "&") + "billid=" + dataId;
}
}
}else{
dataurl = "javascript:void(0);";
}
dataItem.id = dataId;
dataItem.dataurl = dataurl;
dataItem.dataMap = data.dataMap;
listDatas.push(dataItem);
normalDatas.push(convertListShowValue(data, fieldmap, vm.normalview));
});
mUtil.concat(state.list, listDatas);
_list.renderView(normalDatas, result.totalSize, result.totalPageCount, callback);
});
};
function convertListShowValue(billdata, fieldmap, fieldset){
var imgfield = fieldset.imgfield, otherfields = fieldset.otherfields;
//图片字段
var imgDisplay = fieldHelper.isSettingValue(imgfield), imgFValue = '';
imgDisplay && (imgFValue = fieldHelper.getFieldValue(imgfield, billdata, fieldmap, true, vm.options.readonly));
//其他字段
var otherFValue = [];
if(mUtil.isArray(otherfields)){
otherfields.forEach(function(rowfields) {
var rowFieldValue = [];
rowfields.forEach(function(colfield) {
if(fieldHelper.isSettingValue(colfield)){
var colFieldValue = fieldHelper.getFieldValue(colfield, billdata, fieldmap, false, vm.options.readonly);
rowFieldValue.push(colFieldValue);
}
});
if(rowFieldValue.length > 0){
otherFValue.push(rowFieldValue);
}
});
}
return {
id : billdata.keyvalue,
readonly : vm.options.readonly,
imgFieldDisplay : imgDisplay,
imgFieldValue : imgFValue,
titleFieldValue : fieldHelper.getFieldValue(fieldset.titlefield, billdata, fieldmap),
otherFieldValue : otherFValue
};
}
_list.renderView = function(datas, totalSize, totalPageCount, callback){
var templateHtml = [
'{@each datas as d}',
'<li class="wev-table-view-cell wev-media">',
'<a href="javascript:void(0);" class="wev-navigate-right{@if d.readonly == 1} wev-data-readonly"{@else}" data-ignoreviewimg{@/if}>',
'<div class="wev-table-checkbox"></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">',
'{@if typeof(row) === "object"}',
'{@each row as col}',
'<div class="wev-col">$${col}</div>',
'{@/each}',
'{@else}',
'$${row}',
'{@/if}',
'</div>',
'{@/each}',
'</div>',
'</a>',
'</li>',
'{@/each}'
].join('');
var html = mUtil.parseTemplate(templateHtml, {datas: datas});
var $newList = $(html);
if (_list.state.pageNo == 1) {
$list.html("");
}
$list.append($newList);
initLazyImg();
var hasMore = (_list.state.pageNo < totalPageCount) && !vm.options.showOnePage;
var noData = totalSize <= 0;
callback(hasMore, noData);
mUtil.renderVarParser(vm.needParseVar);
};
function initLazyImg() {
require(["lazyImgHelper"]);
}
function getDataItem($el) {
var index = $el.closest(".wev-table-view-cell").index();
return _list.state.list[index];
}
_list.initSearch = _list.initSearch.bind(this);
_list.refreshList = _list.refreshList.bind(this);
_list.loadDataFromFormmode = _list.loadDataFromFormmode.bind(this);
_list.loadDataFromURL = _list.loadDataFromURL.bind(this);
_list.loadDataFromMock = _list.loadDataFromMock.bind(this);
_list.initBtns = _list.initBtns.bind(this);
};
return Component.init(LargeList);
});