common.js
8.58 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
var gb_common = (function() {
var reqCode80 = {0xA1: '请求恢复运营', 0xA2: '申请调档', 0xA3: '出场请求', 0xA5: '进场请求', 0xA7: '加油请求', 0x50: '车辆故障', 0x70: '路阻报告', 0x60: '事故报告', 0x11: '扣证纠纷', 0x12 : '报警'};
var groupBy = function(list, field) {
var rs = {},
key;
$.each(list, function() {
key = this[field];
if (!rs[key])
rs[key] = [];
rs[key].push(this);
});
return rs;
}
var compileTempByDom = function(dom, opts) {
var tps = {},
id;
$('script[type="text/html"]', dom).each(function() {
id = $(this).attr('id');
if (id)
tps[id] = template.compile($(this).html(), opts);
});
return tps;
}
var $get = function(url, data, successFun) {
$.ajax({
url: url,
data: data,
complete: function(xhr, ts) {
ajaxComplete(xhr, ts, successFun);
}
});
}
var $post = function(url, data, successFun) {
$.ajax({
url: url,
method: 'POST',
data: data,
complete: function(xhr, ts) {
ajaxComplete(xhr, ts, successFun);
}
});
}
var $post_arr = function(url, data, successFun) {
$.ajax({
url: url,
method: 'POST',
traditional: true,
data: data,
complete: function(xhr, ts) {
ajaxComplete(xhr, ts, successFun);
}
});
}
var $del = function(url, successFun){
$post(url, {'_method': 'delete'},function(rs){
successFun && successFun(rs);
});
}
var errorHead='<span style="color:red;">服务器出现异常:</span>';
function successHandle(json, handle) {
var status = json.status;
if (status == 407) {
alert('被注销的登录');
location.href = '/';
return;
}
if (!status) {
handle && handle(json);
return;
}
if (status == 'ERROR')
UIkit.modal.alert(errorHead+ (json.msg ? json.msg : '未知异常'), {labels:{Ok: '确定'}});
else
handle && handle(json);
}
function ajaxComplete(xhr, ts, succ) {
if (ts == 'success') {
successHandle(JSON.parse(xhr.responseText), succ);
} else if (ts == 'error') {
UIkit.modal.alert(errorHead + xhr.responseText, {labels:{Ok: '确定'}});
}
}
var get_vals = function(json) {
var array = [];
for (var key in json) {
array.push(json[key]);
}
return array;
}
var get_keys = function(json) {
var array = [];
for (var key in json) {
array.push(key);
}
return array;
}
var get_device_tree_data = function() {
var treeData = [];
var data = groupBy(get_vals(gb_data_gps.allGps), 'lineId');
var name;
for (var code in data) {
name = gb_data_basic.codeToLine[code].name;
data[name] = groupBy(data[code], 'upDown');
treeData.push({
'text': name,
'a_attr':{'type': 'line'},
'children': [{
'text': '上行',
'children': grabs(data[name][0]),
'a_attr': {
'type': 'route',
'route': code+'_0'
}
}, {
'text': '下行',
'children': grabs(data[name][1]),
'a_attr': {
'type': 'route',
'route': code+'_1'
}
}]
});
}
function grabs(array) {
if (!array)
return;
var rs = [];
$.each(array, function() {
rs.push({
'text': this.nbbm,
'a_attr': {
'type': 'device',
'device': this.deviceId
},
'icon': 'uk-icon-bus'
});
});
return rs;
}
return treeData;
}
var lineAutocomplete = function(element) {
//construction data
var data = [],
code2Name = gb_data_basic.lineCode2NameAll(),
name;
for (var code in code2Name) {
name = code2Name[code];
data.push({
value: name,
code: code,
fullChars: pinyin.getFullChars(name).toUpperCase(),
camelChars: pinyin.getCamelChars(name)
});
}
init_autocom_pinyin(element, data);
};
var personAutocomplete = function(element, personMaps) {
var data = [],name;
// $.each(personList, function(){
// name=this.personnelName;
// data.push({
// value: this.jobCode+'/'+name,
// fullChars: pinyin.getFullChars(name).toUpperCase(),
// camelChars: pinyin.getCamelChars(name),
// brancheCompany: this.brancheCompany
// });
// });
// console.log('data', data);
for(var jobCode in personMaps){
name=personMaps[jobCode];
data.push({
value: jobCode+'/'+name,
fullChars: pinyin.getFullChars(name).toUpperCase(),
camelChars: pinyin.getCamelChars(name)
});
}
init_autocom_pinyin(element, data);
};
var carAutocomplete=function(element, list){
var data = [];
$.each(list, function(){
data.push({value: this});
});
init_autocomplete(element, data);
}
var init_autocom_pinyin=function(element, data){
// init autocomplete
var autocomplete = UIkit.autocomplete(element, {
minLength: 1,
delay: 50,
source: function(release) {
var q = $('input', element).val().toUpperCase(),
rs = [],
count = 0;
$.each(data, function() {
if (this.value.indexOf(q) != -1 || this.fullChars.indexOf(q) != -1 || this.camelChars.indexOf(q) != -1){
rs.push(this);
count++;
}
if (count >= 10)
return false;
});
release && release(rs);
}
});
}
var init_autocomplete=function(element, data){
var autocomplete = UIkit.autocomplete(element, {
minLength: 1,
delay: 50,
source: function(release) {
var q = $('input', element).val().toUpperCase(),
rs = [],
count = 0;
$.each(data, function() {
if (this.value.indexOf(q) != -1){
rs.push(this);
count++;
}
if (count >= 10)
return false;
});
release && release(rs);
}
});
}
// function whichTransitionEvent() {
// var t;
// var el = document.createElement('fakeelement');
// var transitions = {
// 'transition': 'transitionend',
// 'OTransition': 'oTransitionEnd',
// 'MozTransition': 'transitionend',
// 'WebkitTransition': 'webkitTransitionEnd',
// 'MsTransition': 'msTransitionEnd'
// }
// for (t in transitions) {
// if (el.style[t] !== undefined) {
// return transitions[t];
// }
// }
// }
return {
reqCode80: reqCode80,
groupBy: groupBy,
compileTempByDom: compileTempByDom,
$get: $get,
$post: $post,
$post_arr: $post_arr,
$del: $del,
get_vals: get_vals,
get_keys: get_keys,
get_device_tree_data: get_device_tree_data,
lineAutocomplete: lineAutocomplete,
personAutocomplete: personAutocomplete,
carAutocomplete: carAutocomplete
//whichTransitionEvent:whichTransitionEvent
};
})();