%PDF- %PDF-
| Direktori : /disk/www/wwwroot/test-lingceed.echineselearning.biz/js/ |
| Current File : /disk/www/wwwroot/test-lingceed.echineselearning.biz/js/country-select-tool.js |
(function() {
var countrySelectTool = {
data : {
$countryListDiv : null, // 国家列表弹出层的jQuery对象
// $fadeDiv : null, // 背景层的jQuery对象
selectAfterFunction : null, // 选择国家后调用的方法
$countryUl : null, // 国家列表UL的jQuery对象
selectCode : '', // 已经选中的国家的编码
_scrollTop : 0 // 打开国家列表时记录的下拉高度
},
init : function(config) {
// 初始化参数
this.data.$countryListDiv = config.$countryListDiv;
// this.data.$fadeDiv = config.$fadeDiv;
this.data.selectAfterFunction = config.selectAfterFunction;
this.data.$countryUl = config.$countryListDiv.find('ul.js-country-ul');
this.data.selectCode = config.selectCode;
this._initSearch();
},
_initSearch : function() {
// 初始化按首字母筛选的功能
var _$countryListDiv = this.data.$countryListDiv;
_$countryListDiv.find('p.p_search').each(function() {
$(this).click(function(e) {
e.stopPropagation(); // 阻止冒泡
_$countryListDiv.find('p.p_search').removeClass('p_selected');
$(this).addClass('p_selected');
_insertCountry({
type : "fl",
value : $(this).text()
});
});
});
// 初始化国家搜索功能
_$countryListDiv.find('input[name="js-country-search"]').each(function() {
$(this).val('');
$(this).focus(function() {
$(this).css("box-shadow","inset 0 0px 0px rgba(0, 0, 0, 0), 0 0 8px rgba(0, 0, 0, 0)");
_$countryListDiv.find('p.p_search').removeClass('p_selected');
});
$(this).keyup(function() {
_insertCountry({
type : "sr",
value : $(this).val()
});
});
});
},
showCountryList : function() {
// 显示国家列表
_insertCountry();
_showCountryDiv();
},
closeCountryList : function() {
// 关闭国家列表
_closeCountryDiv();
}
};
var country = [
[ "af", "Afghanistan +93" ],[ "ax", "Aland Islands +358" ], [ "al", "Albania +355" ],
[ "dz", "Algeria +213" ], [ "as", "American Samoa +1" ], [ "ad", "Andorra +376" ], [ "ao", "Angola +244" ],
[ "ai", "Anguilla +1" ], [ "aq", "Antarctica +0" ], [ "ag", "Antigua and Barbuda +1" ], [ "ar", "Argentina +54" ],
[ "am", "Armenia +374" ], [ "aw", "Aruba +297" ], [ "au", "Australia +61" ], [ "at", "Austria +43" ], [ "az", "Azerbaijan +994" ],
[ "bs", "Bahamas +1" ], [ "bh", "Bahrain +973" ], [ "bd", "Bangladesh +880" ], [ "bb", "Barbados +1" ],
[ "by", "Belarus +375" ], [ "be", "Belgium +32" ], [ "bz", "Belize +501" ], [ "bj", "Benin +229" ], [ "bm", "Bermuda +1" ],
[ "bt", "Bhutan +975" ], [ "bo", "Bolivia +591" ],[ "bq", "Bonaire +599" ], [ "ba", "Bosnia and Herzegovina +387" ], [ "bw", "Botswana +267" ],
[ "bv", "Bouvet Island +47" ],[ "br", "Brazil +55" ],[ "io", "British Indian Ocean Territory +246" ],
[ "vg", "British Virgin Islands +1" ], [ "bn", "Brunei +673" ],[ "bg", "Bulgaria +359" ], [ "bf", "Burkina Faso +226" ],
[ "bi", "Burundi +257" ], [ "cv", "Cape Verde +238" ],[ "kh", "Cambodia +855" ],
[ "cm", "Cameroon +237" ],[ "ca", "Canada +1" ], [ "ky", "Cayman Islands +1" ], [ "cf", "Central African Republic +236" ],[ "td", "Chad +235" ],
[ "cl", "Chile +56" ], [ "cn", "China +86" ], [ "cx", "Christmas Island +61" ], [ "cc", "Cocos Islands +61" ],
[ "co", "Colombia +57" ], [ "km", "Comoros +269" ], [ "cg", "Congo +242" ], [ "cd", "Congo(DRC) +243" ],[ "ck", "Cook Islands +682" ],
[ "cr", "Costa Rica +506" ],[ "ci", "Côte d'Ivoire +225" ], [ "hr", "Croatia +385" ], [ "cu", "Cuba +53" ], [ "cw", "Curaçao +599" ],[ "cy", "Cyprus +357" ],
[ "cz", "Czech Republic +420" ], [ "dk", "Denmark +45" ], [ "dj", "Djibouti +253" ], [ "dm", "Dominica +1" ],
[ "do", "Dominican Republic +1" ], [ "tp", "East Timor +670" ], [ "ec", "Ecuador +593" ], [ "eg", "Egypt +20" ],
[ "sv", "El Salvador +503" ], [ "gq", "Equatorial Guinea +240" ], [ "er", "Eritrea +291" ], [ "ee", "Estonia +372" ],
[ "et", "Ethiopia +251" ], [ "fk", "Falkland Islands +500" ], [ "fo", "Faroe Islands +298" ], [ "fj", "Fiji +679" ],[ "fi", "Finland +358" ],
[ "fr", "France +33" ], [ "gf", "French Guiana +594" ], [ "pf", "French Polynesia +689" ],[ "tf", "French Southern Territories +262" ],
[ "ga", "Gabon +241" ], [ "gm", "Gambia +220" ], [ "ge", "Georgia +995" ], [ "de", "Germany +49" ], [ "gh", "Ghana +233" ],
[ "gi", "Gibraltar +350" ], [ "gr", "Greece +30" ], [ "gl", "Greenland +299" ], [ "gd", "Grenada +1" ],
[ "gp", "Guadeloupe +590" ], [ "gu", "Guam +1" ], [ "gt", "Guatemala +502" ],[ "gg", "Guernsey +44" ],[ "gn", "Guinea +224" ], [ "gw", "Guinea-Bissau +245" ],
[ "gy", "Guyana +592" ],[ "hk", "Hong Kong +852" ],[ "ht", "Haiti +509" ], [ "hn", "Honduras +504" ],
[ "hu", "Hungary +36" ], [ "is", "Iceland +354" ], [ "in", "India +91" ], [ "id", "Indonesia +62" ],
[ "ir", "Iran +98" ], [ "iq", "Iraq +964" ], [ "ie", "Ireland +353" ],[ "im", "Isle of Man +44" ], [ "il", "Israel +972" ], [ "it", "Italy +39" ],
[ "ci", "Ivory Coast +225" ], [ "jm", "Jamaica +1" ],[ "jy", "Jan Mayen +47" ], [ "jp", "Japan +81" ],[ "je", "Jersey +44" ], [ "jo", "Jordan +962" ],
[ "kz", "Kazakhstan +7" ], [ "ke", "Kenya +254" ], [ "ki", "Kiribati +686" ],[ "xk", "Kosovo +383" ],[ "kr", "Korea. South +82" ],[ "kp", "Korea. North +850" ],
[ "kw", "Kuwait +965" ], [ "kg", "Kyrgyzstan +996" ], [ "la", "Laos +856" ], [ "lv", "Latvia +371" ],
[ "lb", "Lebanon +961" ], [ "ls", "Lesotho +266" ], [ "lr", "Liberia +231" ], [ "ly", "Libya +218" ],
[ "li", "Liechtenstein +423" ], [ "lt", "Lithuania +370" ], [ "lu", "Luxembourg +352" ], [ "mo", "Macau +853" ],
[ "mk", "Macedonia +389" ], [ "mg", "Madagascar +261" ], [ "mw", "Malawi +265" ], [ "my", "Malaysia +60" ],
[ "mv", "Maldives +960" ], [ "ml", "Mali +223" ], [ "mt", "Malta +356" ], [ "mh", "Marshall Islands +692" ],
[ "mq", "Martinique +596" ], [ "mr", "Mauritania +222" ], [ "mu", "Mauritius +230" ], [ "yt", "Mayotte +262" ],
[ "mx", "Mexico +52" ], [ "fm", "Micronesia +691" ], [ "md", "Moldova +373" ], [ "mc", "Monaco +377" ], [ "mn", "Mongolia +976" ],[ "me", "Montenegro +382" ],
[ "ms", "Montserrat +1" ], [ "ma", "Morocco +212" ], [ "mz", "Mozambique +258" ], [ "mm", "Myanmar +95" ],
[ "na", "Namibia +264" ], [ "nr", "Nauru +674" ], [ "np", "Nepal +977" ], [ "nl", "Netherlands +31" ],
[ "an", "Netherlands Antilles +599" ], [ "nc", "New Caledonia +687" ], [ "nz", "New Zealand +64" ],
[ "ni", "Nicaragua +505" ], [ "ne", "Niger +227" ], [ "ng", "Nigeria +234" ], [ "nu", "Niue +683" ],
[ "nf", "Norfolk Island +672" ], [ "mp", "Northern Mariana Islands +1" ], [ "no", "Norway +47" ], [ "om", "Oman +968" ],
[ "pk", "Pakistan +92" ], [ "pw", "Palau +680" ],[ "ps", "Palestinian Authority +970" ], [ "pa", "Panama +507" ], [ "pg", "Papua New Guinea +675" ],
[ "py", "Paraguay +595" ], [ "pe", "Peru +51" ], [ "ph", "Philippines +63" ],[ "pn", "Pitcairn Islands +64" ],
[ "pl", "Poland +48" ], [ "pt", "Portugal +351" ], [ "pr", "Puerto Rico +1" ], [ "qa", "Qatar +974" ], [ "re", "Reunion +262" ],
[ "ro", "Romania +40" ], [ "ru", "Russia +7" ], [ "rw", "Rwanda +250" ],[ "sg", "Singapore +65" ],[ "bqsa", "Saba +599" ],[ "bl", "Saint Barthelemy +590" ],
[ "kn", "Saint Kitts and Nevis +1" ],[ "lc", "Saint Lucia +1" ], [ "ws", "Saint Martin +590" ],[ "vc", "Saint Vincent and the Grenadines +1" ],[ "ws", "Samoa +685" ],
[ "sm", "San Marino +378" ], [ "st", "Sao Tome and Principe +239" ],
[ "sa", "Saudi Arabia +966" ], [ "sn", "Senegal +221" ],[ "yu", "Serbia +381" ], [ "sc", "Seychelles +248" ], [ "sl", "Sierra Leone +232" ],[ "bqse", "Sint Eustatius +599" ],[ "sx", "Sint Maarten +1" ],
[ "sk", "Slovakia +421" ], [ "si", "Slovenia +386" ], [ "sp", "Solomon Islands +677" ],
[ "so", "Somalia +252" ], [ "za", "South Africa +27" ],[ "ss", "South Sudan +211" ], [ "es", "Spain +34" ], [ "lk", "Sri Lanka +94" ],
[ "sh", "St. Helena +290" ],[ "pm", "St. Pierre and Miquelon +508" ], [ "sd", "Sudan +249" ], [ "sr", "Suriname +597" ],
[ "sz", "Swaziland +268" ], [ "se", "Sweden +46" ], [ "ch", "Switzerland +41" ], [ "sy", "Syria +963" ], [ "tw", "Taiwan +886" ],
[ "tj", "Tajikistan +992" ], [ "tz", "Tanzania +255" ], [ "th", "Thailand +66" ],[ "tl", "Timor Leste +670" ], [ "tg", "Togo +228" ], [ "tk", "Tokelau +690" ],
[ "to", "Tonga +676" ], [ "tt", "Trinidad and Tobago +1" ], [ "tn", "Tunisia +216" ], [ "tr", "Turkey +90" ],
[ "tm", "Turkmenistan +993" ],[ "tc", "Turks and Caicos Islands +1" ], [ "tv", "Tuvalu +688" ],[ "us", "United States +1" ], [ "gb", "United Kingdom +44" ],
[ "oi", "U.S. Outlying Islands +1" ],[ "ui", "U.S. Virgin Islands +1" ], [ "ug", "Uganda +256" ], [ "ua", "Ukraine +380" ],
[ "ae", "United Arab Emirates +971" ], [ "uy", "Uruguay +598" ], [ "uz", "Uzbekistan +998" ],
[ "vu", "Vanuatu +678" ], [ "va", "Vatican City +379" ], [ "ve", "Venezuela +58" ], [ "vn", "Vietnam +84" ],[ "vi", "Virgin Islands +1" ],
[ "wf", "Wallis and Futuna +681" ], [ "ye", "Yemen +967" ],
[ "zr", "Zaire +243" ], [ "zm", "Zambia +260" ], [ "zw", "Zimbabwe +263" ]
];
var country_top = [
[ "us", "United States +1" ],[ "hk", "Hong Kong +852" ],[ "gb", "United Kingdom +44" ],
[ "sg", "Singapore +65" ], [ "ca", "Canada +1" ],[ "au", "Australia +61" ]
];
/**
* 插入国家
* @param filter 过滤器
*/
function _insertCountry(filter) {
// 先清空国家列表
countrySelectTool.data.$countryUl.empty();
// 通过过滤器筛选的时候不加载头部的几个国家
if (!filter) {
// 插入头部的几个国家
_insertCountryLi(country_top);
// 插入横线
_insertLine();
}
// 插入全部国家
_insertCountryLi(country, filter);
}
/**
* 插入国家数据的li
* @param countryList 国家数据
* @param filter 过滤器
*/
function _insertCountryLi(countryList, filter) {
if (countryList && countryList.length > 0) {
// 隐藏没有国家显示的提示
countrySelectTool.data.$countryListDiv.find('p.js-country-not-found').hide();
// 插入国家
var countryLength = countryList.length;
var adj, last, qq, str, li, ins;
for (var i = 0; i < countryLength; i++) {
adj = countryList[i][1]; // 获取国家信息
last = adj.substr(adj.lastIndexOf("+")); // 截取区号
qq = adj.lastIndexOf("+");
str = adj.substr(0, qq - 3); // 截取国家名称
// 没有过滤,允许插入
ins = filter ? false : true;
// 按首字母过滤
if (!ins) {
ins = (filter.type == 'fl' && filter.value.toLowerCase() == adj.substr(0, 1).toLowerCase());
}
// 按国家名字搜索过滤
if (!ins) {
ins = (filter.type == 'sr' && (str.toLowerCase().indexOf(filter.value.toLowerCase()) >= 0) || new RegExp(filter.value).test(str.toLowerCase()));
}
// 插入国家
if (ins) {
li = $('<li class="lis" valuess="' + countryList[i][0] + '"><span class="country">' + str + '</span> <span class="country_tels">' + last + '</span></li><div style="clear:both"></div>');
countrySelectTool.data.$countryUl.append(li);
li.click(function(e) {
e.stopPropagation(); // 阻止冒泡
_selectCountryLi($(this));
});
}
}
// 已经选中的国家修改一下样式
if (countrySelectTool.data.selectCode != '') {
countrySelectTool.data.$countryUl.find('li[valuess=' + countrySelectTool.data.selectCode + ']').css("background", "#d5ecf7");
}
// 没有国家显示的给出提示
if (countrySelectTool.data.$countryUl.find('li').length <= 0) {
countrySelectTool.data.$countryListDiv.find('p.js-country-not-found').show();
}
}
}
/**
* 插入一条横线
*/
function _insertLine() {
countrySelectTool.data.$countryUl.append('<li><div style="width: 100%;height:1px;background-color: #cccccc;"></div></li><div style="clear:both">');
}
/**
* 选择一个国家
* @param $li 选择的国家的li的jQuery对象
*/
function _selectCountryLi($li) {
// 获取选择的国家的数据
// var country_code = $li.attr("value"); //PC端获取不到value了
var country_code = $li.attr("valuess");
var country_name = $li.find(".country").html();
var country_tels = $li.find(".country_tels").html();
var country_areacode = country_tels.substr(country_tels.lastIndexOf("+") + 1);
// 构建反馈对象
var res = {
country_code : country_code,
country_name : country_name,
country_areacode : country_areacode
};
// 调用回调函数
countrySelectTool.data.selectAfterFunction(res);
// 关闭国家列表框
_closeCountryDiv();
}
/**
* 展示国家列表框
*/
function _showCountryDiv() {
// 把遮罩层、国家列表层显示出来
// var documentH = $(document).height();
// countrySelectTool.data.$fadeDiv.css("height", documentH);
// countrySelectTool.data.$fadeDiv.show();
countrySelectTool.data.$countryListDiv.show();
// 点击遮罩层的时候关闭国家列表
// countrySelectTool.data.$fadeDiv.click(function() {
// _closeCountryDiv();
// });
// 整体页面不可滑动,国家下拉可以滑动
// countrySelectTool.data._scrollTop = $(window).scrollTop(); // 获取页面的scrollTop
// $('body').css("top", -countrySelectTool.data._scrollTop + "px"); // 给body一个负的top值
// $('body,html').addClass('addss');
// countrySelectTool.data.$countryUl.find('.lis').css('overflow','auto');
// 搜索框获得焦点
countrySelectTool.data.$countryListDiv.find('input[name="js-country-search"]').focus();
}
/**
* 关闭国家列表框
*/
function _closeCountryDiv() {
countrySelectTool.data.$countryListDiv.hide();
// countrySelectTool.data.$fadeDiv.hide();
// $('body').css('overflow','auto');
// $('body').css("top", "");
$('body,html').removeClass('addss');
// $(window).scrollTop(countrySelectTool.data._scrollTop);
}
this.countrySelectTool = countrySelectTool;
}).call(this);