-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkuCity.js
253 lines (226 loc) · 11.4 KB
/
kuCity.js
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
/*
* 城市选择jquer插件
*
* Licensed under the MIT license:
* https://github.com/callmeJozo/kuCity
*
* Author: Naraku(http://segmentfault.com/u/naraku_)
*
* Version: 1.0
*
*/
(function($) {
var allCities = ['北京|beijing|bj', '上海|shanghai|sh', '重庆|chongqing|cq', '深圳|shenzhen|sz', '广州|guangzhou|gz', '杭州|hangzhou|hz',
'南京|nanjing|nj', '苏州|shuzhou|sz', '天津|tianjin|tj', '成都|chengdu|cd', '南昌|nanchang|nc', '三亚|sanya|sy', '青岛|qingdao|qd',
'厦门|xiamen|xm', '西安|xian|xa', '长沙|changsha|cs', '合肥|hefei|hf', '西藏|xizang|xz', '内蒙古|neimenggu|nmg', '安庆|anqing|aq', '阿泰勒|ataile|atl', '安康|ankang|ak',
'阿克苏|akesu|aks', '包头|baotou|bt', '北海|beihai|bh', '百色|baise|bs', '保山|baoshan|bs', '长治|changzhi|cz', '长春|changchun|cc', '常州|changzhou|cz', '昌都|changdu|cd',
'朝阳|chaoyang|cy', '常德|changde|cd', '长白山|changbaishan|cbs', '赤峰|chifeng|cf', '大同|datong|dt', '大连|dalian|dl', '达县|daxian|dx', '东营|dongying|dy', '大庆|daqing|dq', '丹东|dandong|dd',
'大理|dali|dl', '敦煌|dunhuang|dh', '鄂尔多斯|eerduosi|eeds', '恩施|enshi|es', '福州|fuzhou|fz', '阜阳|fuyang|fy', '贵阳|guiyang|gy',
'桂林|guilin|gl', '广元|guangyuan|gy', '格尔木|geermu|gem', '呼和浩特|huhehaote|hhht', '哈密|hami|hm',
'黑河|heihe|hh', '海拉尔|hailaer|hle', '哈尔滨|haerbin|heb', '海口|haikou|hk', '黄山|huangshan|hs', '邯郸|handan|hd',
'汉中|hanzhong|hz', '和田|hetian|ht', '晋江|jinjiang|jj', '锦州|jinzhou|jz', '景德镇|jingdezhen|jdz',
'嘉峪关|jiayuguan|jyg', '井冈山|jinggangshan|jgs', '济宁|jining|jn', '九江|jiujiang|jj', '佳木斯|jiamusi|jms', '济南|jinan|jn',
'喀什|kashi|ks', '昆明|kunming|km', '康定|kangding|kd', '克拉玛依|kelamayi|klmy', '库尔勒|kuerle|kel', '库车|kuche|kc', '兰州|lanzhou|lz',
'洛阳|luoyang|ly', '丽江|lijiang|lj', '林芝|linzhi|lz', '柳州|liuzhou|lz', '泸州|luzhou|lz', '连云港|lianyungang|lyg', '黎平|liping|lp',
'连成|liancheng|lc', '拉萨|lasa|ls', '临沧|lincang|lc', '临沂|linyi|ly', '芒市|mangshi|ms', '牡丹江|mudanjiang|mdj', '满洲里|manzhouli|mzl', '绵阳|mianyang|my',
'梅县|meixian|mx', '漠河|mohe|mh', '南充|nanchong|nc', '南宁|nanning|nn', '南阳|nanyang|ny', '南通|nantong|nt', '那拉提|nalati|nlt',
'宁波|ningbo|nb', '攀枝花|panzhihua|pzh', '衢州|quzhou|qz', '秦皇岛|qinhuangdao|qhd', '庆阳|qingyang|qy', '齐齐哈尔|qiqihaer|qqhe',
'石家庄|shijiazhuang|sjz', '沈阳|shenyang|sy', '思茅|simao|sm', '铜仁|tongren|tr', '塔城|tacheng|tc', '腾冲|tengchong|tc', '台州|taizhou|tz',
'通辽|tongliao|tl', '太原|taiyuan|ty', '威海|weihai|wh', '梧州|wuzhou|wz', '文山|wenshan|ws', '无锡|wuxi|wx', '潍坊|weifang|wf', '武夷山|wuyishan|wys', '乌兰浩特|wulanhaote|wlht',
'温州|wenzhou|wz', '乌鲁木齐|wulumuqi|wlmq', '万州|wanzhou|wz', '乌海|wuhai|wh', '兴义|xingyi|xy', '西昌|xichang|xc', '襄樊|xiangfan|xf',
'西宁|xining|xn', '锡林浩特|xilinhaote|xlht', '西双版纳|xishuangbanna|xsbn', '徐州|xuzhou|xz', '义乌|yiwu|yw', '永州|yongzhou|yz', '榆林|yulin|yl', '延安|yanan|ya', '运城|yuncheng|yc',
'烟台|yantai|yt', '银川|yinchuan|yc', '宜昌|yichang|yc', '宜宾|yibin|yb', '盐城|yancheng|yc', '延吉|yanji|yj', '玉树|yushu|ys', '伊宁|yining|yn', '珠海|zhuhai|zh', '昭通|zhaotong|zt',
'张家界|zhangjiajie|zjj', '舟山|zhoushan|zs', '郑州|zhengzhou|zz', '中卫|zhongwei|zw', '芷江|zhijiang|zj', '湛江|zhanjiang|zj'
];
var regEx = /^([\u4E00-\u9FA5\uf900-\ufa2d]+)\|(\w+)\|(\w)\w*$/i, // 匹配汉字,拼音
regExChiese = /([\u4E00-\u9FA5\uf900-\ufa2d]+)/, // 只匹配拼音
reg_ah = /^[a-h]$/i, // 匹配首字母为 a-h
reg_ip = /^[i-p]/i, // 匹配首字母为 i-p
reg_qz = /^[q-z]/i; // 匹配首字母为 q-z
//构建城市分类字面量
var city = {
hot: {},
ABCDEFGH: {},
IJKLMNOP: {},
QRSTUVWXYZ: {}
};
//城市按首字母分类,填充到分类字面量
(function() {
for (var i = 0, len = allCities.length; i < len; i++) {
var part = regEx.exec(allCities[i]),
en = part[1], //中文名
letter = part[2], //拼音
spletter = part[3], //拼音简写
first = letter[0].toUpperCase(), //拼音首字母
ltPart; //当前字母下的城市
if (reg_ah.test(first)) {
ltPart = 'ABCDEFGH';
} else if (reg_ip.test(first)) {
ltPart = 'IJKLMNOP';
} else if (reg_qz.test(first)) {
ltPart = 'QRSTUVWXYZ';
}
city[ltPart][first] ? city[ltPart][first].push(en) : (city[ltPart][first] = [], city[ltPart][first].push(en));
//设置前16个城市为热门城市
if (i < 16) {
city.hot['hot'] ? city.hot['hot'].push(part[1]) : (city.hot['hot'] = [], city.hot['hot'].push(part[1]));
}
}
})();
var kucity = null;
var KuCity = function(target) {
this.target = target;
this.container = null;
this.resultct = null;
this.init();
};
KuCity.prototype = {
constructor: KuCity,
//初始化
init: function() {
this.creatItem();
this.tabChange();
this.citySelect();
this.inputSearch();
this.stopPropagation();
},
//创建市列表
creatItem: function() {
var template = '<div class="kucity"><div class="citybox"><h3 class="kucity_header">热门城市(支持汉字/拼音搜索)</h3><ul class="kucity_nav"><li class="active">热门城市</li><li>ABCDEFGH</li><li>IJKLMNOP</li><li>QRSTUVWXYZ</li></ul><div class="kucity_body"></div></div><ul class="result"></ul></div>';
$('body').append(template);
this.container = $('.kucity');
this.resultct = $('.result');
for (var group in city) {
var itemKey = [];
for (var item in city[group]) {
itemKey.push(item);
}
itemKey.sort();
var itembox = $('<div class="kucity_item">');
itembox.addClass(group);
for (var i = 0, iLen = itemKey.length; i < iLen; i++) {
var dl = $('<dl>'),
dt = '<dt>' + (itemKey[i] == 'hot' ? '' : itemKey[i]) + '</dt>',
dd = $('<dd>'),
str = '';
for (var j = 0, jLen = city[group][itemKey[i]].length; j < jLen; j++) {
str += '<span>' + city[group][itemKey[i]][j] + '</span>'
}
dd.append(str);
dl.append(dt).append(dd);
itembox.append(dl);
}
$('.kucity_body').append(itembox);
this.container.find('.hot').addClass('active');
}
},
//列表切换
tabChange: function() {
$('.kucity_nav').on('click', 'li', function(e) {
var current = $(e.target),
index = current.index();
current.addClass('active').siblings().removeClass('active');
$('.kucity_item').eq(index).addClass('active').siblings().removeClass('active');
$(' .kucity_body').scrollTop(0);
})
},
//城市选择
citySelect: function() {
var self = this;
$('.kucity_item dd').on('click', 'span', function(e) {
self.target.val(($(e.target).text()));
self.container.hide();
})
},
//搜索
inputSearch: function() {
var self = this;
this.target.on('keyup', function(e) {
self.throttle(search, this);
})
// 输入框搜索
function search(e) {
console.log(1)
var container = self.container;
self.triggleShow(false);
var value = $(this).val();
if (value) {
var url = 'https://sjipiao.alitrip.com/city_search.do?_ksTS=1439362066383_11337&lines=10&_input_charset=utf-8&needProvince=true&q=' + value;
$.ajax({
url: url,
type: 'get',
dataType: 'jsonp'
}).done(function(re) {
creatResult(re);
})
} else {
self.triggleShow(true);
}
function creatResult(re) {
var result = re.result,
len = result.length,
str = '';
if (!!len) {
for (var i = 0; i < len; i++) {
str += '<li><span class="name">' + result[i].cityName + '</span><span class="letter">' + result[i].py + '</span></li>'
}
container.find('.result').html('').html(str).find('li').eq(0).addClass('active');
} else {
container.find('.result').html('<li>没有找到<span class="noresult">' + value + '</span>相关信息</li>');
}
}
}
},
//列表,结果,整体 显示切换
triggleShow: function(open) {
var container = this.container;
if (open === 'all') {
container.hide()
} else if (open) {
container.find('.citybox').show().end().find('.result').hide();
} else {
container.find('.citybox').hide().end().find('.result').show();
}
},
//函数节流
throttle: function(fn, context) {
clearTimeout(fn.tId);
fn.tId = setTimeout(function(){
fn.call(context);
}, 100)
},
//阻止事件冒泡
stopPropagation: function() {
var self = this;
//阻止事件冒泡
this.container.on('click', stopPropagation)
this.target.on('click', stopPropagation)
//页面点击 隐藏
$(document).on('click', function() {
self.container.hide();
})
function stopPropagation(e) {
e.stopPropagation();
}
}
};
$.fn.kuCity = function(options) {
var target = $(this);
target.on('focus', function(e) {
var top = $(this).offset().top + $(this).height(),
left = $(this).offset().left;
kucity = kucity ? (kucity.container.show(), kucity) : new KuCity(target);
kucity.container.offset({
'top': top + 10,
'left': left
});
kucity.target = $(e.target);
kucity.triggleShow(true);
kucity.resultct.on('click', 'li', function() {
kucity.target.val($(this).find('.name').text());
kucity.triggleShow('all');
})
})
return this;
};
})(jQuery)