fieldPicker.js
15.8 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('fieldPicker', ['components/common', 'scroll', 'i18n/lang', 'utils', 'htmlEditor', 'jquery'], function (common, Scroll, lang, utils, htmlEditor) {
var template = '\
<div class="field-picker" id={{id}}>\
<ul class="nav nav-tabs">\
<li class="active" data-tab="fieldSelect">'+ SystemEnv.getHtmlNoteName(4139) /* 选择字段 */ +'</li>\
<li data-tab="dtFieldSelect">'+ SystemEnv.getHtmlNoteName(4865) /* 明细字段 */ +'</li>\
<li data-tab="fieldInput">'+ SystemEnv.getHtmlNoteName(4753) /* 输入Html */ +'</li>\
</ul>\
<div class="tab-panel active" id="fieldSelect">\
{{search}}\
<div class="normal">\
<label class="field-label field-none" fieldid="0">' + SystemEnv.getHtmlNoteName(4168) /* 无 */ + '</label>\
</div>\
</div>\
<div class="tab-panel" id="dtFieldSelect">\
{{search}}\
<div class="detail-normal">\
<label class="field-label field-none" fieldid="0">' + SystemEnv.getHtmlNoteName(4168) /* 无 */ + '</label>\
</div>\
</div>\
<div class="custom tab-panel" id="fieldInput">\
<div class="field-content"><div class="htmlEditor"></div><div class="btn btn-confirm btn-block">' + SystemEnv.getHtmlNoteName(3451) /* 确定 */ + '</div></div>\
</div>\
<div class="arrow"></div>\
</div>';
var cssText = '\
.field-picker { margin: 8px 0 0 0px; border: 1px solid rgba(0,0,0,0.2); -webkit-background-clip: padding-box; background-clip: padding-box; background-color: #fff; border-radius: 6px; box-sizing: border-box; color: #333; line-height: 20px; padding: 10px; position: fixed; text-align: left; top: -999px; left: -999px; white-space: normal; width: 440px; z-index: 1010; display: none; }\
.field-picker .arrow{ position: absolute; width: 22px; height:11px; top:0px; left: 0; right: 0; margin: 0 auto;}\
.field-picker.top .arrow{ bottom: 0; top: auto;}\
.field-picker.top { margin: -10px 0 0 0;} \
.field-picker.top .arrow:after { top: 100%; border-bottom: 0px solid rgba(0,0,0,0); border-top: 10px solid #fff;}\
.field-picker.top .arrow:before { top: 100%; border-bottom: 0px solid rgba(0,0,0,0); border-top: 11px solid rgba(0,0,0,0.247059);}\
.field-picker .arrow:before,.field-picker .arrow:after { content: " "; display: block; position: absolute; width: 22px; height: 11px; white-space: normal; color: #333; line-height: 20px; text-align: left; font-size: 13px; box-sizing: border-box; }\
.field-picker .arrow:before { border-bottom: 11px solid rgba(0,0,0,0.247059); border-left: 11px solid rgba(0,0,0,0); border-right: 11px solid rgba(0,0,0,0); border-top: 0 solid rgba(0,0,0,0); top: -11px; z-index: 1011; }\
.field-picker .arrow:after { border-bottom: 10px solid #fff; border-left: 10px solid rgba(0,0,0,0); border-right: 10px solid rgba(0,0,0,0); border-top: 0 solid rgba(0,0,0,0); top: -10px; z-index: 1012; }\
.field-picker.left:before, .field-picker.left:after { left: 25px; margin: 0; }\
.field-picker .nav-tabs { border-bottom: 1px solid #dfdfdf; text-align: center;}\
.field-picker .field-search + .normal .field-none { margin-top: 0;}\
.field-picker .nav>li { transition: none;}\
.field-picker .normal,.field-picker .detail-normal {position: relative;margin-right: -10px; max-height: 160px; overflow-y: auto;}\
.field-picker .normal:after,.field-picker .detail-normal:after { content: " "; display: table; clear: both; }\
.field-picker .custom { margin: 5px 0 2px 0; padding: 6px 0 0 0; }\
.field-picker .field-search { padding: 6px 0; }\
.field-picker .nav>li:nth-child(1) { padding-left: 0; }\
.field-picker .nav-tabs li { padding: 3px 0; margin-right: 5px; display: inline-block; cursor: pointer; } \
.field-picker .nav-tabs li.active { border-bottom: 3px solid #0066b1; color: #0066b1;}\
.field-picker .tab-panel { margin: 0;}\
.field-picker .nav-tabs .active:after, .field-picker .tab-panel.active:after,.field-picker .tab-panel.active:before { content: ""; display:none; }\
.field-picker .field-label { margin: 0; background: url(/mobilemode/images/mec/flag_wev8.png) no-repeat; background-position: 0 1px; color: #333; cursor: pointer; display: block; font-family: "Microsoft Yahei","Helvetica Neue",Helvetica,Arial,sans-serif; font-size: 14px; height: 22px; line-height: 21px; overflow: hidden; padding: 1px 2px 1px 23px; text-align: left; text-overflow: ellipsis; white-space: nowrap; width: 140px; float: left;}\
.field-picker .field-label.checked { background: url(/mobilemode/images/mec/flag2_wev8.png) no-repeat; background-position: -1px 1px;}\
.field-picker .custom .field-label { width: 100%; cursor: text; font-size: 12px; color: #666; line-height: 23px; height: auto; }\
.field-picker .custom .htmlEditor { border: 1px solid #ccc; height: 200px; width: 100%; overflow: auto; margin-bottom: 5px; }\
.field-picker .field-none { float: none; margin-top: 5px; width: 100%; }\
';
var settings = {
sourceId: '',
callback: {
afterSelect: function (field, target) { } // 字段对象, 源目标
}
}
var FP = function (options) {
var t = this;
t.id = options.id = 'fp_' + options.id;
t.settings = $.extend({}, settings, options);
t.el = null;
t.target = null; // 源目标
t.fields = null; // 内容来源字段的html片段
t.dtfields = null; // 内容来源明细字段的html片段
t.render();
return t;
};
FP.pt = FP.prototype;
FP.pt.vessel = function () { // 创建容器
var config = this.settings;
config.search = config.sourceId && '<div class="field-search"> <input type="text" placeholder="'+ SystemEnv.getHtmlNoteName(4748) /* 请输入检索项 */ +'" class="form-control"/> </div>' || '';
return common.tempEngine(template, config);
};
FP.pt.create = function () { // 创建元素
var fp = this,
tmp = fp.vessel();
fp.el = $(tmp);
$('body').append(fp.el);
new Scroll(fp.el.find(".normal,.detail-normal"), true);
return fp;
};
FP.pt.getFields = function (sourceId) {
var fp = this,
deferred = $.Deferred();
if (fp.fields) return deferred.resolve();
if (!sourceId) return deferred.reject();
var fieldTemplate = '<label class="field-label" title="{{fieldName}}" fieldDescPy="{{fieldDescPy}}" fieldid="{{fieldid}}">{{fieldDesc}}</label>'; // 后台中fielddesc对应为fieldDesc
if(String(sourceId).indexOf("api_") == 0){//接口
utils.api('api', {
action: "info",
type: "get",
data: {
"id": sourceId.substring(4)
}
}).then(function (result) {
var mainFields = [], resultDefine = result.data.resultDefine || [], resultKey = fp.settings.resultKey;
!resultKey && resultDefine.every(function(ele, i) {
if(ele.type === "ARRAY"){
resultKey = ele.name;
return false;
}
return true;
});
resultKey && resultDefine.forEach(function(ele, i) {
var pname = ele.name;
if(pname !== resultKey && pname.substring(resultKey.length).indexOf(".") === 0){
mainFields.push({
fieldid: ele.id,
fieldName: ele.name,
fieldDesc: ele.remark,
fieldDescPy: ele.remarkPy
});
}
});
fp.fields = common.tempEngine(fieldTemplate, mainFields);
deferred.resolve(result.data);
});
}else if(String(sourceId).indexOf("sql_") == 0){
sourceId = sourceId.substring(4);
var sourceArr = sourceId.split("_datasource_");
utils.api("designer/plugin", {
action: "getSourceFields",
notification: false,
type: 'POST',
data: {
source: "3",
datasource: compressByLZ(sourceArr[1]),
sql: compressByLZ(sourceArr[0])
}
}).then(function(result){
var data = result.data || [];
var fields = common.tempEngine(fieldTemplate, data);
fp.fields = fields;
deferred.resolve(data);
});
}else{
var parseSearchFields = function(data){
var mainFields = [], dtFields = [];
data && data.forEach(function(item){
item.fieldDesc = MLanguage.parse(item.fieldDesc);
item.detailtable ? dtFields.push(item) : mainFields.push(item);
});
var fields = common.tempEngine(fieldTemplate, mainFields);
var dtfields = common.tempEngine(fieldTemplate, dtFields);
fp.fields = fields;
fp.dtfields = dtfields;
deferred.resolve(data);
};
if(String(sourceId).indexOf("search_") == 0){//表单建模查询列表id
utils.api("designer/plugin", {
action: "getSourceFields",
type: 'POST',
data: {
source: "1",
searchid: sourceId.substring(7)
},
}).then(function(result){
parseSearchFields(result.data);
});
}else{//appFormUI id
common.ajax({
url: common.jionActionUrl("com.weaver.formmodel.mobile.ui.servlet.MobileAppUIAction",
"action=getUIField&id=" + sourceId)
}, function (data) {
parseSearchFields(data);
});
}
}
return deferred;
};
FP.pt.renderFields = function (callback) { // 渲染字段列表
var fp = this,
fpEl = fp.el,
sourceId = fp.settings.sourceId;
$.when(fp.getFields(sourceId))
.then(function (data) {
fpEl.find('.normal').append($(fp.fields));
fpEl.find('.detail-normal').append($(fp.dtfields));
fpEl.find('.checked').removeClass('checked');
$.isFunction(callback) && callback(data);
});
return fp;
};
FP.pt.mounted = function(){
var fp = this,
fpEl = fp.el;
if(!fp.dtfields){
fpEl.find(".nav-tabs li[data-tab='dtFieldSelect']").remove();
fpEl.find("#dtFieldSelect").remove();
}
return fp;
}
FP.pt.render = function () {
return this.destory().create().events().customEvents();
};
FP.pt.events = function () {
var fp = this,
el = fp.el,
config = fp.settings;
var setField = function (field) {
$(fp.target).data('field', field);
config.callback.afterSelect.call(null, field, fp.target);
fp.destory();
};
el.find('.btn').one('click', function (e) { // 设置自定义字段值
setField({
'fieldid': '-1',
'fielddesc': $.trim(fp.editor ? fp.editor.getValue() : "")
});
});
el.find('.nav-tabs').on('click', 'li', function (e) {
var $target = $(e.target), tab, $pane;
if ($target.hasClass('active')) return;
tab = $target.data('tab'),
$pane = $(common.toId(tab));
el.find('.active').removeClass('active');
$target.addClass('active');
$pane.addClass('active');
setTimeout(function() {
$pane.find('input').focus();
});
fp.editor && fp.editor.focus();
fp.position();
});
el.one('mousedown.checked', '.normal .field-label,.detail-normal .field-label', function () { // 设置字段值
var $checked = $(this); // 被选中字段
setField({
fieldid: $checked.attr("fieldid"),
fielddesc: $.trim($checked.html())
});
}).on('mousedown', function (e) { // 阻止点击fieldPicker时关闭
return false;
});
$('body').off('mousedown.fp')
.one('mousedown.fp', function (e) { // 自动关闭
fp.destory();
e.stopPropagation();
});
if (!config.sourceId) return fp;
el.find('input').on('input', function (e) { // 搜索功能 默认有内容来源时才显示
var $parent = $(this).closest(".tab-panel");
$parent.find('.field-label').each(function () {
var $this = $(this),
searchTxt = common.toLowerCase(e.target.value),
pattern = new RegExp('.*' + searchTxt.split('').join('.*') + '.*'),
text = common.toLowerCase($this.html()),
py = common.toLowerCase($this.attr("fieldDescPy"));
if (text.indexOf(searchTxt) == -1 && py.match(pattern) == null) {
return $(this).hide();
}
$(this).show();
});
});
setTimeout(function() {
el.find("input").focus();
});
return fp;
};
FP.pt.customEvents = function () {
var fp = this;
fp.el.on('field:change', function () {
var fpEl = $(this),
field = $(fp.target).data('field');
fp.editor = htmlEditor.editor(fpEl.find(".htmlEditor")[0], 'html', ['Mobile_NS']);
if (!field) return;
var $tabpanel = fpEl.find('[fieldid=' + field.fieldid + ']').addClass('checked').closest('.tab-panel');
fp.el.find('.nav-tabs li').eq($tabpanel.index()-1).trigger('click');
if (field.fieldid == -1) {
fpEl.find('[data-tab=fieldInput]').trigger('click');
fp.editor.setValue(field.fielddesc || "", true);
}
});
return fp;
};
FP.pt.position = function () {
var position = common.position(this.target, this.el, "center");
this.el.toggleClass("top", !!position);
var tOffset = this.target.offset(),
tWidth = this.target.outerWidth(),
elWidth = this.el.outerWidth(),
tOffsetRight = $('body').outerWidth() - tOffset.left - tWidth/2;
if(tOffsetRight < elWidth/2 + 10){//计算角标位置
this.el.find(".arrow").css("right", (tOffsetRight - elWidth/2 - 10)*2);
}
return this.el;
};
FP.pt.destory = function () {
this.el && this.el.remove();
return this;
}
common.importCssText('fieldPicker', cssText);
var fps = {
setFP: function (fp) {
this[fp.id] = fp;
},
getFP: function (id) {
return this['fp_' + id];
}
};
return {
run: function (settings) {
var fp = new FP(settings);
fps.setFP(fp);
},
show: function (el, id) {
var fp = fps.getFP(id);
fp.target = $(el);
$('.field-picker').remove(); // 清除所有fieldPicker
return fp.render()
.renderFields().mounted().position().trigger('field:change').show();
},
change: function (sourceId, callback, id, resultKey) {
var fp = fps.getFP(id);
if(utils.isArray(sourceId)){
}else{
fp.settings.sourceId = sourceId;
fp.settings.resultKey = resultKey;
}
fp.fields = null;
fp.dtfields = null;
return fp.renderFields(callback).mounted();
},
resetField: function (el) {
$(el).data('field', null);
},
setField: function (el, field, id) {
$(el).data('field', field);
if(id) {
var fp = fps.getFP(id);
fp.settings.callback.afterSelect(field, el);
}
}
}
});