/* ============================================================ * jquery.modal.search.js 搜索页面用地区js * ============================================================ */ !function($) { var backdropLayerTpl = ''; var htmlLayerTpl = [ '' ].join(''); // 点击显示地区分类 $('#showSearchModal').off().on('click', function() { var titleValue = $(this).data('title'); var multipleValue = eval($(this).data('multiple')); var maxNumValue = eval($(this).data('maxnum')); var widthValue = eval($(this).data('width')); var isSpell = app_spell; var htmlCity = ''; var isSubsite = eval(qscms.is_subsite); // 是否是分站 var subsiteLevelNum = eval(qscms.subsite_level); var subsiteLevel1 = true; // 是否是一级分站 if (!isSubsite) { // 如果不是分站 一级分站始终为false subsiteLevel1 = false; } else { if (subsiteLevelNum > 1) { // 如果是分站,分站级数大于1 则为false subsiteLevel1 = false; } } var subsiteLevel1Width = 372; if (isSubsite) { if (isSpell) { // 拼音 if (!subsiteLevel1) { // 分站是二级 if (QS_city_spell_parent) { htmlCity += ''; } } else { // 分站为一级 if (QS_city_spell_parent) { htmlCity += ''; } } } else { if (!subsiteLevel1) { // 分站是二级 if (QS_city_parent) { htmlCity += ''; } } else { // 分站为一级 if (QS_city_parent) { htmlCity += ''; } } } } else { if (isSpell) { // 拼音 if (QS_city_spell_parent) { htmlCity += ''; } } else { if (QS_city_parent) { htmlCity += ''; } } } prepareModal(titleValue, multipleValue, maxNumValue); $('.J_modal_content').html(htmlCity); $('.J_btnyes').attr('id', 'J_btnyes_city'); $('.J_modal_content .right_box .list_nav').eq(0).show(); $('.J_list_city_parent').eq(0).addClass('current'); var msWidthValue = widthValue; if (subsiteLevel1) { // 一级分站宽度 msWidthValue = subsiteLevel1Width; } $('.modal_dialog').css({ width: msWidthValue + 'px', left: ($(window).width() - msWidthValue)/2, top: ($(window).height() - $('.modal_dialog').outerHeight())/2 + $(document).scrollTop() }); $('.modal_backdrop').addClass('in'); // 恢复选中 var recoverValue = $('#recoverSearchCityModalCode').val(); if (recoverValue.length) { if (multipleValue) { var recoverValueArray = recoverValue.split(','); if (subsiteLevel1) { // 一级分站 for (var i = 0; i < recoverValueArray.length; i++) { $('.J_list_city_parent').each(function(index, el) { if ($(this).data('code') == recoverValueArray[i]) { $(this).addClass('current'); }; }); }; } else { for (var i = 0; i < recoverValueArray.length; i++) { $('.J_list_city').each(function(index, el) { if ($(this).data('code') == recoverValueArray[i]) { $(this).addClass('seledted'); }; }); }; } copyCitySelectedSecond(); } else { if (subsiteLevel1) { // 一级分站 $('.J_list_city_parent').removeClass('seledted current'); $('.J_list_city_parent').each(function(index, el) { if ($(this).data('code') == recoverValue) { $(this).addClass('current'); }; }); } else { $('.J_list_city').each(function(index, el) { if ($(this).data('code') == recoverValue) { $(this).addClass('seledted'); }; }); } } // 不是一级分站才需要进行下一步 if (!subsiteLevel1) { $('.J_list_city_parent').removeClass('seledted current'); var subscriptValue = 0; $('.J_list_city.seledted').each(function(index, el) { var thisGroup = $(this).closest('.J_list_city_group'); subscriptValue = $('.J_list_city_group').index(thisGroup); $('.J_list_city_parent').eq(subscriptValue).addClass('seledted'); }); $('.J_list_city_group').eq(subscriptValue).show().siblings('.J_list_city_group').hide(); } } // 一级地区点击 $('.J_list_city_parent').on('click', function() { $(this).addClass('current').siblings('.J_list_city_parent').removeClass('current'); var subscriptValue = $('.J_list_city_parent').index(this); $('.J_list_city_group').eq(subscriptValue).show().siblings('.J_list_city_group').hide(); }); // 不限 $('.J_list_city_nolimit').off().on('click', function() { var thisGroup = $(this).closest('.J_list_city_group'); thisGroup.find('.J_list_city').not('.J_list_city_nolimit').removeClass('seledted'); }); // 二级地区点击 $('.J_list_city').off().on('click', function() { if ($(this).hasClass('seledted')) { $(this).removeClass('seledted'); if (multipleValue) { copyCitySelectedSecond(); }; var thisGroup = $(this).closest('.J_list_city_group'); var subscriptValue = $('.J_list_city_group').index(thisGroup); if (!thisGroup.find('.seledted').length) { $('.J_list_city_parent').eq(subscriptValue).removeClass('seledted').addClass('current'); }; } else { $(this).addClass('seledted'); if (multipleValue) { if (!$(this).is('.J_list_city_nolimit')) { var thisGroup = $(this).closest('.J_list_city_group'); thisGroup.find('.J_list_city_nolimit').removeClass('seledted'); }; if ($('.J_list_city.seledted').length > maxNumValue) { $(this).removeClass('seledted'); disapperTooltip("remind", '最多选择'+ maxNumValue +'个'); return false; } else { copyCitySelectedSecond(); } var thisGroup = $(this).closest('.J_list_city_group'); var subscriptValue = $('.J_list_city_group').index(thisGroup); $('.J_list_city_parent').eq(subscriptValue).addClass('seledted'); } else { var code = $(this).data('code'); var title = $(this).data('title'); $('#searchCityModalCode').val(code); $('#recoverSearchCityModalCode').val(code); $('#showSearchModal').text(title); $('#showSearchModal').attr('title', title); $('.modal_backdrop').remove(); $('.modal').remove(); } } }); function copyCitySelectedSecond() { var htmlListed = ''; $('.J_list_city.seledted').each(function(index, el) { var listedCode = $(this).data('code'); var listedTitle = $(this).data('title'); htmlListed += [ '
', '', '' + listedTitle + '
', '
', '
' ].join(''); }); $('#J_listed_content').html(htmlListed); $('#J_listed_group').show(); } $('.J_listed_city').off().on('click', function() { var listedValue = $(this).data('code'); $('.J_list_city').each(function(index, el) { if ($(this).data('code') == listedValue) { $(this).removeClass('seledted'); var thisGroup = $(this).closest('.J_list_city_group'); var subscriptValue = $('.J_list_city_group').index(thisGroup); if (!thisGroup.find('.seledted').length) { $('.J_list_city_parent').eq(subscriptValue).removeClass('seledted'); }; }; }); copyCitySelectedSecond(); }); $('#J_listed_clear').on('click', function() { $('.J_list_city.seledted').each(function(index, el) { $(this).removeClass('seledted'); }); $('.J_list_city_parent').removeClass('seledted'); copyCitySelectedSecond(); }); // 确定 $('#J_btnyes_city').off().on('click', function() { var checkedArray = new Array(); if (isSubsite && subsiteLevel1) { // 是分站并且是一级分站 checkedArray = $('.J_list_city_parent.current'); } else { checkedArray = $('.J_list_city.seledted'); } var codeArray = new Array(); var titleArray = new Array(); $.each(checkedArray, function(index, val) { codeArray[index] = $(this).data('code'); titleArray[index] = $(this).data('title'); }); $('#searchCityModalCode').val(codeArray.join(',')); $('#recoverSearchCityModalCode').val(codeArray.join(',')); $('#showSearchModal').text(titleArray.length ? titleArray.join('+') : '请选择'); $('#showSearchModal').attr('title', titleArray.length ? titleArray.join('+') : '请选择'); removeModal(); }); }); function prepareModal(titleValue, multipleValue, maxNumValue) { var ie = !-[1,]; var ie6 = !-[1,]&&!window.XMLHttpRequest; $(backdropLayerTpl).appendTo($(document.body)); if (ie6) { $('.modal_backdrop').css("height", $(document).height()); } $(htmlLayerTpl).appendTo($(document.body)); $('.J_modal_title').text(titleValue); if (multipleValue) { $('.J_modal_max').text('(最多选择'+ maxNumValue +'个)'); }; $(".J_hoverbut").hover( function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); } ); // 可拖动 var newObj = $('.modal_dialog'); var newTit = newObj.find(".modal_header"); newTit.mousedown(function(e) { var offset = newObj.offset(); var x = e.pageX - offset.left; var y = e.pageY - offset.top; $(document).bind('mousemove', function(ev) { newObj.bind('selectstart', function() { return false; }); var newx = ev.pageX - x; var newy = ev.pageY - y; newObj.css({ 'left': newx + "px", 'top': newy + "px" }); }); }); $(document).mouseup(function() { $(this).unbind("mousemove"); }) if (ie) { if (window.PIE) { $('.pie_about').each(function() { PIE.attach(this); }); } }; } $('.J_dismiss_modal').on('click', function() { removeModal(); }); $(document).on('keydown', function(event) { if (event.keyCode == 27) { removeModal(); } }); function removeModal() { setTimeout(function() { $('.modal_backdrop').remove(); $('.modal').remove(); },50) } }(window.jQuery);