common.js
4.7 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
/**
* 删除提示框
* text 文本
* url 删除接口
* success 删除成功之后的回调
*/
function removeConfirm(text, url, success) {
layer.confirm(html_encode(text), {
btn : [ '确定删除', '取消' ],
icon : 3,
skin : 'layui-layer-cfm-delete'
}, function(){
layer.msg('正在删除...', {icon: 16});
$post(url, {'_method': 'delete'},function(rs){
layer.msg('删除成功!');
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('被注销的登录');
location.reload(true);
return;
}
if(!status){
handle && handle(json);
return ;
}
if(status == 'ERROR')
layer.alert(json.msg?json.msg:'未知异常', {icon: 2, title: 'Operation failed'});
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: 'Operation failed'});
}
}
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){
//车辆Internal code下拉搜索框
$e.select2({
placeholder: 'Search vehicle...',
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;">No vehicle found!</span>';
},
inputTooShort : function(e) {
return '<span style="color:gray;font-size: 12px;"><i class="fa fa-search"></i> Enter the self-number to search for the vehicle</span>';
},
searching : function() {
return '<span style="color:gray;font-size: 12px;"> Searching vehicle...</span>';
}
}
});
return $e;
}