List_wev8.js
15.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
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
define(['mUtil', "Component", "fieldParseHelper", "pageExpandHelper", "mApi/popup", "wev-loading"], function (mUtil, Component, fieldHelper, pageExpandHelper) {
var WevLoading = require("wev-loading");
var List = function (options) {
var _list = {}, $comp, $list;
Component.super(this, options);
this.type = "List";
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: "请输入", //搜索提示语
fields: [] //指定查询列
},
advancedSearch: {},
btns: [],
options: {
dataurl: "",
readonly: false, //是否只读
selectable: false, //数据可选
showOnePage: false, //显示一页
},
onload: function () { },
standalone: true
};
this.beforeMount = function(){
//过滤历史数据中静态页中的扩展按钮,改为动态获取
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;
vm.isMock = (vm.contentSource && vm.contentSource == 2);
$comp = this.$comp;
$list = $comp.find(".wev-table-view");
// 初始化列表数据
_list.refreshList(function () {
mUtil.eval(vm.onload, that.pageid);
});
// 初始化搜索功能
_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.dataMap) < 0 && checkList.push(item.dataMap);
}else{
var index = checkList.indexOf(item.dataMap);
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.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);
}
}
}).on("click.swipeAction", ".btnBox", function () {
if (vm.swipe && mUtil.isFunction(vm.swipe.click)) {
vm.swipe.click.call(this, getDataItem($(this)));
}
});
// 解析布局url,当表单建模只存在默认布局时生效
!vm.isMock && !vm.options.readonly && 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);
};
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.dataMap) < 0 && checkList.push(item.dataMap);
});
}
_list.state.checkList = checkList;
};
_list.state = {
timestamp: 0,
pageNo: 0,
list: [],
conditions: {},
dynamicParam: {},
checkList: []
};
_list.initBtns = function (pageid) {
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 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.refreshList();
this.blur();
});
if (vm.isMock || !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 = [];
state.checkList = [];
_list.loadData(function (hasMore, noData) {
loading.hide();
coms.moreLoading.hide(hasMore, noData);
cb && cb();
mUtil.trigger('dataload', that.pageid, that.id);
// 支持上拉刷新功能
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 state = _list.state, actionUrl;
var timestamp = new Date().valueOf(); //时间戳
state.timestamp = timestamp;
state.pageNo++;
var pageSize = vm.pageSize;
var pageNo = state.pageNo;
if(vm.isMock){
var mockData = vm.mockData;
var result;
if(mUtil.isArray(mockData)){
var searchKey = decodeURIComponent(_list.state.conditions.searchKey||"");
var titlefield = vm.normalview.titlefield.fielddesc || "";
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;
}
result = fieldHelper.convertFieldMockData(mockData, pageNo, pageSize);
}
if(result && result != ""){
_list.renderData(result, callback);
}else{
callback(false, true);
}
}else{
var parseFields = fieldHelper.parseFieldIds(JSON.stringify(vm));
var reqparam = {action: "getDatas", searchid: vm.sourceid, pageNo: pageNo, pageSize: pageSize, fieldparse: parseFields, searchFields: encodeURIComponent(JSON.stringify(vm.normalSearch.fields || []))};
//拓展请求参数
$.extend(true, reqparam, _list.state.dynamicParam);
actionUrl = mUtil.getActionUrl(this.type, reqparam, this.pageid);
mUtil.getJSON(actionUrl, state.conditions, function (result) {
result = result.data;
if (timestamp != state.timestamp) return;
_list.renderData(result, callback);
});
}
};
_list.renderData = function(result, callback){
var that = this;
var state = _list.state;
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);
var normalDataItem = convertShowValue(data, fieldmap, vm.normalview);
normalDatas.push(normalDataItem);
});
mUtil.concat(state.list, listDatas);
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">'+(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">',
'{@each row as col}',
'<div class="wev-col">$${col}</div>',
'{@/each}',
'</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: normalDatas});
var $newList = $(html);
if (state.pageNo == 1) {
$list.html("");
}
$list.append($newList);
initLazyImg();
var hasMore = (state.pageNo < result.totalPageCount) && !vm.options.showOnePage;
var noData = result.totalSize <= 0;
callback(hasMore, noData);
mUtil.renderVarParser(vm.needParseVar);
initSwipe($newList, that.pageid);
};
function convertShowValue(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,
swipeContent : getSwipeContent(vm.swipe.content, billdata.dataMap, vm.swipe.params)
};
}
function getSwipeContent(swipeContentTmp, jsonData, swipeParams) {
if (!swipeContentTmp) return "";
var swipeContent = fieldHelper.replaceVal(swipeContentTmp, jsonData);
swipeParams = swipeParams || [];
swipeParams.forEach(function (p) {
if (p.paramFunction !== "普通按钮") return;
var field = p.paramField;
var decode = fieldHelper.replaceVal(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;
}
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 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.loadData = _list.loadData.bind(this);
_list.renderData = _list.renderData.bind(this);
_list.initBtns = _list.initBtns.bind(this);
};
return Component.init(List);
});