Advertisement
Guest User

ie-select-width-fix.js

a guest
Apr 15th, 2010
2,902
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. YAHOO.namespace( 'YAHOO.Hack' ).FixIESelectWidth = new function()
  2.     {
  3.     var oSelf = this;
  4.     var YUE = YAHOO.util.Event;
  5.     var YUD = YAHOO.util.Dom;
  6.     var oTimer = {};
  7.     var oAnim = {};
  8.     var nTimerId = 0 ;
  9.     var dLastFocalItem;
  10.     var ie7 = !!(document.uniqueID && typeof(XMLHttpRequest)!='undefined' )
  11.     function init(el)
  12.     {
  13.    
  14.    
  15.     el = el || this;
  16.    
  17.    
  18.    
  19.     if( el.tagName.toLowerCase() != 'select')
  20.     {
  21.     throw Error('element [' + el.id + '] is not <select>');
  22.     return;
  23.     };
  24.    
  25.     if(!YUD.hasClass( el.parentNode, 'select-box'))
  26.     {
  27.     throw Error('className select-box is not included for element [' + el.id + ']');
  28.     return;
  29.     };
  30.    
  31.     var oRs = el.runtimeStyle;
  32.     var oPRs = el.parentNode.runtimeStyle;
  33.    
  34.    
  35.     oPRs.fonSize = 0;
  36.    
  37.    
  38.     var sDisplay = el.parentNode.currentStyle.display.toLowerCase() ;
  39.     if( sDisplay=='' || sDisplay=='inline' || sDisplay=='inline-block' )
  40.     {
  41.     oPRs.display = 'inline-block';
  42.     oPRs.width = el.offsetWidth + 'px';
  43.     oPRs.height =el.offsetHeight + 'px';
  44.     oPRs.position = 'relative';
  45.     oRs.position = 'absolute';
  46.     oRs.top = 0;
  47.     oRs.left = 0;
  48.     };
  49.    
  50.    
  51.    
  52.     el._timerId = ( nTimerId+=1 );
  53.    
  54.     el.selectedIndex = Math.max( 0 , el.selectedIndex );
  55.    
  56.     oTimer[ '_' + el._timerId ] = setTimeout('void(0)',0);
  57.     oAnim [ 'A' + el._timerId ] = setTimeout('void(0)',0);
  58.    
  59.     YUE.on( el, 'mouseover' , onMouseOver);
  60.     YUE.on( document, 'mousedown' ,onMouseDown , el, true);
  61.     YUE.on( el, 'change' ,collapseSelect , el, true);
  62.     }
  63.    
  64.    
  65.     function collapseSelect(e)
  66.     {
  67.     status++;
  68.     this.runtimeStyle.width = '';
  69.     }
  70.    
  71.     function onMouseOver(e )
  72.     {
  73.    
  74.     var el = this;
  75.     if(dLastFocalItem && dLastFocalItem !=el)
  76.     {
  77.     onMouseDown.call( dLastFocalItem , e );
  78.     };
  79.    
  80.     var sTimerId ='_' + el._timerId ;
  81.     var sAniId = 'A' + el._timerId ;
  82.     clearTimeout( oTimer[ sTimerId ] );
  83.    
  84.    
  85.    
  86.     var onTween = function()
  87.     {
  88.     clearTimeout( oAnim [ sAniId ] );
  89.     if( Math.abs( nEndWidth - nStartWidth ) > 3 )
  90.     {
  91.     nStartWidth += (nEndWidth - nStartWidth ) /3;
  92.     el.runtimeStyle.width = nStartWidth + 'px';
  93.     oAnim [ sAniId ] = setTimeout( onTween ,0 );
  94.     }
  95.     else
  96.     {
  97.     el.runtimeStyle.width = 'auto';
  98.     el.selectedIndex = Math.max( 0 , el.selectedIndex );
  99.     }
  100.     }
  101.    
  102.     var nStartWidth = el.offsetWidth ;
  103.     el.runtimeStyle.width = 'auto';
  104.     var nEndWidth = el.offsetWidth;
  105.    
  106.    
  107.     clearTimeout( oAnim [ sAniId ] );
  108.     onTween();
  109.    
  110.     el.focus();
  111.     dLastFocalItem = el;
  112.     }
  113.    
  114.     function onMouseDown(e , el )
  115.     {
  116.     el = ( e.srcElement || e.target );
  117.    
  118.    
  119.    
  120.     if( el == this && e.type!='mouseover' )
  121.     {
  122.     status++;
  123.     YUE.stopEvent(e);
  124.     return false;
  125.     };
  126.    
  127.    
  128.     el = this;
  129.    
  130.     clearTimeout( oAnim [ 'A' + el._timerId ] );
  131.    
  132.    
  133.     var sTimerId ='_' + el._timerId ;
  134.     var doItLater = function()
  135.     {
  136.     el.runtimeStyle.width = '';
  137.     };
  138.     if( e.type=='mouseover')
  139.     { doItLater();}
  140.     else{
  141.     oTimer[ sTimerId ] = setTimeout(doItLater,100);
  142.     }
  143.     }
  144.    
  145.    
  146.    
  147.     function constructor(sId)
  148.     {
  149.     sId = [ sId , ''].join('');
  150.     //Only fix for IE55 ~ IE7
  151.    
  152.     if(document.uniqueID && window.createPopup )
  153.     {
  154.     YUE.onAvailable(sId ,init );
  155.     return true;
  156.    
  157.     }else{return false};
  158.     };
  159.    
  160.     return constructor;
  161.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement