// JavaScript Document
//瑁戒綔鑰匉Hans
//鏃ユ湡锛?012-07-19
//濂椾欢浠嬬垂锛氳嚜瑷傜京select涓嬫媺寮忛伕鍠
//淇敼: 2012-09-21
//淇敼: 2012-09-25 澶氶噸妯e紡
//淇敼: 2013-01-17 multi Ajax
//淇敼锛 2013-03-20 鍕曟厠灞€
//淇敼锛 2013-06-13 scrollbar
//淇敼锛 2013-08-20 icon
$.fn.hs_select = function(options) {
/*鍋垫脯function 鏄惁杓夊叆*/
if (!$.isFunction($.fn.draggable)) {
alert('璜嬭級鍏raggable濂椾欢!!');
return false;
}
if (!$.isFunction($.fn.mousewheel)) {
alert('璜嬭級鍏ousewheel濂椾欢!!');
return false;
}
var defaults = {
//婊戦紶鎸変笅浜嬩欢
clickEvent:null,//婊戦紶閬镐腑鏅 function(閬镐腑鐨勫€?{}
insertElement:function(obj,title,icon){
var icon_url='';
if(icon){
icon_url="";
}
return "
"+icon_url+title+"";
},
closeDelay:200,//闆㈤枊鍗€濉婂咕绉掑緦闂滆捣渚
closeEffect:'fadeOut',//鏀跺悎鏅傜殑鏁堟灉
closeEffectSpeed:'fast',//鏀跺悎鏅傜殑閫熷害
openEffect:'slideDown',//灞曢枊鏅傜殑鏁堟灉
openEffectSpeed:'fast',////灞曢枊鏅傜殑閫熷害
dataListHeight:null,//涓嬫媺閬稿柈楂樺害
className:'sp_selecter',//妯e紡
margin:0,//澶栬窛
select_auto_width:false, //鑷嫊瀵害
ul_width_tuning:0,//瀵害寰
li_padding:0,
default_Value:true,//鏄惁浣跨敤闋愯ō鍊
default_value_text:'-select-',
footer_corner:false,//鏄惁浣跨敤搴曢儴鍦撹
arr_multi_select:[],//闂滆伅寮忎笅鎷夋槸閬稿柈
default_choice:true,//鏄惁闋愬厛閬稿彇绗竴鍊嬮爡鐩
attrValue:[],//鍌冲€肩殑鍚嶇ū闋愯ō['va']
type:"GET",
dropdown_tuning:0, //涓嬫媺寮忓崁鍩熺殑楂樺害浣嶇疆寰
track_width:20,
drag_content:null,
position_range:15, //婊戦紶婊捐吉鐨勪綅绉婚枔闅
data_reverse:false //璩囨枡鍒椾笂涓嬬炕杞
};
var set=$.extend(defaults,options);
var objThis=$(this);
//鍒ゆ柗attr鏄惁瀹氱京
if(set.attrValue.length<1){
set.attrValue[0]='va';
}
//鎻掑叆attrValue锛宨nsertElement瑾跨敤
function insertOtherValue(obj){
var return_text='';
for(key in obj){
return_text+=key+"='"+obj[key]+"' ";
}
return return_text;
}
//arrToObj
function arrToObj(targetObj){
var dataValue={};
var dataValueName=set.attrValue;
for(var i=0; i";
temp_body+="";
temp_body+="";
temp_body+="";
temp_body+="";
temp_body+="";
temp_body+="";
temp_body+="
";
if($.isFunction(set.insertElement)){
$('> option',$this).each(function(){
if($(this).attr('selected')){
has_default_choice=true;
default_title=$(this).text();
default_value=$(this).val();
default_icon=$(this).attr('icon_url');
}
var insert_element_obj={};
var icon_url;
insert_element_obj[set.attrValue[0]]=$(this).val();
//铏曠悊闋堟姄鍙栫殑璩囨枡灞€у€
for(var i=1; i";
if(set.footer_corner){
temp_body+="";
}
temp_body+="
";
$newThis=$(temp_body).insertAfter($this);
//娌掓湁闋愰伕閬搁爡浠ュ強瑕佷娇鐢ㄩ爯閬
if(!has_default_choice){
if(set.default_choice){
default_value=$("ul > li",$newThis).eq(0).attr(set.attrValue[0]);
default_title=$("ul > li",$newThis).eq(0).text();
default_icon=$("ul > li ",$newThis).eq(0).children('img').attr('src');
if(typeof(default_value)=='undefined'){
default_value=0;
}
if(typeof(default_title)=='undefined'){
default_title=default_title_value;
}
}else{
default_value=0;
default_title=default_title_value;
}
}
if(default_title==''){
default_title=default_title_value;
}
if(default_value==''){
default_value=0;
}
$('input[type=hidden]',$newThis).val(default_value);
$('>.center',$newThis).text(default_title);
if(default_icon){
$('>.center',$newThis).prepend("");
}
//鐒¤嚜鍕曞搴︾殑铏曡!
if(!set.select_auto_width){
var overflow_value=$("> .left",$newThis).width()+$("> .right",$newThis).width();
$newThis.find('.center').width($this.width()-overflow_value-(set.li_padding));
}
//鍒櫎鍘熸湰鐨剆elect
$this.remove();
style_init($newThis);
//鐗╀欢鐢㈢敓瀹岀暍 first ajax init
if(unitCount==i+1){
var arr_multi_select=set.arr_multi_select;
var arr_multi_select_count=arr_multi_select.length;
for(var kk=0; kk li',obj).length;
var _optionAreaOpen=false;
var hh=obj.height();
var temp_width=obj.width();
var maxHeight;
//鏂囧瓧鍗€鍩熺殑瀵害=绺介暦-鍓嶉潰鍦栫墖-寰岄潰鍦栫墖
if(!old){
var _centerW=obj.width()-$('.left',obj).width()-$('.right',obj).width();
}else{
var _centerW=$("> .center",obj).width();
}
$('.center',obj).width(_centerW);
var ulPx=0;
var ulWidth=obj.width()-(set.margin*2)+set.ul_width_tuning;
if(set.dataListHeight){
maxHeight=set.dataListHeight+'px';
}else{
maxHeight="auto";
}
$('ul',obj).css({'max-height':maxHeight});
//var select_listdata_area_height=obj.find('.select_listdata_area').height();
var select_listdata_area_height;
if(set.data_reverse){
select_listdata_area_height=(obj.find('.select_listdata_area').height())*-1;
}else{
select_listdata_area_height=hh+set.dropdown_tuning;
}
obj.find('.select_listdata_area').css({'top':(select_listdata_area_height),
'left':ulPx+'px',
'width':ulWidth+'px',
'padding-top':(0)+'px'});
obj.find('li').css({'width':(ulWidth-(set.li_padding*2))+'px',
'padding-left':set.li_padding+'px',
'padding-right':set.li_padding+'px'});
obj.find('.select_footer').css({'width':ulWidth+'px',
'padding-left':'0px',
'padding-right':'0px'
});
//铏曠悊scrollbar
var area_height=$('.select_listdata_area',obj).show().children('ul')[0].scrollHeight;
$('.select_listdata_area',obj).hide();
if(set.dataListHeight){
if(area_height > set.dataListHeight){
var drag_content='';
if($.isFunction(set.drag_content)){
drag_content=set.drag_content();
}
$('.select_listdata_area',obj).append('');
var height_range=area_height / set.dataListHeight;
$('.drag',obj).height($('.track',obj).height()/height_range);
/*drag*/
$('.drag',obj).draggable({
axis: "y",
drag:function (event,ui){
//鎺у埗 drag
if(ui.position.top<0){
ui.position.top=0;
}
if(ui.position.top+$(this).height()>=$('.track',obj).height()){
ui.position.top=$('.track',obj).height()-$(this).height();
}
//鎺у埗鍏у
$('.select_listdata_area > ul',obj).scrollTop(ui.position.top*height_range);
}
});
/*mouse wheel*/
$('.select_listdata_area > ul',obj).mousewheel(function(event,delta){
var now_position=$('.drag',obj).position().top;
if(delta>0){
if(now_position-set.position_range<=0){
$('.drag',obj).css('top',0);
}else{
$('.drag',obj).css('top',now_position-set.position_range);
}
}else{
if(now_position+$('.drag',obj).height()+set.position_range>=$('.track',obj).height()){
$('.drag',obj).css('top',$('.track',obj).height()-$('.drag',obj).height());
}else{
$('.drag',obj).css('top',now_position+set.position_range);
}
}
$('.select_listdata_area > ul',obj).scrollTop($('.drag',obj).position().top*height_range);
})
$('.select_listdata_area > ul > li',obj).width($('.select_listdata_area',obj).width()-set.track_width-(set.li_padding*2));
}
}
/**/
// 20211130 Elin jQuery鍗囩礆锛寀nbind浜嬩欢宸叉鐢紝unbind-->off('',)
$(obj).off('hover');
obj.hover(function(){
if(_optionAreaOpen ){
var thisUl=$(this).find('.select_listdata_area');
thisUl.delay(set.closeDelay).queue(fn_closeEffect(thisUl,set.closeEffect,set.closeEffectSpeed));
_optionAreaOpen=false;
$(this).removeClass('this');
}
})
var obj_div=$('> div',obj);
obj_div.off('click');
obj_div.click(function(){
//铏曠悊闅庡堡
$(document).find('.'+set.className).css('z-index','500');
$(this).parent().css('z-index','600');
if(!_optionAreaOpen ){
obj.addClass('this');
var thisUl=$(this).parent().find('.select_listdata_area');
thisUl.queue(fn_openEffect(thisUl,set.openEffect,set.openEffectSpeed));
_optionAreaOpen=true;
}
})
//鐣舵寜閳曢伕瀹氭檪
var obj_li=$('ul > li',obj);
// 20211130 Elin jQuery鍗囩礆锛宑lick浜嬩欢鎺ㄨ枽浣跨敤on锛宑lick-->on('click')
obj_li.on('click',function(){
var icon_url=$(this).children('img').attr('src');
var dropArea=$('div.select_listdata_area',obj);
$('.sp_selecterInput',obj).text($(this).text());
$('input[type=hidden]',obj).val($(this).attr(set.attrValue[0]));
if($.isFunction(set.clickEvent)){
set.clickEvent($(this).attr(set.attrValue[0]),$(this));
}
if(typeof(icon_url)!='undefined'){
$('.sp_selecterInput',obj).prepend("");
}
dropArea.delay(set.closeDelay).queue(fn_closeEffect(dropArea,set.closeEffect,set.closeEffectSpeed));
_optionAreaOpen=false;
obj.removeClass('this');
return false;
});
}
//鏀跺悎鏁堟灉
function fn_closeEffect(obj,effect,speed){
switch(effect){
case 'slideUp':
obj.slideUp(speed);
break;
case 'fadeOut':
obj.fadeOut(speed);
break;
case 'hide':
obj.hide(speed);
break;
default:
obj.slideUp(speed);
}
}
//闁嬪暉鏁堟灉
function fn_openEffect(obj,effect,speed){
switch(effect){
case 'slideDown':
obj.slideDown(speed);
break;
case 'fadeIn':
obj.fadeIn(speed);
break;
case 'show':
obj.show(speed);
break;
default:
obj.slideDown(speed);
}
}
//Ajax
function ajaxMulti(arr_multi_select){
//alert(set.arr_multi_select[0].target_info.length);
var currentObj=$(arr_multi_select['current']);
$('ul > li',currentObj).click(function(){
relationSelectRest(currentObj);
for(var i=0;i li',obj).remove();
$('> .center',obj).text(obj.attr('default_title'));
$('input[type=hidden]',obj).val('');
/*scroll reset*/
// 20211130 Elin jQuery鍗囩礆锛寀nbind浜嬩欢宸叉鐢紝unbind-->off('',)
$('.drag',obj).off('draggable');
$('.select_listdata_area > ul',obj).off('mousewheel');
$('.track',obj).remove();
}
//闂滈€i噸缃?閬炲洖)
function relationSelectRest(obj){//obj 鐣跺墠琚伕鎿囩墿浠
for(var i=0; i