common.js
6.92 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
/**
* 删除提示框
* text 文本
* url 删除接口
* success 删除成功之后的回调
*/
function removeConfirm(text, url, success) {
layer.confirm(html_encode(text), {
btn : [ i18n('txt-2426'), i18n('txt-3817') ],
icon : 3,
skin : 'layui-layer-cfm-delete'
}, function(){
layer.msg(i18n('txt-2419'), {icon: 16});
$post(url, {'_method': 'delete'},function(rs){
layer.msg(i18n('txt-1903'));
success && success(rs);
});
});
}
function html_encode(str) {
var s = "";
if (str.length == 0) return "";
s = str.replace(/&/g, ">");
s = s.replace(/</g, "<");
s = s.replace(/>/g, ">");
s = s.replace(/ /g, " ");
s = s.replace(/\'/g, "'");
s = s.replace(/\"/g, """);
return s;
}
function successHandle(json, handle){
var status = json.status;
if(status == 407){
alert(i18n('txt-1324'));
location.reload(true);
return;
}
if(!status){
handle && handle(json);
return ;
}
if(status == 'ERROR')
layer.alert(json.msg?json.msg:i18n('txt-2423'), {icon: 2, title: i18n('txt-2424')});
else
handle && handle(json);
}
function ajaxComplete(xhr, ts, succ){
if(ts == 'success'){
successHandle(JSON.parse(xhr.responseText), succ);
}
else if(ts == 'error'){
layer.alert(xhr.responseText, {icon: 2, title: i18n('txt-2424')});
}
}
function $get(url,data, successFun) {
$.ajax({
url: url,
data: data,
complete: function(xhr, ts){
ajaxComplete(xhr, ts, successFun);
}
});
}
function $post(url,data, successFun) {
$.ajax({
url: url,
method: 'POST',
data: data,
complete: function(xhr, ts){
ajaxComplete(xhr, ts, successFun);
}
});
}
//将数据写入表单
function putFormData(json, fId){
json.enable = json.enable==true? 1 : 0;
for(var att in json){
var f = 'name=' +att
,elem = $('input['+f+'],select['+f+'],textarea['+f+']', fId);
if(elem.length > 0){
if(elem.attr('type') == 'checkbox')
elem[0].checked = json[att];
else if(elem.attr('type') == 'radio') {
if(json[att])
$('input['+f+']', fId).get(0).checked = true;
else
$('input['+f+']', fId).get(1).checked = true;
}
else
elem.val(json[att]).change();
}
}
}
/**
* 支持拼音搜索的select2
* @param selector
* @param data
*/
function initPinYinSelect2(selector, data, cb){
$.each(data, function(){
this.fullChars = pinyin.getFullChars(this.text).toUpperCase();
this.camelChars = pinyin.getCamelChars(this.text);
});
$.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
$(selector).select2({
data: data,
matcher: oldMatcher(function(term, text, item){
if(!item.id)
return;
var upTerm = term.toUpperCase();
if(item.fullChars.indexOf(upTerm) != -1
|| item.camelChars.indexOf(upTerm) != -1)
return true;
return text.indexOf(term) != -1;
})
});
cb && cb($(selector));
});
return $(selector);
}
/**
* 将模块List 转换为树结构
* @param arr
* @returns {Array}
*/
function createTreeData(arr){
var treeData = [];
var len = arr.length;
for(var i = 0; i < len; i ++){
var pId = arr[i].pId;
arr[i].text = arr[i].name;
if(!pId){
treeData.push(arr[i]);
}
for(var j = 0; j < len; j ++){
if(pId == arr[j].id){
if(!arr[j].children)
arr[j].children = [];
arr[j].children.push(arr[i]);
break;
}
}
}
return treeData;
}
function createVehSearch($e){
//车辆内部编码下拉搜索框
$e.select2({
placeholder: i18n('txt-2418'),
ajax: {
url: '/realSchedule/sreachVehic',
dataType: 'json',
delay: 150,
data: function(params){
return{nbbm: params.term};
},
processResults: function (data) {
return {
results: data
};
},
cache: true
},
templateResult: function(repo){
if (repo.loading) return repo.text;
var h = '<span>'+repo.text+'</span>';
h += (repo.lineName?' <span class="select2-desc">'+repo.lineName+'</span>':'');
return h;
},
escapeMarkup: function (markup) { return markup; },
minimumInputLength: 1,
templateSelection: function(repo){
return repo.text;
},
language: {
noResults: function(){
return '<span style="color:red;font-size: 12px;">' + i18n('txt-808') + '</span>';
},
inputTooShort : function(e) {
return '<span style="color:gray;font-size: 12px;"><i class="fa fa-search"></i> ' + i18n('txt-551') + '</span>';
},
searching : function() {
return '<span style="color:gray;font-size: 12px;"> ' + i18n('txt-1380') + '</span>';
}
}
});
return $e;
}
var message = {}
$.ajax({
url: '/i18n/message',
async: false,
dataType: 'json',
success: function(data) {
message = data;
}
})
function i18n(expression, args) {
var key = '', start = expression.indexOf('$$$$$${'), end, txt = '', idx;
if (start > -1) {
end = expression.indexOf('}', start);
if (end == -1) {
throw new Error('[' + expression + '] i18n表达式异常')
}
key = expression.substring(start + 7, end);
txt = message[key];
} else {
txt = message[expression];
}
start = txt.indexOf('######{')
while (start > -1) {
end = txt.indexOf('}', start);
if (end == -1) {
throw new Error('[' + txt + '] i18n参数表达式异常')
}
idx = txt.substring(start + 7, end);
txt = txt.replace(txt.substring(start, end + 1), args[parseInt(idx)]);
start = txt.indexOf('######{')
}
return txt;
}
function i18n4txt(html) {
var key = '', start = html.indexOf('$$$$$${'), end;
while (start > -1) {
end = html.indexOf('}', start);
if (end == -1) {
throw new Error('[' + html + '] i18n表达式异常')
}
key = html.substring(start + 7, end);
html = html.replaceAll(html.substring(start, end + 1), i18n(key));
start = html.indexOf('$$$$$${');
}
return html;
}
function i18n4dom() {
$('*[data-i18n]').each(function () {
var prop = $(this).data('i18n'), html = '';
html = prop ? $(this).prop(prop) : $(this).html();
if (!html) {
return;
}
var key = '', start = html.indexOf('$$$$$${'), end;
while (start > -1) {
end = html.indexOf('}', start);
if (end == -1) {
throw new Error('[' + html + '] i18n表达式异常')
}
key = html.substring(start + 7, end);
html = html.replaceAll(html.substring(start, end + 1), i18n(key));
start = html.indexOf('$$$$$${');
}
prop ? $(this).prop(prop, html) : $(this).html(html);
})
}
function i18n4pjax(event, contents) {
$(contents).each(function () {
var html = $(this).html();
if (!html) {
return;
}
var key = '', start = html.indexOf('$$$$$${'), end;
while (start > -1) {
end = html.indexOf('}', start);
if (end == -1) {
throw new Error('[' + html + '] i18n表达式异常')
}
key = html.substring(start + 7, end);
html = html.replaceAll(html.substring(start, end + 1), i18n(key));
start = html.indexOf('$$$$$${');
}
if ($(this).prop('nodeName') == 'SCRIPT') {
$(this).text(html);
} else {
$(this).html(html);
}
})
}
function i18n4get(url, callback) {
$.get(url, function (html) {
callback(i18n4txt(html));
})
}