270 lines
6.6 KiB
JavaScript
270 lines
6.6 KiB
JavaScript
var CheckStaion = {
|
||
runing: false,
|
||
objs: {
|
||
"a": [],
|
||
"b": [],
|
||
"c": [],
|
||
"d": [],
|
||
"e": [],
|
||
"f": [],
|
||
"g": [],
|
||
"h": [],
|
||
"i": [],
|
||
"j": [],
|
||
"k": [],
|
||
"l": [],
|
||
"m": [],
|
||
"n": [],
|
||
"o": [],
|
||
"p": [],
|
||
"q": [],
|
||
"r": [],
|
||
"s": [],
|
||
"t": [],
|
||
"u": [],
|
||
"v": [],
|
||
"w": [],
|
||
"x": [],
|
||
"y": [],
|
||
"z": []
|
||
},
|
||
objsZh: {},
|
||
hotList: [],
|
||
zhObj : {},
|
||
CodeObj:{},
|
||
callback: null,
|
||
init: function() {
|
||
if (CheckStaion.runing) {
|
||
return false;
|
||
}
|
||
CheckStaion.runing = true;
|
||
|
||
CheckStaion.formatStation();
|
||
|
||
CheckStaion.setHtml();
|
||
|
||
CheckStaion.bindEvent();
|
||
},
|
||
formated:false,
|
||
formatStation: function() {
|
||
if(CheckStaion.formated){
|
||
return ;
|
||
}
|
||
CheckStaion.formated = true;
|
||
CheckStaion.names = WIN["station_names"].substr(1);
|
||
CheckStaion.stationList = CheckStaion.names.split('@');
|
||
|
||
var sub = '',
|
||
arr = [];
|
||
|
||
for (var i = 0; i < CheckStaion.stationList.length; i++) {
|
||
sub = CheckStaion.stationList[i][0].toLowerCase();
|
||
if (!CheckStaion.objs[sub]) {
|
||
CheckStaion.objs[sub] = [];
|
||
}
|
||
arr = CheckStaion.stationList[i].split('|');
|
||
CheckStaion.objs[sub].push(arr);
|
||
|
||
sub = arr[1][0];
|
||
if (!CheckStaion.objsZh[sub]) {
|
||
CheckStaion.objsZh[sub] = [];
|
||
}
|
||
CheckStaion.objsZh[sub].push(arr);
|
||
|
||
CheckStaion.zhObj[arr[1]] = arr;
|
||
CheckStaion.CodeObj[arr[2]] = arr;
|
||
};
|
||
|
||
CheckStaion.hotList = WIN["favorite_names"].substr(1).split('@');
|
||
},
|
||
getHisHmtl : function(){
|
||
var his = JSON.parse(localStorage.getItem('stationHis')),
|
||
sp = [],
|
||
html = '';
|
||
if(!his || his.length == 0){
|
||
return '';
|
||
}
|
||
html += '<div class="form_title"><a href="javascript:;" class="title_right" data-fn="remove_his"><i class="icon_remove_b"></i></a>最近查找</div>'
|
||
html += '<div class="box_insert">';
|
||
html += '<ul class="station_list">';
|
||
|
||
for (var i = 0; i < his.length; i++) {
|
||
sp = his[i].split('|');
|
||
html += '<li><a href="javascript:;" title="'+sp[1]+'" data-code="'+sp[0]+'">'+sp[1]+'</a></li>';
|
||
};
|
||
|
||
html += '</ul></div>';
|
||
return html;
|
||
},
|
||
setHtml: function() {
|
||
var html = '<div class="fixed_box" id="search_station"><header class="header"><div class="search_box"><a href="javascript:;" class="search_cancel" id="station_cancel">取消</a><div class="search_ipt"><input type="search" name="" placeholder="例:bj、beijing、北京" id="station_search" class="search_input" autocomplete="off"><i class="icon_remove_s"></i></div></div></header>';
|
||
|
||
|
||
html += '<div id="station_hisbox">';
|
||
html += CheckStaion.getHisHmtl();
|
||
html += '</div>';
|
||
// <li><a href="javascript:;" title="上海" data-code="SHH">上海</a></li>\
|
||
// <li><a href="javascript:;" title="天津" data-code="TJP">天津</a></li>\
|
||
if (CheckStaion.hotList.length > 0) {
|
||
var arr = [];
|
||
html += '<div class="form_title">热门</div><div class="box"><ul class="station_list">';
|
||
for (var i = 0, len = CheckStaion.hotList.length; i < len; i++) {
|
||
arr = CheckStaion.hotList[i].split('|');
|
||
html += '<li><a href="javascript:;" title="' + arr[1] + '" data-code="' + arr[2] + '">' + arr[1] + '</a></li>';
|
||
};
|
||
html += '</ul></div><div class="suggest" id="station_suggest"><ul></ul></div></div>';
|
||
};
|
||
|
||
$('body').append(html);
|
||
},
|
||
show: function() {
|
||
$('#search_station').addClass('fixed_box_show');
|
||
},
|
||
hide: function() {
|
||
$('#search_station').removeClass('fixed_box_show');
|
||
},
|
||
bindEvent: function() {
|
||
//取消
|
||
$('#station_cancel').on('click', function() {
|
||
CheckStaion.hide();
|
||
if (CheckStaion.cancelCallback) {
|
||
CheckStaion.cancelCallback();
|
||
}
|
||
});
|
||
|
||
$('#station_search').on('input', function() {
|
||
var text = $(this).val(),
|
||
list;
|
||
if ($.trim(text)) {
|
||
CheckStaion.showSuggest(CheckStaion.getSuggestList($.trim(text).toLowerCase()));
|
||
} else {
|
||
$('#station_suggest ul').html('');
|
||
$('#station_suggest').hide();
|
||
}
|
||
});
|
||
|
||
$('#search_station [data-code]').live('click', function(events, handler) {
|
||
var name = $(this).attr('title'),
|
||
code = $(this).attr('data-code'),
|
||
his = code + '|' + name,
|
||
stationHis = JSON.parse(localStorage.getItem('stationHis'));
|
||
|
||
if (CheckStaion.callback) {
|
||
CheckStaion.callback(name, code);
|
||
}
|
||
|
||
if(!stationHis){
|
||
stationHis = [];
|
||
}
|
||
|
||
if(stationHis.indexOf(his) != -1){
|
||
stationHis.splice(stationHis.indexOf(his),1);
|
||
}
|
||
|
||
stationHis.unshift(his);
|
||
|
||
if(stationHis.length > 9){
|
||
stationHis.length = 9;
|
||
}
|
||
|
||
localStorage.setItem('stationHis',JSON.stringify(stationHis));
|
||
|
||
$('#station_hisbox').html(CheckStaion.getHisHmtl());
|
||
|
||
$('#station_suggest ul').html('');
|
||
$('#station_suggest').hide();
|
||
$('#station_search').val('');
|
||
});
|
||
|
||
$('[data-fn="remove_his"]').live('click',function(){
|
||
$('#station_hisbox').html('');
|
||
localStorage.removeItem('stationHis');
|
||
});
|
||
},
|
||
showSuggest: function(list) {
|
||
if (!list || list.length == 0) {
|
||
$('#station_suggest ul').html('');
|
||
$('#station_suggest').hide();
|
||
return;
|
||
}
|
||
|
||
var str = '';
|
||
// for (var i = 0, len = list.length>6?6:list.length; i < len; i++) {
|
||
for (var i = 0, len = list.length; i < len; i++) {
|
||
str += '<li><a href="javascript:;" title="' + list[i][1] + '" data-code="' + list[i][2] + '">' + list[i][1] + '</a></li>';
|
||
};
|
||
$('#station_suggest ul').html(str);
|
||
$('#station_suggest').show();
|
||
},
|
||
getSuggestList: function(text) {
|
||
if (!text) {
|
||
return [];
|
||
}
|
||
if (/^[\u4E00-\u9FA5]+$/.test(text[0])) {
|
||
return CheckStaion.searchKeyZh(text);
|
||
} else {
|
||
return CheckStaion.searchKey(text);
|
||
}
|
||
},
|
||
searchKey: function(text) {
|
||
if (!CheckStaion.objs[text[0]]) {
|
||
return [];
|
||
}
|
||
var list = CheckStaion.objs[text[0]],
|
||
arr = [],
|
||
arr1 = [],
|
||
arr2 = [],
|
||
arr3 = [];
|
||
|
||
if (text.length == 0) {
|
||
return list;
|
||
}
|
||
|
||
for (var i = 0, len = list.length; i < len; i++) {
|
||
if (list[i][4].indexOf(text) != -1) {
|
||
if (list[i][4] == text) {
|
||
arr1.unshift(list[i]);
|
||
} else {
|
||
arr1.push(list[i]);
|
||
}
|
||
} else if (list[i][3].indexOf(text) != -1) {
|
||
if (list[i][3] == text) {
|
||
arr2.unshift(list[i]);
|
||
} else {
|
||
arr2.push(list[i]);
|
||
}
|
||
} else if (list[i][0].indexOf(text) != -1) {
|
||
if (list[i][0] == text) {
|
||
arr3.unshift(list[i]);
|
||
} else {
|
||
arr3.push(list[i]);
|
||
}
|
||
}
|
||
};
|
||
arr = arr1.sort(CheckStaion.sort).concat(arr2.sort(CheckStaion.sort), arr3.sort(CheckStaion.sort));
|
||
|
||
return arr;
|
||
},
|
||
searchKeyZh: function(text) {
|
||
if (!CheckStaion.objsZh[text[0]]) {
|
||
return [];
|
||
}
|
||
|
||
var list = CheckStaion.objsZh[text[0]],
|
||
arr = [];
|
||
|
||
if (text.length == 1) {
|
||
return list;
|
||
}
|
||
|
||
for (var i = 0, len = list.length; i < len; i++) {
|
||
if (list[i][1].indexOf(text) != -1) {
|
||
arr.push(list[i]);
|
||
};
|
||
};
|
||
return arr.sort(CheckStaion.sort);
|
||
},
|
||
sort: function(st1, st2) {
|
||
return parseInt(st1[5]) > parseInt(st2[5]) ? 1 : -1;
|
||
}
|
||
} |