common.js
4.86 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
var gb_common = (function() {
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) {
var tps = {},
id;
$('script[type="text/html"]', dom).each(function() {
id = $(this).attr('id');
if (id)
tps[id] = template.compile($(this).html());
});
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);
}
});
}
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')
UIkit.modal.alert('服务器出现异常: ' + (json.msg ? json.msg : '未知异常'));
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: '操作失败'
});
}
}
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,
'children': [{
'text': '上行',
'children': grabs(data[name][0])
}, {
'text': '下行',
'children': grabs(data[name][1])
}]
});
}
function grabs(array) {
if (!array)
return;
var rs = [];
$.each(array, function() {
rs.push({
'text': this.nbbm,
'a_attr': {
'type': 'device'
},
'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 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);
if (count >= 10)
return false;
count++;
});
release && release(rs);
}
});
};
return {
groupBy: groupBy,
compileTempByDom: compileTempByDom,
$get: $get,
$post: $post,
get_vals: get_vals,
get_keys: get_keys,
get_device_tree_data: get_device_tree_data,
lineAutocomplete: lineAutocomplete
};
})();